Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Fix Smart Contract reads without ABI specified #217

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/coinbase/smart_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ def self.create_multi_token_contract(
# @return [Object] The result of the contract call, converted to an appropriate Ruby type
# @raise [Coinbase::ApiError] If there's an error in the API call
def self.read(
network:,
contract_address:,
method:,
network: Coinbase.default_network,
abi: nil,
args: {}
)
Expand All @@ -159,7 +159,7 @@ def self.read(
method: method,
args: (args || {}).to_json,
abi: abi&.to_json
}
}.compact
)
end

Expand Down
45 changes: 29 additions & 16 deletions spec/unit/coinbase/smart_contract_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

let(:network_id) { :base_sepolia }
let(:network) { build(:network, network_id) }
let(:smart_contracts_api) { instance_double(Coinbase::Client::SmartContractsApi) }

let(:default_network) { build(:network, :base_mainnet) }
let(:token_name) { 'Test token' }
let(:token_symbol) { 'TST' }
let(:total_supply) { 1_000_000 }

let(:wallet_id) { model.wallet_id }
let(:address_id) { model.deployer_address }
let(:smart_contracts_api) { instance_double(Coinbase::Client::SmartContractsApi) }
let(:model) do
build(
:smart_contract_model,
Expand All @@ -22,16 +23,11 @@
total_supply: total_supply
)
end
let(:wallet_id) { model.wallet_id }
let(:address_id) { model.deployer_address }

before do
allow(Coinbase::Client::SmartContractsApi).to receive(:new).and_return(smart_contracts_api)

allow(Coinbase::Network)
.to receive(:from_id)
.with(satisfy { |n| n == network || n == network_id || n == network.normalized_id })
.and_return(network)
allow(Coinbase).to receive(:default_network).and_return(default_network)
end

describe '.create_token_contract' do
Expand Down Expand Up @@ -222,7 +218,7 @@
it 'calls read_contract with correct parameters' do
result
expect(smart_contracts_api).to have_received(:read_contract)
.with('base-sepolia', contract_address, hash_including(expected_params))
.with(network.normalized_id, contract_address, hash_including(expected_params))
end

context 'when using a different network' do
Expand All @@ -241,6 +237,23 @@
end
end

context 'when using the default network' do
subject(:result) do
described_class.read(
contract_address: contract_address,
method: method_name,
abi: abi,
args: args
)
end

it 'calls read_contract with correct parameters' do
result
expect(smart_contracts_api).to have_received(:read_contract)
.with(default_network.normalized_id, contract_address, expected_params)
end
end

context 'when using a different contract address' do
let(:contract_address) { '0x9876543210987654321098765432109876543210' }

Expand Down Expand Up @@ -317,15 +330,15 @@
let(:expected_params) do
{
method: method_name,
abi: nil,
args: args.to_json
}
end

it 'calls read_contract with nil ABI' do
it 'calls read_contract without an ABI' do
result

expect(smart_contracts_api).to have_received(:read_contract)
.with('base-sepolia', contract_address, hash_including(expected_params))
.with(network.normalized_id, contract_address, hash_including(expected_params))
end
end

Expand All @@ -342,15 +355,15 @@
let(:expected_params) do
{
method: method_name,
abi: nil,
args: args.to_json
}
end

it 'calls read_contract with nil ABI' do
it 'calls read_contract without an ABI' do
result

expect(smart_contracts_api).to have_received(:read_contract)
.with('base-sepolia', contract_address, hash_including(expected_params))
.with(network.normalized_id, contract_address, expected_params)
end
end

Expand Down
Loading