Class: Cns::Apibc

Inherits:
Object
  • Object
show all
Defined in:
lib/cns/apibc.rb

Overview

classe para acesso dados blockchains

Instance Method Summary collapse

Instance Method Details

#account_es(ads) ⇒ Array<Hash>

Returns lista enderecos e seus saldos.

Examples:

account_es

{
  status: '1',
  message: 'OK',
  result: [
    { account: '0x...', balance: '4000000000000000000' },
    { account: '0x...', balance: '87000000000000000000' }
  ]
}

Parameters:

  • ads (String)

    lista enderecos ETH (max 20)

Returns:

  • (Array<Hash>)

    lista enderecos e seus saldos



32
33
34
35
36
37
38
39
# File 'lib/cns/apibc.rb', line 32

def (ads)
  JSON.parse(
    conn_es.get('/api', action: 'balancemulti', tag: 'latest', address: ads.join(',')).body,
    symbolize_names: true
  )[:result]
rescue StandardError
  []
end

#account_gm(add) ⇒ Hash

Returns endereco e seus saldo/recursos.

Examples:

account_gm

{
  account_name: '...',
  head_block_num: 141_391_122,
  head_block_time: '2020-09-11T16:05:51.000',
  privileged: false,
  last_code_update: '1970-01-01T00:00:00.000',
  created: '2018-06-09T13:14:37.000',

  core_liquid_balance: '1232.0228 EOS',
  total_resources: { owner: '...', net_weight: '1000.1142 EOS', cpu_weight: '1000.1144 EOS', ram_bytes: 8148 },

  ram_quota: 9548,
  net_weight: 10_001_142,
  cpu_weight: 10_001_144,
  net_limit: { used: 0, available: 1_066_648_346, max: 1_066_648_346 },
  cpu_limit: { used: 338, available: 88_498, max: 88_836 },
  ram_usage: 3574,
  permissions: [
    {
      perm_name: 'active',
      parent: 'owner',
      required_auth: {
        threshold: 1, keys: [{ key: 'EOS...', weight: 1 }], accounts: [], waits: []
      }
    },
    {
      perm_name: 'owner',
      parent: '',
      required_auth: {
        threshold: 1, keys: [{ key: 'EOS...', weight: 1 }], accounts: [], waits: []
      }
    }
  ],
  self_delegated_bandwidth: { from: '...', to: '...', net_weight: '1000.1142 EOS', cpu_weight: '1000.1144 EOS' },
  refund_request: nil,
  voter_info: {
    owner: '...',
    proxy: '...',
    producers: [],
    staked: 20_002_286,
    last_vote_weight: '17172913021904.12109375000000000',
    proxied_vote_weight: '0.00000000000000000',
    is_proxy: 0,
    flags1: 0,
    reserved2: 0,
    reserved3: '0.0000 EOS'
  },
  rex_info: nil
}

Parameters:

  • add (String)

    endereco EOS

Returns:

  • (Hash)

    endereco e seus saldo/recursos



93
94
95
96
97
# File 'lib/cns/apibc.rb', line 93

def (add)
  JSON.parse(conn_gm.post('/v1/chain/get_account', { account_name: add }.to_json).body, symbolize_names: true)
rescue StandardError
  { core_liquid_balance: 0, total_resources: { net_weight: 0, cpu_weight: 0 } }
end

#block_es(add, pag = 0, aes = []) ⇒ Array<Hash>

{

status: '1',
message: 'OK',
result: [
  { blockNumber: '15737070', timeStamp: '1665638459', blockReward: '8922867945448231' },
  { blockNumber: '15592786', timeStamp: '1663896779', blockReward: '44997990286623752' },
  { blockNumber: '15570222', timeStamp: '1663622819', blockReward: '261211525682683684' },
  { blockNumber: '15568554', timeStamp: '1663602539', blockReward: '41993833217879559' }
]

}

Examples:

block_es

Parameters:

  • add (String)

    endereco ETH

  • pag (Integer) (defaults to: 0)

    pagina transacoes

  • aes (Array<Hash>) (defaults to: [])

    acumulador transacoes

Returns:

  • (Array<Hash>)

    lista blocos etherscan



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/cns/apibc.rb', line 191

def block_es(add, pag = 0, aes = [])
  res = JSON.parse(
    conn_es.get(
      '/api',
      action: 'getminedblocks',
      blocktype: 'blocks',
      offset: 10_000,
      address: add,
      page: pag += 1
    ).body,
    symbolize_names: true
  )[:result]
  aes += res
  res.count < 10_000 ? aes : block_es(add, pag, aes)
rescue StandardError
  aes
end

#data_bc(uri) ⇒ Array<Hash>

Returns lista dados beaconchain.

Parameters:

  • uri (String)

    ETH2 API

Returns:

  • (Array<Hash>)

    lista dados beaconchain



12
13
14
15
16
17
18
19
# File 'lib/cns/apibc.rb', line 12

def data_bc(uri)
  res = JSON.parse(conn_bc.get(uri).body, symbolize_names: true)[:data] || []
  # calls are rate limited to 10 requests/minute/IP
  sleep(3)
  res.is_a?(Array) ? res : [res]
rescue StandardError
  []
end

#inter_es(add, pag = 0, aes = []) ⇒ Array<Hash>

{

status: '1',
message: 'OK',
result: [
  {
    blockNumber: '15592786',
    timeStamp: '1663896779',
    hash: '0x...',
    from: '0x...',
    to: '0x...',
    value: '22000000000000000',
    contractAddress: '',
    input: '',
    type: 'call',
    gas: '2300',
    gasUsed: '0',
    traceId: '0_1_1',
    isError: '0',
    errCode: ''
  }
]

}

Examples:

inter_es

Parameters:

  • add (String)

    endereco ETH

  • pag (Integer) (defaults to: 0)

    pagina transacoes

  • aes (Array<Hash>) (defaults to: [])

    acumulador transacoes

Returns:

  • (Array<Hash>)

    lista transacoes internas etherscan



167
168
169
170
171
172
173
174
175
176
# File 'lib/cns/apibc.rb', line 167

def inter_es(add, pag = 0, aes = [])
  res = JSON.parse(
    conn_es.get('/api', action: 'txlistinternal', offset: 10_000, address: add, page: pag += 1).body,
    symbolize_names: true
  )[:result]
  aes += res
  res.count < 10_000 ? aes : inter_es(add, pag, aes)
rescue StandardError
  aes
end

#ledger_gm(add, agm = []) ⇒ Array<Hash>

Returns lista completa transacoes greymass.

Examples:

ledger_gm

{
  actions: [
    {
      account_action_seq: 964,
      action_trace: {
        account_ram_deltas: [],
        act: {
          account: 'voicebestapp',
          authorization: [
            { actor: 'thetruevoice', permission: 'active' },
            { actor: 'voicebestapp', permission: 'active' }
          ],
          data: { from: 'voicebestapp', memo: '...', quantity: '1.0001 MESSAGE', to: '...' },
          hex_data: '...',
          name: 'transfer'
        },
        action_ordinal: 10,
        block_num: 141_345_345,
        block_time: '2020-09-11T09:44:04.500',
        closest_unnotified_ancestor_action_ordinal: 5,
        context_free: false,
        creator_action_ordinal: 5,
        elapsed: 6,
        producer_block_id: '...',
        receipt: {
          abi_sequence: 1,
          act_digest: '...',
          auth_sequence: [['thetruevoice', 6_778_215], ['voicebestapp', 435_346]],
          code_sequence: 1,
          global_sequence: 233_283_589_258,
          receiver: '...',
          recv_sequence: 927
        },
        receiver: '...',
        trx_id: '...'
      },
      block_num: 141_345_345,
      block_time: '2020-09-11T09:44:04.500',
      global_action_seq: 233_283_589_258,
      irreversible: true
    },
    {}
  ],
  head_block_num: 141_721_698,
  last_irreversible_block: 141_721_371
}

Parameters:

  • agm (Array<Hash>) (defaults to: [])

    acumulador transacoes

  • add (String)

    endereco EOS

Returns:

  • (Array<Hash>)

    lista completa transacoes greymass



338
339
340
341
342
343
344
345
346
347
# File 'lib/cns/apibc.rb', line 338

def ledger_gm(add, agm = [])
  res = JSON.parse(
    conn_gm.post('/v1/history/get_actions', { account_name: add, pos: agm.count, offset: 100 }.to_json).body,
    symbolize_names: true
  )[:actions]
  agm += res
  res.count < 100 ? agm : ledger_gm(add, agm)
rescue StandardError
  agm
end

#norml_es(add, pag = 0, aes = []) ⇒ Array<Hash>

Returns lista transacoes normais etherscan.

Examples:

norml_es

{
  status: '1',
  message: 'OK',
  result: [
    {
      blockNumber: '4984535',
      timeStamp: '1517094794',
      hash: '0x...',
      nonce: '10',
      blockHash: '0x...',
      transactionIndex: '17',
      from: '0x...',
      to: '0x...',
      value: '52627271000000000000',
      gas: '21000',
      gasPrice: '19000000000',
      isError: '0',
      txreceipt_status: '1',
      input: '0x',
      contractAddress: '',
      gasUsed: '21000',
      cumulativeGasUsed: '566293',
      confirmations: '5848660'
    },
    {}
  ]
}

Parameters:

  • add (String)

    endereco ETH

  • pag (Integer) (defaults to: 0)

    pagina transacoes

  • aes (Array<Hash>) (defaults to: [])

    acumulador transacoes

Returns:

  • (Array<Hash>)

    lista transacoes normais etherscan



131
132
133
134
135
136
137
138
139
140
# File 'lib/cns/apibc.rb', line 131

def norml_es(add, pag = 0, aes = [])
  res = JSON.parse(
    conn_es.get('/api', action: 'txlist', offset: 10_000, address: add, page: pag += 1).body,
    symbolize_names: true
  )[:result]
  aes += res
  res.count < 10_000 ? aes : norml_es(add, pag, aes)
rescue StandardError
  aes
end

#token_es(add, pag = 0, aes = []) ⇒ Array<Hash>

Returns lista token transfer events etherscan.

Examples:

token_es

{
  status: '1',
  message: 'OK',
  result: [
    {
      blockNumber: '3967652',
      timeStamp: '1499081515',
      hash: '0x registo duplicado com todos os dados iguais',
      nonce: '3',
      blockHash: '0x00a49e999036dc13dc6c4244bb1d51d3146fe7f00bfb500a7624d82e299c7328',
      from: '0xd0a6e6c54dbc68db5db3a091b171a77407ff7ccf',
      contractAddress: '0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0',
      to: '0x...',
      value: '0',
      tokenName: 'EOS',
      tokenSymbol: 'EOS',
      tokenDecimal: '18',
      transactionIndex: '83',
      gas: '173399',
      gasPrice: '21000000000',
      gasUsed: '173398',
      input: 'deprecated',
      cumulativeGasUsed: '7484878',
      confirmations: '3442641'
    },
    {}
  ]
}

Parameters:

  • add (String)

    endereco ETH

  • pag (Integer) (defaults to: 0)

    pagina transacoes

  • aes (Array<Hash>) (defaults to: [])

    acumulador transacoes

Returns:

  • (Array<Hash>)

    lista token transfer events etherscan



274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/cns/apibc.rb', line 274

def token_es(add, pag = 0, aes = [])
  # registos duplicados aparecem em token events (ver exemplo acima)
  # -quando ha erros na blockchain (acho)
  # -quando ha token events identicos no mesmo block (acho)
  res = JSON.parse(
    conn_es.get('/api', action: 'tokentx', offset: 10_000, address: add, page: pag += 1).body,
    symbolize_names: true
  )[:result]
  aes += res
  res.count < 10_000 ? aes : token_es(add, pag, aes)
rescue StandardError
  aes
end

#withw_es(add, pag = 0, aes = []) ⇒ Array<Hash>

{

"status":"1",
"message":"OK",
"result":[
   {
      "withdrawalIndex":"14",
      "validatorIndex":"119023",
      "address":"0xb9d7934878b5fb9610b3fe8a5e441e8fad7e293f",
      "amount":"3244098967",
      "blockNumber":"17034877",
      "timestamp":"1681338599"
   }
]

}

Examples:

withw_es

Parameters:

  • add (String)

    endereco ETH

  • pag (Integer) (defaults to: 0)

    pagina transacoes

  • aes (Array<Hash>) (defaults to: [])

    acumulador transacoes

Returns:

  • (Array<Hash>)

    lista blocos etherscan



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/cns/apibc.rb', line 226

def withw_es(add, pag = 0, aes = [])
  res = JSON.parse(
    conn_es.get(
      '/api',
      action: 'txsBeaconWithdrawal',
      address: add,
      offset: 10_000,
      page: pag += 1
    ).body,
    symbolize_names: true
  )[:result]
  aes += res
  res.count < 10_000 ? aes : withw_es(add, pag, aes)
rescue StandardError
  aes
end