Created
October 30, 2018 00:33
-
-
Save serapath/f878613f616ff351cad8951fab9b013c 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=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var helper = {} | |
var logfile = 'browser/terminal.log' | |
var contractSource = 'browser/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) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment