//tips
//smart contract
オライリーを確認していくとparityというソフトを追加使用することでテストネットワークへのデプロイができることがわかった。
流れとしては、
Npm install truffle-hdwallet-provider —save-dev
Export MNEMONIC=“<フレーズ>”
Truffle.config.jsの設定
Parity —chain=goerli
Truffle migrate —network goerli
となる。通常のTruffle migrate —network goerliだけではダメなのかは要確認。
簡単なテストをdexにて行うためchaiとmochaを導入。mochaはtruffleと一緒にダウンロードされるので下記だけで良い。chai-bnはビッグナンバー対応。
npm i chai chai-bn truffle-assertions
また、tokens.solファイルを作成し、_migration.jsファイルのトークン部分を一部シンプルにする。
.solを変更するのでコンパイルデータも消去して新たに生成する。
rm -rf build
truffle compile
作成したtoken.jsはこちらで、
//SPDX-License-Identifier:MIT
pragma solidity 0.8.4;
import "./ERC20.sol";
contract Dai is ERC20{
constructor(string memory _name,string memory _symbol,uint256 _totalSupply)ERC20(_name,_symbol,_totalSupply){
//Daiの引数はそのままERC20のconstructorにスライドさせるというもの
}
}
contract Link is ERC20{
constructor(string memory _name,string memory _symbol,uint256 _totalSupply)ERC20(_name,_symbol,_totalSupply){
}
}
contract Comp is ERC20{
constructor(string memory _name,string memory _symbol,uint256 _totalSupply)ERC20(_name,_symbol,_totalSupply){
}
}
変更箇所を下記のように_migration.jsファイルに適応させる。
//const ERC20=artifacts.require("ERC20");
const Dai=artifacts.require("Dai");
const Link=artifacts.require("Link");
const Comp=artifacts.require("Comp");
const Dex=artifacts.require("Dex");
//これは簡略化できないか考える
const toWei=(number)=>web3.utils.toWei(web3.utils.toBN(number),'ether');
module.exports=async function(deployer){
//tokenのdeploy
//deploy時のconstructorの情報を渡す
//stirng or bignumber表記する必要があるのでweb3.utils.toBN
await deployer.deploy(Dai,"Dai","DAI",toWei(10**10));
const dai =await Dai.deployed();//dexでアドレスを取得するためにインスタンス化
await deployer.deploy(Link,"ChainLink","LINK",toWei(10**6));
const link =await Link.deployed();
await deployer.deploy(Comp,"Compound","COMP",toWei(10**4));
const comp =await Comp.deployed();
//dexのdeploy
//各トークンのアドレスを渡す
await deployer.deploy(Dex,[dai.address,link.address,comp.address]);
const dex=await Dex.deployed();
//dexインスタンスに対して各トークンを供給する
await dai.transfer(dex.address,toWei(10**10));
await link.transfer(dex.address,toWei(10**6));
await comp.transfer(dex.address,toWei(10**4));
}
truffle developの中身を見ていたらMnemonic:が自動生成されていた。これはテスト用でセキュリティが甘いので実際のチェーンへのデプロイには使わないでねとのこと。
⚠️ Important ⚠️ : This mnemonic was created for you by Truffle. It is not secure.
Ensure you do not use it on production blockchains, or else you risk losing funds.
infuraを使わない場合の通常の流れはインストールした truffle-hdwallet-providerに対してフレーズを渡して、parityでのチェーン確定と、Truffle migrate —network goerliによるデプロイとなりそう。
今回はtestフォルダ内の記述はそこまで難しくないのでデプロイには関係ないのでカット。
フロントエンドとの連携を確認する。kickstartではreactを多用したが、今回はhtml,cssのファイルをgithubからダウンロードして、そちらと.solとの連携をみる。
Clientファルダとして、そちらをプロジェクトフォルダに格納。
Vscodeのliveserverにてhttp://127.0.0.1:5500/client/index.htmlでページを開くことができたが、そもそも5500を指定した覚えがなく不思議に思ったので、liveserverの仕組みについても調べた。
テキストエディタの変更内容を、リアルタイムでブラウザに反映できるもので、LiveServerを使った場合はウェブブラウザが起動すると共に「127.0.0.1:5500」という場所(ループバックアドレス)を参照する。なのでURLにはその番号が記載されるよう。
Kickstartではnpm run devでlocalhostからページを見ていたが、truffleを使用した場合にも同じことが可能なのかも確認しておく。
npm run dev
npm ERR! Missing script: "dev"
npm ERR!
npm ERR! To see a list of scripts, run:
npm ERR! npm run
エラーが出た。このdevを作成していたのがreactだった模様。
現段階ではreactを導入していないので、代わりにLiveServerを使っているという認識が正しいのかもしれない。
そうするとTruffle Develop started at http://127.0.0.1:9545/とはなんぞやと調べると、これはローカルネットワークを使用したコントラクトなどのチェーンをやりとりをする窓口であってhtmlなどを反映する機能などは含んでいないということがわかった。