Class: Cns::Greymass

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

Overview

classe para processar transacoes do greymass

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dad, pop) ⇒ Greymass

Returns API greymass - processar transacoes.

Parameters:

  • dad (Hash)

    todos os dados bigquery

  • pop (Thor::CoreExt::HashWithIndifferentAccess)

    opcoes trabalho

Options Hash (pop):

  • :h (Hash) — default: {}

    configuracao dias ajuste reposicionamento temporal

  • :v (Boolean) — default: false

    mostra dados transacoes?

  • :t (Boolean) — default: false

    mostra transacoes todas ou somente novas?



22
23
24
25
26
# File 'lib/cns/greymass.rb', line 22

def initialize(dad, pop)
  @api = Apibc.new
  @bqd = dad
  @ops = pop
end

Instance Attribute Details

#apiApibc (readonly)

Returns API blockchains.

Returns:

  • (Apibc)

    API blockchains



10
11
12
# File 'lib/cns/greymass.rb', line 10

def api
  @api
end

#bqdArray<Hash> (readonly)

Returns todos os dados bigquery.

Returns:

  • (Array<Hash>)

    todos os dados bigquery



12
13
14
# File 'lib/cns/greymass.rb', line 12

def bqd
  @bqd
end

#opsThor::CoreExt::HashWithIndifferentAccess (readonly)

Returns opcoes trabalho.

Returns:

  • (Thor::CoreExt::HashWithIndifferentAccess)

    opcoes trabalho



14
15
16
# File 'lib/cns/greymass.rb', line 14

def ops
  @ops
end

Instance Method Details

#base_bc(wbq) ⇒ Hash

Returns dados greymass - address, saldo & transacoes.

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:

  • wbq (Hash)

    wallet bigquery

Returns:

  • (Hash)

    dados greymass - address, saldo & transacoes



57
58
59
60
61
62
63
64
# File 'lib/cns/greymass.rb', line 57

def base_bc(wbq)
  xbq = wbq[:ax]
  {
    ax: xbq,
    sl: greymass_sl(xbq).inject(:+),
    tx: filtrar_tx(xbq, api.ledger_gm(xbq))
  }
end

#bcdArray<Hash>

Returns todos os dados greymass - saldos & transacoes.

Returns:

  • (Array<Hash>)

    todos os dados greymass - saldos & transacoes



39
40
41
# File 'lib/cns/greymass.rb', line 39

def bcd
  @bcd ||= bqd[:wb].map { |obj| base_bc(obj) }
end

#bq_bc(wbq, hbc) ⇒ Hash

Returns dados juntos bigquery & greymass.

Parameters:

  • hbc (Hash)

    dados greymass - address, saldo & transacoes

  • wbq (Hash)

    wallet bigquery

Returns:

  • (Hash)

    dados juntos bigquery & greymass



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cns/greymass.rb', line 69

def bq_bc(wbq, hbc)
  xbq = wbq[:ax]
  {
    id: wbq[:id],
    ax: xbq,
    bs: wbq[:sl],
    bt: bqd[:nt].select { |obj| obj[:iax] == xbq },
    es: hbc[:sl],
    et: hbc[:tx]
  }
end

#dadosArray<Hash>

Returns todos os dados juntos bigquery & greymass.

Returns:

  • (Array<Hash>)

    todos os dados juntos bigquery & greymass



44
45
46
# File 'lib/cns/greymass.rb', line 44

def dados
  @dados ||= bqd[:wb].map { |obq| bq_bc(obq, bcd.select { |obj| obq[:ax] == obj[:ax] }.first) }
end

#filtrar_tx(add, ary) ⇒ Array<Hash>

Returns lista transacoes filtrada.

Parameters:

  • ary (Array<Hash>)

    lista transacoes

  • add (String)

    endereco EOS

Returns:

  • (Array<Hash>)

    lista transacoes filtrada



96
97
98
99
100
101
102
103
# File 'lib/cns/greymass.rb', line 96

def filtrar_tx(add, ary)
  # elimina transferencia from: (lax) to: (add) - esta transferencia aparece em from: (add) to: (lax)
  # adiciona chave indice itx & adiciona identificador da carteira iax
  (ary.delete_if do |odl|
    adt = odl[:action_trace][:act][:data]
    adt[:to] == add && lax.include?(adt[:from])
  end).map { |omp| omp.merge(itx: omp[:global_action_seq], iax: add) }
end

#formata_carteira(hjn) ⇒ String

Returns texto formatado duma carteira.

Parameters:

  • hjn (Hash)

    dados juntos bigquery & greymass

Returns:

  • (String)

    texto formatado duma carteira



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cns/greymass.rb', line 122

def formata_carteira(hjn)
  format(
    '%<s1>-12.12s %<v1>14.4f %<n1>4i %<v2>14.4f %<n2>4i %<ok>-3s',
    s1: hjn[:ax],
    v1: hjn[:es],
    n1: hjn[:et].count,
    v2: hjn[:bs],
    n2: hjn[:bt].count,
    ok: ok?(hjn) ? 'OK' : 'NOK'
  )
end

#formata_ledger(hlx) ⇒ String

Returns texto formatado ledger 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:

  • hlx (Hash)

    ledger greymass

Returns:

  • (String)

    texto formatado ledger greymass



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/cns/greymass.rb', line 143

def formata_ledger(hlx)
  format(
    '%<bn>12i %<fr>-12.12s %<to>-12.12s %<ac>-10.10s %<dt>10.10s %<vl>12.4f %<sy>-6.6s',
    ac: (act = hlx[:action_trace][:act])[:name],
    fr: (adt = act[:data])[:from],
    vl: (aqt = adt[:quantity].to_s).to_d,
    bn: hlx[:itx],
    to: adt[:to],
    dt: Date.parse(hlx[:block_time]),
    sy: aqt[/[[:upper:]]+/]
  )
end

#greymass_sl(add) ⇒ Array<BigDecimal>

Returns lista recursos - liquido, net, spu.

Parameters:

  • ary (Array<Hash>)

    lista transacoes

  • add (String)

    endereco EOS

Returns:

  • (Array<BigDecimal>)

    lista recursos - liquido, net, spu



83
84
85
86
87
88
89
90
91
# File 'lib/cns/greymass.rb', line 83

def greymass_sl(add)
  hac = api.(add)
  htr = hac[:total_resources]
  [
    hac[:core_liquid_balance].to_d,
    htr[:net_weight].to_d,
    htr[:cpu_weight].to_d
  ]
end

#idtArray<Integer>

Returns lista indices transacoes novas.

Returns:

  • (Array<Integer>)

    lista indices transacoes novas



49
50
51
52
# File 'lib/cns/greymass.rb', line 49

def idt
  @idt ||= bcd.map { |obc| obc[:tx].map { |obj| obj[:itx] } }.flatten -
           (ops[:t] ? [] : bqd[:nt].map { |obq| obq[:itx] })
end

#laxArray<String>

Returns lista dos meus enderecos.

Returns:

  • (Array<String>)

    lista dos meus enderecos



34
35
36
# File 'lib/cns/greymass.rb', line 34

def lax
  @lax ||= bqd[:wb].map { |obj| obj[:ax] }
end

#mostra_configuracao_ajuste_diasString

Returns texto configuracao ajuste dias das transacoes.

Returns:

  • (String)

    texto configuracao ajuste dias das transacoes



165
166
167
168
169
# File 'lib/cns/greymass.rb', line 165

def mostra_configuracao_ajuste_dias
  return unless novax.count.positive?

  puts("\nstring ajuste dias\n-h=#{sorax.map { |obj| "#{obj[:itx]}:0" }.join(' ')}")
end

#mostra_resumoString

Returns texto carteiras & transacoes & ajuste dias.

Returns:

  • (String)

    texto carteiras & transacoes & ajuste dias



111
112
113
114
115
116
117
118
# File 'lib/cns/greymass.rb', line 111

def mostra_resumo
  return unless dados.count.positive?

  puts("\naddress            greymass  ntx       bigquery  ntx")
  dados.each { |obj| puts(formata_carteira(obj)) }
  mostra_transacoes_novas
  mostra_configuracao_ajuste_dias
end

#mostra_transacoes_novasString

Returns texto transacoes.

Returns:

  • (String)

    texto transacoes



157
158
159
160
161
162
# File 'lib/cns/greymass.rb', line 157

def mostra_transacoes_novas
  return unless ops[:v] && novax.count.positive?

  puts("\nsequence num from         to           accao      data              valor moeda")
  sorax.each { |obj| puts(formata_ledger(obj)) }
end

#novaxArray<Hash>

Returns lista transacoes novas.

Returns:

  • (Array<Hash>)

    lista transacoes novas



29
30
31
# File 'lib/cns/greymass.rb', line 29

def novax
  @novax ||= bcd.map { |obc| obc[:tx].select { |obj| idt.include?(obj[:itx]) } }.flatten
end

#ok?(hjn) ⇒ Boolean

Returns carteira tem transacoes novas(sim=NOK, nao=OK)?.

Parameters:

  • hjn (Hash)

    dados juntos bigquery & greymass

Returns:

  • (Boolean)

    carteira tem transacoes novas(sim=NOK, nao=OK)?



136
137
138
# File 'lib/cns/greymass.rb', line 136

def ok?(hjn)
  hjn[:bs] == hjn[:es] && hjn[:bt].count == hjn[:et].count
end

#soraxArray<Hash>

Returns lista ordenada transacoes novas.

Returns:

  • (Array<Hash>)

    lista ordenada transacoes novas



106
107
108
# File 'lib/cns/greymass.rb', line 106

def sorax
  novax.sort { |ant, prx| prx[:itx] <=> ant[:itx] }
end