Skip to content

Commit

Permalink
Adding support for links in the deployer, chai to code and other fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mirek committed May 16, 2018
1 parent c71b2bf commit 94759c9
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 91 deletions.
32 changes: 25 additions & 7 deletions bin/cobalt-deploy.js
@@ -1,49 +1,67 @@
#!/usr/bin/env node

const { get, isString } = require('lodash')
const path = require('path')
const assert = require('assert')
const { get, set, isString } = require('lodash')
const program = require('commander')
const pkg = require('../package.json')
const makeWeb3 = require('../web3')
const makeProvider = require('../make-provider')
const isHex0x = require('../is-hex0x')
const isAddress = require('../is-address')
// const isBytecodePlaceholder = require('../is-bytecode-placeholder')

function mergeLink(e, r) {
const [ k, v ] = e.split('=')
assert((/__.{36}__/).test(k), `Expected bytecode placeholder in /__.{36}__/ format instead of ${JSON.stringify(k)} in --link ${JSON.stringify(e)} argument.`)
assert(isAddress(v), `Expected an address in 0x... format instead of ${JSON.stringify(v)} in --link ${JSON.stringify(e)} argument.`)
return set(r, k, v)
}

function jsonParse(value) {
return JSON.parse(value)
}

program
.version(pkg.version)
.option('-p, --provider [provider]', 'Set provider.', 'http://localhost:8545')
.option('-r, --root [root]', 'Contract\'s root directory.', './contracts')
.option('-s, --sol <sol>', 'Solidity file.')
.option('-c, --contract [contract]', 'Contract name, defaults to solidity file without extension.')
.option('-a, --args [args]', 'Constructor arguments as json.', '[]')
.option('-a, --args [args]', 'Constructor arguments as json.', jsonParse, '[]')
.option('-g, --gas [gas]', 'Gas.', '50000000')
.option('-f, --from <from>', '0x... address.')
.option('-l, --link <link>', 'Link in 0xfa9c654833f3e977b0f7c07c60bb69b656a47af7:__contracts/Library.sol:Library_________ format.', mergeLink, {})
.parse(process.argv)

// Solidity file is required.
if (!program.sol) {
console.log('Please provide solidity file via -s, --sol argument, ie. "-s Foo.sol".')
process.exit(-1)
}

// If contract is not provided use solidity file name without extension.
// If contract is not provided use basename from solidity file.
const contract = program.contract ?
program.contract :
program.sol.slice(0, -4)
path.basename(program.sol, '.sol')

// `from` has to be set.
if (!isHex0x(program.from)) {
console.error('Please provide -f, --from argument, ie. "-f 0xfa9c654833f3e977b0f7c07c60bb69b656a47af7".')
process.exit(-1)
}

const provider = makeProvider(program.provider)
const { root, sol, args, from, gas } = program
const { root, sol, args, from, gas, link: links } = program
const { web3 } = makeWeb3({ provider, root })

web3.require(sol)

web3.deploy(contract, isString(args) ? JSON.parse(args) : args, { from, gas })
web3.deploy(contract, args, { from, gas, links })
.then(result => {
console.log(get(result, 'options.address'))
})
.catch(err => {
console.error(get(err, 'stack', err))
console.error(get(err, 'message', err))
process.exit(-1)
})
4 changes: 2 additions & 2 deletions contracts/Ambiguous.sol
@@ -1,4 +1,4 @@
pragma solidity ^0.4.18;
pragma solidity ^0.4.23;

import "./Library.sol";
import "./LibraryNameThatOverflowsItsPlaceholderA.sol";
Expand All @@ -15,7 +15,7 @@ contract Ambiguous {
uint longLengthB = LibraryNameThatOverflowsItsPlaceholderB.longLengthB(bytes(_string));

if (length > 0) {
Event(_string);
emit Event(_string);
}

return length + longLengthA + longLengthB;
Expand Down
2 changes: 1 addition & 1 deletion contracts/HelloWorld.sol
@@ -1,4 +1,4 @@
pragma solidity ^0.4.18;
pragma solidity ^0.4.23;

contract HelloWorld {

Expand Down
2 changes: 1 addition & 1 deletion contracts/Library.sol
@@ -1,4 +1,4 @@
pragma solidity ^0.4.18;
pragma solidity ^0.4.23;

library Library {

Expand Down
2 changes: 1 addition & 1 deletion contracts/LibraryNameThatOverflowsItsPlaceholderA.sol
@@ -1,4 +1,4 @@
pragma solidity ^0.4.18;
pragma solidity ^0.4.23;

library LibraryNameThatOverflowsItsPlaceholderA {

Expand Down
2 changes: 1 addition & 1 deletion contracts/LibraryNameThatOverflowsItsPlaceholderB.sol
@@ -1,4 +1,4 @@
pragma solidity ^0.4.18;
pragma solidity ^0.4.23;

library LibraryNameThatOverflowsItsPlaceholderB {

Expand Down
6 changes: 3 additions & 3 deletions contracts/OwnedSet.sol
Expand Up @@ -12,7 +12,7 @@
//! See the License for the specific language governing permissions and
//! limitations under the License.

pragma solidity ^0.4.15;
pragma solidity ^0.4.23;

import "./interfaces/Owned.sol";
import "./interfaces/ValidatorSet.sol";
Expand Down Expand Up @@ -69,7 +69,7 @@ contract OwnedSet is Owned, ValidatorSet {
// Was the last validator change finalized. Implies validators == pending
bool public finalized;

function OwnedSet(address[] _initial) public {
constructor(address[] _initial) public {
pending = _initial;
for (uint i = 0; i < _initial.length - 1; i++) {
pendingStatus[_initial[i]].isIn = true;
Expand All @@ -90,7 +90,7 @@ contract OwnedSet is Owned, ValidatorSet {
// Log desire to change the current list.
function initiateChange() private when_finalized {
finalized = false;
emit InitiateChange(block.blockhash(block.number - 1), getPending());
emit InitiateChange(blockhash(block.number - 1), getPending());
}

function finalizeChange() public only_system_and_not_finalized {
Expand Down
4 changes: 2 additions & 2 deletions contracts/WithLinks.sol
@@ -1,4 +1,4 @@
pragma solidity ^0.4.18;
pragma solidity ^0.4.23;

import "./Library.sol";
import "./LibraryNameThatOverflowsItsPlaceholderA.sol";
Expand All @@ -12,7 +12,7 @@ contract WithLinks {
uint longLengthA = LibraryNameThatOverflowsItsPlaceholderA.longLengthA(bytes(_string));

if (length > 0) {
Event(_string);
emit Event(_string);
}

return length + longLengthA;
Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/Owned.sol
Expand Up @@ -14,7 +14,7 @@
//! See the License for the specific language governing permissions and
//! limitations under the License.

pragma solidity ^0.4.15;
pragma solidity ^0.4.23;

contract Owned {
modifier only_owner { require(msg.sender == owner); _; }
Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/ValidatorSet.sol
Expand Up @@ -12,7 +12,7 @@
//! See the License for the specific language governing permissions and
//! limitations under the License.

pragma solidity ^0.4.15;
pragma solidity ^0.4.23;

contract ValidatorSet {
/// Issue this log event to signal a desired change in validator set.
Expand Down
75 changes: 17 additions & 58 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -19,8 +19,7 @@
},
"homepage": "https://github.com/appliedblockchain/cobalt#readme",
"devDependencies": {
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"code": "^5.2.0",
"eslint": "^4.19.1",
"mocha": "^5.1.1"
},
Expand Down
4 changes: 2 additions & 2 deletions test/bytecode-placeholders.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions test/web3.test.js
@@ -1,9 +1,9 @@

require('chai').use(require('chai-as-promised'))
process.on('warning', err => console.warn(err.stack))

const { expect } = require('code')
const { web3, accounts } = require('../web3')({ accounts: 10 })
const isChecksumAddress = require('../is-checksum-address')
const { expect } = require('chai')

const from = accounts[0].address
const gas = 50000000
Expand All @@ -15,7 +15,7 @@ describe('web3', function () {
const helloWorld = await web3.deploy('HelloWorld', [], { from, gas })
expect(isChecksumAddress(helloWorld.options.address)).to.be.true
const helloWorld2 = web3.at('HelloWorld', helloWorld.options.address)
expect(await helloWorld2.methods.helloWorld().call()).to.eq('Hello world!')
expect(await helloWorld2.methods.helloWorld().call()).to.equal('Hello world!')
})

describe('link', function () {
Expand All @@ -25,12 +25,12 @@ describe('web3', function () {
})

it('should require contract with placeholders', () => {
web3.require('WithLinks.sol')
expect(web3.require('WithLinks.sol')).to.be.an.object()
})

it('should fail to deploy contract with placeholders without links', async () => {
await expect(web3.deploy('WithLinks', [], { from, gas })).to.rejectedWith(
'Missing links for "__Library.sol:Library___________________", "__LibraryNameThatOverflowsItsPlacehold__".'
await expect(web3.deploy('WithLinks', [], { from, gas })).to.reject(
'Missing links for "__Library.sol:Library___________________", "__LibraryNameThatOverflowsItsPlacehold__". Did you forget to provide those links when deploying?'
)
})

Expand All @@ -45,7 +45,7 @@ describe('web3', function () {
'__LibraryNameThatOverflowsItsPlacehold__': libraryA.options.address
}
const withLinks = await web3.deploy('WithLinks', [], { from, gas, links })
expect(isChecksumAddress(withLinks.options.address)).to.be.true
expect(isChecksumAddress(withLinks.options.address)).to.be.true()
})

it('should reject extra links', async () => {
Expand All @@ -54,7 +54,7 @@ describe('web3', function () {
'__Library.sol:Library___________________': '0x0000000000000000000000000000000000000000',
'__LibraryNameThatOverflowsItsPlacehold__': '0x0000000000000000000000000000000000000000'
}
await expect(web3.deploy('WithLinks', [], { from, gas, links })).to.be.rejectedWith(
await expect(web3.deploy('WithLinks', [], { from, gas, links })).to.reject(
'Unnecessary extra links provided "__Extra.sol:Extra_______________________".'
)
})
Expand Down
2 changes: 1 addition & 1 deletion test/without-solc.test.js
@@ -1,5 +1,5 @@

const { expect } = require('chai')
const { expect } = require('code')

const { web3, accounts } = require('../web3')({
root: `${__dirname}/../contracts`
Expand Down
6 changes: 6 additions & 0 deletions web3.js
Expand Up @@ -40,6 +40,12 @@ const providers = {
gasLimit,
blocktime
})

// Apparently there are a lot of listeners.
if (provider.getMaxListeners() === 10) {
provider.setMaxListeners(35)
}

return { provider, accounts, close: null }
}
}
Expand Down

0 comments on commit 94759c9

Please sign in to comment.