Class: ContractDetailsCache

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

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ContractDetailsCache

Returns a new instance of ContractDetailsCache.



46
47
48
49
50
51
52
53
# File 'lib/abibase.rb', line 46

def initialize( path )
  @path = path
  @table = if File.exist?( path )
               build_table( read_csv( path ) )
           else
               {}
           end
end

Instance Method Details

#[](addr) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/abibase.rb', line 55

def [](addr)
   if @table.has_key?( addr )
       @table[ addr ]
   else  ## fetch missing data
      data = Etherscan.getcontractdetails( contractaddress: addr )
      ## note: add new data to cache
      @table[ addr ] = data
      data
   end
end

#build_table(recs) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/abibase.rb', line 68

def build_table( recs )
  h = {}
  recs.each do |rec|
      ## (re)use contractdetails format / hash keys
      h[ rec['address'] ] = {
            'contractAddress' => rec['address'],
            'contractCreator' => rec['creator'],
            'txHash'          => rec['txid'],
            'blockNumber'     => rec['blocknumber'],
            'timestamp'       => rec['timestamp']
         }
  end
  h
end

#saveObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/abibase.rb', line 83

def save
  ##############
  ## save cache - sort by blocknumber
  entries = @table.values.sort { |l,r| l['blockNumber'].to_i(16) <=> r['blockNumber'].to_i(16)  }
  buf = ''
  buf << ['blocknumber', 'timestamp', 'address', 'creator', 'txid'].join( ', ' )
  buf << "\n"
  entries.each do |entry|
     buf << [entry['blockNumber'],
             entry['timestamp'],
             entry['contractAddress'],
             entry['contractCreator'],
             entry['txHash']
             ].join( ', ' )
     buf << "\n"
  end
  write_text( @path, buf )
end