Skip to content

Instantly share code, notes, and snippets.

@serapath
Created October 30, 2018 00:47
Show Gist options
  • Save serapath/3a82b4c661d647707fd32cf1cc067425 to your computer and use it in GitHub Desktop.
Save serapath/3a82b4c661d647707fd32cf1cc067425 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.25+commit.59dbf8f1.js&optimize=true&gist=
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;
}
}
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)
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) {
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) => {})
.then(newContractInstance => {
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) { return done(e) }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment