Class: Etherscan::Call

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

Constant Summary collapse

CHAINS =
{
  mainnet: 'http://api.etherscan.io/api?',
  ropsten: 'https://ropsten.etherscan.io/api?',
  kovan: 'https://kovan.etherscan.io/api?',
  rinkeby: 'https://rinkeby.etherscan.io/api?'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chain, mod, action) ⇒ Call

Returns a new instance of Call.



40
41
42
43
44
45
46
# File 'lib/etherscan/call.rb', line 40

def initialize(chain, mod, action)
  @chain = chain.to_sym
  @base = CHAINS[chain.to_sym]
  @mod = mod
  @action = action
  @api_key = false
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def action
  @action
end

#addressObject

Returns the value of attribute address.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def address
  @address
end

#api_keyObject

Returns the value of attribute api_key.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def api_key
  @api_key
end

#blocknoObject

Returns the value of attribute blockno.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def blockno
  @blockno
end

#blocktypeObject

Returns the value of attribute blocktype.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def blocktype
  @blocktype
end

#booleanObject

Returns the value of attribute boolean.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def boolean
  @boolean
end

#chainObject

Returns the value of attribute chain.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def chain
  @chain
end

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def data
  @data
end

#endblockObject

Returns the value of attribute endblock.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def endblock
  @endblock
end

#fromBlockObject

Returns the value of attribute fromBlock.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def fromBlock
  @fromBlock
end

#gasObject

Returns the value of attribute gas.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def gas
  @gas
end

#gasPriceObject

Returns the value of attribute gasPrice.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def gasPrice
  @gasPrice
end

#hexObject

Returns the value of attribute hex.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def hex
  @hex
end

#indexObject

Returns the value of attribute index.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def index
  @index
end

#modObject

Returns the value of attribute mod.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def mod
  @mod
end

#offsetObject

Returns the value of attribute offset.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def offset
  @offset
end

#pageObject

Returns the value of attribute page.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def page
  @page
end

#positionObject

Returns the value of attribute position.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def position
  @position
end

#sortObject

Returns the value of attribute sort.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def sort
  @sort
end

#startblockObject

Returns the value of attribute startblock.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def startblock
  @startblock
end

#tagObject

Returns the value of attribute tag.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def tag
  @tag
end

#toObject

Returns the value of attribute to.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def to
  @to
end

#toBlockObject

Returns the value of attribute toBlock.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def toBlock
  @toBlock
end

#topicsObject

Returns the value of attribute topics.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def topics
  @topics
end

#txhashObject

Returns the value of attribute txhash.



5
6
7
# File 'lib/etherscan/call.rb', line 5

def txhash
  @txhash
end

Instance Method Details

#fetchObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/etherscan/call.rb', line 48

def fetch
  query_url = to_s
  Etherscan.logger.debug query_url
  res = Faraday.get(query_url).body
  Etherscan.logger.debug res
  data = JSON.parse(res)
  return [:error, data['error']] if data['error']
  return [:error, data['message']] if (data['status'] && data['status'] != '1')
  return [:ok, data['result']]
rescue => e
  Etherscan.logger.error "Error: #{e}"
  Etherscan.logger.error e.backtrace[0, 20].join("\n")
  return [:error, e.message]
end

#to_sObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/etherscan/call.rb', line 63

def to_s
  uri = 'module=' + mod + '&action=' + action
  uri += '&apikey=' + api_key if api_key
  uri += address_fragment
  uri += '&startblock=' + startblock.to_s if startblock
  uri += '&endblock=' + endblock.to_s if endblock
  uri += '&blocktype=' + blocktype if blocktype
  uri += '&txhash=' + txhash if txhash
  uri += '&blockno=' + blockno.to_s if blockno
  uri += '&offset=' + offset.to_s if offset
  uri += '&sort=' + sort if sort
  uri += '&page=' + page.to_s if page
  uri += '&tag=' + tag if tag
  uri += '&hex=' + hex if hex
  uri += '&boolean=' + boolean if boolean
  uri += '&data=' + data if data
  uri += '&to=' + to if to
  uri += '&index=' + index if index
  uri += '&position=' + position if position
  uri += '&gasPrice=' + gasPrice if gasPrice
  uri += '&fromBlock=' + fromBlock if fromBlock
  uri += '&toBlock=' + toBlock if toBlock
  if topics
    topics.each do |topic|
      uri += "&#{topic['name']}=" + topic['value']
    end
  end
  @base + uri
end