Class: Coinbase::ContractInvocation
- Inherits:
-
Object
- Object
- Coinbase::ContractInvocation
- Defined in:
- lib/coinbase/contract_invocation.rb
Overview
A representation of a Contract Invocation.
Class Method Summary collapse
-
.create(address_id:, wallet_id:, contract_address:, abi:, method:, amount:, asset_id:, network:, args: {}) ⇒ ContractInvocation
Creates a new ContractInvocation object.
-
.list(wallet_id:, address_id:) ⇒ Enumerable<Coinbase::ContractInvocation>
Enumerates the payload signatures for a given address belonging to a wallet.
Instance Method Summary collapse
-
#abi ⇒ Array<Hash>
Returns the ABI of the Contract Invocation.
-
#address_id ⇒ String
Returns the Address ID of the Contract Invocation.
-
#amount ⇒ BigDecimal
Returns the amount of the native asset sent to a payable contract method, if applicable.
-
#args ⇒ Hash
Returns the arguments of the Contract Invocation.
-
#broadcast! ⇒ ContractInvocation
Broadcasts the ContractInvocation to the Network.
-
#contract_address ⇒ String
Returns the Contract Address of the Contract Invocation.
-
#id ⇒ String
Returns the Contract Invocation ID.
-
#initialize(model) ⇒ ContractInvocation
constructor
Returns a new ContractInvocation object.
-
#inspect ⇒ String
Same as to_s.
-
#method ⇒ String
Returns the method of the Contract Invocation.
-
#network ⇒ Coinbase::Network
Returns the Network of the Contract Invocation.
-
#reload ⇒ ContractInvocation
# Reload reloads the Contract Invocation model with the latest version from the server side.
-
#sign(key) ⇒ ContractInvocation
Signs the Contract Invocation transaction with the given key.
-
#status ⇒ String
Returns the status of the Contract Invocation.
-
#to_s ⇒ String
Returns a String representation of the Contract Invocation.
-
#transaction ⇒ Coinbase::Transaction
Returns the transaction.
-
#wait!(interval_seconds = 0.2, timeout_seconds = 20) ⇒ ContractInvocation
Waits until the Contract Invocation is signed or failed by polling the server at the given interval.
-
#wallet_id ⇒ String
Returns the Wallet ID of the Contract Invocation.
Constructor Details
#initialize(model) ⇒ ContractInvocation
Returns a new ContractInvocation object. Do not use this method directly. Instead use Coinbase::ContractInvocation.create.
89 90 91 92 93 |
# File 'lib/coinbase/contract_invocation.rb', line 89 def initialize(model) raise unless model.is_a?(Coinbase::Client::ContractInvocation) @model = model end |
Class Method Details
.create(address_id:, wallet_id:, contract_address:, abi:, method:, amount:, asset_id:, network:, args: {}) ⇒ ContractInvocation
Creates a new ContractInvocation object.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/coinbase/contract_invocation.rb', line 24 def create( address_id:, wallet_id:, contract_address:, abi:, method:, amount:, asset_id:, network:, args: {} ) atomic_amount = nil if amount && asset_id && network network = Coinbase::Network.from_id(network) asset = network.get_asset(asset_id) atomic_amount = asset.to_atomic_amount(amount).to_i_to_s end model = Coinbase.call_api do contract_invocation_api.create_contract_invocation( wallet_id, address_id, contract_address: contract_address, abi: abi.to_json, method: method, args: args.to_json, amount: atomic_amount ) end new(model) end |
.list(wallet_id:, address_id:) ⇒ Enumerable<Coinbase::ContractInvocation>
Enumerates the payload signatures for a given address belonging to a wallet. The result is an enumerator that lazily fetches from the server, and can be iterated over, converted an array, etc…
62 63 64 65 66 67 68 |
# File 'lib/coinbase/contract_invocation.rb', line 62 def list(wallet_id:, address_id:) Coinbase::Pagination.enumerate( ->(page) { fetch_page(wallet_id, address_id, page) } ) do |contract_invocation| new(contract_invocation) end end |
Instance Method Details
#abi ⇒ Array<Hash>
Returns the ABI of the Contract Invocation.
127 128 129 |
# File 'lib/coinbase/contract_invocation.rb', line 127 def abi JSON.parse(@model.abi) end |
#address_id ⇒ String
Returns the Address ID of the Contract Invocation.
109 110 111 |
# File 'lib/coinbase/contract_invocation.rb', line 109 def address_id @model.address_id end |
#amount ⇒ BigDecimal
Returns the amount of the native asset sent to a payable contract method, if applicable.
145 146 147 |
# File 'lib/coinbase/contract_invocation.rb', line 145 def amount BigDecimal(@model.amount) end |
#args ⇒ Hash
Returns the arguments of the Contract Invocation.
139 140 141 |
# File 'lib/coinbase/contract_invocation.rb', line 139 def args JSON.parse(@model.args).transform_keys(&:to_sym) end |
#broadcast! ⇒ ContractInvocation
Broadcasts the ContractInvocation to the Network.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/coinbase/contract_invocation.rb', line 178 def broadcast! raise TransactionNotSignedError unless transaction.signed? @model = Coinbase.call_api do contract_invocation_api.broadcast_contract_invocation( wallet_id, address_id, id, { signed_payload: transaction.signature } ) end @transaction = Coinbase::Transaction.new(@model.transaction) self end |
#contract_address ⇒ String
Returns the Contract Address of the Contract Invocation.
121 122 123 |
# File 'lib/coinbase/contract_invocation.rb', line 121 def contract_address @model.contract_address end |
#id ⇒ String
Returns the Contract Invocation ID.
97 98 99 |
# File 'lib/coinbase/contract_invocation.rb', line 97 def id @model.contract_invocation_id end |
#inspect ⇒ String
Same as to_s.
249 250 251 |
# File 'lib/coinbase/contract_invocation.rb', line 249 def inspect to_s end |
#method ⇒ String
Returns the method of the Contract Invocation.
133 134 135 |
# File 'lib/coinbase/contract_invocation.rb', line 133 def method @model.method end |
#network ⇒ Coinbase::Network
Returns the Network of the Contract Invocation.
115 116 117 |
# File 'lib/coinbase/contract_invocation.rb', line 115 def network @network ||= Coinbase::Network.from_id(@model.network_id) end |
#reload ⇒ ContractInvocation
# Reload reloads the Contract Invocation model with the latest version from the server side.
197 198 199 200 201 202 203 204 205 |
# File 'lib/coinbase/contract_invocation.rb', line 197 def reload @model = Coinbase.call_api do contract_invocation_api.get_contract_invocation(wallet_id, address_id, id) end @transaction = Coinbase::Transaction.new(@model.transaction) self end |
#sign(key) ⇒ ContractInvocation
Signs the Contract Invocation transaction with the given key. This is required before broadcasting the Contract Invocation when not using a Server-Signer.
167 168 169 170 171 172 173 |
# File 'lib/coinbase/contract_invocation.rb', line 167 def sign(key) raise unless key.is_a?(Eth::Key) transaction.sign(key) self end |
#status ⇒ String
Returns the status of the Contract Invocation.
157 158 159 |
# File 'lib/coinbase/contract_invocation.rb', line 157 def status transaction.status end |
#to_s ⇒ String
Returns a String representation of the Contract Invocation.
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/coinbase/contract_invocation.rb', line 231 def to_s Coinbase.pretty_print_object( self.class, id: id, wallet_id: wallet_id, address_id: address_id, network_id: network.id, status: status, abi: abi.to_json, method: method, args: args.to_json, transaction_hash: transaction.transaction_hash, transaction_link: transaction.transaction_link ) end |
#transaction ⇒ Coinbase::Transaction
Returns the transaction.
151 152 153 |
# File 'lib/coinbase/contract_invocation.rb', line 151 def transaction @transaction ||= Coinbase::Transaction.new(@model.transaction) end |
#wait!(interval_seconds = 0.2, timeout_seconds = 20) ⇒ ContractInvocation
Waits until the Contract Invocation is signed or failed by polling the server at the given interval. Raises a Timeout::Error if the Contract Invocation takes longer than the given timeout. in seconds.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/coinbase/contract_invocation.rb', line 213 def wait!(interval_seconds = 0.2, timeout_seconds = 20) start_time = Time.now loop do reload return self if transaction.terminal_state? raise Timeout::Error, 'Contract Invocation timed out' if Time.now - start_time > timeout_seconds self.sleep interval_seconds end self end |
#wallet_id ⇒ String
Returns the Wallet ID of the Contract Invocation.
103 104 105 |
# File 'lib/coinbase/contract_invocation.rb', line 103 def wallet_id @model.wallet_id end |