尝试使用代码发送和谐一号硬币发送HRC20代币.我做了一些更改,但出现了错误



Harmony One SDK〔在此处输入链接描述〕[1]

我使用了将Harmony一个代币发送到另一个Harmony one地址的代码。像以太坊一样,我已经包含了转移HRC20代币的方法。但它不起作用。

在参考中,没有提供在我们发送代币的情况下签署交易的代码。

let encodedABI = transfer.encodeABI();
async function transferTokens() {
await setSharding(); //cross shard transaction
const txn = hmy.transactions.newTx({
to: '0x9fac0935f055882969bE6f68671B4F5673573e60', //token contract address
value: 0x0, // One coin
gasLimit: '210000',
shardID: 0,
toShardID: 0,
gasPrice: new hmy.utils.Unit('100').asGwei().toWei(),
data: encodedABI,
});
// sign the transaction
const signedTxn = await hmy.wallet.signTransaction(txn);
console.log("n n signed TXN:::::::::::::::::::::", signedTxn)
// to listen events
signedTxn
.observed()
.on('transactionHash', (txnHash) => {
console.log('');
console.log('--- hash ---');
console.log('');
console.log(txnHash);
console.log('');
})
.on('receipt', (receipt) => {
console.log('');
console.log('--- receipt ---');
console.log('');
console.log(receipt);
console.log('');
})
.on('cxReceipt', (receipt) => {
console.log('');
console.log('--- cxReceipt ---');
console.log('');
console.log(receipt);
console.log('');
})
.on('error', (error) => {
console.log('');
console.log('--- error ---');
console.log('');
console.log(error);
console.log('');
});
// send the txn, get [Transaction, transactionHash] as result
const [sentTxn, txnHash] = await signedTxn.sendTransaction();
// to confirm the result if it is already there
const confiremdTxn = await sentTxn.confirm(txnHash);
console.log("nnConfirmed TXN :::::::::::::::::::;", confiremdTxn )
// if the transactino is cross-shard transaction
if (!confiremdTxn.isCrossShard()) {
if (confiremdTxn.isConfirmed()) {
console.log('--- Result ---');
console.log('');
console.log('Normal transaction');
console.log(`${txnHash} is confirmed`);
console.log('');
process.exit();
}
}
if (confiremdTxn.isConfirmed() && confiremdTxn.isCxConfirmed()) {
console.log('--- Result ---');
console.log('');
console.log('Cross-Shard transaction');
console.log(`${txnHash} is confirmed`);
console.log('');
process.exit();
}
}
transferTokens();```

  [1]: https://github.com/harmony-one/sdk/tree/master/packages/harmony-transaction

您可以在合约方法上调用signTransaction((,然后调用sendTransaction。

参考:

  • https://github.com/harmony-one/sdk/blob/master/packages/harmony-contract/src/contractFactory.ts
  • https://github.com/harmony-one/sdk/tree/master/packages/harmony-contract

Harmany处于早期阶段,因此建议查看他们的官方资源,仅限SDK。。

我认为您错过了应该放在之前的代码块

hmy.transactions.newTx({…

创建一个新的事务对象。

const hmy = new Harmony('https://api.s0.b.hmny.io',
    {
        chainType: ChainType.Harmony,
        chainId: ChainID.HmyTestnet,
    },
);
hmy.wallet.addByPrivateKey('YOUR PRIVATE KEY');
hmy.blockchain.getShardingStructure();

最新更新