lib/flow_client/client.rb in flow_client-0.2.0 vs lib/flow_client/client.rb in flow_client-0.2.1

- old
+ new

@@ -24,13 +24,16 @@ def ping req = Access::PingRequest.new @stub.ping(req) end - # Accounts + # :section: Accounts - # Gets an account + # Returns an account for the address specified at the latest + # block. + # + # @return [FlowClient::Account] the account def get_account(address) req = Access::GetAccountAtLatestBlockRequest.new(address: to_bytes(address)) begin res = @stub.get_account_at_latest_block(req) @@ -51,18 +54,17 @@ weight: key.weight ) end account.contracts = res.account.contracts - account end end # Creates a new account # - # + # @return [FlowClient::Account] the newly created account def create_account(new_account_public_keys, contracts, payer_account, signer) script = File.read(File.join("lib", "cadence", "templates", "create-account.cdc")) arguments = [ CadenceType.Array( @@ -103,18 +105,12 @@ # Adds a public key to an account def add_account_key(public_key_hex, payer_account, signer, weight) script = File.read(File.join("lib", "cadence", "templates", "add-account-key.cdc")) arguments = [ - { - type: "String", - value: public_key_hex - }.to_json, - { - type: "UFix64", - value: weight.to_s - }.to_json + CadenceType.String(public_key_hex), + CadenceType.UFix64(weight) ] transaction = FlowClient::Transaction.new transaction.script = script transaction.reference_block_id = get_latest_block.id @@ -136,18 +132,12 @@ def add_contract(name, code, payer_account, signer) script = File.read(File.join("lib", "cadence", "templates", "add-contract.cdc")) code_hex = code.unpack1("H*") arguments = [ - { - type: "String", - value: name - }.to_json, - { - type: "String", - value: code_hex - }.to_json + CadenceType.String(name), + CadenceType.String(code_hex) ] transaction = FlowClient::Transaction.new transaction.script = script transaction.reference_block_id = get_latest_block.id @@ -168,14 +158,11 @@ # Removes a contract from an account def remove_contract(name, payer_account, signer) script = File.read(File.join("lib", "cadence", "templates", "remove-contract.cdc")) arguments = [ - { - type: "String", - value: name - }.to_json + CadenceType.String(name), ] transaction = FlowClient::Transaction.new transaction.script = script transaction.reference_block_id = get_latest_block.id @@ -197,18 +184,12 @@ def update_contract(name, code, payer_account, signer) script = File.read(File.join("lib", "cadence", "templates", "update-contract.cdc")) code_hex = code.unpack1("H*") arguments = [ - { - type: "String", - value: name - }.to_json, - { - type: "String", - value: code_hex - }.to_json + CadenceType.String(name), + CadenceType.String(code_hex) ] transaction = FlowClient::Transaction.new transaction.script = script transaction.reference_block_id = get_latest_block.id @@ -224,11 +205,13 @@ wait_for_transaction(res.id) do |response| raise CadenceRuntimeError, response.error_message if response.status_code != 0 end end - # Scripts + # :section: Scripts + + # Executes a script on the blockchain def execute_script(script, args = []) processed_args = [] args.to_a.each do |arg| processed_arg = arg.class == OpenStruct ? Utils.openstruct_to_json(arg) : arg processed_args << processed_arg @@ -241,45 +224,61 @@ res = @stub.execute_script_at_latest_block(req) parse_json(res.value) end - # Blocks + # :section: Blocks + + # Returns the latest block + # + # @return [FlowClient::Block] the block def get_latest_block(is_sealed: true) req = Access::GetLatestBlockRequest.new( is_sealed: is_sealed ) res = @stub.get_latest_block(req) Block.parse_grpc_block_response(res) end + # Returns the block with id + # + # @return [FlowClient::Block] the block def get_block_by_id(id) req = Access::GetBlockByIDRequest.new( id: to_bytes(id) ) res = @stub.get_block_by_id(req) Block.parse_grpc_block_response(res) end + # Returns the latest with height + # + # @return [FlowClient::Block] the block def get_block_by_height(height) req = Access::GetBlockByHeightRequest.new( height: height ) res = @stub.get_block_by_height(req) Block.parse_grpc_block_response(res) end - # Collections + # :section: Collections + + # Returns the collection with id + # + # @return [FlowClient::Collection] the collection def get_collection_by_id(id) req = Access::GetCollectionByIDRequest.new( id: to_bytes(id) ) res = @stub.get_collection_by_id(req) Collection.parse_grpc_type(res) end - # Events + # :section: Events + + # Returns events of the given type between the start and end block heights. def get_events(type, start_height, end_height) req = Access::GetEventsForHeightRangeRequest.new( type: type, start_height: start_height, end_height: end_height @@ -291,13 +290,15 @@ else res.results.map { |event| EventsResult.parse_grpc_type(event) } end end - # Transactions + # :section: Transactions # Sends a transaction to the blockchain + # + # @return [FlowClient::TransactionResponse] the transaction response def send_transaction(transaction) transaction.address_aliases = @address_aliases req = Access::SendTransactionRequest.new( transaction: transaction.to_protobuf_message ) @@ -309,10 +310,13 @@ else TransactionResponse.parse_grpc_type(res) end end + # Returns the transaction with transaction_id + # + # @return [FlowClient::Transaction] the transaction def get_transaction(transaction_id) req = Access::GetTransactionRequest.new( id: to_bytes(transaction_id) ) @@ -324,10 +328,12 @@ Transaction.parse_grpc_type(res.transaction) end end # Returns a transaction result + # + # @return [FlowClient::TransactionResult] the transaction result def get_transaction_result(transaction_id) req = Access::GetTransactionRequest.new( id: to_bytes(transaction_id) ) @@ -338,12 +344,14 @@ else TransactionResult.parse_grpc_type(res) end end + # Polls the blockchain for the transaction result until it is sealed + # or expired def wait_for_transaction(transaction_id) response = get_transaction_result(transaction_id) - while response.status != :SEALED + while ![:SEALED, :EXPIRED].include? response.status sleep(0.5) response = get_transaction_result(transaction_id) end yield(response)