テストネットにつなぐ
この辺はテストネット上でEthereumをやりとりするを参照
ネットワーク設定
app.js
var Tx = require('ethereumjs-tx')
const Web3 = require('web3')
const web3 = new Web3('https://ropsten.infura.io/v3/xxxxxxxxxx')
アカウント設定
app.js
const account1 = 'xxxxx'
const privateKey1 = Buffer.from('your private key', 'hex')
#コントラクトをデプロイする
今回は、SolidityのブラウザIDEのRemixで提供されている「ballot.sol」というサンプルのコントラクトをデプロイしてみましょう。
サンプルをそのままコンパイルして、できたら「Detail」をクリックします
DetailのWEB3DEPLOYというところの ballotという変数の中のデータをコピーします。
コントラクトをデプロイしましょう
app.js
web3.eth.getTransactionCount(account1, (err, txCount) => {
//Smart contract data
const data = '0x608060405234801561001057600080fd5b506040516020806108b88339810180604052602081101561003057600080fd5b8101908080519060200190929190505050336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060018060008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600001819055508060ff166002816100fa9190610101565b5050610154565b81548183558181111561012857818360005260206000209182019101610127919061012d565b5b505050565b61015191905b8082111561014d5760008082016000905550600101610133565b5090565b90565b610755806101636000396000f3fe608060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680635c19a95c14610067578063609ff1bd146100b85780639e7b8d61146100e9578063b3f98adc1461013a575b600080fd5b34801561007357600080fd5b506100b66004803603602081101561008a57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610178565b005b3480156100c457600080fd5b506100cd6104cd565b604051808260ff1660ff16815260200191505060405180910390f35b3480156100f557600080fd5b506101386004803603602081101561010c57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610549565b005b34801561014657600080fd5b506101766004803603602081101561015d57600080fd5b81019080803560ff169060200190929190505050610646565b005b6000600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002090508060010160009054906101000a900460ff16156101d857506104ca565b5b600073ffffffffffffffffffffffffffffffffffffffff16600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060010160029054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff161415801561030657503373ffffffffffffffffffffffffffffffffffffffff16600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060010160029054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1614155b1561037557600160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060010160029054906101000a900473ffffffffffffffffffffffffffffffffffffffff1691506101d9565b3373ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1614156103af57506104ca565b60018160010160006101000a81548160ff021916908315150217905550818160010160026101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506000600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002090508060010160009054906101000a900460ff16156104b057816000015460028260010160019054906101000a900460ff1660ff1681548110151561049157fe5b90600052602060002001600001600082825401925050819055506104c7565b816000015481600001600082825401925050819055505b50505b50565b6000806000905060008090505b6002805490508160ff161015610544578160028260ff168154811015156104fd57fe5b906000526020600020016000015411156105375760028160ff1681548110151561052357fe5b906000526020600020016000015491508092505b80806001019150506104da565b505090565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415806105f15750600160008273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060010160009054906101000a900460ff165b156105fb57610643565b60018060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600001819055505b50565b6000600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002090508060010160009054906101000a900460ff16806106ae57506002805490508260ff1610155b156106b95750610726565b60018160010160006101000a81548160ff021916908315150217905550818160010160016101000a81548160ff021916908360ff160217905550806000015460028360ff1681548110151561070a57fe5b9060005260206000200160000160008282540192505081905550505b5056fea165627a7a72305820b5e309db74e14d16e04e4df096eb99edd55692775a24d42661feccf420b6ac8f0029'
// Build the transaction
const txObject = {
nonce: web3.utils.toHex(txCount),
gasLimit: web3.utils.toHex(1000000),
gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
data: data
}
// Sign the transaction
const tx = new Tx(txObject)
tx.sign(privateKey1)
const serializedTransaction = tx.serialize()
const row = '0x' + serializedTransaction.toString('hex')
// Broadcast the transaction
web3.eth.sendSignedTransaction(row, (err, txHash) => {
console.log('err:', err, 'txHash', txHash)
})
})
無事にデプロイされたのを確認したら、コントラクトを読み込んでみましょう。
上記のコントラクトアドレスとちょっと前に見たWEB3DEPLOYの「ballotContract」という変数の中にいるABIを持ってきます。
app.js
const contractABI = [{
"constant":false,
"inputs":[{"name":"to","type":"address"}],
"name":"delegate","outputs":[],
"payable":false,"stateMutability":"nonpayable","type":"function"
},{
"constant":true,
"inputs":[],
"name":"winningProposal",
"outputs":[{"name":"_winningProposal","type":"uint8"}],
"payable":false,
"stateMutability":"view","type":"function"
},{
"constant":false,"inputs":[{"name":"toVoter","type":"address"}],
"name":"giveRightToVote","outputs":[],
"payable":false,"stateMutability":"nonpayable","type":"function"
},{
"constant":false,"inputs":[{"name":"toProposal","type":"uint8"}],
"name":"vote","outputs":[],
"payable":false,"stateMutability":"nonpayable","type":"function"
},{
"inputs":[{"name":"_numProposals","type":"uint8"}],
"payable":false,"stateMutability":"nonpayable",
"type":"constructor"
}]
const contractAddress = '0x841d2f2621a9a73edbbf86689e55b95f60063a2b'
const ballotContract = new web3.eth.Contract(contractABI, contractAddress)
console.log(ballotContract)
ログでコントラクトの中身が出てきたら成功です!