LoginSignup
0
1

More than 5 years have passed since last update.

truffleでERC20トークンのtransfer関数をテストする.

Posted at

概要

truffleを使ってMyTokenというERC20トークンを作成してみた.
そのテストを書いてハマったので備忘録を残す.

書いたテストスクリプト

最終的に以下のようなテストスクリプトを作成した.

my_token_test.js
var MyToken = artifacts.require("./MyToken.sol");

contract('MyTokenTest', function(accounts) {

    it("should transfer MyToken", function() {
        var strage;
        return MyToken.deployed().then(function(instance) {
            strage = instance;
            //return strage.transfer(accounts[1], web3.fromWei(2000e18), {from: accounts[0]});
            return strage.transfer(accounts[1], 1000e18, {from: accounts[0]});
        }).then(function () {
            return strage.balanceOf(accounts[1])
        }).then(function(message) {
            //return assert.equal(message, 2000, "correct")
            return assert.equal(message, 1000e18, "correct")
            //return console.log(message)
        });
    });
});

何にハマったか

上のコードのコメントアウトしてあるところがポイント.
balanceOf関数を使って,accounts[1]に転送されたMyTokenの量をテストしたかった.
1000e18とは,転送にはWeiを用いて転送する量を設定するため,etherの単位に直すためにe18をつけている.
この部分が曲者で,console.logを使って値を表示させていたのだが,なかなか思う通りの値が表示されていなくて非常に混乱しハマっていた(実際にはちゃんと転送されていた).
ちなみに,上側のコメントアウトしてあるものを使用すると,Weiの単位での転送をしてくれる.
実用性があるのかは謎.

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1