From 235ba164d11fc1ac6e769b2587fe5f70ec976a85 Mon Sep 17 00:00:00 2001 From: Yuga Cohler Date: Mon, 15 Apr 2024 18:35:20 -0400 Subject: [PATCH] Passing tests for wait --- lib/coinbase/transfer.rb | 5 ++- spec/coinbase/transfer_spec.rb | 69 ++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/lib/coinbase/transfer.rb b/lib/coinbase/transfer.rb index e68ea58d..9c0bcc57 100644 --- a/lib/coinbase/transfer.rb +++ b/lib/coinbase/transfer.rb @@ -106,7 +106,8 @@ def status end end - # Waits until the Transfer is completed or failed by polling the Network at the given interval. + # Waits until the Transfer is completed or failed by polling the Network at the given interval. Raises a + # Timeout::Error if the Transfer takes longer than the given timeout. # @param interval_seconds [Integer] The interval at which to poll the Network, in seconds # @param timeout_seconds [Integer] The maximum amount of time to wait for the Transfer to complete, in seconds # @return [Transfer] The completed Transfer object @@ -118,7 +119,7 @@ def wait!(interval_seconds = 0.2, timeout_seconds = 10) raise Timeout::Error, 'Transfer timed out' if Time.now - start_time > timeout_seconds - sleep interval_seconds + self.sleep interval_seconds end self diff --git a/spec/coinbase/transfer_spec.rb b/spec/coinbase/transfer_spec.rb index 1aa8631a..61fd3d17 100644 --- a/spec/coinbase/transfer_spec.rb +++ b/spec/coinbase/transfer_spec.rb @@ -212,4 +212,73 @@ end end end + + describe '#wait!' do + before do + allow(client).to receive(:eth_getTransactionCount).with(from_address_id, 'latest').and_return('0x7') + allow(client).to receive(:eth_gasPrice).and_return('0x7b') + # TODO: This isn't working for some reason. + allow(transfer).to receive(:sleep) + end + + context 'when the transfer is completed' do + let(:onchain_transaction) { { 'blockHash' => '0xdeadbeef' } } + let(:transaction_receipt) { { 'status' => '0x1' } } + + before do + transfer.transaction.sign(from_key) + allow(client) + .to receive(:eth_getTransactionByHash) + .with(transfer.transaction_hash) + .and_return(onchain_transaction) + allow(client) + .to receive(:eth_getTransactionReceipt) + .with(transfer.transaction_hash) + .and_return(transaction_receipt) + end + + it 'returns the completed Transfer' do + expect(transfer.wait!).to eq(transfer) + expect(transfer.status).to eq(Coinbase::Transfer::Status::COMPLETE) + end + end + + context 'when the transfer is failed' do + let(:onchain_transaction) { { 'blockHash' => '0xdeadbeef' } } + let(:transaction_receipt) { { 'status' => '0x0' } } + + before do + transfer.transaction.sign(from_key) + allow(client) + .to receive(:eth_getTransactionByHash) + .with(transfer.transaction_hash) + .and_return(onchain_transaction) + allow(client) + .to receive(:eth_getTransactionReceipt) + .with(transfer.transaction_hash) + .and_return(transaction_receipt) + end + + it 'returns the failed Transfer' do + expect(transfer.wait!).to eq(transfer) + expect(transfer.status).to eq(Coinbase::Transfer::Status::FAILED) + end + end + + context 'when the transfer times out' do + let(:onchain_transaction) { { 'blockHash' => nil } } + + before do + transfer.transaction.sign(from_key) + allow(client) + .to receive(:eth_getTransactionByHash) + .with(transfer.transaction_hash) + .and_return(onchain_transaction) + end + + it 'raises a Timeout::Error' do + expect { transfer.wait!(0.2, 0.00001) }.to raise_error(Timeout::Error, 'Transfer timed out') + end + end + end end