|
2 | 2 |
|
3 | 3 | require 'json'
|
4 | 4 |
|
5 |
| -library_code = 'function createContract(abiDefinition) { return web3.eth.contract(abiDefinition);}function deployContract(contract, input, account, code, gas) { var instance = contract.new(input,{from:account, data: code, gas: gas}, function (e, contract) { if(!e) { if(!contract.address) { console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined..."); } else { console.log("Contract mined! Address: " + contract.address); console.log(contract); } } else { console.log(e) } }); return instance;} |
| 5 | +def library_code |
| 6 | + 'function createContract(abiDefinition) { return web3.eth.contract(abiDefinition);}function deployContract(account, gas, contract, code, input) { if (typeof(input)===\'undefined\') input = null; var instance = contract.new(input,{from:account, data: code, gas: gas}, function (e, contract) { if(!e) { if(!contract.address) { console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined..."); } else { console.log("Contract mined! Address: " + contract.address); console.log(contract); } } else { console.log(e) } }); return instance;} |
6 | 7 | '
|
| 8 | +end |
| 9 | + |
| 10 | + |
| 11 | +def compile_solidity(code) |
| 12 | + json_string= `curl -s -X POST --data '{"jsonrpc":"2.0","method":"eth_compileSolidity","params":["#{code}"],"id":1}' http://127.0.0.1:8100` |
| 13 | + json_object = JSON.parse(json_string) |
| 14 | + compiled_object = json_object['result'] |
| 15 | + compiled_object |
| 16 | +end |
| 17 | + |
| 18 | +def deploy_contract(code) |
| 19 | +end |
| 20 | + |
| 21 | + |
| 22 | +def javascript_file_name(file_name) |
| 23 | + file_name.split('.')[0] + '_compiled.js' |
| 24 | +end |
| 25 | + |
| 26 | +def get_contract_to_deploy(compiled_object) |
| 27 | + return compiled_object.keys[0] if compiled_object.keys.count == 1 |
| 28 | + puts "Which contract do you want to deploy?" |
| 29 | + choice = 0 |
| 30 | + while choice > 0 && choice < compiled_object.keys.count |
| 31 | + compiled_object.keys.each do |i| |
| 32 | + puts "#{(i+1)}. "+compiled_object.keys[i] |
| 33 | + end |
| 34 | + choice = $stdin.gets.to_i |
| 35 | + end |
| 36 | +end |
| 37 | + |
| 38 | +def get_input |
| 39 | + puts "Enter Input: " |
| 40 | + input = $stdin.gets |
| 41 | + input.strip.chomp == "" ? "null" : input |
| 42 | +end |
| 43 | + |
| 44 | +def get_gas |
| 45 | + gas = 0 |
| 46 | + while gas == 0 |
| 47 | + puts "Enter Gas: " |
| 48 | + gas = $stdin.gets.to_i |
| 49 | + end |
| 50 | + gas |
| 51 | +end |
| 52 | + |
7 | 53 | file_name = ARGV[0]
|
8 | 54 | code = File.read(file_name).gsub("\n",'')
|
9 | 55 |
|
10 |
| -json_string= `curl -s -X POST --data '{"jsonrpc":"2.0","method":"eth_compileSolidity","params":["#{code}"],"id":1}' http://127.0.0.1:8100` |
| 56 | +compiled_object = compile_solidity(code) |
| 57 | +javascript_file_name = javascript_file_name(file_name) |
| 58 | + |
| 59 | +current_contract = get_contract_to_deploy(compiled_object) |
| 60 | + |
| 61 | +compiled_variable_name = "#{current_contract}Compiled" |
| 62 | +contract_variable_name = "#{current_contract}Contract" |
| 63 | +contract_instance_variable_name = "#{current_contract}" |
| 64 | + |
| 65 | +gas = get_gas |
| 66 | +input = get_input |
| 67 | +puts input |
11 | 68 |
|
12 |
| -json_object = JSON.parse(json_string) |
13 |
| -compiled_object = json_object['result'].to_json |
14 |
| -javascript_file_name = file_name.split('.')[0] + '_compiled.js' |
15 | 69 |
|
16 | 70 | File.open(javascript_file_name, 'w') do |f|
|
17 |
| - f.write("#{library_code};\nvar compiled = #{compiled_object};") |
| 71 | + f.write("#{library_code};\nvar #{compiled_variable_name} = #{compiled_object.to_json};") |
| 72 | + f.write("#{contract_variable_name} = createContract(#{compiled_variable_name}.#{current_contract}.info.abiDefinition);") |
| 73 | + f.write("#{contract_instance_variable_name} = deployContract(eth.coinbase,#{gas},#{contract_variable_name},#{compiled_variable_name}.#{current_contract}.code,#{input});") |
18 | 74 | end
|
| 75 | + |
19 | 76 | puts "loadScript('#{`pwd`}/#{javascript_file_name}')".gsub("\n",'')
|
0 commit comments