Skip to content

Commit

Permalink
Passing tests for wait
Browse files Browse the repository at this point in the history
  • Loading branch information
yuga-cb committed Apr 15, 2024
1 parent 6ed8212 commit 235ba16
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/coinbase/transfer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
69 changes: 69 additions & 0 deletions spec/coinbase/transfer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 235ba16

Please sign in to comment.