大佬,请教下,A账户给B账户合约授权后,B账户可以通过合约发起转账, 转账给C账户,这时候需要用key签名,为什么是使用A账户的key,而不是B账户的key? 如果使用B账户的key,变成B转C,会有交易hash,但一直处于pendding状态。而用A账户的key,才可以转账成功?代码如下: 也疑问,这种的到底是使用 transfer 还是 transferFrom ?
const http = 'https://cloudflare-eth.com'
let web3 = new Web3(new Web3.providers.HttpProvider(http));
const currentAccount = '0xAAAAAAAAA'
const toAccount = '0xBBBBBBBBBB'
const contractAddress = '0xHHHHHHHHHHH'
const _t = this
web3.eth.getTransactionCount(currentAccount, web3.eth.defaultBlock).then(function(nonce){
const amout = 1000000
let instance = new web3.eth.Contract(_t.abi, contractAddress)
const data = instance.methods.transfer(currentAccount,amout).encodeABI()
console.log(data) // 0xa9059cbb0000000000000000000000xAAAAAAAAA0000000000000000000f420
var txData = {
nonce: web3.utils.toHex(nonce),
gasLimit: web3.utils.toHex(250000),
gasPrice: web3.utils.toHex(10e9),
to: contractAddress,
from: currentAccount,
value: '0x',
data
}
// const common = { chain: 'mainnet', hardfork: 'Istanbul' }
const tx = Transaction.fromTxData(txData)
const privateKey = Buffer.from(
'AAAAAAAAAAAAAkey',
'hex'
)
const signedTx = tx.sign(privateKey)
const serializedTx = signedTx.serialize()
const singData = '0x' + serializedTx.toString('hex')
web3.eth.sendSignedTransaction(singData, function(err, hash) {
if (!err) {
console.log(hash);
} else {
console.error(err);
}
}).on('receipt', console.log);;