Dapp 在用户执行特定活动(例如接受 BNB 发送到 ICO)后,将 Bep20 令牌从远程钱包(我的钱包)发送给用户,它在安装了 metamask 的桌面上运行良好。执行此操作的代码是:
var amountTosend = amount;
var privateKey = 'PRIVATE_KEY';
var accountFrom = web3provider.eth.accounts.privateKeyToAccount(privateKey).address;
var contractCall = contractInstance.methods.transfer(addressTo, "0x"+amountTosend.toString(16));
var icoAccount = web3provider.eth.accounts.privateKeyToAccount(privateKey);
web3provider.eth.accounts.wallet.add(icoAccount);
contractCall.send({ from: accountFrom, gas: 2000000 }).then(function(hashdata) {
console.log(hashdata);
var rawTransaction = {
"from": accountFrom,
"nonce": nonce,
"gasPrice": 20000000000,
"gas": gas,
"to": SmartContractAddress,
"value": "0x" + amountTosend.toString(16),
"data": hashdata.events.Transfer.raw.data,
"chainId": chainid
};
var privKey = new ethereumjs.Buffer.Buffer(privateKey, 'hex');
let tx = new ethereumjs.Tx(rawTransaction);
tx.sign(privKey)
let serializedTx = tx.serialize();
console.log('serializedTx:', serializedTx);
web3provider.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'), function(err, receipt) {
if (!err) {
console.log('success token sent to user: ' + receipt);
alert('You have received your Tokens in your Wallet');
} else {
console.log('error: ' + err);
alert("An error Occured: " + err);
}
});
})
在使用wallet connect作为提供者与web3modal连接的trust和metamask mobile等移动钱包上,我成功与钱包交互,提示用户发送BNB但
contractCall.send({ from: accountFrom, gas: 2000000 })
用于更改智能合约的状态以在将远程钱包添加到本地后发送用户 Bep20 令牌失败并出现未知帐户错误,使用:
web3provider.eth.accounts.wallet.add(icoAccount);
注意:如果我尝试跳过
contractCall.send({ from: accountFrom, gas: 2000000 })
并使用 contractCall.encodeABI() 作为数据参数,它工作正常,但实际上没有令牌发送给用户,我猜是因为智能联系人状态需要更改/更改
myContract.methods.myMethod([param1[, param2[, ...]]]).send
请我需要任何有用的帮助/提示来使这项工作在移动钱包上工作,我已经在这个错误上待了几天了,谢谢