@fugitivesclub/nft
Version:
A SDK for NFT creation with Hedera and FileCoin
138 lines (137 loc) • 5.79 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HederaSdk = void 0;
const sdk_1 = require("@hashgraph/sdk");
const axios_1 = __importDefault(require("axios"));
const hedera_interface_1 = require("../models/hedera.interface");
const HEDERA_CREATE_NFT_FEES = 1;
class HederaSdk {
constructor(hederaAccount) {
this.hederaAccount = hederaAccount;
this.client = this.setClient({
accountId: this.hederaAccount.accountId,
privateKey: this.hederaAccount.privateKey,
environment: this.hederaAccount.environment,
});
}
async createNFT({ name, symbol, customFees, supply, cids, nfts, }) {
var _a;
try {
const urls = [];
/* Create a royalty fee */
const customRoyaltyFee = [];
if (customFees) {
customFees.map((customFee) => {
const fee = new sdk_1.CustomRoyaltyFee()
.setNumerator(customFee.numerator) // The numerator of the fraction
.setDenominator(customFee.denominator); // The denominator of the fraction
if (customFee.fallbackFee) {
fee.setFallbackFee(new sdk_1.CustomFixedFee().setHbarAmount(new sdk_1.Hbar(customFee.fallbackFee))); // The fallback fee
}
fee.setFeeCollectorAccountId(customFee.collectorAccountId); // The account that will receive the royalty fee
customRoyaltyFee.push(fee);
});
}
const supplyKey = sdk_1.PrivateKey.generate();
/* Create the NFT */
const tx = new sdk_1.TokenCreateTransaction()
.setTokenType(sdk_1.TokenType.NonFungibleUnique)
.setTokenName(name)
.setTokenSymbol(symbol)
.setSupplyKey(supplyKey)
.setSupplyType(sdk_1.TokenSupplyType.Finite)
.setInitialSupply(0)
.setMaxSupply(supply !== null && supply !== void 0 ? supply : nfts.length)
.setTreasuryAccountId(this.hederaAccount.accountId)
.setAutoRenewAccountId(this.hederaAccount.accountId)
.setCustomFees(customRoyaltyFee);
const transaction = await tx.signWithOperator(this.client);
/* submit to the Hedera network */
const response = await transaction.execute(this.client);
/* Get the receipt of the transaction */
const receipt = await response.getReceipt(this.client);
/* Get the token ID from the receipt */
const tokenId = receipt.tokenId;
/* Mint the token */
const nftIds = [];
const limit_chunk = 8;
let index = 0;
const max = supply !== null && supply !== void 0 ? supply : nfts.length;
for (let idx = 0; idx < max; idx += limit_chunk) {
const limit = idx + limit_chunk > max ? max - idx : limit_chunk;
const mintTransaction = new sdk_1.TokenMintTransaction().setTokenId(tokenId);
for (let i = 0; i < limit; i++) {
const url = (_a = cids[index * limit_chunk + i]) !== null && _a !== void 0 ? _a : cids[0];
mintTransaction.addMetadata(Buffer.from(url));
if (i === 0 || !supply) {
urls.push(url);
}
}
/* Sign with the supply private key of the token */
const signTx = await mintTransaction
.freezeWith(this.client)
.sign(supplyKey);
/* Submit the transaction to a Hedera network */
const resp = await signTx.execute(this.client);
const receiptMint = await resp.getReceipt(this.client);
/* Get the Serial Number */
const serialNumber = receiptMint.serials;
/* Get the NftId */
for (const nftSerial of serialNumber.values()) {
nftIds.push(new sdk_1.NftId(tokenId, nftSerial).toString());
}
index = index + 1;
}
return {
urls,
txId: response.transactionId.toString(),
tokenId: tokenId.toString(),
nftIds,
};
}
catch (e) {
return Promise.reject(e);
}
}
/**
* Hedera fees for NFT's creation
*/
async getFees() {
const hbarPrice = await this.getHbarToCurrency();
return {
usd: HEDERA_CREATE_NFT_FEES,
hbar: +parseFloat((HEDERA_CREATE_NFT_FEES / hbarPrice).toFixed(3)),
};
}
/**
* Set Hedera SDK Client
* @param accountId
* @param privateKey
* @param environment
*/
setClient({ accountId, privateKey, environment }) {
let client;
if (environment === hedera_interface_1.HederaEnvironment.MAINNET) {
client = sdk_1.Client.forMainnet();
}
else {
client = sdk_1.Client.forTestnet();
}
client.setOperator(accountId, privateKey);
return client;
}
/**
* Getting the current HBAR price in usd
*/
getHbarToCurrency() {
return axios_1.default
.get(`https://api.coingecko.com/api/v3/coins/hedera-hashgraph?market_data=true`)
.then((res) => {
return +res.data.market_data.current_price['usd'];
});
}
}
exports.HederaSdk = HederaSdk;