diff --git a/lib/coinbase/smart_contract.rb b/lib/coinbase/smart_contract.rb index ad9ca00f..bd7ae5c7 100644 --- a/lib/coinbase/smart_contract.rb +++ b/lib/coinbase/smart_contract.rb @@ -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: {} ) @@ -159,7 +159,7 @@ def self.read( method: method, args: (args || {}).to_json, abi: abi&.to_json - } + }.compact ) end diff --git a/spec/unit/coinbase/smart_contract_spec.rb b/spec/unit/coinbase/smart_contract_spec.rb index 40d447de..49174a89 100644 --- a/spec/unit/coinbase/smart_contract_spec.rb +++ b/spec/unit/coinbase/smart_contract_spec.rb @@ -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, @@ -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 @@ -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 @@ -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' } @@ -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 @@ -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