在 Hedera hashgraph 中转移 ERC-20

huangapple go评论93阅读模式
英文:

Transfering ERC-20 in Hedera hashgraph

问题

我正在尝试编写一个函数,用于在Hedera Hashgraph中转移ERC-20代币。

以下是一个简单的Solidity合约:

pragma solidity ^0.8.7;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";    
contract Coins is ERC20 {
    constructor(string memory name_, string memory symbol_)  ERC20(name_, symbol_) {
        _mint(msg.sender, 1000000000000000000000);
    }
}

然后我在Hedera上部署它:

tx, err := hedera.NewContractCreateFlow().
    SetBytecode(bytecode).
    SetGas(1_000_000).
    SetConstructorParameters(
        hedera.NewContractFunctionParameters().
            AddString("Coins").
            AddString("coin"),
    ).
    Execute(client)

接下来,我尝试将一些ERC-20代币转移到其他地址:

ps, _ := hedera.NewContractFunctionParameters().
    AddAddress(hedera.AccountID{Account: 34937758}.ToSolidityAddress())
ps.AddUint256(big.NewInt(1).Bytes())

contractExecTx, err := hedera.NewContractExecuteTransaction().
    SetContractID(newContractID).
    SetGas(1000000).
    SetFunction("transfer", ps).
    Execute(client)

然后我收到了错误消息 StatusContractRevertExecuted (33)。

是否可以像在以太坊中一样使用ERC-20代币?还是我只能使用HTS?
是否可以使用Solidity合约并在Hedera中注册为代币?

非常感谢!

英文:

I'm trying to write function which will be transfer erc-20 tokens in hedera hashgraph.

pragma solidity ^0.8.7;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";    
contract Coins is ERC20 {
        constructor(string memory name_, string memory symbol_)  ERC20(name_, symbol_) {
            _mint(msg.sender, 1000000000000000000000);
        }
    }

very simple solidity contract

then i deploy it in hedera

tx, err := hedera.NewContractCreateFlow().
		SetBytecode(bytecode).
		SetGas(1_000_000).
		SetConstructorParameters(
			hedera.NewContractFunctionParameters().
				AddString("Coins").
				AddString("coin"),
		).
		Execute(client)

then i've trying to transfer some erc-20 tokens to other address

ps, _ := hedera.NewContractFunctionParameters().
	AddAddress(hedera.AccountID{Account: 34937758}.ToSolidityAddress())
ps.AddUint256(big.NewInt(1).Bytes())

contractExecTx, err := hedera.NewContractExecuteTransaction().
	SetContractID(newContractID).
	SetGas(1000000).
	SetFunction("transfer", ps).
	Execute(client)

and i got error StatusContractRevertExecuted (33)

is it possible to use ERC-20 tokens as a usual in Ethereum? or i have to use HTS only?
is it possible to use solidity contract and register it as token in Hedera?

thank you so much

答案1

得分: 2

UPD: 解决方案

看起来对于 uint256,字节与 Hedera uint256 不兼容,并且 big.NewInt(number).Bytes() 对于 Hedera uint256 不起作用。

我使用了一些 ABI 库

github.com/dcb9/go-ethereum/accounts/abi

添加 Uint256(abi.U256(big.NewInt(some number)))

然后转回 big.Int

query.GetUint256(0) // 查询获取 uint256

bInt := new(big.Int)
bInt.SetBytes(query.GetUint256(0))

现在它完美地工作了。

英文:

UPD: solution

looks like bytes for uint256 not compatible with hedera uint256 and big.NewInt(number).Bytes() is not working for hedera uint256

i've takes some abi lib

github.com/dcb9/go-ethereum/accounts/abi


AddUint256(abi.U256(big.NewInt(some number)))

and back to big.Int

query.GetUint256(0) // query for getting uint256

bInt := new(big.Int)
bInt.SetBytes(query.GetUint256(0))

and now it is works perfect

答案2

得分: 1

你好!以下是翻译好的内容:

很好,你找到了解决方案。我没有尝试过使用Go的web3库,但是在Java和JavaScript中,我成功地使用了现有的web3库与SDK结合使用。这里有一个存储库,其中包含一些示例,也许在Go中也可以使用。

https://github.com/hashgraph/hedera-smart-contracts-libs-lab

英文:

Well done for finding the solution, I've not tried it with Go web3 libraries, but had success using existing web3 libraries in conjunction with the SDK in both java and javascript, here is a repo with some examples, maybe it will also work in Go.

https://github.com/hashgraph/hedera-smart-contracts-libs-lab

huangapple
  • 本文由 发表于 2022年7月14日 05:39:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/72972918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定