UNPKG

@tatumio/tatum-v1

Version:

Tatum API client allows browsers and Node.js clients to interact with Tatum API.

839 lines 171 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.sendCeloTransferMultiTokenTransaction = exports.sendCeloMintMultiTokenBatchTransaction = exports.sendCeloMintMultiTokenTransaction = exports.sendCeloDeployMultiTokenTransaction = exports.sendCeloDeployErc721Transaction = exports.sendCeloTransferErc721Transaction = exports.sendCeloUpdateCashbackForAuthorErc721Transaction = exports.sendCeloBurnErc721Transaction = exports.sendCeloMintMultipleCashbackErc721Transaction = exports.sendCeloMintMultipleErc721Transaction = exports.sendCeloMintMultipleErc721ProvenanceTransaction = exports.sendCeloMintErc721ProvenanceTransaction = exports.sendCeloMintCashbackErc721Transaction = exports.sendCeloMintErc721Transaction = exports.sendCeloErc20Transaction = exports.sendCeloOrcUsdTransaction = exports.prepareCeloStoreDataSignedTransaction = exports.prepareCeloOrCUsdSignedTransaction = exports.prepareCeloBurnMultiTokenSignedTransaction = exports.prepareCeloBurnMultiTokenBatchSignedTransaction = exports.prepareCeloBatchTransferMultiTokenSignedTransaction = exports.prepareCeloTransferMultiTokenSignedTransaction = exports.prepareCeloMintMultiTokenBatchSignedTransaction = exports.prepareCeloMintMultiTokenSignedTransaction = exports.prepareCeloUpdateCashbackForAuthorErc721SignedTransaction = exports.prepareCeloMintMultipleErc721SignedTransaction = exports.prepareCeloMintMultipleCashbackErc721SignedTransaction = exports.prepareCeloBurnErc20SignedTransaction = exports.prepareCeloTransferErc20SignedTransaction = exports.getCeloErc20ContractDecimals = exports.sendCeloSmartContractMethodInvocationTransaction = exports.sendCeloStoreDataSignedTransaction = exports.sendCeloDeployErc20Transaction = exports.sendCeloSmartContractReadMethodInvocationTransaction = exports.prepareCeloSmartContractWriteMethodInvocation = exports.getCeloClient = exports.prepareCeloMintErc20SignedTransaction = exports.prepareCeloDeployErc20SignedTransaction = exports.prepareCeloBurnErc721SignedTransaction = exports.prepareCeloTransferErc721SignedTransaction = exports.prepareCeloMintErc721SignedTransaction = exports.prepareCeloMintMultipleErc721ProvenanceSignedTransaction = exports.prepareCeloMintErc721ProvenanceSignedTransaction = exports.prepareCeloMintCashbackErc721SignedTransaction = exports.prepareCeloDeployErc721SignedTransaction = exports.prepareCeloDeployMultiTokenSignedTransaction = exports.signCeloKMSTransaction = exports.prepareCeloDeployAuctionSignedTransaction = exports.prepareCeloDeployMarketplaceListingSignedTransaction = exports.prepareCeloGenerateCustodialWalletSignedTransaction = void 0; exports.sendCeloDeployMarketplaceListingSignedTransaction = exports.sendCeloGenerateCustodialWalletSignedTransaction = exports.sendCeloBurnMultiTokenBatchTransaction = exports.sendCeloBurnMultiTokenTransaction = exports.sendCeloTransferMultiTokenBatchTransaction = void 0; const celo_ethers_wrapper_1 = require("@celo-tools/celo-ethers-wrapper"); const bignumber_1 = require("@ethersproject/bignumber"); const bignumber_js_1 = require("bignumber.js"); const web3_1 = __importDefault(require("web3")); const web3_utils_1 = require("web3-utils"); const blockchain_1 = require("../blockchain"); const tatum_1 = require("../connector/tatum"); const constants_1 = require("../constants"); const erc1155_abi_1 = __importDefault(require("../contracts/erc1155/erc1155_abi")); const erc1155_bytecode_1 = __importDefault(require("../contracts/erc1155/erc1155_bytecode")); const token_abi_1 = __importDefault(require("../contracts/erc20/token_abi")); const token_bytecode_1 = __importDefault(require("../contracts/erc20/token_bytecode")); const erc721_abi_1 = __importDefault(require("../contracts/erc721Cashback/erc721_abi")); const erc721_bytecode_1 = __importDefault(require("../contracts/erc721Cashback/erc721_bytecode")); const erc721Provenance_abi_1 = __importDefault(require("../contracts/erc721Provenance/erc721Provenance_abi")); const erc721Provenance_bytecode_1 = __importDefault(require("../contracts/erc721Provenance/erc721Provenance_bytecode")); const marketplace_1 = require("../contracts/marketplace"); const model_1 = require("../model"); const nft_1 = require("../nft"); const custodial_1 = require("../wallet/custodial"); const erc721_abi_2 = __importDefault(require("../contracts/erc721General/erc721_abi")); const erc721_bytecode_2 = __importDefault(require("../contracts/erc721General/erc721_bytecode")); const obtainWalletInformation = async (wallet, feeCurrencyContractAddress) => { const [txCount, gasPrice, from] = await Promise.all([ wallet.getTransactionCount(), wallet.getGasPrice(feeCurrencyContractAddress), wallet.getAddress(), ]); return { txCount, gasPrice: [constants_1.CUSD_ADDRESS_MAINNET, constants_1.CUSD_ADDRESS_TESTNET].includes(feeCurrencyContractAddress || '') && gasPrice.lte(0x1dcd6500) ? bignumber_1.BigNumber.from(0x3B9ACA00) : gasPrice, from, }; }; const getFeeCurrency = (feeCurrency, testnet) => { switch (feeCurrency) { case model_1.Currency.CEUR: return testnet ? constants_1.CEUR_ADDRESS_TESTNET : constants_1.CEUR_ADDRESS_MAINNET; case model_1.Currency.CUSD: return testnet ? constants_1.CUSD_ADDRESS_TESTNET : constants_1.CUSD_ADDRESS_MAINNET; default: return undefined; } }; /** * Sign CELO generate custodial wallet address transaction with private keys locally. Nothing is broadcast to the blockchain. * @param testnet * @param body content of the transaction to broadcast * @param provider url of the Bsc Server to connect to. If not set, default public server will be used. * @returns transaction data to be broadcast to blockchain, or signatureId in case of Tatum KMS */ const prepareCeloGenerateCustodialWalletSignedTransaction = async (testnet, body, provider) => { await tatum_1.validateBody(body, model_1.GenerateCustodialAddress); const p = new celo_ethers_wrapper_1.CeloProvider(provider || `${process.env.TATUM_API_URL || constants_1.TATUM_API_URL}/v3/celo/web3/${process.env.TATUM_API_KEY}`); const network = await p.ready; const feeCurrency = body.feeCurrency || model_1.Currency.CELO; const feeCurrencyContractAddress = getFeeCurrency(feeCurrency, testnet); const { abi, code } = custodial_1.obtainCustodialAddressType(body); // @ts-ignore const contract = new (new web3_1.default()).eth.Contract(abi); const deploy = contract.deploy({ data: code, }); if (body.signatureId) { return JSON.stringify({ chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce: body.nonce, gasLimit: '0', data: deploy.encodeABI(), }); } const wallet = new celo_ethers_wrapper_1.CeloWallet(body.fromPrivateKey, p); const { txCount, gasPrice, from } = await obtainWalletInformation(wallet, feeCurrencyContractAddress); const transaction = { chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce: body.nonce || txCount, gasLimit: '0', gasPrice, data: deploy.encodeABI(), from, }; transaction.gasLimit = (await wallet.estimateGas(transaction)).add(feeCurrency === model_1.Currency.CELO ? 0 : 100000).toHexString(); return wallet.signTransaction(transaction); }; exports.prepareCeloGenerateCustodialWalletSignedTransaction = prepareCeloGenerateCustodialWalletSignedTransaction; const deployContract = async (testnet, abi, bytecode, args, feeCurrency = model_1.Currency.CELO, fromPrivateKey, nonce, signatureId, provider) => { const p = new celo_ethers_wrapper_1.CeloProvider(provider || `${process.env.TATUM_API_URL || constants_1.TATUM_API_URL}/v3/celo/web3/${process.env.TATUM_API_KEY}`); const network = await p.ready; const feeCurrencyContractAddress = getFeeCurrency(feeCurrency, testnet); // @ts-ignore const contract = new (new web3_1.default()).eth.Contract(abi); const deploy = contract.deploy({ data: bytecode, arguments: args, }); if (signatureId) { return JSON.stringify({ chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce, gasLimit: '0', data: deploy.encodeABI(), }); } const wallet = new celo_ethers_wrapper_1.CeloWallet(fromPrivateKey, p); const { txCount, gasPrice, from } = await obtainWalletInformation(wallet, feeCurrencyContractAddress); const transaction = { chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce: nonce || txCount, gasLimit: '0', gasPrice, data: deploy.encodeABI(), from, }; transaction.gasLimit = (await wallet.estimateGas(transaction)).add(feeCurrency === model_1.Currency.CELO ? 0 : 100000).toHexString(); return wallet.signTransaction(transaction); }; /** * Sign CELO generate custodial wallet address transaction with private keys locally. Nothing is broadcast to the blockchain. * @param testnet * @param body content of the transaction to broadcast * @param provider url of the Bsc Server to connect to. If not set, default public server will be used. * @returns transaction data to be broadcast to blockchain, or signatureId in case of Tatum KMS */ const prepareCeloDeployMarketplaceListingSignedTransaction = async (testnet, body, provider) => { await tatum_1.validateBody(body, model_1.DeployMarketplaceListing); return deployContract(testnet, marketplace_1.listing.abi, marketplace_1.listing.data, [body.marketplaceFee, body.feeRecipient], body.feeCurrency, body.fromPrivateKey, body.nonce, body.signatureId, provider); }; exports.prepareCeloDeployMarketplaceListingSignedTransaction = prepareCeloDeployMarketplaceListingSignedTransaction; /** * Sign CELO deploy NFT Auction contract transaction with private keys locally. Nothing is broadcast to the blockchain. * @param testnet * @param body content of the transaction to broadcast * @param provider url of the Bsc Server to connect to. If not set, default public server will be used. * @returns transaction data to be broadcast to blockchain, or signatureId in case of Tatum KMS */ const prepareCeloDeployAuctionSignedTransaction = async (testnet, body, provider) => { await tatum_1.validateBody(body, model_1.DeployNftAuction); return deployContract(testnet, marketplace_1.auction.abi, marketplace_1.auction.data, [body.auctionFee, body.feeRecipient], body.feeCurrency, body.fromPrivateKey, body.nonce, body.signatureId, provider); }; exports.prepareCeloDeployAuctionSignedTransaction = prepareCeloDeployAuctionSignedTransaction; /** * Sign Celo pending transaction from Tatum KMS * @param tx pending transaction from KMS * @param fromPrivateKey private key to sign transaction with. * @param testnet mainnet or testnet version * @param provider url of the Ethereum Server to connect to. If not set, default public server will be used. * @returns transaction data to be broadcast to blockchain, or signatureId in case of Tatum KMS */ const signCeloKMSTransaction = async (tx, fromPrivateKey, testnet, provider) => { if (tx.chain !== model_1.Currency.CELO) { throw Error('Unsupported chain.'); } const p = new celo_ethers_wrapper_1.CeloProvider(provider || `${process.env.TATUM_API_URL || constants_1.TATUM_API_URL}/v3/celo/web3/${process.env.TATUM_API_KEY}`); await p.ready; const wallet = new celo_ethers_wrapper_1.CeloWallet(fromPrivateKey, p); const transaction = JSON.parse(tx.serializedTransaction); const { txCount, gasPrice, from } = await obtainWalletInformation(wallet, transaction.feeCurrency); transaction.nonce = transaction.nonce || txCount; transaction.gasPrice = transaction.gasPrice || gasPrice; transaction.from = from; transaction.gasLimit = transaction.gasLimit === '0' || !transaction.gasLimit ? (await wallet.estimateGas(transaction)).add(100000).toHexString() : transaction.gasLimit; return wallet.signTransaction(transaction); }; exports.signCeloKMSTransaction = signCeloKMSTransaction; /** * Prepare a signed Celo deploy multi token transaction with the private key locally. Nothing is broadcasted to the blockchain. * @returns raw transaction data in hex, to be broadcasted to blockchain. */ const prepareCeloDeployMultiTokenSignedTransaction = async (testnet, body, provider) => { await tatum_1.validateBody(body, model_1.CeloDeployMultiToken); const { fromPrivateKey, uri, feeCurrency, nonce, signatureId, publicMint, } = body; const p = new celo_ethers_wrapper_1.CeloProvider(provider || `${process.env.TATUM_API_URL || constants_1.TATUM_API_URL}/v3/celo/web3/${process.env.TATUM_API_KEY}`); const network = await p.ready; const feeCurrencyContractAddress = getFeeCurrency(feeCurrency, testnet); // @ts-ignore const contract = new (new web3_1.default()).eth.Contract(erc1155_abi_1.default); const deploy = contract.deploy({ data: erc1155_bytecode_1.default, arguments: [uri, publicMint ? publicMint : false], }); if (signatureId) { return JSON.stringify({ chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce, gasLimit: '0', data: deploy.encodeABI(), }); } const wallet = new celo_ethers_wrapper_1.CeloWallet(fromPrivateKey, p); const { txCount, gasPrice, from } = await obtainWalletInformation(wallet, feeCurrencyContractAddress); const transaction = { chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce: nonce || txCount, gasLimit: '0', gasPrice, data: deploy.encodeABI(), from, }; transaction.gasLimit = (await wallet.estimateGas(transaction)).add(feeCurrency === model_1.Currency.CELO ? 0 : 100000).toHexString(); return wallet.signTransaction(transaction); }; exports.prepareCeloDeployMultiTokenSignedTransaction = prepareCeloDeployMultiTokenSignedTransaction; /** * Prepare a signed Celo deploy erc721 transaction with the private key locally. Nothing is broadcasted to the blockchain. * @returns raw transaction data in hex, to be broadcasted to blockchain. */ const prepareCeloDeployErc721SignedTransaction = async (testnet, body, provider) => { await tatum_1.validateBody(body, model_1.CeloDeployErc721); const { fromPrivateKey, name, symbol, feeCurrency, nonce, signatureId, provenance, cashback, publicMint, } = body; const p = new celo_ethers_wrapper_1.CeloProvider(provider || `${process.env.TATUM_API_URL || constants_1.TATUM_API_URL}/v3/celo/web3/${process.env.TATUM_API_KEY}`); const network = await p.ready; const feeCurrencyContractAddress = getFeeCurrency(feeCurrency, testnet); if (provenance && cashback) { throw new Error('Only one of provenance or cashback must be present and true.'); } let abi = erc721_abi_2.default; let data = erc721_bytecode_2.default; if (provenance) { abi = erc721Provenance_abi_1.default; data = erc721Provenance_bytecode_1.default; } else if (cashback) { abi = erc721_abi_1.default; data = erc721_bytecode_1.default; } // @ts-ignore const contract = new (new web3_1.default()).eth.Contract(abi); const deploy = contract.deploy({ data, arguments: [name, symbol, publicMint ? publicMint : false], }); if (signatureId) { return JSON.stringify({ chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce, gasLimit: '0', data: deploy.encodeABI(), }); } const wallet = new celo_ethers_wrapper_1.CeloWallet(fromPrivateKey, p); const { txCount, gasPrice, from } = await obtainWalletInformation(wallet, feeCurrencyContractAddress); const transaction = { chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce: nonce || txCount, gasLimit: '0', gasPrice, data: deploy.encodeABI(), from, }; transaction.gasLimit = (await wallet.estimateGas(transaction)).add(feeCurrency === model_1.Currency.CELO ? 0 : 100000).toHexString(); return wallet.signTransaction(transaction); }; exports.prepareCeloDeployErc721SignedTransaction = prepareCeloDeployErc721SignedTransaction; /** * Prepare a signed Celo mint cashback erc721 transaction with the private key locally. Nothing is broadcasted to the blockchain. * @returns raw transaction data in hex, to be broadcasted to blockchain. */ const prepareCeloMintCashbackErc721SignedTransaction = async (testnet, body, provider) => { await tatum_1.validateBody(body, model_1.CeloMintErc721); const { fromPrivateKey, url, to, tokenId, contractAddress, feeCurrency, nonce, signatureId, authorAddresses, cashbackValues, erc20, } = body; const p = new celo_ethers_wrapper_1.CeloProvider(provider || `${process.env.TATUM_API_URL || constants_1.TATUM_API_URL}/v3/celo/web3/${process.env.TATUM_API_KEY}`); const network = await p.ready; if (contractAddress && feeCurrency) { const feeCurrencyContractAddress = getFeeCurrency(feeCurrency, testnet); // @ts-ignore const contract = new (new web3_1.default()).eth.Contract(erc721_abi_1.default, contractAddress.trim()); const cb = []; for (const c of cashbackValues) { cb.push(`0x${new bignumber_js_1.BigNumber(web3_utils_1.toWei(c, 'ether')).toString(16)}`); } if (signatureId) { return JSON.stringify({ chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce, to: contractAddress.trim(), gasLimit: '0', data: erc20 ? contract.methods.mintWithCashback(to.trim(), tokenId, url, authorAddresses, cb, erc20).encodeABI() : contract.methods.mintWithCashback(to.trim(), tokenId, url, authorAddresses, cb).encodeABI(), }); } const wallet = new celo_ethers_wrapper_1.CeloWallet(fromPrivateKey, p); const { txCount, gasPrice, from } = await obtainWalletInformation(wallet, feeCurrencyContractAddress); const transaction = { chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce: nonce || txCount, gasLimit: '0', to: contractAddress.trim(), gasPrice, data: erc20 ? contract.methods.mintWithCashback(to.trim(), tokenId, url, authorAddresses, cb, erc20).encodeABI() : contract.methods.mintWithCashback(to.trim(), tokenId, url, authorAddresses, cb).encodeABI(), from, }; transaction.gasLimit = (await wallet.estimateGas(transaction)).add(feeCurrency === model_1.Currency.CELO ? 0 : 100000).toHexString(); return wallet.signTransaction(transaction); } throw new Error('Contract address and fee currency should not be empty!'); }; exports.prepareCeloMintCashbackErc721SignedTransaction = prepareCeloMintCashbackErc721SignedTransaction; /** * Prepare a signed Celo mint provenance erc732 transaction with the private key locally. Nothing is broadcasted to the blockchain. * @returns raw transaction data in hex, to be broadcasted to blockchain. */ const prepareCeloMintErc721ProvenanceSignedTransaction = async (testnet, body, provider) => { await tatum_1.validateBody(body, model_1.CeloMintErc721); const { fromPrivateKey, url, to, tokenId, contractAddress, feeCurrency, nonce, signatureId, cashbackValues, authorAddresses, fixedValues, erc20, } = body; const p = new celo_ethers_wrapper_1.CeloProvider(provider || `${process.env.TATUM_API_URL || constants_1.TATUM_API_URL}/v3/celo/web3/${process.env.TATUM_API_KEY}`); const network = await p.ready; if (contractAddress && feeCurrency) { const feeCurrencyContractAddress = getFeeCurrency(feeCurrency, testnet); // @ts-ignore const contract = new (new web3_1.default()).eth.Contract(erc721Provenance_abi_1.default, contractAddress.trim()); const cb = []; const fv = []; if (cashbackValues && fixedValues && authorAddresses) { cashbackValues.map(c => cb.push(`0x${new bignumber_js_1.BigNumber(c).multipliedBy(100).toString(16)}`)); fixedValues.map(c => fv.push(`0x${new bignumber_js_1.BigNumber(web3_utils_1.toWei(c, 'ether')).toString(16)}`)); } const data = erc20 ? contract.methods.mintWithTokenURI(to.trim(), tokenId, url, authorAddresses ? authorAddresses : [], cb, fv, erc20).encodeABI() : contract.methods.mintWithTokenURI(to.trim(), tokenId, url, authorAddresses ? authorAddresses : [], cb, fv).encodeABI(); if (signatureId) { return JSON.stringify({ chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce, to: contractAddress.trim(), gasLimit: '0', data: data, }); } const wallet = new celo_ethers_wrapper_1.CeloWallet(fromPrivateKey, p); const { txCount, gasPrice, from } = await obtainWalletInformation(wallet, feeCurrencyContractAddress); const transaction = { chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce: nonce || txCount, gasLimit: '0', to: contractAddress.trim(), gasPrice, data: data, from, }; transaction.gasLimit = (await wallet.estimateGas(transaction)).add(feeCurrency === model_1.Currency.CELO ? 0 : 100000).toHexString(); return wallet.signTransaction(transaction); } throw new Error('Contract address and fee currency should not be empty!'); }; exports.prepareCeloMintErc721ProvenanceSignedTransaction = prepareCeloMintErc721ProvenanceSignedTransaction; /** * Prepare a signed Celo mint multiple provenance cashback erc721 transaction with the private key locally. Nothing is broadcasted to the blockchain. * @returns raw transaction data in hex, to be broadcasted to blockchain. */ const prepareCeloMintMultipleErc721ProvenanceSignedTransaction = async (testnet, body, provider) => { await tatum_1.validateBody(body, model_1.CeloMintMultipleErc721); const { fromPrivateKey, to, tokenId, contractAddress, url, feeCurrency, nonce, signatureId, authorAddresses, cashbackValues, fixedValues, erc20, } = body; const p = new celo_ethers_wrapper_1.CeloProvider(provider || `${process.env.TATUM_API_URL || constants_1.TATUM_API_URL}/v3/celo/web3/${process.env.TATUM_API_KEY}`); const network = await p.ready; const feeCurrencyContractAddress = getFeeCurrency(feeCurrency, testnet); // @ts-ignore const contract = new (new web3_1.default()).eth.Contract(erc721Provenance_abi_1.default, contractAddress.trim()); const cb = []; const fv = []; if (authorAddresses && cashbackValues && fixedValues) { for (let i = 0; i < cashbackValues.length; i++) { const cb2 = []; const fv2 = []; for (let j = 0; j < cashbackValues[i].length; j++) { cb2.push(`0x${new bignumber_js_1.BigNumber(cashbackValues[i][j]).multipliedBy(100).toString(16)}`); fv2.push(`0x${new bignumber_js_1.BigNumber(web3_utils_1.toWei(fixedValues[i][j], 'ether')).toString(16)}`); } cb.push(cb2); fv.push(fv2); } } const data = erc20 ? contract.methods.mintMultiple(to.map(t => t.trim()), tokenId, url, authorAddresses ? authorAddresses : [], cb, fv, erc20).encodeABI() : contract.methods.mintMultiple(to.map(t => t.trim()), tokenId, url, authorAddresses ? authorAddresses : [], cb, fv).encodeABI(); if (signatureId) { return JSON.stringify({ chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce, gasLimit: '0', to: contractAddress.trim(), data: data, }); } const wallet = new celo_ethers_wrapper_1.CeloWallet(fromPrivateKey, p); const { txCount, gasPrice, from } = await obtainWalletInformation(wallet, feeCurrencyContractAddress); const transaction = { chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce: nonce || txCount, gasLimit: '0', to: contractAddress.trim(), gasPrice, data: data, from, }; transaction.gasLimit = (await wallet.estimateGas(transaction)).add(feeCurrency === model_1.Currency.CELO ? 0 : 100000).toHexString(); return wallet.signTransaction(transaction); }; exports.prepareCeloMintMultipleErc721ProvenanceSignedTransaction = prepareCeloMintMultipleErc721ProvenanceSignedTransaction; /** * Prepare a signed Celo mint erc732 transaction with the private key locally. Nothing is broadcasted to the blockchain. * @returns raw transaction data in hex, to be broadcasted to blockchain. */ const prepareCeloMintErc721SignedTransaction = async (testnet, body, provider) => { await tatum_1.validateBody(body, model_1.CeloMintErc721); const { fromPrivateKey, url, to, tokenId, contractAddress, feeCurrency, nonce, signatureId, } = body; const p = new celo_ethers_wrapper_1.CeloProvider(provider || `${process.env.TATUM_API_URL || constants_1.TATUM_API_URL}/v3/celo/web3/${process.env.TATUM_API_KEY}`); const network = await p.ready; if (contractAddress && feeCurrency) { const feeCurrencyContractAddress = getFeeCurrency(feeCurrency, testnet); // @ts-ignore const contract = new (new web3_1.default()).eth.Contract(erc721_abi_1.default, contractAddress.trim()); if (signatureId) { return JSON.stringify({ chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce, to: contractAddress.trim(), gasLimit: '0', data: contract.methods.mintWithTokenURI(to.trim(), tokenId, url).encodeABI(), }); } const wallet = new celo_ethers_wrapper_1.CeloWallet(fromPrivateKey, p); const { txCount, gasPrice, from } = await obtainWalletInformation(wallet, feeCurrencyContractAddress); const transaction = { chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce: nonce || txCount, gasLimit: '0', to: contractAddress.trim(), gasPrice, data: contract.methods.mintWithTokenURI(to.trim(), tokenId, url).encodeABI(), from, }; transaction.gasLimit = (await wallet.estimateGas(transaction)).add(feeCurrency === model_1.Currency.CELO ? 0 : 100000).toHexString(); return wallet.signTransaction(transaction); } throw new Error('Contract address and fee currency should not be empty!'); }; exports.prepareCeloMintErc721SignedTransaction = prepareCeloMintErc721SignedTransaction; /** * Prepare a signed Celo transfer erc721 transaction with the private key locally. Nothing is broadcasted to the blockchain. * @returns raw transaction data in hex, to be broadcasted to blockchain. */ const prepareCeloTransferErc721SignedTransaction = async (testnet, body, provider) => { await tatum_1.validateBody(body, model_1.CeloTransferErc721); const { fromPrivateKey, to, tokenId, contractAddress, feeCurrency, nonce, signatureId, value, provenance, provenanceData, tokenPrice, } = body; const p = new celo_ethers_wrapper_1.CeloProvider(provider || `${process.env.TATUM_API_URL || constants_1.TATUM_API_URL}/v3/celo/web3/${process.env.TATUM_API_KEY}`); const network = await p.ready; const feeCurrencyContractAddress = getFeeCurrency(feeCurrency, testnet); // @ts-ignore const contract = new (new web3_1.default()).eth.Contract(provenance ? erc721Provenance_abi_1.default : erc721_abi_1.default, contractAddress.trim()); const dataBytes = provenance ? Buffer.from(provenanceData + '\'\'\'###\'\'\'' + web3_utils_1.toWei(tokenPrice, 'ether'), 'utf8') : ''; const tokenData = provenance ? contract.methods.safeTransfer(to.trim(), tokenId, `0x${dataBytes.toString('hex')}`).encodeABI() : contract.methods.safeTransfer(to.trim(), tokenId).encodeABI(); if (signatureId) { return JSON.stringify({ chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, gasLimit: '0', nonce, to: contractAddress.trim(), data: tokenData, value: value ? `0x${new bignumber_js_1.BigNumber(value).multipliedBy(1e18).toString(16)}` : undefined, }); } const wallet = new celo_ethers_wrapper_1.CeloWallet(fromPrivateKey, p); const { txCount, gasPrice, from } = await obtainWalletInformation(wallet, feeCurrencyContractAddress); const transaction = { chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce: nonce || txCount, gasLimit: '0', to: contractAddress.trim(), gasPrice, data: tokenData, from, value: value ? `0x${new bignumber_js_1.BigNumber(value).multipliedBy(1e18).toString(16)}` : undefined, }; transaction.gasLimit = (await wallet.estimateGas(transaction)).add(feeCurrency === model_1.Currency.CELO ? 0 : 100000).toHexString(); return wallet.signTransaction(transaction); }; exports.prepareCeloTransferErc721SignedTransaction = prepareCeloTransferErc721SignedTransaction; /** * Prepare a signed Celo burn erc721 transaction with the private key locally. Nothing is broadcasted to the blockchain. * @returns raw transaction data in hex, to be broadcasted to blockchain. */ const prepareCeloBurnErc721SignedTransaction = async (testnet, body, provider) => { await tatum_1.validateBody(body, model_1.CeloBurnErc721); const { fromPrivateKey, tokenId, contractAddress, feeCurrency, nonce, signatureId, } = body; const p = new celo_ethers_wrapper_1.CeloProvider(provider || `${process.env.TATUM_API_URL || constants_1.TATUM_API_URL}/v3/celo/web3/${process.env.TATUM_API_KEY}`); const network = await p.ready; const feeCurrencyContractAddress = getFeeCurrency(feeCurrency, testnet); // @ts-ignore const contract = new (new web3_1.default()).eth.Contract(erc721_abi_1.default, contractAddress.trim()); if (signatureId) { return JSON.stringify({ chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce, gasLimit: '0', to: contractAddress.trim(), data: contract.methods.burn(tokenId).encodeABI(), }); } const wallet = new celo_ethers_wrapper_1.CeloWallet(fromPrivateKey, p); const { txCount, gasPrice, from } = await obtainWalletInformation(wallet, feeCurrencyContractAddress); const transaction = { chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce: nonce || txCount, gasLimit: '0', to: contractAddress.trim(), gasPrice, data: contract.methods.burn(tokenId).encodeABI(), from, }; transaction.gasLimit = (await wallet.estimateGas(transaction)).add(feeCurrency === model_1.Currency.CELO ? 0 : 100000).toHexString(); return wallet.signTransaction(transaction); }; exports.prepareCeloBurnErc721SignedTransaction = prepareCeloBurnErc721SignedTransaction; /** * Prepare a signed Celo deploy erc20 transaction with the private key locally. Nothing is broadcasted to the blockchain. * @returns raw transaction data in hex, to be broadcasted to blockchain. */ const prepareCeloDeployErc20SignedTransaction = async (testnet, body, provider) => { await tatum_1.validateBody(body, model_1.DeployCeloErc20); const { fromPrivateKey, name, symbol, supply, address, digits, feeCurrency, nonce, signatureId, totalCap, } = body; const p = new celo_ethers_wrapper_1.CeloProvider(provider || `${process.env.TATUM_API_URL || constants_1.TATUM_API_URL}/v3/celo/web3/${process.env.TATUM_API_KEY}`); const network = await p.ready; const feeCurrencyContractAddress = getFeeCurrency(feeCurrency, testnet); // @ts-ignore const contract = new (new web3_1.default()).eth.Contract(token_abi_1.default); const deploy = contract.deploy({ data: token_bytecode_1.default, arguments: [ name, symbol, address, digits, `0x${new bignumber_js_1.BigNumber(totalCap || supply).multipliedBy(new bignumber_js_1.BigNumber(10).pow(digits)).toString(16)}`, `0x${new bignumber_js_1.BigNumber(supply).multipliedBy(new bignumber_js_1.BigNumber(10).pow(digits)).toString(16)}`, ], }); if (signatureId) { return JSON.stringify({ chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce, gasLimit: '0', data: deploy.encodeABI(), }); } const wallet = new celo_ethers_wrapper_1.CeloWallet(fromPrivateKey, p); const { txCount, gasPrice, from } = await obtainWalletInformation(wallet, feeCurrencyContractAddress); const transaction = { chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce: nonce || txCount, gasLimit: '0', gasPrice, data: deploy.encodeABI(), from, }; transaction.gasLimit = (await wallet.estimateGas(transaction)).add(feeCurrency === model_1.Currency.CELO ? 0 : 100000).toHexString(); return wallet.signTransaction(transaction); }; exports.prepareCeloDeployErc20SignedTransaction = prepareCeloDeployErc20SignedTransaction; /** * Prepare a signed Celo mint erc20 transaction with the private key locally. Nothing is broadcasted to the blockchain. * @returns raw transaction data in hex, to be broadcasted to blockchain. */ const prepareCeloMintErc20SignedTransaction = async (testnet, body, provider) => { await tatum_1.validateBody(body, model_1.MintCeloErc20); const { fromPrivateKey, amount, to, contractAddress, feeCurrency, nonce, signatureId, } = body; const url = provider || `${process.env.TATUM_API_URL || constants_1.TATUM_API_URL}/v3/celo/web3/${process.env.TATUM_API_KEY}`; const p = new celo_ethers_wrapper_1.CeloProvider(url); const network = await p.ready; const feeCurrencyContractAddress = getFeeCurrency(feeCurrency, testnet); // @ts-ignore const contract = new (new web3_1.default(url)).eth.Contract(token_abi_1.default, contractAddress.trim()); const decimals = await contract.methods.decimals().call(); if (signatureId) { return JSON.stringify({ chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce, gasLimit: '0', to: contractAddress.trim(), data: contract.methods.mint(to.trim(), '0x' + new bignumber_js_1.BigNumber(amount).multipliedBy(10 ** decimals).toString(16)).encodeABI(), }); } const wallet = new celo_ethers_wrapper_1.CeloWallet(fromPrivateKey, p); const { txCount, gasPrice, from } = await obtainWalletInformation(wallet, feeCurrencyContractAddress); const transaction = { chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce: nonce || txCount, gasLimit: '0', to: contractAddress.trim(), gasPrice, data: contract.methods.mint(to.trim(), '0x' + new bignumber_js_1.BigNumber(amount).multipliedBy(10 ** decimals).toString(16)).encodeABI(), from, }; transaction.gasLimit = (await wallet.estimateGas(transaction)).add(feeCurrency === model_1.Currency.CELO ? 0 : 100000).toHexString(); return wallet.signTransaction(transaction); }; exports.prepareCeloMintErc20SignedTransaction = prepareCeloMintErc20SignedTransaction; const getCeloClient = (provider) => new web3_1.default(provider || `${process.env.TATUM_API_URL || constants_1.TATUM_API_URL}/v3/celo/web3/${process.env.TATUM_API_KEY}`); exports.getCeloClient = getCeloClient; /** * Prepare a smart contract write method invocation transaction with the private key locally. Nothing is broadcasted to the blockchain. * @returns raw transaction data in hex, to be broadcasted to blockchain. */ const prepareCeloSmartContractWriteMethodInvocation = async (testnet, body, provider) => { var _a; await tatum_1.validateBody(body, model_1.CeloSmartContractMethodInvocation); const { fromPrivateKey, feeCurrency, fee, params, methodName, methodABI, contractAddress, nonce, signatureId, amount, } = body; const url = provider || `${process.env.TATUM_API_URL || constants_1.TATUM_API_URL}/v3/celo/web3/${process.env.TATUM_API_KEY}`; const p = new celo_ethers_wrapper_1.CeloProvider(url); const network = await p.ready; const feeCurrencyContractAddress = getFeeCurrency(feeCurrency, testnet); // @ts-ignore const contract = new (new web3_1.default(url)).eth.Contract([methodABI], contractAddress.trim()); const transaction = { chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce, value: amount ? `0x${new bignumber_js_1.BigNumber(web3_utils_1.toWei(amount, 'ether')).toString(16)}` : undefined, gasLimit: (fee === null || fee === void 0 ? void 0 : fee.gasLimit) ? `0x${new bignumber_js_1.BigNumber(fee.gasLimit).toString(16)}` : undefined, gasPrice: (fee === null || fee === void 0 ? void 0 : fee.gasPrice) ? `0x${new bignumber_js_1.BigNumber(web3_utils_1.toWei(fee.gasPrice, 'gwei')).toString(16)}` : undefined, to: contractAddress.trim(), data: contract.methods[methodName](...params).encodeABI(), }; if (signatureId) { return JSON.stringify(transaction); } const wallet = new celo_ethers_wrapper_1.CeloWallet(fromPrivateKey, p); const { txCount, gasPrice, from } = await obtainWalletInformation(wallet, feeCurrencyContractAddress); transaction.nonce = transaction.nonce || txCount; transaction.from = from; const estimateGas = (await wallet.estimateGas(transaction)).add(feeCurrency === model_1.Currency.CELO ? 0 : 100000); transaction.gasLimit = (_a = transaction.gasLimit) !== null && _a !== void 0 ? _a : estimateGas.toHexString(); transaction.gasPrice = (fee === null || fee === void 0 ? void 0 : fee.gasPrice) ? '0x' + new bignumber_js_1.BigNumber(web3_utils_1.toWei(fee.gasPrice, 'gwei')).toString(16) : gasPrice.toHexString(); return wallet.signTransaction(transaction); }; exports.prepareCeloSmartContractWriteMethodInvocation = prepareCeloSmartContractWriteMethodInvocation; /** * Prepare a signed Celo smart contract read method invocation transaction with the private key locally. Nothing is broadcasted to the blockchain. * @returns raw transaction data in hex, to be broadcasted to blockchain. */ const sendCeloSmartContractReadMethodInvocationTransaction = async (testnet, body, provider) => { await tatum_1.validateBody(body, model_1.SmartContractReadMethodInvocation); const { params, methodName, methodABI, contractAddress, } = body; const url = provider || `${process.env.TATUM_API_URL || constants_1.TATUM_API_URL}/v3/celo/web3/${process.env.TATUM_API_KEY}`; // @ts-ignore const contract = new (new web3_1.default(url)).eth.Contract([methodABI], contractAddress.trim()); return { data: await contract.methods[methodName](...params).call() }; }; exports.sendCeloSmartContractReadMethodInvocationTransaction = sendCeloSmartContractReadMethodInvocationTransaction; const sendCeloDeployErc20Transaction = async (testnet, body, provider) => blockchain_1.celoBroadcast(await exports.prepareCeloDeployErc20SignedTransaction(testnet, body, provider), body.signatureId); exports.sendCeloDeployErc20Transaction = sendCeloDeployErc20Transaction; const sendCeloStoreDataSignedTransaction = async (testnet, body, provider) => blockchain_1.celoBroadcast(await exports.prepareCeloStoreDataSignedTransaction(testnet, body, provider), body.signatureId); exports.sendCeloStoreDataSignedTransaction = sendCeloStoreDataSignedTransaction; /** * Send Celo smart contract method invocation transaction to the blockchain. This method broadcasts signed transaction to the blockchain. * This operation is irreversible. * @param testnet * @param body content of the transaction to broadcast * @param provider * @returns transaction id of the transaction in the blockchain */ const sendCeloSmartContractMethodInvocationTransaction = async (testnet, body, provider) => { if (body.methodABI.stateMutability === 'view') { return exports.sendCeloSmartContractReadMethodInvocationTransaction(testnet, body, provider); } const celoBody = body; return blockchain_1.celoBroadcast(await exports.prepareCeloSmartContractWriteMethodInvocation(testnet, celoBody, provider), celoBody.signatureId); }; exports.sendCeloSmartContractMethodInvocationTransaction = sendCeloSmartContractMethodInvocationTransaction; const getCeloErc20ContractDecimals = async (testnet, contractAddress, provider) => { if (!contractAddress) { throw new Error('Contract address not set.'); } const url = provider || `${process.env.TATUM_API_URL || constants_1.TATUM_API_URL}/v3/celo/web3/${process.env.TATUM_API_KEY}`; // @ts-ignore const contract = new (new web3_1.default(url)).eth.Contract(token_abi_1.default, contractAddress.trim()); return await contract.methods.decimals().call(); }; exports.getCeloErc20ContractDecimals = getCeloErc20ContractDecimals; /** * Prepare a signed Celo transfer erc20 transaction with the private key locally. Nothing is broadcasted to the blockchain. * @returns raw transaction data in hex, to be broadcasted to blockchain. */ const prepareCeloTransferErc20SignedTransaction = async (testnet, body, provider) => { await tatum_1.validateBody(body, model_1.TransferCeloOrCeloErc20Token); const { fromPrivateKey, to, amount, contractAddress, feeCurrency, nonce, signatureId, fee, } = body; if (!contractAddress) { throw new Error('Contract address not set.'); } const url = provider || `${process.env.TATUM_API_URL || constants_1.TATUM_API_URL}/v3/celo/web3/${process.env.TATUM_API_KEY}`; const p = new celo_ethers_wrapper_1.CeloProvider(url); const network = await p.ready; const feeCurrencyContractAddress = getFeeCurrency(feeCurrency, testnet); // @ts-ignore const contract = new (new web3_1.default(url)).eth.Contract(token_abi_1.default, contractAddress.trim()); const decimals = await contract.methods.decimals().call(); if (signatureId) { return JSON.stringify({ chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce, gasLimit: (fee === null || fee === void 0 ? void 0 : fee.gasLimit) ? '0x' + new bignumber_js_1.BigNumber(fee.gasLimit).toString(16) : undefined, gasPrice: (fee === null || fee === void 0 ? void 0 : fee.gasPrice) ? '0x' + new bignumber_js_1.BigNumber(web3_utils_1.toWei(fee.gasPrice, 'gwei')).toString(16) : undefined, to: contractAddress.trim(), data: contract.methods.transfer(to.trim(), '0x' + new bignumber_js_1.BigNumber(amount).multipliedBy(10 ** decimals).toString(16)).encodeABI(), }); } const wallet = new celo_ethers_wrapper_1.CeloWallet(fromPrivateKey, p); const { txCount, gasPrice, from } = await obtainWalletInformation(wallet, feeCurrencyContractAddress); const transaction = { chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce: nonce || txCount, gasLimit: (fee === null || fee === void 0 ? void 0 : fee.gasLimit) ? '0x' + new bignumber_js_1.BigNumber(fee.gasLimit).toString(16) : undefined, to: contractAddress.trim(), gasPrice: (fee === null || fee === void 0 ? void 0 : fee.gasPrice) ? '0x' + new bignumber_js_1.BigNumber(web3_utils_1.toWei(fee.gasPrice, 'gwei')).toString(16) : gasPrice, data: contract.methods.transfer(to.trim(), '0x' + new bignumber_js_1.BigNumber(amount).multipliedBy(10 ** decimals).toString(16)).encodeABI(), from, }; transaction.gasLimit = transaction.gasLimit || (await wallet.estimateGas(transaction)).add(feeCurrency === model_1.Currency.CELO ? 0 : 100000).toHexString(); return wallet.signTransaction(transaction); }; exports.prepareCeloTransferErc20SignedTransaction = prepareCeloTransferErc20SignedTransaction; /** * Prepare a signed Celo burn erc20 transaction with the private key locally. Nothing is broadcasted to the blockchain. * @returns raw transaction data in hex, to be broadcasted to blockchain. */ const prepareCeloBurnErc20SignedTransaction = async (testnet, body, provider) => { await tatum_1.validateBody(body, model_1.BurnCeloErc20); const { fromPrivateKey, amount, contractAddress, feeCurrency, nonce, signatureId, } = body; const url = provider || `${process.env.TATUM_API_URL || constants_1.TATUM_API_URL}/v3/celo/web3/${process.env.TATUM_API_KEY}`; const p = new celo_ethers_wrapper_1.CeloProvider(url); const network = await p.ready; const feeCurrencyContractAddress = getFeeCurrency(feeCurrency, testnet); // @ts-ignore const contract = new (new web3_1.default(url)).eth.Contract(token_abi_1.default, contractAddress.trim()); const decimals = await contract.methods.decimals().call(); if (signatureId) { return JSON.stringify({ chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce, gasLimit: '0', to: contractAddress.trim(), data: contract.methods.burn('0x' + new bignumber_js_1.BigNumber(amount).multipliedBy(10 ** decimals).toString(16)).encodeABI(), }); } const wallet = new celo_ethers_wrapper_1.CeloWallet(fromPrivateKey, p); const { txCount, gasPrice, from } = await obtainWalletInformation(wallet, feeCurrencyContractAddress); const transaction = { chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce: nonce || txCount, gasLimit: '0', to: contractAddress.trim(), gasPrice, data: contract.methods.burn('0x' + new bignumber_js_1.BigNumber(amount).multipliedBy(10 ** decimals).toString(16)).encodeABI(), from, }; transaction.gasLimit = (await wallet.estimateGas(transaction)).add(feeCurrency === model_1.Currency.CELO ? 0 : 100000).toHexString(); return wallet.signTransaction(transaction); }; exports.prepareCeloBurnErc20SignedTransaction = prepareCeloBurnErc20SignedTransaction; /** * Prepare a signed Celo mint multiple cashback erc721 transaction with the private key locally. Nothing is broadcasted to the blockchain. * @returns raw transaction data in hex, to be broadcasted to blockchain. */ const prepareCeloMintMultipleCashbackErc721SignedTransaction = async (testnet, body, provider) => { await tatum_1.validateBody(body, model_1.CeloMintMultipleErc721); const { fromPrivateKey, to, tokenId, contractAddress, url, feeCurrency, nonce, signatureId, authorAddresses, cashbackValues, erc20, } = body; const p = new celo_ethers_wrapper_1.CeloProvider(provider || `${process.env.TATUM_API_URL || constants_1.TATUM_API_URL}/v3/celo/web3/${process.env.TATUM_API_KEY}`); const network = await p.ready; const feeCurrencyContractAddress = getFeeCurrency(feeCurrency, testnet); // @ts-ignore const contract = new (new web3_1.default()).eth.Contract(erc721_abi_1.default, contractAddress.trim()); const cashbacks = cashbackValues; const cb = []; for (const c of cashbacks) { const cb2 = []; for (const c2 of c) { cb2.push(`0x${new bignumber_js_1.BigNumber(web3_utils_1.toWei(c2, 'ether')).toString(16)}`); } cb.push(cb2); } if (signatureId) { return JSON.stringify({ chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce, gasLimit: '0', to: contractAddress.trim(), data: erc20 ? contract.methods.mintMultipleCashback(to.map(t => t.trim()), tokenId, url, authorAddresses, cb, erc20).encodeABI() : contract.methods.mintMultipleCashback(to.map(t => t.trim()), tokenId, url, authorAddresses, cb).encodeABI(), }); } const wallet = new celo_ethers_wrapper_1.CeloWallet(fromPrivateKey, p); const { txCount, gasPrice, from } = await obtainWalletInformation(wallet, feeCurrencyContractAddress); const transaction = { chainId: network.chainId, feeCurrency: feeCurrencyContractAddress, nonce: nonce || txCount, gasLimit: '0', to: contractAddress.trim(), gasPrice, data: erc20 ? contract.methods.mintMultipleCashback(to.map(t => t.trim()), tokenId, url, authorAddresses, cb, erc20).encodeABI() : contract.methods.mintMultipleCashback(to.map(t => t.trim()), tokenId, url, authorAddresses, cb).encodeABI(), from, }; transaction.gasLimit = (await wallet.estimateGas(transaction)).add(feeCurrency === model_1.Currency.CELO ? 0 : 100000).toHexString(); return wallet.signTransaction(transaction); }; exports.prepareCeloMintMultipleCashbackErc721SignedTransaction = prepareCeloMintMultipleCashbackErc721SignedTransaction; /** * Prepare a signed Celo mint multiple erc721 transaction with the private key locally. Nothing is broadcasted to the blockchain. * @returns raw transaction data in hex, to be broadcasted to block