Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing errors and specs #13

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions lib/ethereum/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(name, code, abi)
@events = []
@constructor_inputs = @abi.detect {|x| x["type"] == "constructor"}["inputs"] rescue nil
@abi.select {|x| x["type"] == "function" }.each do |abifun|
@functions << Ethereum::Function.new(abifun)
@functions << Ethereum::Function.new(abifun)
end
@abi.select {|x| x["type"] == "event" }.each do |abievt|
@events << Ethereum::ContractEvent.new(abievt)
Expand Down Expand Up @@ -43,7 +43,16 @@ def build(connection)
end
end
deploy_payload = deploy_code + deploy_arguments
deploytx = connection.send_transaction({from: self.sender, gas: self.gas, gasPrice: self.gas_price, data: "0x" + deploy_payload})["result"]
tx_payload = {
jsonrpc: "2.0",
method: "eth_sendTransaction",
params: [{
from: self.sender,
data: deploy_payload
}],
id: connection.get_id
}.to_json
deploytx = JSON.parse(connection.send_single(tx_payload))["result"]
instance_variable_set("@deployment", Ethereum::Deployment.new(deploytx, connection))
end

Expand All @@ -66,7 +75,7 @@ def build(connection)
end

define_method :at do |addr|
instance_variable_set("@address", addr)
instance_variable_set("@address", addr)
self.events.each do |event|
event.set_address(addr)
event.set_client(connection)
Expand All @@ -82,7 +91,7 @@ def build(connection)
end

define_method :sender do
instance_variable_get("@sender") || connection.coinbase["result"]
instance_variable_get("@sender") || connection.eth_coinbase["result"]
end

define_method :set_gas_price do |gp|
Expand All @@ -97,7 +106,7 @@ def build(connection)
instance_variable_set("@gas", gas)
end

define_method :gas do
define_method :gas do
instance_variable_get("@gas") || 3000000
end

Expand All @@ -119,11 +128,11 @@ def build(connection)
logs["result"].each do |result|
inputs = evt.input_types
outputs = inputs.zip(result["topics"][1..-1])
data = {blockNumber: result["blockNumber"].hex, transactionHash: result["transactionHash"], blockHash: result["blockHash"], transactionIndex: result["transactionIndex"].hex, topics: []}
data = {blockNumber: result["blockNumber"].hex, transactionHash: result["transactionHash"], blockHash: result["blockHash"], transactionIndex: result["transactionIndex"].hex, topics: []}
outputs.each do |output|
data[:topics] << formatter.from_payload(output)
end
collection << data
collection << data
end
return collection
end
Expand All @@ -135,11 +144,11 @@ def build(connection)
logs["result"].each do |result|
inputs = evt.input_types
outputs = inputs.zip(result["topics"][1..-1])
data = {blockNumber: result["blockNumber"].hex, transactionHash: result["transactionHash"], blockHash: result["blockHash"], transactionIndex: result["transactionIndex"].hex, topics: []}
data = {blockNumber: result["blockNumber"].hex, transactionHash: result["transactionHash"], blockHash: result["blockHash"], transactionIndex: result["transactionIndex"].hex, topics: []}
outputs.each do |output|
data[:topics] << formatter.from_payload(output)
end
collection << data
collection << data
end
return collection
end
Expand Down Expand Up @@ -169,7 +178,7 @@ def build(connection)
arg_types.zip(args).each do |arg|
payload << formatter.to_payload(arg)
end
raw_result = connection.call({to: self.address, from: self.sender, data: payload.join()})["result"]
raw_result = connection.eth_call({to: self.address, from: self.sender, data: "0x" + payload.join()})["result"]
formatted_result = fun.outputs.collect {|x| x.type }.zip(raw_result.gsub(/^0x/,'').scan(/.{64}/))
output = formatted_result.collect {|x| formatter.from_payload(x) }
return {data: "0x" + payload.join(), raw: raw_result, formatted: output}
Expand All @@ -178,9 +187,9 @@ def build(connection)
define_method call_function_name do |*args|
data = self.send(call_raw_function_name, *args)
output = data[:formatted]
if output.length == 1
if output.length == 1
return output[0]
else
else
return output
end
end
Expand All @@ -195,7 +204,7 @@ def build(connection)
arg_types.zip(args).each do |arg|
payload << formatter.to_payload(arg)
end
txid = connection.send_transaction({to: self.address, from: self.sender, data: "0x" + payload.join(), gas: self.gas, gasPrice: self.gas_price})["result"]
txid = connection.send_single({to: self.address, from: self.sender, data: "0x" + payload.join(), gas: self.gas, gasPrice: self.gas_price}.to_json)["result"]
return Ethereum::Transaction.new(txid, self.connection, payload.join(), args)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/ethereum/contract_initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def link_libraries
end

def build(connection)
@contract = Ethereum::Contract.new(@name, @binary, @abi)
@contract = Ethereum::Contract.new(@name, @binary, @abi)
@contract.build(connection)
end

Expand Down
14 changes: 7 additions & 7 deletions lib/ethereum/deployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@ def initialize(txid, connection)

def mined?
return true if @mined
@mined = @connection.get_transaction_by_hash(@id)["result"]["blockNumber"].present? rescue nil
@mined = @connection.eth_get_transaction_by_hash(@id)["result"]["blockNumber"].present? rescue nil
@mined ||= false
end

def has_address?
return true if @contract_address.present?
return false unless self.mined?
@contract_address ||= @connection.get_transaction_receipt(@id)["result"]["contractAddress"]
return false unless self.mined?
@contract_address ||= @connection.eth_get_transaction_receipt(@id)["result"]["contractAddress"]
return @contract_address.present?
end

def deployed?
return true if @valid_deployment
return false unless self.has_address?
@valid_deployment = @connection.get_code(@contract_address)["result"] != "0x"
@valid_deployment = @connection.eth_get_transaction_receipt(@contract_address)["result"] != "0x"
end

def wait_for_deployment(timeout = 1500.seconds)
start_time = Time.now
while self.deployed? == false
raise "Transaction #{@id} timed out." if ((Time.now - start_time) > timeout)
sleep 5
return true if self.deployed?
raise "Transaction #{@id} timed out." if ((Time.now - start_time) > timeout)
sleep 5
return true if self.deployed?
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/ethereum/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ class Initializer
def initialize(file, client = Ethereum::IpcClient.new)
@file = File.read(file)
@client = client
sol_output = @client.compile_solidity(@file)
sol_output = @client.eth_compile_solidity(@file)
contracts = sol_output["result"].keys
@contracts = []
contracts.each do |contract|
abi = sol_output["result"][contract]["info"]["abiDefinition"]
name = contract
abi = sol_output["result"][contract]["info"]["abiDefinition"]
name = contract.sub('<stdin>:', '')
code = sol_output["result"][contract]["code"]
@contracts << Ethereum::Contract.new(name, code, abi)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

describe 'IpcClient' do
before(:each) do
@client = Ethereum::IpcClient.new("#{ENV['HOME']}/EtherDev/data/geth.ipc")
@client = Ethereum::IpcClient.new("#{ENV['HOME']}/.ethereum/testnet/geth.ipc")
end

it 'should work' do
Expand Down
8 changes: 4 additions & 4 deletions spec/ethereum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
describe Ethereum do

before(:all) do
@client = Ethereum::HttpClient.new("172.16.135.102", "8545")
@client = Ethereum::HttpClient.new("localhost", "8545")
@formatter = Ethereum::Formatter.new
end

describe "Ethereum Version" do
it 'has a version number' do
expect(Ethereum::VERSION).to eq("0.4.90")
expect(Ethereum::VERSION).to eq("1.5.2")
end
end

describe "Deployment" do
it "should deploy a contract with parameters" do
@init = Ethereum::Initializer.new("#{ENV['PWD']}/spec/fixtures/ContractWithParams.sol", @client)
@init.build_all
@contract_with_params = ContractWithParams.new
@coinbase = @contract_with_params.connection.coinbase["result"]
@coinbase = @contract_with_params.connection.eth_coinbase["result"]
@contract_with_params.deploy_and_wait(60, @coinbase)
address = @contract_with_params.call_get_setting__
expect(address).to eq(@coinbase)
Expand Down
4 changes: 2 additions & 2 deletions spec/fixtures/SimpleNameRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ contract SimpleNameRegistry {
bytes16 public fifth;
bytes32 public sixth;

modifier ifowner { if (msg.sender == owner) _ }
modifier ifowner { if (msg.sender == owner) _; }

function SimpleNameRegistry() {
owner = msg.sender;
Expand Down Expand Up @@ -63,4 +63,4 @@ contract SimpleContract {
function SimpleContract() {
owner = msg.sender;
}
}
}