Created
January 21, 2020 04:36
-
-
Save nhancv/bb14f0b1cbd5dd788b0d7744d0fcc44e to your computer and use it in GitHub Desktop.
TRC10 Token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import TronWeb from 'tronweb' | |
| export default class TRONTokenClient { | |
| async start(): Promise<any> { | |
| const mainNetProvider = 'https://api.trongrid.io'; | |
| const testNetProvider = 'https://api.shasta.trongrid.io'; | |
| const netProvider = testNetProvider; | |
| const HttpProvider = TronWeb.providers.HttpProvider; // Optional provider, can just use a url for the nodes instead | |
| const fullNode = new HttpProvider(`${netProvider}`); // Full node http endpoint | |
| const solidityNode = new HttpProvider(`${netProvider}`); // Solidity node http endpoint | |
| const eventServer = `${netProvider}`; // Contract events http endpoint | |
| const privateKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; | |
| const tronWeb = new TronWeb( | |
| fullNode, | |
| solidityNode, | |
| eventServer, | |
| privateKey | |
| ); | |
| const defaultTestAddress = tronWeb.address.fromPrivateKey(privateKey); | |
| await this.createTestTRC10Token(tronWeb, defaultTestAddress, privateKey); | |
| // Verify at: https://shasta.tronscan.org/#/ | |
| } | |
| async signAndSubmitTx(tronWeb, rawTxObject, privateKey) { | |
| console.log(rawTxObject); | |
| const sign = await tronWeb.trx.sign(rawTxObject, privateKey); | |
| console.log({sign}); | |
| const tx = await tronWeb.trx.sendRawTransaction(sign); | |
| // console.log(tx); | |
| return tx; | |
| } | |
| async createTestTRC10Token(tronWeb, issuerAddress, privateKey) { | |
| try { | |
| let options = { | |
| name: 'EricCaoToken', | |
| abbreviation: 'nhancv', | |
| description: 'Nhan Cao Token for Test', | |
| url: 'nhancv.com', | |
| totalSupply: 1000000000, | |
| trxRatio: 1, // How much TRX will tokenRatio cost? | |
| tokenRatio: 1, // How many tokens will trxRatio afford? | |
| saleStart: Date.now() + 10000, // sale start mus > now | |
| saleEnd: Date.now() + 31536000000, // 365 days | |
| freeBandwidth: 0, // The creator's "donated" bandwidth for use by token holders | |
| freeBandwidthLimit: 0, // Out of totalFreeBandwidth, the amount each token holder get | |
| frozenAmount: 0, | |
| frozenDuration: 0 | |
| }; | |
| const issuerHex = tronWeb.address.toHex(issuerAddress); | |
| const rawTxObj = await tronWeb.transactionBuilder.createToken(options, issuerHex); | |
| // https://shasta.tronscan.org/#/ | |
| console.log({rawTxObj}); | |
| const res = await this.signAndSubmitTx(tronWeb, rawTxObj, privateKey); | |
| console.log({res}); | |
| } catch (e) { | |
| console.error(e); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment