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
200 stars 86 forks source link

eth/client: add transfer_erc20 function #197

Closed q9f closed 1 year ago

q9f commented 1 year ago
geth = Client.create "/tmp/geth.ipc"
# => #<Eth::Client::Ipc:0x000056433dfb0f28 @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:0x000056433e09a678>
geth.deploy_and_wait(erc, "FooBar", "FOO")
# => "0xb14BD4a61E0f8d0e390A8e109e67dedb5965EEDe"
key = Key.new
# => #<Eth::Key:0x000056433d98b620 @private_key=#<Secp256k1::PrivateKey:0x000056433d98a478 @data="E\xAC\x8C\"\xFB\xEC+\xA6u'\x9E\x02@\xAFWy\x03X\"dY]\x13m\xF9\\\x15\v\xE07\xDFn">, @public_key=#<Secp256k1::PublicKey:0x000056433d989ac8>>
geth.transfer_and_wait(key.address.to_s, 1 * Unit::ETHER)
# => "0xa1a8a9e13bd54585d6e761ff31fc1db191c9bca83853e240712b190af8cc6b30"
geth.transact_and_wait(erc, "mint", key.address.to_s, 1 * Unit::ETHER)
# => "0x2e4bf86faf18f5450f5c15304c5236378937953283c8fdd15ab2fd2019cb0991"
geth.call(erc, "balanceOf", key.address.to_s)
# => 1000000000000000000
geth.transfer_erc20_and_wait(erc, "0x49eB8f597De9B22D9141392d287BAfDc80994D51", 1, sender_key: key)
# => "0x5669979670d204bc1624ac9042c28fdef7c5e26d722872026cc7bb980482e280"
geth.call(erc, "balanceOf", key.address.to_s)
# => 999999999999999999
geth.call(erc, "balanceOf", "0x49eB8f597De9B22D9141392d287BAfDc80994D51")
# => 1

fix #145

codecov-commenter commented 1 year ago

Codecov Report

Merging #197 (0d3440e) into main (096d793) will decrease coverage by 0.04%. The diff coverage is 93.33%.

:exclamation: Current head 0d3440e differs from pull request most recent head db1e14f. Consider uploading reports for the commit db1e14f to get more accurate results

@@            Coverage Diff             @@
##             main     #197      +/-   ##
==========================================
- Coverage   99.76%   99.72%   -0.05%     
==========================================
  Files          74       74              
  Lines        4345     4375      +30     
==========================================
+ Hits         4335     4363      +28     
- Misses         10       12       +2     
Impacted Files Coverage Δ
lib/eth/client.rb 98.92% <60.00%> (-1.08%) :arrow_down:
spec/eth/client_spec.rb 100.00% <100.00%> (ø)

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more