Class: Coinbase::SmartContract
- Inherits:
-
Object
- Object
- Coinbase::SmartContract
- Defined in:
- lib/coinbase/smart_contract.rb
Overview
A representation of a SmartContract on the blockchain.
Class Method Summary collapse
-
.create_token_contract(address_id:, wallet_id:, name:, symbol:, total_supply:) ⇒ SmartContract
Creates a new ERC20 token contract, that can subsequently be deployed to the blockchain.
-
.list_events(network_id:, protocol_name:, contract_address:, contract_name:, event_name:, from_block_height:, to_block_height:) ⇒ Enumerable<Coinbase::ContractEvent>
Returns a list of ContractEvents for the provided network, contract, and event details.
Instance Method Summary collapse
-
#abi ⇒ Array<Hash>
Returns the ABI of the Smart Contract.
-
#contract_address ⇒ String
Returns the contract address of the SmartContract.
-
#deploy! ⇒ SmartContract
Deploys the signed SmartContract to the blockchain.
-
#deployer_address ⇒ String
Returns the address of the deployer of the SmartContract.
-
#id ⇒ String
Returns the SmartContract ID.
-
#initialize(model) ⇒ SmartContract
constructor
Returns a new SmartContract object.
-
#inspect ⇒ String
Same as to_s.
-
#network ⇒ Coinbase::Network
Returns the Network of the SmartContract.
-
#options ⇒ Coinbase::Client::SmartContractOptions
Returns the options of the SmartContract.
-
#reload ⇒ SmartContract
Reloads the Smart Contract model with the latest version from the server side.
-
#sign(key) ⇒ SmartContract
Signs the SmartContract deployment transaction with the given key.
-
#status ⇒ String
Returns the status of the SmartContract.
-
#to_s ⇒ String
Returns a string representation of the SmartContract.
-
#transaction ⇒ Coinbase::Transaction
Returns the transaction.
-
#type ⇒ Coinbase::Client::SmartContractType
Returns the type of the SmartContract.
-
#wait!(interval_seconds = 0.2, timeout_seconds = 20) ⇒ SmartContract
Waits until the Smart Contract deployment is signed or failed by polling the server at the given interval.
-
#wallet_id ⇒ String
Returns the ID of the wallet that deployed the SmartContract.
Constructor Details
#initialize(model) ⇒ SmartContract
Returns a new SmartContract object.
110 111 112 113 114 |
# File 'lib/coinbase/smart_contract.rb', line 110 def initialize(model) raise unless model.is_a?(Coinbase::Client::SmartContract) @model = model end |
Class Method Details
.create_token_contract(address_id:, wallet_id:, name:, symbol:, total_supply:) ⇒ SmartContract
Creates a new ERC20 token contract, that can subsequently be deployed to the blockchain.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/coinbase/smart_contract.rb', line 50 def self.create_token_contract( address_id:, wallet_id:, name:, symbol:, total_supply: ) contract = Coinbase.call_api do smart_contracts_api.create_smart_contract( wallet_id, address_id, { type: Coinbase::Client::SmartContractType::ERC20, options: Coinbase::Client::TokenContractOptions.new( name: name, symbol: symbol, total_supply: BigDecimal(total_supply).to_i.to_s ).to_body } ) end new(contract) end |
.list_events(network_id:, protocol_name:, contract_address:, contract_name:, event_name:, from_block_height:, to_block_height:) ⇒ Enumerable<Coinbase::ContractEvent>
Returns a list of ContractEvents for the provided network, contract, and event details.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/coinbase/smart_contract.rb', line 15 def self.list_events( network_id:, protocol_name:, contract_address:, contract_name:, event_name:, from_block_height:, to_block_height: ) Coinbase::Pagination.enumerate( lambda { |page| list_events_page( network_id, protocol_name, contract_address, contract_name, event_name, from_block_height, to_block_height, page ) } ) do |contract_event| Coinbase::ContractEvent.new(contract_event) end end |
Instance Method Details
#abi ⇒ Array<Hash>
Returns the ABI of the Smart Contract.
144 145 146 |
# File 'lib/coinbase/smart_contract.rb', line 144 def abi JSON.parse(@model.abi) end |
#contract_address ⇒ String
Returns the contract address of the SmartContract.
132 133 134 |
# File 'lib/coinbase/smart_contract.rb', line 132 def contract_address @model.contract_address end |
#deploy! ⇒ SmartContract
Deploys the signed SmartContract to the blockchain.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/coinbase/smart_contract.rb', line 193 def deploy! raise TransactionNotSignedError unless transaction.signed? @model = Coinbase.call_api do smart_contracts_api.deploy_smart_contract( wallet_id, deployer_address, id, { signed_payload: transaction.signature } ) end @transaction = Coinbase::Transaction.new(@model.transaction) self end |
#deployer_address ⇒ String
Returns the address of the deployer of the SmartContract.
138 139 140 |
# File 'lib/coinbase/smart_contract.rb', line 138 def deployer_address @model.deployer_address end |
#id ⇒ String
Returns the SmartContract ID. NOTE: This is not the contract address and is primarily used before the contract is deployed.
120 121 122 |
# File 'lib/coinbase/smart_contract.rb', line 120 def id @model.smart_contract_id end |
#inspect ⇒ String
Same as to_s.
253 254 255 |
# File 'lib/coinbase/smart_contract.rb', line 253 def inspect to_s end |
#network ⇒ Coinbase::Network
Returns the Network of the SmartContract.
126 127 128 |
# File 'lib/coinbase/smart_contract.rb', line 126 def network @network ||= Coinbase::Network.from_id(@model.network_id) end |
#options ⇒ Coinbase::Client::SmartContractOptions
Returns the options of the SmartContract.
162 163 164 |
# File 'lib/coinbase/smart_contract.rb', line 162 def @model. end |
#reload ⇒ SmartContract
Reloads the Smart Contract model with the latest version from the server side.
212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/coinbase/smart_contract.rb', line 212 def reload @model = Coinbase.call_api do smart_contracts_api.get_smart_contract( wallet_id, deployer_address, id ) end @transaction = Coinbase::Transaction.new(@model.transaction) self end |
#sign(key) ⇒ SmartContract
Signs the SmartContract deployment transaction with the given key. This is required before broadcasting the SmartContract.
184 185 186 187 188 |
# File 'lib/coinbase/smart_contract.rb', line 184 def sign(key) raise unless key.is_a?(Eth::Key) transaction.sign(key) end |
#status ⇒ String
Returns the status of the SmartContract.
174 175 176 |
# File 'lib/coinbase/smart_contract.rb', line 174 def status transaction.status end |
#to_s ⇒ String
Returns a string representation of the SmartContract.
259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/coinbase/smart_contract.rb', line 259 def to_s Coinbase.pretty_print_object( self.class, network: network.id, contract_address: contract_address, deployer_address: deployer_address, type: type, status: status, options: Coinbase.pretty_print_object('Options', **) ) end |
#transaction ⇒ Coinbase::Transaction
Returns the transaction.
168 169 170 |
# File 'lib/coinbase/smart_contract.rb', line 168 def transaction @transaction ||= Coinbase::Transaction.new(@model.transaction) end |
#type ⇒ Coinbase::Client::SmartContractType
Returns the type of the SmartContract.
156 157 158 |
# File 'lib/coinbase/smart_contract.rb', line 156 def type @model.type end |
#wait!(interval_seconds = 0.2, timeout_seconds = 20) ⇒ SmartContract
Waits until the Smart Contract deployment is signed or failed by polling the server at the given interval. deployment to land on-chain, in seconds
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/coinbase/smart_contract.rb', line 232 def wait!(interval_seconds = 0.2, timeout_seconds = 20) start_time = Time.now loop do reload return self if transaction.terminal_state? if Time.now - start_time > timeout_seconds raise Timeout::Error, 'SmartContract deployment timed out. Try waiting again.' end self.sleep interval_seconds end self end |
#wallet_id ⇒ String
Returns the ID of the wallet that deployed the SmartContract.
150 151 152 |
# File 'lib/coinbase/smart_contract.rb', line 150 def wallet_id @model.wallet_id end |