Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Upgraded to truffle 5.0.0
  • Loading branch information
filiplazovic committed Dec 13, 2018
1 parent 563a142 commit a7dbe10
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 305 deletions.
25 changes: 13 additions & 12 deletions contracts/helpers/forwarder.js
@@ -1,20 +1,17 @@
const fs = require('fs');
const solc = require('solc');
const { getCoinbase, compileCode } = require('./utils');
const web3 = require('web3');
const { checkAddressChecksum, toChecksumAddress } = web3.utils;

async function createForwarder(address) {
async function createForwarder(address, constructorData) {
if (!checkAddressChecksum(address)) {
address = toChecksumAddress(address);
}
const solidityCode = `
pragma solidity ^0.4.24;
contract Forwarder {
constructor(bytes _data) public {
if (_data.length > 0) {
require(address(${address}).delegatecall(_data), "Initialization failed");
}
require(_data.length > 0, "Transaction data not passed");
require(address(${address}).delegatecall(_data), "Initialization failed");
}
function() external payable {
require(msg.sig != 0x0, "Function signature not specified");
Expand All @@ -29,13 +26,17 @@ async function createForwarder(address) {
}
}
`;
const source = { 'Forwarder': solidityCode }
const options = {
'contracts_directory': 'contracts',
solc
}
const coinbase = await getCoinbase();
return compileCode(source, options, coinbase);
const data = solc.compile(solidityCode, 1).contracts[':Forwarder'];
const contract = new web3.eth.Contract(JSON.parse(data.interface));
return contract.deploy({
data: data.bytecode,
arguments: [constructorData]
})
.send({
gas: 6721975,
from: coinbase
});
}

module.exports = createForwarder;
25 changes: 2 additions & 23 deletions contracts/helpers/utils.js
@@ -1,7 +1,4 @@
const truffleContract = require('truffle-contract');
const compile = require('truffle-compile');

function getEncodedCall(web3, instance, method, params = []) {
function getEncodedCall(instance, method, params = []) {
const contract = new web3.eth.Contract(instance.abi)
return contract.methods[method](...params).encodeABI()
}
Expand All @@ -17,25 +14,7 @@ async function getCoinbase() {
});
}

function compileCode(source, options, coinbase) {
return new Promise(function (resolve, reject) {
compile(source, options, function (err, val) {
if (err) {
reject(err)
}
const Forwarder = truffleContract(val.Forwarder);
Forwarder.setProvider(web3.currentProvider);
Forwarder.defaults({
from: coinbase,
gas: 6721975
})
resolve(Forwarder)
});
});
}

module.exports = {
getEncodedCall,
getCoinbase,
compileCode
getCoinbase
};

0 comments on commit a7dbe10

Please sign in to comment.