Skip to content

Commit

Permalink
Add gas limit override option for contract deployments (q9f#128)
Browse files Browse the repository at this point in the history
* Add gas limit override option for contract deployments

* Formatting :donut:

* Add gas limit override to other contract calls. Add some tests.
  • Loading branch information
dansimpson authored and mculp committed Jul 28, 2022
1 parent 1d34b37 commit 5902dae
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
26 changes: 21 additions & 5 deletions lib/eth/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def transfer(destination, amount, sender_key = nil, legacy = false)
# *args Optional variable constructor parameter list
# **sender_key [Eth::Key] the sender private key.
# **legacy [Boolean] enables legacy transactions (pre-EIP-1559).
# **gas_limit [Integer] optional gas limit override for deploying the contract.
# @return [String] the contract address.
def deploy_and_wait(contract, *args, **kwargs)
hash = wait_for_tx(deploy(contract, *args, **kwargs))
Expand All @@ -176,6 +177,7 @@ def deploy_and_wait(contract, *args, **kwargs)
# *args Optional variable constructor parameter list
# **sender_key [Eth::Key] the sender private key.
# **legacy [Boolean] enables legacy transactions (pre-EIP-1559).
# **gas_limit [Integer] optional gas limit override for deploying the contract.
# @return [String] the transaction hash.
# @raise [ArgumentError] in case the contract does not have any source.
def deploy(contract, *args, **kwargs)
Expand All @@ -185,7 +187,11 @@ def deploy(contract, *args, **kwargs)
unless args.empty?
data += encode_constructor_params(contract, args)
end
gas_limit = Tx.estimate_intrinsic_gas(data) + Tx::CREATE_GAS
gas_limit = if kwargs[:gas_limit]
kwargs[:gas_limit]
else
Tx.estimate_intrinsic_gas(data) + Tx::CREATE_GAS
end
params = {
value: 0,
gas_limit: gas_limit,
Expand Down Expand Up @@ -231,12 +237,13 @@ def deploy(contract, *args, **kwargs)
# @param contract [Eth::Contract] subject contract to call.
# @param function_name [String] method name to be called.
# @param value [Integer|String] function arguments.
# @overload call(contract, function_name, value, sender_key, legacy)
# @overload call(contract, function_name, value, sender_key, legacy, gas_limit)
# @param contract [Eth::Contract] subject contract to call.
# @param function_name [String] method name to be called.
# @param value [Integer|String] function arguments.
# @param sender_key [Eth::Key] the sender private key.
# @param legacy [Boolean] enables legacy transactions (pre-EIP-1559).
# @param gas_limit [Integer] optional gas limit override for deploying the contract.
# @return [Object] returns the result of the call.
def call(contract, function_name, *args, **kwargs)
func = contract.functions.select { |func| func.name == function_name }[0]
Expand All @@ -259,16 +266,21 @@ def call(contract, function_name, *args, **kwargs)
# @param contract [Eth::Contract] subject contract to call.
# @param function_name [String] method name to be called.
# @param value [Integer|String] function arguments.
# @overload transact(contract, function_name, value, sender_key, legacy, address)
# @overload transact(contract, function_name, value, sender_key, legacy, address, gas_limit)
# @param contract [Eth::Contract] subject contract to call.
# @param function_name [String] method name to be called.
# @param value [Integer|String] function arguments.
# @param sender_key [Eth::Key] the sender private key.
# @param legacy [Boolean] enables legacy transactions (pre-EIP-1559).
# @param address [String] contract address.
# @param gas_limit [Integer] optional gas limit override for deploying the contract.
# @return [Object] returns the result of the call.
def transact(contract, function_name, *args, **kwargs)
gas_limit = Tx.estimate_intrinsic_gas(contract.bin) + Tx::CREATE_GAS
gas_limit = if kwargs[:gas_limit]
kwargs[:gas_limit]
else
Tx.estimate_intrinsic_gas(contract.bin) + Tx::CREATE_GAS
end
fun = contract.functions.select { |func| func.name == function_name }[0]
params = {
value: 0,
Expand Down Expand Up @@ -393,7 +405,11 @@ def wait_for_tx(hash)

# Non-transactional function call called from call().
def call_raw(contract, func, *args, **kwargs)
gas_limit = Tx.estimate_intrinsic_gas(contract.bin) + Tx::CREATE_GAS
gas_limit = if kwargs[:gas_limit]
kwargs[:gas_limit]
else
Tx.estimate_intrinsic_gas(contract.bin) + Tx::CREATE_GAS
end
params = {
gas_limit: gas_limit,
chain_id: chain_id,
Expand Down
19 changes: 19 additions & 0 deletions spec/eth/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@
expect(address).to start_with "0x"
end

it "deploys the contract with a gas limit override" do
address = geth_dev_http.deploy_and_wait(contract, gas_limit: 1_000_000)
expect(address).to start_with "0x"
end

it "deploy the contract with constructor params" do
contract = Contract.from_file(file: "spec/fixtures/contracts/greeter.sol", contract_index: 0)
address = geth_dev_http.deploy_and_wait(contract, "Hello!")
Expand Down Expand Up @@ -123,6 +128,12 @@
expect(result).to eq(0)
end

it "calls a function with gas_limit override" do
geth_dev_http.deploy_and_wait(contract)
result = geth_dev_http.call(contract, "get", gas_limit: 60_000)
expect(result).to eq(0)
end

it "return nil if raw result is 0x" do
expect(geth_dev_http.call(erc20_contract, "balanceOf", address)).to be_nil
end
Expand Down Expand Up @@ -152,6 +163,14 @@
expect(response).to eq([12, 24])
end

it "transacts with gas limit override" do
address = geth_dev_http.deploy_and_wait(test_contract)
txn_hash = geth_dev_http.transact_and_wait(test_contract, "set", 12, 24, address: address, gas_limit: 100_000_000)
response = geth_dev_http.eth_get_transaction_by_hash(txn_hash)
response = geth_dev_http.call(test_contract, "get")
expect(response).to eq([12, 24])
end

it "calls the function with constructor params" do
contract = Contract.from_file(file: "spec/fixtures/contracts/greeter.sol", contract_index: 0)
address = geth_dev_http.deploy_and_wait(contract, "Hello!")
Expand Down

0 comments on commit 5902dae

Please sign in to comment.