Web3js
ERC20 代幣轉移嘗試導致定價過低(嘗試改變價格)
它幾乎是它的精確副本,但是當我執行它時,我收到以下錯誤消息。
(節點:5189)UnhandledPromiseRejectionWarning:錯誤:返回錯誤:交易低估
下面是我執行的程式碼。
var Tx = require('ethereumjs-tx'); var Web3 = require('web3') const { ethers } = require('ethers') const web3 = new Web3(new Web3.providers.HttpProvider("https://api.avax-test.network/ext/bc/C/rpc")) let mnemonic = "word word word...."; const wallet = new ethers.Wallet.fromMnemonic(mnemonic); eth_privatekey = wallet.privateKey; const tokenAddress = "0xe66BC94A29A01ea0E897C1bAFe0625C5dA31BC58" //SHITTKN CONTRACT ADDRESS(deployed by me) // set token source, destination and amount var myAddress = wallet.address var toAddress = "0x22b2b66191390738e861D25E987FAB2554060bf6" var amount = web3.utils.toHex(1.2e18) // <1.2 token i think // get transaction count, later will used as nonce var count = web3.eth.getTransactionCount(myAddress); // set your private key here, we'll sign the transaction below var privateKey = new Buffer.from( eth_privatekey.substring(2), 'hex') // Get abi array here https://etherscan.io/address/0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0#code var abiArray = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"bytes32"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"stop","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"guy","type":"address"},{"name":"wad","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"owner_","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"src","type":"address"},{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"dst","type":"address"},{"name":"wad","type":"uint128"}],"name":"push","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"name_","type":"bytes32"}],"name":"setName","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"wad","type":"uint128"}],"name":"mint","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"src","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"stopped","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"authority_","type":"address"}],"name":"setAuthority","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"src","type":"address"},{"name":"wad","type":"uint128"}],"name":"pull","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"wad","type":"uint128"}],"name":"burn","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"bytes32"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"start","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"authority","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"src","type":"address"},{"name":"guy","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"symbol_","type":"bytes32"}],"payable":false,"type":"constructor"},{"anonymous":true,"inputs":[{"indexed":true,"name":"sig","type":"bytes4"},{"indexed":true,"name":"guy","type":"address"},{"indexed":true,"name":"foo","type":"bytes32"},{"indexed":true,"name":"bar","type":"bytes32"},{"indexed":false,"name":"wad","type":"uint256"},{"indexed":false,"name":"fax","type":"bytes"}],"name":"LogNote","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"authority","type":"address"}],"name":"LogSetAuthority","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"}],"name":"LogSetOwner","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"}] var contractAddress = tokenAddress var contract = new web3.eth.Contract(abiArray, contractAddress, {from: myAddress}) var rawTransaction = {"from":myAddress, "gasPrice":web3.utils.toHex(2 * 1e9), "gasLimit":web3.utils.toHex(210000),"to":contractAddress,"value":"0x0", "data":contract.methods.transfer(toAddress, amount).encodeABI(),"nonce":web3.utils.toHex(count)} var transaction = new Tx(rawTransaction) transaction.sign(privateKey) web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex')) // check the balance contract.methods.balanceOf(myAddress).call().then(function(balance){console.log(balance)})
我也試過以下玩一次
var nonce = await web3.eth.getTransactionCount(myAddress, 'pending'); console.log(nonce) count = nonce + 1; console.log(count)
所有與網路互動的函式都會返回 Promise。您必須將所有內容包裝在非同步函式中並等待它們。
例如
var Tx = require('ethereumjs-tx'); var Web3 = require('web3'); const {ethers} = require('ethers'); (async () => { ... // get transaction count, later will used as nonce var count = await web3.eth.getTransactionCount(myAddress); ... await web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex')) // check the balance await contract.methods.balanceOf(myAddress).call().then(function(balance){console.log(balance)}) })();