首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在ThunderCore主网上发送事务和传输TT?

如何在ThunderCore主网上发送事务和传输TT?
EN

Stack Overflow用户
提问于 2020-04-24 14:04:56
回答 1查看 276关注 0票数 1

如果不使用ThunderCore集线器或任何支持雷电令牌的Web3钱包,我如何以编程方式发送事务或传输雷电令牌?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-30 12:19:36

若要在Thundercore上发送事务,请在事务中设置以下字段:

(eth.net.getId())

eth_gasPrice() (getGasPrice)

请参阅以下代码中的submitTx方法:

transfer.js

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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为目标,通过浏览器将signTxeth.sendSignedTransaction替换为web3.eth.sendTransaction来发送事务。

完整的示例在此transfer回购的field-support分支中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61410248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档