-
Notifications
You must be signed in to change notification settings - Fork 2
/
createNFTContract.js
65 lines (60 loc) · 2.06 KB
/
createNFTContract.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import axios from 'axios';
import web3 from './web3Util';
import PixuraNFT from './abis/PixuraNFT';
const PIXURA_API_URL = 'https://devcon.pixura.io';
/**
* Description [ Create new ERC721 Smart Contract]
* @param {string} name - The name of the NFT contract.
* @param {string} symbol - The symbol for the NFT tokens.
* @return { Object } Eth Tx Receipt
*/
export const createNewNFTContract = async (name, symbol) => {
try {
const accounts = await web3.eth.getAccounts();
const nftContract = new web3.eth.Contract(PixuraNFT.abi);
const deployArgs = {
data: PixuraNFT.bytecode,
arguments: [name, symbol],
};
const encodedABI = nftContract.deploy(deployArgs).encodeABI();
const txDetails = { data: encodedABI, from: accounts[0] };
return web3.eth.sendTransaction(txDetails);
} catch (err) {
throw new Error(`Failed to create new NFT contract ${name} ${symbol} | ${err}`);
}
};
/**
* Description [ Register an ERC721 Smart Contract to be indexed]
* @param {number} blockNum - The block number to index from.
* @param {string} address - The address of the NFT contract.
* @return { undefined }
*/
export const registerContractToIndex = async (blockNum, address) => {
const CONTRACT_ENDPOINT = `${PIXURA_API_URL}/register-contract`;
const registration = {
address,
startBlock: blockNum,
smartRestart: true,
contractType: 'ERC721Contract',
};
try {
await axios.post(CONTRACT_ENDPOINT, registration);
} catch (err) {
throw new Error(`Failed to register contract ${address} | ${err}`);
}
};
/**
* Description [ Create and register a new ERC721 Smart Contract for indexing.]
* @param {string} name - The name of the NFT contract.
* @param {string} symbol - The symbol for the NFT tokens.
* @return { undefined }
*/
export const createAndRegisterNFTContract = async (name, symbol) => {
try {
const txReceipt = await createNewNFTContract(name, symbol);
const { address, blockNumber } = txReceipt;
await registerContractToIndex(blockNumber, address);
} catch (err) {
throw new Error(err);
}
};