Skip to content
This repository was archived by the owner on Dec 4, 2021. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 674a6c5

Browse files
authoredAug 20, 2018
Add deploy_contract utility (oasislabs#139)
1 parent 4e0fac3 commit 674a6c5

File tree

5 files changed

+2615
-0
lines changed

5 files changed

+2615
-0
lines changed
 

‎tests/deploy_contract/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

‎tests/deploy_contract/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# deploy_contract.js
2+
3+
Deploys a contract stored on disk to a web3 gateway.
4+
5+
## Usage
6+
7+
First `npm install -g .`.
8+
Then `deploy_contract /path/to/contract`.
9+
10+
Parameters:
11+
12+
* `--gateway`: HTTP URL of web3 gateway [default: http://localhost:8545]
13+
* `--dump-json`: print cURLable JSON to stdout
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env node
2+
3+
let fs = require('fs');
4+
let program = require('commander');
5+
let Web3 = require('web3');
6+
let Tx = require('ethereumjs-tx');
7+
8+
const web3 = new Web3(new Web3.providers.HttpProvider(program.gateway));
9+
web3.eth.defaultAccount = '0x1cca28600d7491365520b31b466f88647b9839ec';
10+
11+
// private key corresponding to defaultAccount. generated from mnemonic:
12+
// patient oppose cotton portion chair gentle jelly dice supply salmon blast priority
13+
const PRIVATE_KEY = new Buffer('c61675c22aee77da8f6e19444ece45557dc80e1482aa848f541e94e3e5d91179', 'hex');
14+
15+
program
16+
.option('--gateway <gateway>', 'gateway http address', 'http://localhost:8545')
17+
.option('--dump-json', 'dump cURLable json')
18+
.parse(process.argv);
19+
20+
const contractFilename = program.args[0] || fs.readdirSync('target').reduce((f, d) => {
21+
return f || (d.endsWith('.wasm') && 'target/' + d);
22+
}, undefined);
23+
24+
const contract = fs.readFileSync(contractFilename).toString('hex');
25+
26+
web3.eth.getTransactionCount(web3.eth.defaultAccount).then(nonce => {
27+
const tx = new Tx({
28+
data: '0x' + contract.toString('hex'),
29+
gasLimit: '0xfffffffffffff',
30+
gasPrice: 0,
31+
nonce: nonce,
32+
value: 0,
33+
});
34+
tx.sign(PRIVATE_KEY);
35+
36+
let serializedTx = '0x' + tx.serialize().toString('hex');
37+
38+
if (program.dumpJson) {
39+
console.log(JSON.stringify({
40+
jsonrpc: '2.0',
41+
id: 2,
42+
method: 'eth_sendRawTransaction',
43+
params: [serializedTx],
44+
}));
45+
return;
46+
}
47+
48+
return web3.eth.sendSignedTransaction(serializedTx).then(receipt => {
49+
console.log(receipt);
50+
});
51+
}).catch(err => {
52+
console.error('ERROR: Could not deploy contract')
53+
console.error(err)
54+
});

‎tests/deploy_contract/package-lock.json

+2,528
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/deploy_contract/package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "deploy_contract",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "deploy_contract.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"bin": {
11+
"deploy_contract": "./deploy_contract.js"
12+
},
13+
"license": "MIT",
14+
"dependencies": {
15+
"commander": "^2.17.1",
16+
"ethereumjs-tx": "^1.3.7",
17+
"web3": "^1.0.0-beta.35"
18+
}
19+
}

0 commit comments

Comments
 (0)
This repository has been archived.