Skip to content

Commit

Permalink
Merge pull request #68 from protofire/fix-contract-deploy
Browse files Browse the repository at this point in the history
Fix contract deploy
  • Loading branch information
fvictorio authored Jun 24, 2019
2 parents 31fd5fa + 0f62625 commit fe76b08
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 33 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
"repl.history": "^0.1.4",
"rlp": "^2.2.3",
"tslib": "^1",
"web3": "^1.0.0-beta.52",
"web3-eth-accounts": "^1.0.0-beta.52",
"web3-providers": "^1.0.0-beta.52"
"web3": "^1.0.0-beta.55",
"web3-eth-accounts": "^1.0.0-beta.55",
"web3-providers": "^1.0.0-beta.55"
},
"devDependencies": {
"@oclif/dev-cli": "^1",
Expand Down Expand Up @@ -84,8 +84,8 @@
"lint": "tslint --project . && npm run prettier-check",
"postpack": "rm -f oclif.manifest.json",
"prepack": "rm -rf lib tsconfig.tsbuildinfo && tsc -b && oclif-dev manifest && oclif-dev readme",
"prettier": "prettier --write '**/*.{ts,tsx,json}' --ignore-path .gitignore",
"prettier-check": "prettier --check '**/*.{ts,tsx,json}' --ignore-path .gitignore",
"prettier": "prettier --write \"**/*.{ts,tsx,json}\" --ignore-path .gitignore",
"prettier-check": "prettier --check \"**/*.{ts,tsx,json}\" --ignore-path .gitignore",
"test": "jest --coverage --detectOpenHandles ./test",
"version": "oclif-dev readme && git add README.md",
"clearjest": "jest --clearCache"
Expand Down
50 changes: 33 additions & 17 deletions src/helpers/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as fs from 'fs'
import Web3 from 'web3'
import { TransactionReceipt } from 'web3-core/types'
import { Contract } from 'web3-eth-contract/types'
import { Unit } from 'web3-utils/types'

import { add0x } from './utils'

Expand All @@ -9,35 +11,49 @@ interface DeployResult {
address: string
}

export function deploy(url: string, privateKey: string, bin: string): Promise<DeployResult> {
export function deploy(url: string, privateKey: string, binPath: string): Promise<DeployResult> {
const transactionConfirmationBlocks = 3
const options = {
transactionConfirmationBlocks,
}

const web3 = new Web3(new Web3.providers.HttpProvider(url))
privateKey = add0x(privateKey)

const { address } = web3.eth.accounts.wallet.add(privateKey)

const data = add0x(fs.readFileSync(bin).toString())
const data = add0x(fs.readFileSync(binPath).toString())

const contract = new web3.eth.Contract([])
const Contract: any = web3.eth.Contract // ts hack: transactionConfirmationBlocks is not a valid option
const contract = new Contract([], undefined, options)

const deploy = contract.deploy({ data })
let receipt: TransactionReceipt

return deploy
.estimateGas({
from: address,
})
.then(gas => {
const contract = deploy.send({
from: address,
gas,
})

const receiptPromise = new Promise<TransactionReceipt>(resolve => {
contract.on('receipt', receipt => resolve(receipt))
})

return Promise.all([contract, receiptPromise])
})
.then(([contract, receipt]) => {
return { address: contract.options.address, receipt }
.then((gas: Unit) => {
process.stderr.write(`Estimated gas: ${gas}\n`)

return deploy
.send({
from: address,
gas,
})
.on('transactionHash', (tx: string) => {
process.stderr.write(`TX: ${tx}\n`)
})
.on(
'confirmation',
(confirmationNumber: number, transactionReceipt: TransactionReceipt) => {
receipt = transactionReceipt
process.stderr.write(
`Confirmation ${confirmationNumber} of ${transactionConfirmationBlocks}\n`,
)
},
)
.then((contract: Contract) => ({ address: contract.options.address, receipt }))
})
}
2 changes: 1 addition & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"file-name-casing": false,
"binary-expression-operand-order": false,
"no-http-string": false,
"no-implicit-dependencies": [true, ["web3-core", "web3-eth", "web3-eth-contract"]]
"no-implicit-dependencies": [true, ["web3-core", "web3-eth", "web3-eth-contract", "web3-utils"]]
}
}
25 changes: 15 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,9 @@
integrity sha512-1YKeT4JitGgE4SOzyB9eMwO0nGVNkNEsm9qlIt1Lqm/tG2QEiSMTD4kS3aO6L+w5SClLVxALmIBESK6Mk5wX0A==

"@types/node@^10.12.18", "@types/node@^10.3.2":
version "10.14.8"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.14.8.tgz#fe444203ecef1162348cd6deb76c62477b2cc6e9"
integrity sha512-I4+DbJEhLEg4/vIy/2gkWDvXBOOtPKV9EnLhYjMoqxcRW+TTZtUftkHktz/a8suoD5mUL7m6ReLrkPvSsCQQmw==
version "10.14.9"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.14.9.tgz#2e8d678039d27943ce53a1913386133227fd9066"
integrity sha512-NelG/dSahlXYtSoVPErrp06tYFrvzj8XLWmKA+X8x0W//4MqbUyZu++giUG/v0bjAT6/Qxa8IjodrfdACyb0Fg==

"@types/stack-utils@^1.0.1":
version "1.0.1"
Expand Down Expand Up @@ -1724,9 +1724,9 @@ ethereumjs-util@^6.1.0:
secp256k1 "^3.0.1"

ethers@^4.0.27:
version "4.0.29"
resolved "https://registry.yarnpkg.com/ethers/-/ethers-4.0.29.tgz#b698f5d8a5a89bfd3d12dba2ad5a4cd9a95a93bb"
integrity sha512-WCaH8an3Y+i85zW6Y6fmt0oQE9GXJy9NjqNVDTJVUJ/WBLIB1z17nG16lbOz3zVYWFgarfnzVakN2G7AyXk1Xg==
version "4.0.30"
resolved "https://registry.yarnpkg.com/ethers/-/ethers-4.0.30.tgz#140653fd08adf2834bb2f3aceefa10d04205b47d"
integrity sha512-1a39Y+q5zTfrXCLndV+CHsTHq+T5/TvAx5y0S/PKd700C0lfU70CJnU7q89bd+4pIuWp05TkrEsrTj2dXhkcSA==
dependencies:
"@types/node" "^10.3.2"
aes-js "3.0.0"
Expand Down Expand Up @@ -5050,11 +5050,16 @@ tslib@1.9.0:
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.0.tgz#e37a86fda8cbbaf23a057f473c9f4dc64e5fc2e8"
integrity sha512-f/qGG2tUkrISBlQZEjEqoZ3B2+npJjIf04H1wuAv9iA8i04Icp+61KRXxFdha22670NJopsZCIjhC3SnjPRKrQ==

tslib@^1, tslib@^1.7.1, tslib@^1.8.0, tslib@^1.8.1, tslib@^1.9.0, tslib@^1.9.3:
tslib@^1, tslib@^1.7.1, tslib@^1.8.0, tslib@^1.8.1, tslib@^1.9.3:
version "1.9.3"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.3.tgz#d7e4dd79245d85428c4d7e4822a79917954ca286"
integrity sha512-4krF8scpejhaOgqzBEcGM7yDIEfi0/8+8zDRZhNZZ2kjmHJ4hv3zCbQWxoJGz1iw5U0Jl0nma13xzHXcncMavQ==

tslib@^1.9.0:
version "1.10.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.10.0.tgz#c3c19f95973fb0a62973fb09d90d961ee43e5c8a"
integrity sha512-qOebF53frne81cf0S9B41ByenJ3/IuH8yJKngAX35CmiZySA0khhkovshKK+jGCaMnVomla7gVlIcc3EvKPbTQ==

tslint-config-prettier@^1.18.0:
version "1.18.0"
resolved "https://registry.yarnpkg.com/tslint-config-prettier/-/tslint-config-prettier-1.18.0.tgz#75f140bde947d35d8f0d238e0ebf809d64592c37"
Expand Down Expand Up @@ -5352,7 +5357,7 @@ web3-eth-abi@1.0.0-beta.55:
lodash "^4.17.11"
web3-utils "1.0.0-beta.55"

web3-eth-accounts@1.0.0-beta.55, web3-eth-accounts@^1.0.0-beta.52:
web3-eth-accounts@1.0.0-beta.55, web3-eth-accounts@^1.0.0-beta.55:
version "1.0.0-beta.55"
resolved "https://registry.yarnpkg.com/web3-eth-accounts/-/web3-eth-accounts-1.0.0-beta.55.tgz#ba734ffdc1e3cc8ac0ea01de5241323a0c2f69f3"
integrity sha512-VfzvwpSDHXqRVelIxsBVhgbV9BkFvhJ/q+bKhnVUUXV0JAhMK/7uC92TsqKk4EBYuqpHyZ1jjqrL4n03fMU7zw==
Expand Down Expand Up @@ -5464,7 +5469,7 @@ web3-net@1.0.0-beta.55:
web3-providers "1.0.0-beta.55"
web3-utils "1.0.0-beta.55"

web3-providers@1.0.0-beta.55, web3-providers@^1.0.0-beta.52:
web3-providers@1.0.0-beta.55, web3-providers@^1.0.0-beta.55:
version "1.0.0-beta.55"
resolved "https://registry.yarnpkg.com/web3-providers/-/web3-providers-1.0.0-beta.55.tgz#639503517741b69baaa82f1f940630df6a25992b"
integrity sha512-MNifc7W+iF6rykpbDR1MuX152jshWdZXHAU9Dk0Ja2/23elhIs4nCWs7wOX9FHrKgdrQbscPoq0uy+0aGzyWVQ==
Expand Down Expand Up @@ -5511,7 +5516,7 @@ web3-utils@1.0.0-beta.55:
randombytes "^2.1.0"
utf8 "2.1.1"

web3@^1.0.0-beta.52:
web3@^1.0.0-beta.55:
version "1.0.0-beta.55"
resolved "https://registry.yarnpkg.com/web3/-/web3-1.0.0-beta.55.tgz#8845075129299da172c2eb41a748c8a87c2a2b5a"
integrity sha512-yJpwy4IUA3T/F9hWzYQVn0GbJCrAaZ0KTIO3iuqkhaYH0Y09KV7k4GzFi4hN7hT4cFTj4yIKaeVCwQ5kzvi2Vg==
Expand Down

0 comments on commit fe76b08

Please sign in to comment.