我在node.js中有一个函数,使用这样的api传输erc20token:
app.post('/sendtokens', async(req, res) =>{
console.log(req.body)
let privateKey = req.body.privateKey;
let receiverPublicKey = req.body.receiverPublicKey;
let amountToSend = req.body.amountToSend;
// change here yout contract address
let contractAddress = 'contractaddresss';
// put your infura API here
let provider = new HDWalletProvider(privateKey, "nodeurl");
let web3 = new Web3(provider);
let gasPrice ;
console.log(gasPrice)
let {data} = await axios.get('https://api.etherscan.io/api?module=gastracker&action=gasoracle');
gasPrice = parseInt(data.result.FastGasPrice) * 1000000000;
console.log('gasPrice = '+gasPrice);
const mytoken = new web3.eth.Contract(abiarray, mytoken.methods.balanceOf(receiverPublicKey).call({},function(error, result){
console.log("balance ");
console.log(result);
})
let account = await web3.eth.getAccounts();
console.log(account[0])
let receiptToSend = null;
BigNumber.config({ EXPONENTIAL_AT: 40 })
let tokens = new BigNumber(amountToSend);
tokens = tokens.multipliedBy(new BigNumber(Math.pow(10,18)));
console.log(tokens.toString())
tokens = tokens.toString()
await mytoken.methods.transfer(receiverPublicKey, tokens).send({
from: account[0],
gasPrice: gasPrice,
gas: '60000'
}).then(function(receipt){
console.log('hi')
receiptToSend = receipt;
console.log(receipt);
})
res.send(receiptToSend);
})
下面是Laravel中的api代码:
$privateKey = "privatekey";
$receiverAddress= 'etherium wallet';
$amountTosend = 'token amount';
$res = $client->post('https://website.com/sendtokens', [
'json' => [
'privateKey' => $privateKey,
'receiverPublicKey' => $receiverAddress,
'amountToSend' => $amountTosend,
]
]);
$data = $res->getBody();
$decode = json_decode($data);
$transHash = $decode->transactionHash;
$transStatus = $decode->status;
if($transStatus == confirmed){
//transaction completed and confirmed
}
else if ($transStatus == pending){
//transaction completed and is pending
}
else if ($transStatus == anyknown error){
//transaction error and error name
}
else{
//unknown error
}
在这里,我想获得的状态名称,如什么字符串或代码应该取代'确认'的状态,如果条件,也为'待定'状态和任何'知道和错误'状态。你能告诉我如何确定交易的状态吗?
我真的不太了解以太坊,但是如果你看一下文档,你会发现他们为你想要做的操作指定了可能的返回值。
0表示失败,1表示通过。
在我看来好像没有一个待定事务的状态。或者是Null.