Class: Libra::Client
- Inherits:
-
Object
- Object
- Libra::Client
- Defined in:
- lib/libra_client.rb
Instance Method Summary collapse
- #get_account_state(address) ⇒ Object
- #get_account_transaction(address, sequence_number, fetch_events = true) ⇒ Object
- #get_balance(address) ⇒ Object
-
#get_events(address_hex, path, start_sequence_number, ascending = true, limit = 1) ⇒ Object
Returns events specified by ‘access_path` with sequence number in range designated by `start_seq_num`, `ascending` and `limit`.
- #get_events_received(address_hex, start_sequence_number, ascending = true, limit = 1) ⇒ Object
- #get_events_sent(address_hex, start_sequence_number, ascending = true, limit = 1) ⇒ Object
- #get_latest_events_received(address_hex, limit = 1) ⇒ Object
- #get_latest_events_sent(address_hex, limit = 1) ⇒ Object
- #get_latest_transaction_version ⇒ Object
- #get_sequence_number(address) ⇒ Object
- #get_transaction(start_version) ⇒ Object
- #get_transactions(start_version, limit = 1, fetch_events = false) ⇒ Object
-
#initialize(network = "testnet") ⇒ Client
constructor
A new instance of Client.
- #mint_coins_with_faucet_service(receiver, num_coins, is_blocking) ⇒ Object
- #mint_coins_with_local_faucet_account ⇒ Object
- #update_to_latest_ledger(requested_items) ⇒ Object
- #wait_for_transaction(account, sequence_number) ⇒ Object
Constructor Details
#initialize(network = "testnet") ⇒ Client
Returns a new instance of Client.
41 42 43 44 45 |
# File 'lib/libra_client.rb', line 41 def initialize(network="testnet") raise LibraError.new("only support testnet now.") unless network.to_s == "testnet" @host = NETWORKS[network.to_sym][:host] @faucet_host = NETWORKS[network.to_sym][:faucet_host] end |
Instance Method Details
#get_account_state(address) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/libra_client.rb', line 71 def get_account_state(address) query = Types::GetAccountStateRequest.new(address: AccountAddress.hex_to_bytes(address)) item = Types::RequestItem.new(get_account_state_request: query) resp = update_to_latest_ledger([item]) state = resp.response_items[0].get_account_state_response.account_state_with_proof map = Libra::AccountState.deserialize(state.blob.blob).blob resource = map[AccountConfig::ACCOUNT_RESOURCE_PATH] Libra::AccountResource.deserialize(resource.pack('C*')) end |
#get_account_transaction(address, sequence_number, fetch_events = true) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/libra_client.rb', line 95 def get_account_transaction(address, sequence_number, fetch_events=true) addr = AccountAddress.hex_to_bytes(address) query = Types::GetAccountTransactionBySequenceNumberRequest.new(account: addr, sequence_number: sequence_number, fetch_events: fetch_events) item = Types::RequestItem.new(get_account_transaction_by_sequence_number_request: query) resp = update_to_latest_ledger([item]) transaction = resp.response_items[0].get_account_transaction_by_sequence_number_response.signed_transaction_with_proof #Types::SignedTransactionWithProof [:version, :signed_transaction, :proof, :events] transaction end |
#get_balance(address) ⇒ Object
66 67 68 69 |
# File 'lib/libra_client.rb', line 66 def get_balance(address) state = get_account_state(address) state.balance end |
#get_events(address_hex, path, start_sequence_number, ascending = true, limit = 1) ⇒ Object
Returns events specified by ‘access_path` with sequence number in range designated by `start_seq_num`, `ascending` and `limit`. If ascending is true this query will return up to `limit` events that were emitted after `start_event_seq_num`. Otherwise it will return up to `limit` events in the reverse order. Both cases are inclusive.
109 110 111 112 113 114 115 |
# File 'lib/libra_client.rb', line 109 def get_events(address_hex, path, start_sequence_number, ascending=true, limit=1) access_path = AccessPath.new(address_hex, path).to_proto query = Types::GetEventsByEventAccessPathRequest.new(access_path: access_path, start_event_seq_num: start_sequence_number, ascending: ascending, limit: limit) item = Types::RequestItem.new(get_events_by_event_access_path_request: query) resp = update_to_latest_ledger([item]) resp.response_items[0].get_events_by_event_access_path_response.events_with_proof end |
#get_events_received(address_hex, start_sequence_number, ascending = true, limit = 1) ⇒ Object
122 123 124 125 |
# File 'lib/libra_client.rb', line 122 def get_events_received(address_hex, start_sequence_number, ascending=true, limit=1) path = AccountConfig.account_received_event_path get_events(address_hex, path, start_sequence_number, ascending, limit) end |
#get_events_sent(address_hex, start_sequence_number, ascending = true, limit = 1) ⇒ Object
117 118 119 120 |
# File 'lib/libra_client.rb', line 117 def get_events_sent(address_hex, start_sequence_number, ascending=true, limit=1) path = AccountConfig.account_sent_event_path get_events(address_hex, path, start_sequence_number, ascending, limit) end |
#get_latest_events_received(address_hex, limit = 1) ⇒ Object
131 132 133 |
# File 'lib/libra_client.rb', line 131 def get_latest_events_received(address_hex, limit=1) get_events_received(address_hex, 2**64-1, false, limit) end |
#get_latest_events_sent(address_hex, limit = 1) ⇒ Object
127 128 129 |
# File 'lib/libra_client.rb', line 127 def get_latest_events_sent(address_hex, limit=1) get_events_sent(address_hex, 2**64-1, false, limit) end |
#get_latest_transaction_version ⇒ Object
47 48 49 50 51 |
# File 'lib/libra_client.rb', line 47 def get_latest_transaction_version stub = AdmissionControl::AdmissionControl::Stub.new(@host,:this_channel_is_insecure) resp = stub.update_to_latest_ledger(Types::UpdateToLatestLedgerRequest.new()) resp.ledger_info_with_sigs.ledger_info.version end |
#get_sequence_number(address) ⇒ Object
61 62 63 64 |
# File 'lib/libra_client.rb', line 61 def get_sequence_number(address) state = get_account_state(address) state.sequence_number end |
#get_transaction(start_version) ⇒ Object
90 91 92 93 |
# File 'lib/libra_client.rb', line 90 def get_transaction(start_version) #Types::SignedTransaction [:raw_txn_bytes, :sender_public_key, :sender_signature] get_transactions(start_version)[0] end |
#get_transactions(start_version, limit = 1, fetch_events = false) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/libra_client.rb', line 81 def get_transactions(start_version, limit=1, fetch_events=false) query = Types::GetTransactionsRequest.new(start_version: start_version, limit: limit, fetch_events: fetch_events) item = Types::RequestItem.new(get_transactions_request: query) resp = update_to_latest_ledger([item]) txn_list_with_proof = resp.response_items[0].get_transactions_response.txn_list_with_proof #[:transactions, :infos, :events_for_versions, :first_transaction_version, :proof_of_first_transaction, :proof_of_last_transaction] txn_list_with_proof.transactions end |
#mint_coins_with_faucet_service(receiver, num_coins, is_blocking) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/libra_client.rb', line 138 def mint_coins_with_faucet_service(receiver, num_coins, is_blocking) resp = RestClient.post "http://#{@faucet_host}?amount=#{num_coins}&address=#{receiver}", {} if resp.code != 200 raise LibraError.new("Failed to query remote faucet server[status=#{resp.code}]: #{resp.body}") end sequence_number = resp.body.to_i if is_blocking puts AccountConfig.association_address.hex self.wait_for_transaction(AccountConfig.association_address.hex, sequence_number); end end |
#mint_coins_with_local_faucet_account ⇒ Object
135 136 |
# File 'lib/libra_client.rb', line 135 def mint_coins_with_local_faucet_account end |
#update_to_latest_ledger(requested_items) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/libra_client.rb', line 53 def update_to_latest_ledger(requested_items) request = Types::UpdateToLatestLedgerRequest.new(client_known_version: 0, requested_items: requested_items) stub = AdmissionControl::AdmissionControl::Stub.new(@host,:this_channel_is_insecure) response = stub.update_to_latest_ledger(request) # [:response_items, :ledger_info_with_sigs, :validator_change_events] response end |
#wait_for_transaction(account, sequence_number) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/libra_client.rb', line 150 def wait_for_transaction(account, sequence_number) max_iterations = 500 puts("waiting ") while (max_iterations > 0 )do $stdout.flush max_iterations -= 1; transaction = get_account_transaction(account, sequence_number - 1, true) if transaction && transaction.events puts "transaction is stored!" if transaction.events.events.size == 0 puts "no events emitted" end return else print(".") sleep(0.01) end end puts "wait_for_transaction timeout" end |