Skip to content

Instantly share code, notes, and snippets.

@serapath
Created October 30, 2018 00:42
Show Gist options
  • Save serapath/96fb682a9f20ea2e5a59db76c7c25d4c to your computer and use it in GitHub Desktop.
Save serapath/96fb682a9f20ea2e5a59db76c7c25d4c to your computer and use it in GitHub Desktop.

Revisions

  1. serapath created this gist Oct 30, 2018.
    35 changes: 35 additions & 0 deletions Escrow.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 1,35 @@
    pragma solidity ^0.4.25;

    contract Escrow {

    address public buyer;
    address public seller;
    address public arbiter;
    uint256 public escrowAmount;

    constructor(address _buyer, address _arbiter, uint256 _escrowAmount) public {
    seller = msg.sender;
    buyer = _buyer;
    arbiter = _arbiter;
    escrowAmount = _escrowAmount;
    }

    function fund() payable public {
    require(msg.value == escrowAmount);
    }

    function payoutSeller() public {
    require(msg.sender == buyer || msg.sender == arbiter);
    seller.transfer(address(this).balance);
    }

    function refundBuyer() public {
    require(msg.sender == seller || msg.sender == arbiter);
    buyer.transfer(address(this).balance);
    }

    function contractBalance() public view returns (uint256){
    return address(this).balance;
    }

    }
    91 changes: 91 additions & 0 deletions script.js
    Original file line number Diff line number Diff line change
    @@ -0,0 1,91 @@
    var helper = {}
    var logfile = 'browser/terminal.log'
    var contractSource = 'gist/Escrow.sol'
    var g = { callback }

    helper.log = (type, msg) => {
    remix.getFile(logfile, (error, content) => {
    if (error) throw new Error('could not read logFile')
    remix.setFile(logfile, `${new Date()} ${type}:${'\n' msg}${'\n' content}`)
    })
    if (type === 'ERR') return console.error(msg)
    if (type === 'INFO') return console.info(msg)
    else console.log(msg)
    }
    helper.info = msg => helper.log('INFO', msg)
    helper.error = msg => helper.log('ERR', msg)

    remix.getFile(contractSource, (error, code) => {
    if (error) return global.error(error)
    // helper.info(`[CONTRACT]:\n\n${code}`)
    compilers['solidity'].event.register('compilationFinished', g, g.callback)
    compilers['solidity'].compile({'Escrow.sol': { content: code } })
    })

    function callback (status, metadata, code) {
    compilers['solidity'].event.unregister('compilationFinished', g, g.callback)
    web3.eth.getAccounts((err, localAddresses) => {
    if (err) return helper.error(err)
    const abi = metadata.contracts['Escrow.sol'].Escrow.abi
    const bytecode = metadata.contracts['Escrow.sol'].Escrow.evm.bytecode.object
    deploy1(localAddresses[0], abi, bytecode)
    })
    }

    function deploy1 (sender, abi, bytecode) {
    // helper.info(abi)
    // helper.info(bytecode)
    // helper.info(params)
    // helper.info(sender)
    const contract = new web3.eth.Contract(abi)
    const deployAll = deployer(bytecode, contract)

    const sendersList = [
    sender,
    sender,
    sender
    ]

    const paramsList = [
    ['0x410C51728BF98626719b55D9B2ebD42717125118', '0x6b02b375704558B42eF84A40110A660a92F38476', 300000000000000000],
    ['0x410C51728BF98626719b55D9B2ebD42717125118', '0x6b02b375704558B42eF84A40110A660a92F38476', 300000000000000000],
    ['0x410C51728BF98626719b55D9B2ebD42717125118', '0x6b02b375704558B42eF84A40110A660a92F38476', 300000000000000000]
    ]

    deployAll(sendersList, paramsList, (err, result) => {
    if (err) throw err
    helper.info(result)
    remix.setFile('browser/deployedContracts.json', JSON.stringify(result, null, 2))
    })
    }
    function deployer (bytecode, contract) {
    return function deployAll (sendersList, paramsList, done) {
    const contractAddresses = {}
    const len = sendersList.length
    paramsList.forEach((params, i) => {
    const [ buyer, arbiter, price ] = params
    const sender = sendersList[i]
    deployContract(i, sender, [buyer, arbiter, price], done)
    })
    function deployContract (i, sender, params, done) {
    try {
    contract.deploy({ data: '0x' bytecode, arguments: params })
    .send({ from: sender, gas: 1500000 }, (error, transactionHash) => {})
    //.on('error', error => helper.error('error ' error))
    //.on('transactionHash', transactionHash => helper.info('transactionHash: ' transactionHash))
    //.on('receipt', receipt => helper.info('receipt: ' receipt)) // contains the new contract address
    //.on('confirmation', (confirmationNumber, receipt) => helper.info('confirmation: ' confirmationNumber))
    .then(newContractInstance => {
    // helper.info('contract address: ' newContractInstance.options.address)
    console.log('confirm ' i)
    contractAddresses[newContractInstance.options.address] = { sender, params }

    if (Object.keys(contractAddresses).length === len) done(null, contractAddresses)
    }) // instance with the new contract address
    } catch (e) {
    // helper.error(e)
    return done(e)
    }
    }
    }
    }