如何在 ThunderCore 主网上发送交易和转账 TT?
How do I send transactions and transfer TT on ThunderCore mainnet?
在不使用 ThunderCore Hub 或任何支持 Thunder Token 的 Web3 钱包的情况下,我如何以编程方式发送交易或转移 Thunder Tokens?
要在 Thundercore 上发送交易,请在交易中设置这些字段:
- 设置
chainId
来自 net_version()
(eth.net.getId()
)
- 设置
nonce
为eth_getTransactionCount()
(eth.getTransactionCount
) 的值
- 从
eth_gasPrice()
(getGasPrice
) 设置 gas price
- 从
eth_estimateGas
(eth.estimateGas
) 设置 gas limit
参见下面代码中的submitTx
方法:
transfer.js
const fs = require('fs');
const path = require('path');
const Accounts = require('web3-eth-accounts');
const Eth = require('web3-eth');
const Web3 = require('web3');
const BN = Web3.utils.BN;
const pretty = require('./pretty');
const erc20Abi = require('./ERC20.abi.json');
const web3Provider = () => {
return Eth.giveProvider || 'https://mainnet-rpc.thundercore.com';
}
const signTx = async (fromAccount, tx) => {
const signedTx = await fromAccount.signTransaction(tx)
return signedTx.rawTransaction // hex string
}
class ChainHelper {
constructor(eth, chainId, fromAccount) {
this.eth = eth;
this.chainId = chainId;
this.fromAccount = fromAccount;
this.fromAddress = fromAccount.address;
}
async submitTx(toAddress, value, txData) {
const eth = this.eth;
const promiseResults = await Promise.all([
eth.getTransactionCount(this.fromAccount.address),
eth.getGasPrice(),
]);
const nonce = promiseResults[0];
const gasPrice = promiseResults[1];
const fromAddress = this.fromAddress;
const tx = {
'gasLimit': 0,
'chainId': this.chainId,
'gasPrice': gasPrice,
'nonce': nonce,
'from': fromAddress,
'to': toAddress,
'value': value,
'data': txData,
}
const gasMultiple = new BN(1.0);
tx.gasLimit = '0x' + (new BN(await eth.estimateGas(tx))).mul(gasMultiple).toString(16);
console.log('tx:', pretty.format(tx));
const rawTxStr = await signTx(this.fromAccount, tx);
return eth.sendSignedTransaction(rawTxStr);
}
async transferToken (contractAddress, toAddress, value) {
const eth = this.eth;
const contractAbi = erc20Abi;
const contract = new eth.Contract(contractAbi, contractAddress);
const txData = contract.methods.transfer(toAddress, value).encodeABI();
return this.submitTx(contractAddress, 0, txData);
}
}
const create = async (privateKey) => {
const accounts = new Accounts();
if (!privateKey.startsWith('0x')) {
privateKey = '0x' + privateKey;
}
const account = accounts.privateKeyToAccount(privateKey);
const eth = new Eth(web3Provider());
const networkId = await eth.net.getId();
return new ChainHelper(eth, networkId, account);
}
const readKeys = () => {
const privateKeys = fs.readFileSync(path.join(__dirname, '..', '.private-keys'),
{encoding: 'ascii'}).split('\n').filter(x => x.length > 0);
return privateKeys;
}
module.exports = {
create: create,
readKeys: readKeys,
};
testTransfer.js
const fs = require('fs');
const path = require('path');
const ChainHelper = require('../src/transfer.js');
const toAddress = '0x6f0d809e0fa6650460324f26df239bde6c004ecf';
describe('transfer', () => {
it('transfer TT', async() => {
const privateKey = ChainHelper.readKeys()[0]
const c = await ChainHelper.create(privateKey);
c.submitTx(toAddress, 1, '');
});
it('tokenTransfer', async() => {
const privateKey = ChainHelper.readKeys()[0]
const c = await ChainHelper.create(privateKey);
/* Token.issue() */
const jsonBuf = fs.readFileSync(path.join(__dirname, '..', 'build', 'contracts', 'Token.json'));
const contractData = JSON.parse(jsonBuf);
const contractAbi = contractData['abi'];
const contractAddress = contractData['networks'][c.chainId]['address'];
const contract = new c.eth.Contract(contractAbi, contractAddress);
const toAddress = c.fromAddress;
const tokenAmount = 2;
let txData = contract.methods.issue(tokenAmount, c.fromAddress).encodeABI();
let r = await c.submitTx(toAddress, 0, txData);
console.log('Token.issue receipt:', r);
/* Token.transfer() */
r = await c.transferToken(contractAddress, toAddress, tokenAmount)
console.log('Token.transfer receipt:', r);
});
});
上面的代码针对命令行或服务器端 node.js
,从浏览器发送交易 用 web3.eth.sendTransaction
[=39= 替换 signTx
和 eth.sendSignedTransaction
]
完整示例位于此 field-support
存储库的 transfer
分支中。
在不使用 ThunderCore Hub 或任何支持 Thunder Token 的 Web3 钱包的情况下,我如何以编程方式发送交易或转移 Thunder Tokens?
要在 Thundercore 上发送交易,请在交易中设置这些字段:
- 设置
chainId
来自net_version()
(eth.net.getId()
) - 设置
nonce
为eth_getTransactionCount()
(eth.getTransactionCount
) 的值
- 从
eth_gasPrice()
(getGasPrice
) 设置 - 从
eth_estimateGas
(eth.estimateGas
) 设置
gas price
gas limit
参见下面代码中的submitTx
方法:
transfer.js
const fs = require('fs');
const path = require('path');
const Accounts = require('web3-eth-accounts');
const Eth = require('web3-eth');
const Web3 = require('web3');
const BN = Web3.utils.BN;
const pretty = require('./pretty');
const erc20Abi = require('./ERC20.abi.json');
const web3Provider = () => {
return Eth.giveProvider || 'https://mainnet-rpc.thundercore.com';
}
const signTx = async (fromAccount, tx) => {
const signedTx = await fromAccount.signTransaction(tx)
return signedTx.rawTransaction // hex string
}
class ChainHelper {
constructor(eth, chainId, fromAccount) {
this.eth = eth;
this.chainId = chainId;
this.fromAccount = fromAccount;
this.fromAddress = fromAccount.address;
}
async submitTx(toAddress, value, txData) {
const eth = this.eth;
const promiseResults = await Promise.all([
eth.getTransactionCount(this.fromAccount.address),
eth.getGasPrice(),
]);
const nonce = promiseResults[0];
const gasPrice = promiseResults[1];
const fromAddress = this.fromAddress;
const tx = {
'gasLimit': 0,
'chainId': this.chainId,
'gasPrice': gasPrice,
'nonce': nonce,
'from': fromAddress,
'to': toAddress,
'value': value,
'data': txData,
}
const gasMultiple = new BN(1.0);
tx.gasLimit = '0x' + (new BN(await eth.estimateGas(tx))).mul(gasMultiple).toString(16);
console.log('tx:', pretty.format(tx));
const rawTxStr = await signTx(this.fromAccount, tx);
return eth.sendSignedTransaction(rawTxStr);
}
async transferToken (contractAddress, toAddress, value) {
const eth = this.eth;
const contractAbi = erc20Abi;
const contract = new eth.Contract(contractAbi, contractAddress);
const txData = contract.methods.transfer(toAddress, value).encodeABI();
return this.submitTx(contractAddress, 0, txData);
}
}
const create = async (privateKey) => {
const accounts = new Accounts();
if (!privateKey.startsWith('0x')) {
privateKey = '0x' + privateKey;
}
const account = accounts.privateKeyToAccount(privateKey);
const eth = new Eth(web3Provider());
const networkId = await eth.net.getId();
return new ChainHelper(eth, networkId, account);
}
const readKeys = () => {
const privateKeys = fs.readFileSync(path.join(__dirname, '..', '.private-keys'),
{encoding: 'ascii'}).split('\n').filter(x => x.length > 0);
return privateKeys;
}
module.exports = {
create: create,
readKeys: readKeys,
};
testTransfer.js
const fs = require('fs');
const path = require('path');
const ChainHelper = require('../src/transfer.js');
const toAddress = '0x6f0d809e0fa6650460324f26df239bde6c004ecf';
describe('transfer', () => {
it('transfer TT', async() => {
const privateKey = ChainHelper.readKeys()[0]
const c = await ChainHelper.create(privateKey);
c.submitTx(toAddress, 1, '');
});
it('tokenTransfer', async() => {
const privateKey = ChainHelper.readKeys()[0]
const c = await ChainHelper.create(privateKey);
/* Token.issue() */
const jsonBuf = fs.readFileSync(path.join(__dirname, '..', 'build', 'contracts', 'Token.json'));
const contractData = JSON.parse(jsonBuf);
const contractAbi = contractData['abi'];
const contractAddress = contractData['networks'][c.chainId]['address'];
const contract = new c.eth.Contract(contractAbi, contractAddress);
const toAddress = c.fromAddress;
const tokenAmount = 2;
let txData = contract.methods.issue(tokenAmount, c.fromAddress).encodeABI();
let r = await c.submitTx(toAddress, 0, txData);
console.log('Token.issue receipt:', r);
/* Token.transfer() */
r = await c.transferToken(contractAddress, toAddress, tokenAmount)
console.log('Token.transfer receipt:', r);
});
});
上面的代码针对命令行或服务器端 node.js
,从浏览器发送交易 用 web3.eth.sendTransaction
[=39= 替换 signTx
和 eth.sendSignedTransaction
]
完整示例位于此 field-support
存储库的 transfer
分支中。