q9f / eth.rb

a straightforward library to build, sign, and broadcast ethereum transactions anywhere you can run ruby.
https://q9f.github.io/eth.rb
Apache License 2.0
204 stars 88 forks source link

Token Transfer #145

Closed gouravnaik closed 1 year ago

gouravnaik commented 2 years ago

Hi,

I am trying to transfer some of my tokens to another address and here are the steps I followed.

cli = Eth::Client.create "https://polygon-rpc.com"
my_key = Eth::Key.new priv: "268be6f4a68c40f6862b7ac9aed8f701dc25a95ddb9a44d8b1f520b75f440a9a"

token_address = "0x7CdC0421469398e0F3aA8890693d86c840Ac8931" //Azuki Token(ERC20)
erc20_abi = ERC20ABI
token_name = "AZUKI"

contract = Eth::Contract.from_abi(name: token_name, address: token_address, abi: erc20_abi)

cli.transact_and_wait(contract, "transfer", account_public_address, '10000000000000000', my_key)

But getting an error

/eth-0.5.6/lib/eth/abi.rb:52:in `block in encode': undefined method `size' for nil:NilClass (NoMethodError)

        .map { |i| parsed_types[i].size or 32 }

I feel the issue is in lib/eth/abi.rb in line 52

q9f commented 2 years ago

Yes, this is something we need to work on for sure!

skeevis commented 2 years ago

@gouravnaik have you been able to get past this issue?

gouravnaik commented 1 year ago

@gouravnaik have you been able to get past this issue?

I am generating data and then send the transaction. Steps I am using currently @skeevis

def self.get_transfer_data(recipient, amount)
        transfer_method = "0xa9059cbb"  #Encoded Abi for transfer method name
        binary_data = Eth::Abi.encode(["address","uint256"],[recipient.public_key, amount])
        hex_data = binary_data.unpack('H*').first
        data = transfer_method+hex_data
    end
def self.token_transfer(chainId, token, amount, recipient, sender)
    transfer_data = MyDapp.get_transfer_data(recipient,amount)
    data = Hash["chainId", chainId, "to", token, "data", transfer_data]
    payload = create_payload(data, sender.public_key)
    send_transaction(payload, sender)
end
q9f commented 1 year ago

Should be addressed in #191 - I'll make sure to write some tests.

q9f commented 1 year ago

Confirming this should be fixed in #191

geth = Client.create "/tmp/geth.ipc"
# => #<Eth::Client::Ipc:0x0000557f2508dc80 @gas_limit=21000, @id=0, @max_fee_per_gas=0.4269e11, @max_priority_fee_per_gas=0.101e10, @path="/tmp/geth.ipc">
erc = Contract.from_file(file: "spec/fixtures/contracts/erc20.sol")
# => #<Eth::Contract::ERC20:0x0000557f2517b160>
geth.deploy_and_wait(erc, "FooBar", "FOO")
# => "0x582738E45b3Ed823709F7A251352f55E4a6f146E"
erc.address
# => "0x582738E45b3Ed823709F7A251352f55E4a6f146E"
geth.call(erc, "name")
# => "FooBar"
geth.call(erc, "symbol")
# => "FOO"
geth.call(erc, "decimals")
# => 18
geth.transact_and_wait(erc, "mint", geth.default_account.to_s, 1 * Unit::ETHER)
# => "0x5246c13331ed7868e623e2a442879891e4b6e6170f2d7091fa7f56962cd647ec"
geth.call(erc, "balanceOf", geth.default_account.to_s)
# => 1000000000000000000
adr = Key.new.address.to_s
# => "0x49eB8f597De9B22D9141392d287BAfDc80994D51"
geth.transact_and_wait(erc, "transfer", adr, 1)
# => "0xf13020c546dffaac15b3e804b61db183a47947c860aad9418cc894822d4f1fa1"
geth.call(erc, "balanceOf", geth.default_account.to_s)
# => 999999999999999999
q9f commented 1 year ago

https://github.com/q9f/eth.rb/wiki/ERC20-Transfers