Skip to content
Snippets Groups Projects
Commit 44ddcbb8 authored by Adrien Béraud's avatar Adrien Béraud Committed by Guillaume Roguez
Browse files

contract: externalize build using makefile


New versions of web3 don't include compilation of contract,
that we also might want to compile separately.

Adapt the node to the new API by calling the solidity compiler
directly and move the contract code and new build system
to the contract directory.

Change-Id: I341034c0e7227d78881e6fb8885eb2de10ee03ea
Reviewed-by: default avatarGuillaume Roguez <guillaume.roguez@savoirfairelinux.com>
parent 90dfbf1a
No related branches found
No related tags found
No related merge requests found
.PHONY: contract
contract:
$(MAKE) -C contract
SOLC ?= solc
registrar.out.json: registrar.sol build.json
$(SOLC) --allow-paths $(realpath .) --standard-json < build.json > registrar.out.json
{
"language": "Solidity",
"sources": {
"registrar": {
"urls":["registrar.sol"]
}
},
"settings": {
"optimizer": {
"enabled": true,
"runs": 500
}
}
}
\ No newline at end of file
pragma solidity ^0.4.0;
/* /*
* Copyright (c) 2014 Gav Wood <g@ethdev.com> * Copyright (c) 2014 Gav Wood <g@ethdev.com>
* Copyright (c) 2016 Savoir-faire Linux Inc. * Copyright (c) 2016 Savoir-faire Linux Inc.
......
/* /*
* Copyright (c) 2016 Savoir-faire Linux Inc. * Copyright (c) 2016-2017 Savoir-faire Linux Inc.
* *
* Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com> * Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
* *
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
'use strict'; 'use strict';
...@@ -49,13 +49,13 @@ console.log(coinbase); ...@@ -49,13 +49,13 @@ console.log(coinbase);
var balance = web3.eth.getBalance(coinbase); var balance = web3.eth.getBalance(coinbase);
console.log(balance.toString(10)); console.log(balance.toString(10));
var REG_ADDR_FILE = "contractAddress.txt"; var REG_FILE = __dirname + "/contract/registrar.out.json";
var REG_ABI_FILE = "contractABI.json"; var REG_ADDR_FILE = __dirname + "/contractAddress.txt";
var REG_ADDR = "0xe53cb2ace8707526a5050bec7bcf979c57f8b44f";
var REG_ABI = [{"constant":true,"inputs":[{"name":"_a","type":"address"}],"name":"name","outputs":[{"name":"o_name","type":"bytes32"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"content","outputs":[{"name":"","type":"bytes32"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"addr","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"subRegistrar","outputs":[{"name":"o_subRegistrar","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_a","type":"address"}],"name":"reserve","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_owner","type":"address"},{"name":"_a","type":"address"}],"name":"reserveFor","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_newOwner","type":"address"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_registrar","type":"address"}],"name":"setSubRegistrar","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"Registrar","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_a","type":"address"},{"name":"_primary","type":"bool"}],"name":"setAddress","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_content","type":"bytes32"}],"name":"setContent","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"bytes32"}],"name":"disown","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"register","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"}],"name":"Changed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"},{"indexed":true,"name":"addr","type":"address"},{"indexed":false,"name":"owner","type":"address"}],"name":"PrimaryChanged","type":"event"}];
var NAME_VALIDATOR = new RegExp('^[a-z0-9-_]{3,32}$'); var NAME_VALIDATOR = new RegExp('^[a-z0-9-_]{3,32}$');
var account; var account;
var regAddress = "0xe53cb2ace8707526a5050bec7bcf979c57f8b44f";
var regABI;
var regContract; var regContract;
var reg; var reg;
...@@ -92,26 +92,26 @@ function loadContract() { ...@@ -92,26 +92,26 @@ function loadContract() {
if (err) { if (err) {
console.log("Can't read contract address: " + err); console.log("Can't read contract address: " + err);
} else { } else {
REG_ADDR = String(content); regAddress = String(content);
} }
fs.readFile(REG_ABI_FILE, function(err, abi_str){ fs.readFile(REG_FILE, function(err, data){
if (err) if (err)
console.log("Can't read contract ABI: " + err); console.log("Can't read contract ABI: " + err);
else else {
REG_ABI = JSON.parse(abi_str); var REG = JSON.parse(data);
console.log("Loading name contract from blockchain at " + REG_ADDR); regABI = JSON.parse(REG.contracts.registrar.GlobalRegistrar.abi);
web3.eth.getCode(REG_ADDR, function(error, result) { }
console.log("Loading name contract from blockchain at " + regAddress);
web3.eth.getCode(regAddress, function(error, result) {
if (error) if (error)
console.log("Error getting contract code: " + error); console.log("Error getting contract code: " + error);
/*else
console.log("Contract code at " + REG_ADDR + ": " + result);*/
if (!result || result == "0x") { if (!result || result == "0x") {
console.log("Contract not found at " + REG_ADDR); console.log("Contract not found at " + regAddress);
initContract(); initContract();
} else { } else {
regContract = web3.eth.contract(REG_ABI); regContract = web3.eth.contract(regABI);
regContract.at(REG_ADDR, function(err, result) { regContract.at(regAddress, function(err, result) {
console.log("Contract found and loaded from " + REG_ADDR); console.log("Contract found and loaded from " + regAddress);
if(!err) { if(!err) {
reg = result; reg = result;
startServer(); startServer();
...@@ -127,27 +127,24 @@ function loadContract() { ...@@ -127,27 +127,24 @@ function loadContract() {
} }
function initContract() { function initContract() {
fs.readFile( __dirname + '/registrar.sol', function(err, data) { fs.readFile(REG_FILE, function(err, data) {
if (err) if (err)
throw err; throw err;
web3.eth.compile.solidity(String(data), function(err, compiled) { var REG = JSON.parse(data);
if (err) { regABI = JSON.parse(REG.contracts.registrar.GlobalRegistrar.abi);
console.log("Can't compile contract :" + err); console.log(regABI);
throw err; regContract = web3.eth.contract(regABI);
}
console.log("Contract compiled, instantiating on blockchain...");
REG_ABI = compiled.GlobalRegistrar.info.abiDefinition;
fs.writeFile(REG_ABI_FILE, JSON.stringify(REG_ABI));
regContract = web3.eth.contract(REG_ABI);
waitForGaz(3000000, function(){ waitForGaz(3000000, function(){
regContract.new({from: coinbase, data: compiled.GlobalRegistrar.code, gas: 3000000}, function(e, contract){ regContract.new({ from: coinbase,
data: '0x'+REG.contracts.registrar.GlobalRegistrar.evm.bytecode.object,
gas: 3000000 }, function(e, contract) {
if(!e) { if(!e) {
if(!contract.address) { if(!contract.address) {
console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined..."); console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined...");
} else { } else {
console.log("Contract mined! Address: " + contract.address); console.log("Contract mined! Address: " + contract.address);
REG_ADDR = contract.address; regAddress = contract.address;
fs.writeFile(REG_ADDR_FILE, REG_ADDR); fs.writeFile(REG_ADDR_FILE, regAddress);
reg = contract; reg = contract;
startServer(); startServer();
} }
...@@ -157,7 +154,6 @@ function initContract() { ...@@ -157,7 +154,6 @@ function initContract() {
}); });
}); });
}); });
});
} }
function checkName(name) { function checkName(name) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment