before("Compile", function(done) { this.timeout(10000); // Compile first var result = solc.compile(fs.readFileSync("./test/Example.sol", {encoding: "utf8"}), 1); var compiled = result.contracts["Example"]; abi = JSON.parse(compiled.interface); binary = compiled.bytecode; // Setup network_one = TestRPC.provider({ // logger: { // log: function(msg) { // console.log("Network 1: " + msg) // } // } }); network_two = TestRPC.provider({ // logger: { // log: function(msg) { // console.log("Network 2: " + msg) // } // } }); done(); }),
// opts // testRPCServer - if true, starts a testRPC server // mnemonic - seed for accounts // port - testrpc port // noDeploy - if true, skip adMarket contract deployment // testRPCProvider - http connection string for console testprc instance export default async function (opts) { opts = opts || {} const mnemonic = opts.mnemonic || MNEMONIC const testRPCServer = opts.testRPCServer const port = opts.port || TESTRPC_PORT const noDeploy = opts.noDeploy const defaultAcct = opts.defaultAcct ? opts.defaultAcct : 0 // default: 30 days of 15s blocks on average const channelTimeout = opts.channelTimeout || 172800 // default: 1 day of 15s blocks on average const challengePeriod = opts.challengePeriod || 5760 const ownerUrl = 'foo.net' // START TESTRPC PROVIDER let provider if (opts.testRPCProvider) { provider = new HttpProvider(opts.testRPCProvider) } else { provider = TestRPC.provider({ mnemonic: mnemonic, }) } // START TESTRPC SERVER if (opts.testRPCServer) { console.log('setting up testrpc server') await p(TestRPC.server({ mnemonic: mnemonic }).listen)(port) } // BUILD ETHJS ABSTRACTIONS const eth = new Eth(provider) const contract = new EthContract(eth) const accounts = await eth.accounts() // COMPILE THE CONTRACT const input = { 'ECVerify.sol': fs.readFileSync(SOL_PATH + 'ECVerify.sol').toString() } const output = solc.compile({ sources: input }, 1) if (output.errors) { console.log(Error(output.errors)) } const abi = JSON.parse(output.contracts['ECVerify.sol:ECVerify'].interface) const bytecode = output.contracts['ECVerify.sol:ECVerify'].bytecode // PREPARE THE ECVerify ABSTRACTION OBJECT const ECVerify = contract(abi, bytecode, { from: accounts[defaultAcct], gas: 3000000 }) let txHash, receipt, ecverify if (!noDeploy) { // DEPLOY THE CONTRACT txHash = await ECVerify.new() await wait(1500) // USE THE ADDRESS FROM THE TX RECEIPT TO BUILD THE CONTRACT OBJECT receipt = await eth.getTransactionReceipt(txHash) ecverify = ECVerify.at(receipt.contractAddress) } // MAKE WEB3 const web3 = new Web3() web3.setProvider(provider) web3.eth.defaultAccount = accounts[0] return { ecverify, ECVerify, eth, accounts, web3 } }
const main = 'Main' // contract template where the user commands are injected before compilation const template = args => `contract ${contractName} { function ${main}() constant returns (${args.returnType}) { ${args.content} return ${args.returnExpression} } }` /** Parse the error message thrown with a naive compile in order to determine the actual return type. This is the hacky alternative to parsing an AST. */ const regexpReturnError = /Return argument type (.*) is not implicitly convertible to expected type \(type of first return variable\) bool./ const matchReturnTypeFromError = message => message.match(regexpReturnError) // setup web3 const web3 = new Web3(TestRPC.provider()) const getAccounts = bluebird.promisify(web3.eth.getAccounts.bind(web3.eth)) /** Takes a list of commands and evaluates them inside a contract. Returns a promised result of the last command. Returns null if the command is not an expression. */ const evalSol = commands => { /** Returns true if the given command is an expression that can return a value. */ const isExpression = command => !/[^=]=[^=]/.test(command) const lastCommand = commands[commands.length - 1] let returnType = 'bool' const content = commands.slice(0, commands.length - 1).join('\n') const contentWithReturnExpression = commands.join('\n') const sourceFirstPass = template({ content: contentWithReturnExpression, returnType, returnExpression: isExpression(lastCommand) ? lastCommand : 'false;' }) let source = sourceFirstPass // Attempt to compile the first pass knowing that any return value (other than bool) will fail. Parse the error message to determine the correct return value and generate the correct source.
var TestRPC = require('ethereumjs-testrpc'); module.exports = { networks: { test: { provider: TestRPC.provider(), // in-memory TestRPC provider network_id: "*" // Match any network id }, development: { host: "localhost", port: 8545, network_id: "*" // Match any network id }, rinkeby: { host: '127.0.0.1', port: 8545, network_id: 4, gasPrice: "20000000000", // 20 gwei }, live: { host: "127.0.0.1", port: 8545, network_id: "1", // Only mainnet gasPrice: "10000000000", // 10 gwei gas: "5000000", // 0.02 eth at 4 gwei price }, } };
return ""; } } var TestRPC = require("ethereumjs-testrpc"); var path = require("path"); module.exports = { networks: { development: { host: "localhost", port: 8545, network_id: "*" // Match any network id }, in_memory: { provider: TestRPC.provider(), network_id: "*", }, ropsten:{ provider: new HDWalletProvider(getNmemonic(), "https://ropsten.infura.io/",0), network_id: "3" }, rinkeby:{ provider: new HDWalletProvider(getNmemonic(), "https://rinkeby.infura.io",0), network_id: "4" }, kovan:{ provider: new HDWalletProvider(getNmemonic(), "https://kovan.infura.io/",0), network_id: "42" } },
import assert from 'assert' import solc from 'solc' import NewContract from '../src/index.js' import { promisify } from 'bluebird' import Web3 from 'web3' import { provider } from 'ethereumjs-testrpc' const testprovider = provider() const web3 = new Web3(testprovider) const getAccounts = promisify(web3.eth.getAccounts.bind(web3.eth)) describe('eth-new-contract', () => { it('should create a new contract from a web3 contract constructor', () => { const newContract = NewContract(testprovider) const compilation = solc.compile('contract MyContract { function GetAnswer() constant returns(uint) { return 42; } }') const contract = compilation.contracts[':MyContract'] const bytecode = contract.bytecode const abi = JSON.parse(contract.interface) const MyContract = web3.eth.contract(abi) return getAccounts() .then(accounts => newContract(MyContract, { from: accounts[0], data: bytecode })) .then(contract => { assert(contract.address, 'Address is not defined') return promisify(contract.GetAnswer.bind(contract))().then(val => { assert.equal(val.toString(), '42') })