zeppelin-solidityを使ってdevelopment環境でクラウドセール
zeppelin-solidityというスマートコントラクトのライブラリを使ってクラウドセールしてみた。 github.com
このブログを参考にやりました。 blog.zeppelin.solutions
社内のお菓子を買ってきてみんなでシェアするスペースがあるんですけど、 そこで使うためのOkashiKingdomTokenというのを発行できるように準備しました。 github.com
ブログと同じようにコードを動かしていくとtruffle migration時に以下のエラーが出ました。 同じ問題に遭遇している人がいたので、下記のissueを見て解決した。 https://github.com/OpenZeppelin/zeppelin-solidity/issues/358#issuecomment-335472271
1秒後だとコンパイル時に時間が過ぎちゃって、Crowdsaleをイニシャライズする時にstartTimeが過去の時刻になり、 例外が出されるっぽい。エラーメッセージがわかりにくい><
該当箇所は多分ここ。 https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/crowdsale/Crowdsale.sol#L44
以下はgethのdevelopment環境で動かしたログです。
truffle consoleで叩く
account1の残高を確認
truffle console truffle(development)> account1 = web3.eth.accounts[1] '0xde9b6473c198f5bdfedde9940868323534402d32' truffle(development)> account1 '0xde9b6473c198f5bdfedde9940868323534402d32' truffle(development)> OkashiKingdomTokenInstance.balanceOf(account1).then(balance => balance.toString(10)) '0'
OkashiKingdomTokenCrowdsaleをデプロイ、アドレスを取得。
truffle(development)> OkashiKingdomTokenCrowdsale.deployed().then(inst => {crowdsale = inst}) undefined truffle(development)> crowdsale TruffleContract { constructor: { [Function: TruffleContract] truffle(development)> crowdsale.token().then(addr => { tokenAddress = addr } ) undefined truffle(development)> tokenAddress '0x6b9b1606e5a2bc27bde920b7f41ede699a9a28c2' truffle(development)> OkashiKingdomTokenInstance = OkashiKingdomToken.at(tokenAddress) TruffleContract { constructor: { [Function: TruffleContract]
1ETHでトークンを購入してみます。
truffle(development)> OkashiKingdomTokenCrowdsale.deployed().then(inst => inst.sendTransaction({ from: account1, value: web3.toWei(1, "ether")})) { tx: '0x3a111a3309f8130a76fceae5e4dd9e228d1559b5b778d54ad1189ffed0e3fe91', receipt: { blockHash: '0x35b003990dc7f5f93e8a991c90a0db5b120189e352d57aa4992bd7e92c60bc41', blockNumber: 24007, contractAddress: null, cumulativeGasUsed: 95696, from: '0xde9b6473c198f5bdfedde9940868323534402d32', gasUsed: 95696, logs: [ [Object], [Object], [Object] ], logsBloom: '0x00000000000000000000000000000000000000004000000000000000000001000000000000000000000000000000000000000000000000000000000000008000000000000020000000000008000000000000000000000000000000000000000000000000020000000000000000000800000000000000400000000010000000040000000000000000000000000000400000000000000000000000000000000000000080000000000000000000000000000000000000000000020000000800000000000002000000000000000002000000000001000000000000000000000020000000000000000000000000000000000000020000000020000000000004000000', root: '0xe150697fc054dca05d1074e4c317a86f51c6e92b3e898c02e8d3da4cd50d9af9', to: '0xe6f8cbd962a31da63b0b816e9067052702b8abff', transactionHash: '0x3a111a3309f8130a76fceae5e4dd9e228d1559b5b778d54ad1189ffed0e3fe91', transactionIndex: 0 }, logs: [ { address: '0xe6f8cbd962a31da63b0b816e9067052702b8abff', blockNumber: 24007, transactionIndex: 0, transactionHash: '0x3a111a3309f8130a76fceae5e4dd9e228d1559b5b778d54ad1189ffed0e3fe91', blockHash: '0x35b003990dc7f5f93e8a991c90a0db5b120189e352d57aa4992bd7e92c60bc41', logIndex: 2, removed: false, event: 'TokenPurchase', args: [Object] } ] }
account1の残高を確認すると1000トークンが購入できている。
truffle(development)> OkashiKingdomTokenInstance.balanceOf(account1).then(balance => account1TokenBalance = balance.toString(10)) '1000000000000000000000' truffle(development)> account1TokenBalance '1000000000000000000000' truffle(development)> web3.fromWei(account1TokenBalance, "ether") '1000'
次やること
- テストネットワークにデプロイ 今は以下のエラーが出ているので、それを直してデプロイしてみる。
$ truffle migrate --network ropsten Writing artifacts to ./build/contracts Using network 'ropsten'. Running migration: 1_initial_migration.js Deploying Migrations... ... 0xd817d5b384d567266451089917d8f9b3fec8a3b07e648b814f70c20cd1ea1a41 Migrations: 0x781335ada8314c3621d5a1aebd82a7fd9ac4e96b Saving successful migration to network... ... 0x7b3f1e0cb6423d1180d345bec1df239bdadd0bcd1c0bba5faa504a70ecec0574 Saving artifacts... Running migration: 2_deploy_contracts.js /Users/usr0600244/.nodebrew/node/v8.9.2/lib/node_modules/truffle/build/cli.bundled.js:314177 throw error; ^ Error: Web3ProviderEngine does not support synchronous requests. at ProviderError.ExtendableError (/Users/usr0600244/.nodebrew/node/v8.9.2/lib/node_modules/truffle/build/cli.bundled.js:9328:17) at new ProviderError (/Users/usr0600244/.nodebrew/node/v8.9.2/lib/node_modules/truffle/build/cli.bundled.js:314009:24) at /Users/usr0600244/.nodebrew/node/v8.9.2/lib/node_modules/truffle/build/cli.bundled.js:314133:17 at HDWalletProvider.send (/Users/usr0600244/.nodebrew/node/v8.9.2/lib/node_modules/truffle/build/cli.bundled.js:314171:22) at Object.send (/Users/usr0600244/.nodebrew/node/v8.9.2/lib/node_modules/truffle/build/cli.bundled.js:175476:31) at RequestManager.send (/Users/usr0600244/.nodebrew/node/v8.9.2/lib/node_modules/truffle/build/cli.bundled.js:331128:32) at Eth.get [as blockNumber] (/Users/usr0600244/.nodebrew/node/v8.9.2/lib/node_modules/truffle/build/cli.bundled.js:43421:62) at module.exports (/Users/usr0600244/ghq/github.com/kurotaky-sandbox/okashi-kingdom/migrations/2_deploy_contracts.js:4:48) at /Users/usr0600244/.nodebrew/node/v8.9.2/lib/node_modules/truffle/build/cli.bundled.js:175349:7 at /Users/usr0600244/.nodebrew/node/v8.9.2/lib/node_modules/truffle/build/cli.bundled.js:202043:7