January 25, 2026

Capitalizations Index – B ∞/21M

0から作るDapps入門 〜コントラクト編〜 – Junki Yuasa –

0から作るDapps入門 〜コントラクト編〜 – Junki Yuasa –

ここではテストは以上としておきますが, 色々試してみてください.

Truffleはスマートコントラクトをテストしたり, デプロイしたりできるツールです. 今回はrinkebyテストネットにコントラクトをデプロイするためだけに使います.

nodeとnpmはインストールされていることを前提に話を進めます.

インストールされていない方はこちらの記事などを参考にしてください.

まずTruffleのコマンドラインツールをインストールします.

ディレクトリを作って移動し,truffle init します. このコマンドで色々ファイルが生成されます. その後 truffle-hdwallet-provider をインストールしてください.

次にinfuraでプロジェクトを作ります. infuraはEthereumノードを提供してくれるツールです. infuraを利用することでEthereumブロックチェーンに接続できるようになります.

ユーザー登録を済ませたらプロジェクトを作成し, VIEW PROJECT をクリックします. KEYSENDPOINTRINKEBY を選択します. このエンドポイントはあとで使用します.

次にメタマスクのニーモニックを取得します. メタマスク を起動して右上のアカウントマークをクリック, 設定, Security & Privacyを選択. パスフレーズを表示をクリックし, ニーモニックを取得します.

[注意!]ニーモニックの流出は秘密鍵の流出を意味するので, 絶対にGithubなどにそのまま公開しないようにしましょう!

いろいろファイル作成 & 編集

dapps-sample ディレクトリ下の truffle-config.js を編集します.
以下をコピペして先ほど取得したメタマスク のニーモニックとinfuraのエンドポイントを指定の場所に貼ります.

var HDWalletProvider = require("truffle-hdwallet-provider");
var mnemonic = 'メタマスクのニーモニック';
var accessToken = 'infuraのエンドポイント';
const gas = 4000000;
const gasPrice = 1000000000 * 60;
module.exports = ,
network_id: 4,
gas: gas,
gasPrice: gasPrice,
skipDryRun: true
}
},
compilers:
}
};

次に dapps-sample 下の migrations ディレクトリに 2_deploy_contract.js というファイルを作成してください. 以下のように編集します.

var contract = artifacts.require('Recorder');
module.exports = function(deployer) ;

最後にdapps-sample 下の contract ディレクトリに Recorder.sol というファイルを作成し, Remixで作成したコントラクトを貼ります.

pragma solidity ^0.5.0;
contract Recorder 

address[] public users;

mapping (address => Data) userToData;

function writeData(uint256 weight, uint256 height) public returns(bool)
userToData[msg.sender].weight = weight;
userToData[msg.sender].height = height;
userToData[msg.sender].time = time;
return true;
}

function viewData(address user) public view returns(uint256, uint256, uint256)

function isUserExist(address user) public view returns(bool)
}
return false;
}

}

最終的にこのようなファイル構成になっています.

.
├── contracts
│ ├── Migrations.sol
│ └── Recorder.sol
├── migrations
│ ├── 1_initial_migration.js
│ └── 2_deploy_contract.js
├── test
└── truffle-config.js
3 directories, 5 files

Rinkebyネットワークへのデプロイ

ではコントラクトをデプロイします. rinkebyのETHを持っていないとデプロイ時にGAS代が足りずにはじかれるので、ある程度持っていない人は以下から補充しましょう.

補充したら以下のコマンドを実行します. 極度testnet(デプロイしなさい)!

$ truffle compile
$ truffle migrate --network rinkeby --reset

このように返ってきたらデプロイ成功です!
あなたが作成したスマートコントラクトがブロックチェーンにデプロイされました!

Deploying 'Recorder'
--------------------
> transaction hash: 0x93ec8eab47ddc572f4a153fbb892d36a84a5dec73aa988528c489b90bbab6bf5
> Blocks: 0 Seconds: 9
> contract address: 0x685B279Fcdd9Fc637512ab05B7857aBCCa0EbEF7
> account: 0x9C95Be0EC32C4Da4Aba2e765593F697a899098ff
> balance: 19.767089046
> gas used: 411184
> gas price: 60 gwei
> value sent: 0 ETH
> total cost: 0.02467104 ETH
> Saving artifacts
-------------------------------------
> Total cost: 0.02467104 ETH
Summary
=======
> Total deployments: 2
> Final cost: 0.04106076 ETH

最後に

今回のコントラクト編ではスマートコントラクトの開発, Rinkebyネットワークへのデプロイを行いました. フロントエンド編ではReactを用いてフロントを作っていきます!

今後も,Acompanyからブロックチェーンに関する記事を投稿していきますので,ぜひfollowしていただけると嬉しいです.

Happy Hacking 😎!

Published at Mon, 20 May 2019 15:06:08 +0000

Previous Article

Holders Gonna HODL: Most Of Bitcoin Supply Did Not Move Since 2018

Next Article

“The great story of stablecoins USDQ, KRWQ, CNYQ, JPYQ will never be any close to the Tether’s USDT  scandal”

You might be interested in …

Samsung galaxy s10 with bitcoin and ethereum wallet

Samsung Galaxy S10 with Bitcoin and Ethereum wallet

Samsung Galaxy S10 with bitcoin and Ethereum wallet While Samsung yesterday unveiled its Samsung Galaxy S10 series, a new video has now appeared on the built-in blockchain feature. Currently there is much speculation in the […]

Tezos launch in q2

Tezos launch in Q2

Tezos launch in Q2 Information regarding the launch of the Tezos Protocol!