@tatumio/tatum-v1
Version:
Tatum API client allows browsers and Node.js clients to interact with Tatum API.
857 lines • 108 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendEthMultiTokenBatchTransaction = exports.sendEthMultiTokenTransaction = exports.sendDeployErc721Transaction = exports.sendErc721Transaction = exports.sendUpdateCashbackForAuthorErc721Transaction = exports.sendBurnErc721Transaction = exports.sendMintMultipleErc721Transaction = exports.sendMintMultipleErc721ProvenanceTransaction = exports.sendEthMintMultipleCashbackErc721SignedTransaction = exports.sendMintErc721ProvenanceTransaction = exports.sendMintCashbackErc721Transaction = exports.sendMintErc721Transaction = exports.sendSmartContractMethodInvocationTransaction = exports.sendDeployErc20Transaction = exports.sendCustomErc20Transaction = exports.sendEthOrErc20Transaction = exports.sendStoreDataTransaction = exports.sendSmartContractReadMethodInvocationTransaction = exports.getEthErc20ContractDecimals = exports.prepareEthDeployErc721SignedTransaction = exports.prepareEthDeployMultiTokenSignedTransaction = exports.prepareEthTransferMultiTokenSignedTransaction = exports.prepareEthBatchTransferMultiTokenSignedTransaction = exports.prepareEthBurnMultiTokenSignedTransaction = exports.prepareEthBurnBatchMultiTokenSignedTransaction = exports.prepareEthMintMultiTokenSignedTransaction = exports.prepareEthMintMultiTokenBatchSignedTransaction = exports.prepareEthTransferErc721SignedTransaction = exports.prepareEthUpdateCashbackForAuthorErc721SignedTransaction = exports.prepareEthBurnErc721SignedTransaction = exports.prepareEthMintMultipleErc721SignedTransaction = exports.prepareEthMintMultipleCashbackErc721SignedTransaction = exports.prepareEthMintCashbackErc721SignedTransaction = exports.prepareEthMintErc721SignedTransaction = exports.prepareEthMintMultipleErc721ProvenanceSignedTransaction = exports.prepareEthMintErc721ProvenanceSignedTransaction = exports.prepareEthDeployAuctionSignedTransaction = exports.prepareEthDeployMarketplaceListingSignedTransaction = exports.prepareSmartContractWriteMethodInvocation = exports.prepareDeployErc20SignedTransaction = exports.prepareCustomErc20SignedTransaction = exports.prepareEthOrErc20SignedTransaction = exports.prepareEthBurnErc20SignedTransaction = exports.prepareEthMintErc20SignedTransaction = exports.prepareStoreDataTransaction = exports.prepareEthGenerateCustodialWalletSignedTransaction = exports.signEthKMSTransaction = exports.getClient = exports.ethGetGasPriceInWeiWrapper = exports.ethGetGasPriceInWei = void 0;
exports.sendEthDeployMarketplaceListingSignedTransaction = exports.sendEthGenerateCustodialWalletSignedTransaction = exports.sendEthBurnBatchMultiTokenTransaction = exports.sendEthBurnMultiTokenTransaction = exports.sendEthMintMultiTokenBatchTransaction = exports.sendEthMintMultiTokenTransaction = exports.sendEthDeployMultiTokenTransaction = void 0;
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_abi_2 = __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 wallet_1 = require("../wallet");
const erc721_abi_2 = __importDefault(require("../contracts/erc721General/erc721_abi"));
const erc721_bytecode_2 = __importDefault(require("../contracts/erc721General/erc721_bytecode"));
/**
* Estimate Gas price for the transaction.
*/
const ethGetGasPriceInWei = async () => {
const result = await tatum_1.get(`/v3/blockchain/fee/ETH`);
return result.medium.toString();
};
exports.ethGetGasPriceInWei = ethGetGasPriceInWei;
/**
* (!) Internal function
* Used to be able to override it later
*/
exports.ethGetGasPriceInWeiWrapper = {
ethGetGasPriceInWei: exports.ethGetGasPriceInWei,
};
/**
* Returns Ethereum server to connect to.
*
* @param provider url of the Ethereum Server to connect to. If not set, default public server will be used.
* @param privateKey
*/
const getClient = (provider, privateKey) => {
let url = provider || `${process.env.TATUM_API_URL || constants_1.TATUM_API_URL}/v3/ethereum/web3/${process.env.TATUM_API_KEY}`;
if (process.env.TESTNET_TYPE === 'ethereum-rinkeby') {
url += '?testnetType=ethereum-rinkeby';
}
const web3 = new web3_1.default(url);
if (privateKey) {
web3.eth.accounts.wallet.add(privateKey);
web3.eth.defaultAccount = web3.eth.accounts.wallet[0].address;
}
return web3;
};
exports.getClient = getClient;
/**
* Sign Ethereum pending transaction from Tatum KMS
* @param tx pending transaction from KMS
* @param fromPrivateKey private key to sign transaction with.
* @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.
*/
const signEthKMSTransaction = async (tx, fromPrivateKey, provider) => {
if (tx.chain !== model_1.Currency.ETH) {
throw Error('Unsupported chain.');
}
const client = exports.getClient(provider, fromPrivateKey);
const transactionConfig = JSON.parse(tx.serializedTransaction);
const gas = await client.eth.estimateGas(transactionConfig);
if (!transactionConfig.gas) {
transactionConfig.gas = gas;
}
if (!transactionConfig.nonce) {
transactionConfig.nonce = await blockchain_1.ethGetTransactionsCount(client.eth.defaultAccount);
}
if (!transactionConfig.gasPrice || transactionConfig.gasPrice === '0' || transactionConfig.gasPrice === 0 || transactionConfig.gasPrice === '0x0') {
transactionConfig.gasPrice = await exports.ethGetGasPriceInWeiWrapper.ethGetGasPriceInWei();
}
return (await client.eth.accounts.signTransaction(transactionConfig, fromPrivateKey)).rawTransaction;
};
exports.signEthKMSTransaction = signEthKMSTransaction;
/**
* Sign Eth generate custodial wallet address transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @param provider url of the ETH Server to connect to. If not set, default public server will be used.
* @returns transaction data to be broadcast to blockchain.
*/
const prepareEthGenerateCustodialWalletSignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.GenerateCustodialAddress);
const client = exports.getClient(provider, body.fromPrivateKey);
const { abi, code } = wallet_1.obtainCustodialAddressType(body);
// @ts-ignore
const contract = new client.eth.Contract(abi);
const deploy = contract.deploy({
data: code,
});
const tx = {
from: 0,
data: deploy.encodeABI(),
nonce: body.nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, body.signatureId, body.fromPrivateKey, body.fee);
};
exports.prepareEthGenerateCustodialWalletSignedTransaction = prepareEthGenerateCustodialWalletSignedTransaction;
/**
* Sign Ethereum Store data transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareStoreDataTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.CreateRecord);
const { fromPrivateKey, to, ethFee, data, nonce, signatureId, } = body;
const client = exports.getClient(provider, fromPrivateKey);
const address = (to || client.eth.defaultAccount);
const hexData = client.utils.isHex(data) ? client.utils.stringToHex(data) : client.utils.toHex(data);
const addressNonce = nonce ? nonce : await blockchain_1.ethGetTransactionsCount(address);
const customFee = ethFee ? Object.assign(Object.assign({}, ethFee), { gasPrice: client.utils.toWei(ethFee.gasPrice, 'gwei') }) : {
gasLimit: `${hexData.length * 68 + 21000}`,
gasPrice: await exports.ethGetGasPriceInWeiWrapper.ethGetGasPriceInWei(),
};
const tx = {
from: 0,
to: address.trim(),
value: '0',
gasPrice: customFee.gasPrice,
gas: customFee.gasLimit,
data: hexData,
nonce: addressNonce,
};
if (signatureId) {
return JSON.stringify(tx);
}
return (await client.eth.accounts.signTransaction(tx, fromPrivateKey)).rawTransaction;
};
exports.prepareStoreDataTransaction = prepareStoreDataTransaction;
/**
* Sign Ethereum mint ERC 20 transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareEthMintErc20SignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.MintErc20);
const { fromPrivateKey, amount, to, contractAddress, nonce, signatureId, } = body;
const client = exports.getClient(provider, fromPrivateKey);
// @ts-ignore
const contract = new client.eth.Contract(token_abi_2.default, contractAddress.trim());
const digits = new bignumber_js_1.BigNumber(10).pow(await contract.methods.decimals().call());
const tx = {
from: 0,
to: contractAddress.trim(),
data: contract.methods.mint(to.trim(), `0x${new bignumber_js_1.BigNumber(amount).multipliedBy(digits).toString(16)}`).encodeABI(),
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey);
};
exports.prepareEthMintErc20SignedTransaction = prepareEthMintErc20SignedTransaction;
const prepareEthSignedTransactionAbstraction = async (client, transaction, signatureId, fromPrivateKey, fee) => {
var _a;
const gasPrice = fee ? client.utils.toWei(fee.gasPrice, 'gwei') : await exports.ethGetGasPriceInWeiWrapper.ethGetGasPriceInWei();
const tx = Object.assign(Object.assign({}, transaction), { gasPrice, gas: (fee === null || fee === void 0 ? void 0 : fee.gasLimit) || undefined });
if (signatureId) {
return JSON.stringify(tx);
}
const estimatedGas = await client.eth.estimateGas(tx);
tx.gas = (_a = fee === null || fee === void 0 ? void 0 : fee.gasLimit) !== null && _a !== void 0 ? _a : estimatedGas;
return (await client.eth.accounts.signTransaction(tx, fromPrivateKey)).rawTransaction;
};
/**
* Sign Ethereum burn ERC 721 transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareEthBurnErc20SignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.BurnErc20);
const { fromPrivateKey, amount, contractAddress, nonce, signatureId, } = body;
const client = exports.getClient(provider, fromPrivateKey);
// @ts-ignore
const contract = new client.eth.Contract(token_abi_2.default, contractAddress.trim());
const digits = new bignumber_js_1.BigNumber(10).pow(await contract.methods.decimals().call());
const tx = {
from: 0,
to: contractAddress.trim(),
data: contract.methods.burn(`0x${new bignumber_js_1.BigNumber(amount).multipliedBy(digits).toString(16)}`).encodeABI(),
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey);
};
exports.prepareEthBurnErc20SignedTransaction = prepareEthBurnErc20SignedTransaction;
/**
* Sign Ethereum or supported ERC20 transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareEthOrErc20SignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.TransferErc20);
const { fromPrivateKey, to, amount, currency, fee, data, nonce, signatureId, } = body;
const client = exports.getClient(provider, fromPrivateKey);
let tx;
if (currency === model_1.Currency.ETH) {
tx = {
from: 0,
to: to.trim(),
value: client.utils.toWei(`${amount}`, 'ether'),
data: data ? (client.utils.isHex(data) ? client.utils.stringToHex(data) : client.utils.toHex(data)) : undefined,
nonce,
};
}
else {
// @ts-ignore
const contract = new client.eth.Contract([constants_1.TRANSFER_METHOD_ABI], constants_1.CONTRACT_ADDRESSES[currency]);
const digits = new bignumber_js_1.BigNumber(10).pow(constants_1.CONTRACT_DECIMALS[currency]);
tx = {
from: 0,
to: constants_1.CONTRACT_ADDRESSES[currency],
data: contract.methods.transfer(to.trim(), `0x${new bignumber_js_1.BigNumber(amount).multipliedBy(digits).toString(16)}`).encodeABI(),
nonce,
};
}
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
};
exports.prepareEthOrErc20SignedTransaction = prepareEthOrErc20SignedTransaction;
/**
* Sign Ethereum custom ERC20 transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareCustomErc20SignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.TransferErc20);
const { fromPrivateKey, to, amount, contractAddress, digits, fee, nonce, signatureId, } = body;
const client = exports.getClient(provider, fromPrivateKey);
// @ts-ignore
const contract = new client.eth.Contract([constants_1.TRANSFER_METHOD_ABI], contractAddress);
const decimals = new bignumber_js_1.BigNumber(10).pow(digits);
const tx = {
from: 0,
to: contractAddress,
data: contract.methods.transfer(to.trim(), `0x${new bignumber_js_1.BigNumber(amount).multipliedBy(decimals).toString(16)}`).encodeABI(),
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
};
exports.prepareCustomErc20SignedTransaction = prepareCustomErc20SignedTransaction;
/**
* Sign Ethereum deploy ERC20 transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareDeployErc20SignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.DeployErc20);
const { name, address, symbol, supply, digits, fromPrivateKey, nonce, fee, signatureId, totalCap, } = body;
const client = exports.getClient(provider, fromPrivateKey);
// @ts-ignore
const contract = new client.eth.Contract(token_abi_2.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)}`,
],
});
const tx = {
from: 0,
data: deploy.encodeABI(),
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
};
exports.prepareDeployErc20SignedTransaction = prepareDeployErc20SignedTransaction;
/**
* Sign Ethereum invoke smart contract transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareSmartContractWriteMethodInvocation = async (body, provider) => {
await tatum_1.validateBody(body, model_1.SmartContractMethodInvocation);
const { fromPrivateKey, fee, params, methodName, methodABI, amount, contractAddress, nonce, signatureId, } = body;
const client = exports.getClient(provider, fromPrivateKey);
const contract = new client.eth.Contract([methodABI]);
const tx = {
from: 0,
to: contractAddress.trim(),
value: amount ? `0x${new bignumber_js_1.BigNumber(web3_utils_1.toWei(amount, 'ether')).toString(16)}` : undefined,
data: contract.methods[methodName](...params).encodeABI(),
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
};
exports.prepareSmartContractWriteMethodInvocation = prepareSmartContractWriteMethodInvocation;
const deployContract = async (abi, bytecode, args, fromPrivateKey, fee, nonce, signatureId, provider) => {
const client = await exports.getClient(provider, fromPrivateKey);
// @ts-ignore
const contract = new client.eth.Contract(abi, null, {
data: bytecode,
});
// @ts-ignore
const deploy = contract.deploy({
arguments: args,
});
const tx = {
from: 0,
data: deploy.encodeABI(),
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
};
/**
* Sign ETH generate custodial wallet address transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @param provider url of the ETH 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 prepareEthDeployMarketplaceListingSignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.DeployMarketplaceListing);
return deployContract(marketplace_1.listing.abi, marketplace_1.listing.data, [body.marketplaceFee, body.feeRecipient], body.fromPrivateKey, body.fee, body.nonce, body.signatureId, provider);
};
exports.prepareEthDeployMarketplaceListingSignedTransaction = prepareEthDeployMarketplaceListingSignedTransaction;
/**
* Sign ETH deploy NFT Auction contract transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @param provider url of the ETH 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 prepareEthDeployAuctionSignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.DeployNftAuction);
return deployContract(marketplace_1.auction.abi, marketplace_1.auction.data, [body.auctionFee, body.feeRecipient], body.fromPrivateKey, body.fee, body.nonce, body.signatureId, provider);
};
exports.prepareEthDeployAuctionSignedTransaction = prepareEthDeployAuctionSignedTransaction;
/**
* Sign Ethereum mint ERC 721 provenance transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareEthMintErc721ProvenanceSignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.EthMintErc721);
const { fromPrivateKey, to, tokenId, contractAddress, nonce, fee, url, signatureId, authorAddresses, cashbackValues, fixedValues, erc20, } = body;
const client = exports.getClient(provider, fromPrivateKey);
// @ts-ignore
const contract = new (client).eth.Contract(erc721Provenance_abi_1.default, contractAddress);
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 (contractAddress) {
const tx = {
from: 0,
to: contractAddress.trim(),
data: data,
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
}
throw new Error('Contract address should not be empty!');
};
exports.prepareEthMintErc721ProvenanceSignedTransaction = prepareEthMintErc721ProvenanceSignedTransaction;
/**
* Sign Ethereum mint multiple ERC 721 Provenance transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareEthMintMultipleErc721ProvenanceSignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.EthMintMultipleErc721);
const { fromPrivateKey, to, tokenId, contractAddress, url, nonce, signatureId, authorAddresses, cashbackValues, fixedValues, fee, erc20, } = body;
const client = await exports.getClient(provider, fromPrivateKey);
// @ts-ignore
const contract = new (client).eth.Contract(erc721Provenance_abi_1.default, contractAddress);
const cb = [];
const fv = [];
if (cashbackValues && fixedValues && authorAddresses) {
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();
const tx = {
from: 0,
to: contractAddress.trim(),
data: data,
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
};
exports.prepareEthMintMultipleErc721ProvenanceSignedTransaction = prepareEthMintMultipleErc721ProvenanceSignedTransaction;
/**
* Sign Ethereum mint ERC 721 transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareEthMintErc721SignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.EthMintErc721);
const { fromPrivateKey, to, tokenId, contractAddress, nonce, fee, url, signatureId, } = body;
const client = exports.getClient(provider, fromPrivateKey);
// @ts-ignore
const contract = new (client).eth.Contract(erc721_abi_1.default, contractAddress);
if (contractAddress) {
const tx = {
from: 0,
to: contractAddress.trim(),
data: contract.methods.mintWithTokenURI(to.trim(), tokenId, url).encodeABI(),
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
}
throw new Error('Contract address should not be empty!');
};
exports.prepareEthMintErc721SignedTransaction = prepareEthMintErc721SignedTransaction;
/**
* Sign Ethereum mint multiple ERC 721 transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareEthMintCashbackErc721SignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.EthMintErc721);
const { fromPrivateKey, to, tokenId, contractAddress, authorAddresses, cashbackValues, url, nonce, signatureId, fee, erc20, } = body;
const client = await exports.getClient(provider, fromPrivateKey);
// @ts-ignore
const contract = new (client).eth.Contract(erc721_abi_1.default, contractAddress);
const cashbacks = cashbackValues;
// tslint:disable-next-line: prefer-for-of
const cb = cashbacks.map(c => `0x${new bignumber_js_1.BigNumber(client.utils.toWei(c, 'ether')).toString(16)}`);
if (contractAddress) {
const tx = {
from: 0,
to: contractAddress.trim(),
data: erc20 ? contract.methods.mintWithCashback(to.trim(), tokenId, url, authorAddresses, cb, erc20).encodeABI() : contract.methods.mintWithCashback(to.trim(), tokenId, url, authorAddresses, cb).encodeABI(),
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
}
throw new Error('Contract address should not be empty!');
};
exports.prepareEthMintCashbackErc721SignedTransaction = prepareEthMintCashbackErc721SignedTransaction;
/**
* Sign Ethereum mint multiple ERC 721 Cashback transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareEthMintMultipleCashbackErc721SignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.EthMintMultipleErc721);
const { fromPrivateKey, to, tokenId, contractAddress, url, nonce, signatureId, authorAddresses, cashbackValues, fee, erc20, } = body;
const client = await exports.getClient(provider, fromPrivateKey);
// @ts-ignore
const contract = new (client).eth.Contract(erc721_abi_1.default, contractAddress);
const cashbacks = cashbackValues;
const cb = cashbacks.map(cashback => cashback.map(c => `0x${new bignumber_js_1.BigNumber(client.utils.toWei(c, 'ether')).toString(16)}`));
const tx = {
from: 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(),
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
};
exports.prepareEthMintMultipleCashbackErc721SignedTransaction = prepareEthMintMultipleCashbackErc721SignedTransaction;
/**
* Sign Ethereum mint multiple ERC 721 transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareEthMintMultipleErc721SignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.EthMintMultipleErc721);
const { fromPrivateKey, to, tokenId, contractAddress, url, nonce, signatureId, fee, } = body;
const client = await exports.getClient(provider, fromPrivateKey);
// @ts-ignore
const contract = new (client).eth.Contract(erc721_abi_1.default, contractAddress);
const tx = {
from: 0,
to: contractAddress.trim(),
data: contract.methods.mintMultiple(to.map(t => t.trim()), tokenId, url).encodeABI(),
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
};
exports.prepareEthMintMultipleErc721SignedTransaction = prepareEthMintMultipleErc721SignedTransaction;
/**
* Sign Ethereum burn ERC 721 transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareEthBurnErc721SignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.EthBurnErc721);
const { fromPrivateKey, tokenId, fee, contractAddress, nonce, signatureId, } = body;
const client = exports.getClient(provider, fromPrivateKey);
// @ts-ignore
const contract = new (client).eth.Contract(erc721_abi_1.default, contractAddress);
const tx = {
from: 0,
to: contractAddress.trim(),
data: contract.methods.burn(tokenId).encodeABI(),
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
};
exports.prepareEthBurnErc721SignedTransaction = prepareEthBurnErc721SignedTransaction;
/**
* Sign Ethereum update cashback ERC 721 transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareEthUpdateCashbackForAuthorErc721SignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.EthBurnErc721);
const { fromPrivateKey, cashbackValue, tokenId, fee, contractAddress, nonce, signatureId, } = body;
const client = exports.getClient(provider, fromPrivateKey);
// @ts-ignore
const contract = new (client).eth.Contract(erc721_abi_1.default, contractAddress);
const tx = {
from: 0,
to: contractAddress.trim(),
data: contract.methods.updateCashbackForAuthor(tokenId, `0x${new bignumber_js_1.BigNumber(web3_utils_1.toWei(cashbackValue, 'ether')).toString(16)}`).encodeABI(),
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
};
exports.prepareEthUpdateCashbackForAuthorErc721SignedTransaction = prepareEthUpdateCashbackForAuthorErc721SignedTransaction;
/**
* Sign Ethereum transfer ERC 721 transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareEthTransferErc721SignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.EthTransferErc721);
const { fromPrivateKey, to, tokenId, fee, contractAddress, nonce, signatureId, value, provenanceData, tokenPrice, provenance, } = body;
const client = await exports.getClient(provider, fromPrivateKey);
// @ts-ignore
const contract = new (client).eth.Contract(provenance ? erc721Provenance_abi_1.default : erc721_abi_1.default, contractAddress);
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();
const tx = {
from: 0,
to: contractAddress.trim(),
data: tokenData,
nonce,
value: value ? `0x${new bignumber_js_1.BigNumber(value).multipliedBy(1e18).toString(16)}` : undefined,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
};
exports.prepareEthTransferErc721SignedTransaction = prepareEthTransferErc721SignedTransaction;
/**
* Sign Ethereum mint ERC 1155 transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareEthMintMultiTokenBatchSignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.MintMultiTokenBatch);
const { fromPrivateKey, to, tokenId, contractAddress, nonce, data, fee, amounts, signatureId, } = body;
const client = exports.getClient(provider, fromPrivateKey);
// @ts-ignore
const contract = new (client).eth.Contract(erc1155_abi_1.default, contractAddress);
const amts = amounts.map(amts => amts.map(amt => `0x${new bignumber_js_1.BigNumber(amt).toString(16)}`));
const tx = {
from: 0,
to: contractAddress.trim(),
data: contract.methods.mintBatch(to, tokenId, amts, data ? data : '0x0').encodeABI(),
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
};
exports.prepareEthMintMultiTokenBatchSignedTransaction = prepareEthMintMultiTokenBatchSignedTransaction;
/**
* Sign Ethereum mint ERC 1155 transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareEthMintMultiTokenSignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.MintMultiToken);
const { fromPrivateKey, to, tokenId, contractAddress, nonce, data, fee, amount, signatureId, } = body;
const client = exports.getClient(provider, fromPrivateKey);
// @ts-ignore
const contract = new (client).eth.Contract(erc1155_abi_1.default, contractAddress);
const tx = {
from: 0,
to: contractAddress.trim(),
data: contract.methods.mint(to.trim(), tokenId, `0x${new bignumber_js_1.BigNumber(amount).toString(16)}`, data ? data : '0x0').encodeABI(),
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
};
exports.prepareEthMintMultiTokenSignedTransaction = prepareEthMintMultiTokenSignedTransaction;
/**
* Sign Ethereum burn ERC 1155 batch transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareEthBurnBatchMultiTokenSignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.EthBurnMultiTokenBatch);
const { fromPrivateKey, account, tokenId, amounts, fee, contractAddress, nonce, signatureId, } = body;
const client = exports.getClient(provider, fromPrivateKey);
// @ts-ignore
const contract = new (client).eth.Contract(erc1155_abi_1.default, contractAddress);
const tx = {
from: 0,
to: contractAddress.trim(),
data: contract.methods.burnBatch(account, tokenId, amounts).encodeABI(),
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
};
exports.prepareEthBurnBatchMultiTokenSignedTransaction = prepareEthBurnBatchMultiTokenSignedTransaction;
/**
* Sign Ethereum burn ERC 1155 transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareEthBurnMultiTokenSignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.EthBurnMultiToken);
const { fromPrivateKey, account, tokenId, amount, fee, contractAddress, nonce, signatureId, } = body;
const client = exports.getClient(provider, fromPrivateKey);
// @ts-ignore
const contract = new (client).eth.Contract(erc1155_abi_1.default, contractAddress);
const tx = {
from: 0,
to: contractAddress.trim(),
data: contract.methods.burn(account, tokenId, amount).encodeABI(),
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
};
exports.prepareEthBurnMultiTokenSignedTransaction = prepareEthBurnMultiTokenSignedTransaction;
/**
* Sign Ethereum transfer ERC 1155 Batch transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareEthBatchTransferMultiTokenSignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.TransferMultiTokenBatch);
const { fromPrivateKey, to, tokenId, fee, contractAddress, nonce, signatureId, amounts, data, } = body;
const client = await exports.getClient(provider, fromPrivateKey);
// @ts-ignore
const contract = new (client).eth.Contract(erc1155_abi_1.default, contractAddress);
const amts = amounts.map(amt => `0x${new bignumber_js_1.BigNumber(amt).toString(16)}`);
const tx = {
from: 0,
to: contractAddress.trim(),
data: contract.methods.safeBatchTransfer(to.trim(), tokenId.map(token => token.trim()), amts, data ? data : '0x0').encodeABI(),
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
};
exports.prepareEthBatchTransferMultiTokenSignedTransaction = prepareEthBatchTransferMultiTokenSignedTransaction;
/**
* Sign Ethereum transfer ERC 1155 transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareEthTransferMultiTokenSignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.EthTransferErc721);
const { fromPrivateKey, to, tokenId, fee, contractAddress, nonce, signatureId, amount, data, } = body;
const client = await exports.getClient(provider, fromPrivateKey);
// @ts-ignore
const contract = new (client).eth.Contract(erc1155_abi_1.default, contractAddress);
const tx = {
from: 0,
to: contractAddress.trim(),
data: contract.methods.safeTransfer(to.trim(), tokenId, `0x${new bignumber_js_1.BigNumber(amount).toString(16)}`, data ? data : '0x0').encodeABI(),
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
};
exports.prepareEthTransferMultiTokenSignedTransaction = prepareEthTransferMultiTokenSignedTransaction;
/**
* Sign Ethereum deploy ERC 1155 transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareEthDeployMultiTokenSignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.EthDeployMultiToken);
const { fromPrivateKey, fee, uri, nonce, signatureId, publicMint, } = body;
const client = await exports.getClient(provider, fromPrivateKey);
// @ts-ignore
const contract = new client.eth.Contract(erc1155_abi_1.default, null, {
data: erc1155_bytecode_1.default,
});
// @ts-ignore
const deploy = contract.deploy({
arguments: [uri, publicMint ? publicMint : false],
});
const tx = {
from: 0,
data: deploy.encodeABI(),
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
};
exports.prepareEthDeployMultiTokenSignedTransaction = prepareEthDeployMultiTokenSignedTransaction;
/**
* Sign Ethereum deploy ERC 721 transaction with private keys locally. Nothing is broadcast to the blockchain.
* @param body content of the transaction to broadcast
* @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.
*/
const prepareEthDeployErc721SignedTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.EthDeployErc721);
const { fromPrivateKey, fee, name, symbol, nonce, signatureId, provenance, cashback, publicMint, } = body;
const client = await exports.getClient(provider, fromPrivateKey);
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 client.eth.Contract(abi, null, {
data,
});
// @ts-ignore
const deploy = contract.deploy({
arguments: [name, symbol, publicMint ? publicMint : false],
});
const tx = {
from: 0,
data: deploy.encodeABI(),
nonce,
};
return await prepareEthSignedTransactionAbstraction(client, tx, signatureId, fromPrivateKey, fee);
};
exports.prepareEthDeployErc721SignedTransaction = prepareEthDeployErc721SignedTransaction;
const getEthErc20ContractDecimals = async (testnet, contractAddress, provider) => {
if (!contractAddress) {
throw new Error('Contract address not set.');
}
const client = await exports.getClient(provider);
// @ts-ignore
const contract = new client.eth.Contract(token_abi_1.default, contractAddress.trim());
return await contract.methods.decimals().call();
};
exports.getEthErc20ContractDecimals = getEthErc20ContractDecimals;
/**
* Send Ethereum invoke smart contract transaction to the blockchain.
* Invoked method only reads from blockchain the data and returns them back.
*
* @param body content of the transaction to broadcast
* @param provider url of the Ethereum Server to connect to. If not set, default public server will be used.
*/
const sendSmartContractReadMethodInvocationTransaction = async (body, provider) => {
await tatum_1.validateBody(body, model_1.SmartContractReadMethodInvocation);
const { params, methodName, methodABI, contractAddress, } = body;
const client = exports.getClient(provider);
const contract = new client.eth.Contract([methodABI], contractAddress);
return { data: await contract.methods[methodName](...params).call() };
};
exports.sendSmartContractReadMethodInvocationTransaction = sendSmartContractReadMethodInvocationTransaction;
/**
* Send Ethereum store data transaction to the blockchain. This method broadcasts signed transaction to the blockchain.
* This operation is irreversible.
* @param body content of the transaction to broadcast
* @param provider url of the Ethereum Server to connect to. If not set, default public server will be used.
* @returns transaction id of the transaction in the blockchain
*/
const sendStoreDataTransaction = async (body, provider) => blockchain_1.ethBroadcast(await exports.prepareStoreDataTransaction(body, provider), body.signatureId);
exports.sendStoreDataTransaction = sendStoreDataTransaction;
/**
* Send Ethereum or supported ERC20 transaction to the blockchain. This method broadcasts signed transaction to the blockchain.
* This operation is irreversible.
* @param body content of the transaction to broadcast
* @param provider url of the Ethereum Server to connect to. If not set, default public server will be used.
* @returns transaction id of the transaction in the blockchain
*/
const sendEthOrErc20Transaction = async (body, provider) => blockchain_1.ethBroadcast(await exports.prepareEthOrErc20SignedTransaction(body, provider), body.signatureId);
exports.sendEthOrErc20Transaction = sendEthOrErc20Transaction;
/**
* Send Ethereum custom ERC20 transaction to the blockchain. This method broadcasts signed transaction to the blockchain.
* This operation is irreversible.
* @param body content of the transaction to broadcast
* @param provider url of the Ethereum Server to connect to. If not set, default public server will be used.
* @returns transaction id of the transaction in the blockchain
*/
const sendCustomErc20Transaction = async (body, provider) => blockchain_1.ethBroadcast(await exports.prepareCustomErc20SignedTransaction(body, provider), body.signatureId);
exports.sendCustomErc20Transaction = sendCustomErc20Transaction;
/**
* Send Ethereum deploy ERC20 transaction to the blockchain. This method broadcasts signed transaction to the blockchain.
* This operation is irreversible.
* @param body content of the transaction to broadcast
* @param provider url of the Ethereum Server to connect to. If not set, default public server will be used.
* @returns transaction id of the transaction in the blockchain
*/
const sendDeployErc20Transaction = async (body, provider) => blockchain_1.ethBroadcast(await exports.prepareDeployErc20SignedTransaction(body, provider), body.signatureId);
exports.sendDeployErc20Transaction = sendDeployErc20Transaction;
/**
* Send Ethereum invoke smart contract transaction to the blockchain. This method broadcasts signed transaction to the blockchain.
* This operation is irreversible.
* @param body content of the transaction to broadcast
* @param provider url of the Ethereum Server to connect to. If not set, default public server will be used.
* @returns transaction id of the transaction in the blockchain
*/
const sendSmartContractMethodInvocationTransaction = async (body, provider) => {
if (body.methodABI.stateMutability === 'view') {
return exports.sendSmartContractReadMethodInvocationTransaction(body, provider);
}
return blockchain_1.ethBroadcast(await exports.prepareSmartContractWriteMethodInvocation(body, provider), body.signatureId);
};
exports.sendSmartContractMethodInvocationTransaction = sendSmartContractMethodInvocationTransaction;
/**
* Send Ethereum ERC721 mint transaction to the blockchain. This method broadcasts signed transaction to the blockchain.
* This operation is irreversible.
* @param body content of the transaction to broadcast
* @param provider url of the Ethereum Server to connect to. If not set, default public server will be used.
* @returns transaction id of the transaction in the blockchain
*/
const sendMintErc721Transaction = async (body, provider) => {
if (!body.fromPrivateKey) {
return nft_1.mintNFT(body);
}
return blockchain_1.ethBroadcast(await exports.prepareEthMintErc721SignedTransaction(body, provider), body.signatureId);
};
exports.sendMintErc721Transaction = sendMintErc721Transaction;
/**
* Send Ethereum ERC721 mint with cashback transaction to the blockchain. This method broadcasts signed transaction to