github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/core/wavm/tests/erc20_test.go (about) 1 package tests 2 3 import ( 4 "path/filepath" 5 "testing" 6 ) 7 8 var ( 9 erc20Code = filepath.Join(basepath, "erc20/TokenERC20.compress") 10 // erc20Code = filepath.Join(basepath, "qlang/PERC20.wasm") 11 erc20Abi = filepath.Join(basepath, "erc20/abi.json") 12 ) 13 14 // func newErc20VM(iscreated bool) *wavm.Wavm { 15 // return newWavm(erc20Code, erc20Abi, iscreated) 16 // } 17 // func TestERC20(t *testing.T) { 18 // initialSupply := new(big.Int) 19 // initialSupply.SetString("1000000000000", 10) 20 // tokenName := "bitcoin" 21 // tokenSymbol := "BTC" 22 // amount1 := new(big.Int) 23 // amount1.SetString("10000000", 10) 24 // //to1 := common.HexToAddress("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") 25 // to1 := common.HexToAddress("0x02") 26 // vm := newErc20VM(true) 27 // mutable := wavm.MutableFunction(vm.ChainContext.Abi, vm.Module) 28 // testCreate := func(initialSupply *big.Int, tokenName, tokenSymbol string) { 29 // funcName := "" 30 // res, err := vm.Apply(pack(vm, funcName, initialSupply, tokenName, tokenSymbol), nil, mutable) 31 // if err != nil { 32 // t.Error(err) 33 // } 34 // t.Logf("create contract %s", res) 35 // } 36 // testTransfer := func(to common.Address, value *big.Int) { 37 // vm.ChainContext.IsCreated = false 38 // funcName := "transfer" 39 // res, err := vm.Apply(pack(vm, funcName, to, value), nil, mutable) 40 // if err != nil { 41 // t.Error(err) 42 // } 43 // var flag bool 44 // unPack(vm, &flag, funcName, res) 45 // if flag != true { 46 // t.Errorf("unexpected value : want %t, got %t", true, flag) 47 // } else { 48 // t.Logf("%s success get %t", funcName, flag) 49 // } 50 // type TransferEvent struct { 51 // From common.Address 52 // To common.Address 53 // Value *big.Int 54 // } 55 // var ev TransferEvent 56 // logs := vm.ChainContext.StateDB.GetLogs(common.HexToHash("0x1111")) 57 // res, _ = logs[0].MarshalJSON() 58 // t.Logf("success get event json %s", res) 59 // unPack(vm, &ev, "Transfer", logs[0].Data) 60 // t.Logf("success get event %v", ev) 61 // } 62 63 // testGetTokenName := func() { 64 // vm.ChainContext.IsCreated = false 65 // funcName := "GetTokenName" 66 // res, err := vm.Apply(pack(vm, funcName), nil, mutable) 67 // if err != nil { 68 // t.Error(err) 69 // } 70 // var name string 71 // unPack(vm, &name, funcName, res) 72 // if name != tokenName { 73 // t.Errorf("unexpected value : want %s, got %s", tokenName, name) 74 // } else { 75 // t.Logf("%s success get %s", funcName, name) 76 // } 77 // } 78 79 // testGetTotalSupply := func() { 80 // vm.ChainContext.IsCreated = false 81 // funcName := "GetTotalSupply" 82 // res, err := vm.Apply(pack(vm, funcName), nil, mutable) 83 // if err != nil { 84 // t.Error(err) 85 // } 86 // var supply *big.Int 87 // unPack(vm, &supply, funcName, res) 88 // a := new(big.Int) 89 // if supply.Cmp(a.Mul(initialSupply, new(big.Int).SetUint64(100000000))) != 0 { 90 // t.Errorf("unexpected value : want %d, got %d", a, supply) 91 // } else { 92 // t.Logf("%s success get %d", funcName, supply) 93 // } 94 // } 95 96 // testGetSymbol := func() { 97 // vm.ChainContext.IsCreated = false 98 // funcName := "GetSymbol" 99 // res, err := vm.Apply(pack(vm, funcName), nil, mutable) 100 // if err != nil { 101 // t.Error(err) 102 // } 103 // var sym string 104 // unPack(vm, &sym, funcName, res) 105 // if sym != tokenSymbol { 106 // t.Errorf("unexpected value : want %s, got %s", tokenSymbol, sym) 107 // } else { 108 // t.Logf("%s success get %s", funcName, sym) 109 // } 110 // } 111 112 // testGetDecimals := func() { 113 // vm.ChainContext.IsCreated = false 114 // funcName := "GetDecimals" 115 // res, err := vm.Apply(pack(vm, funcName), nil, mutable) 116 // if err != nil { 117 // t.Error(err) 118 // } 119 // var deci *big.Int 120 // unPack(vm, &deci, funcName, res) 121 // if deci.Uint64() != 8 { 122 // t.Errorf("unexpected value : want %d, got %d", 8, deci) 123 // } else { 124 // t.Logf("%s success get %d", funcName, deci) 125 // } 126 // } 127 128 // testGetAmount := func(addr common.Address) *big.Int { 129 // vm.ChainContext.IsCreated = false 130 // funcName := "GetAmount" 131 // res, err := vm.Apply(pack(vm, funcName, addr), nil, mutable) 132 // if err != nil { 133 // t.Error(err) 134 // } 135 // var amount *big.Int 136 // unPack(vm, &amount, funcName, res) 137 // return amount 138 // // if amount != 4 { 139 // // t.Errorf("unexpected value : want %d, got %d", 4, amount) 140 // // } else { 141 // // t.Logf("%s success get %d", funcName, amount) 142 // // } 143 // } 144 145 // testCreate(initialSupply, tokenName, tokenSymbol) 146 // amount := testGetAmount(caller) 147 // a := new(big.Int) 148 // if amount.Cmp(a.Mul(initialSupply, new(big.Int).SetUint64(100000000))) != 0 { 149 // t.Errorf("unexpected value : want %d, got %d", a, amount) 150 // } else { 151 // t.Logf("%s success get %d", "GetAmount", amount) 152 // } 153 // now := T.Now() 154 // testTransfer(to1, amount1) 155 // after := T.Since(now) 156 // t.Logf("time %s", after.String()) 157 158 // testGetTokenName() 159 // testGetTotalSupply() 160 // testGetSymbol() 161 // testGetDecimals() 162 // amount = testGetAmount(to1) 163 // if amount.Cmp(amount1) != 0 { 164 // t.Errorf("unexpected value : want %d, got %d", amount1, amount) 165 // } else { 166 // t.Logf("%s success get %d", "GetAmount", amount) 167 // } 168 // amount = testGetAmount(caller) 169 // b := new(big.Int) 170 // if new(big.Int).Add(amount, amount1).Cmp(b.Mul(initialSupply, new(big.Int).SetUint64(100000000))) != 0 { 171 // t.Errorf("unexpected value : want %d, got %d", b.Sub(b, amount1), amount) 172 // } else { 173 // t.Logf("%s success get %d", "GetAmount", amount) 174 // } 175 // } 176 177 var ercJsonPath = filepath.Join("", "erc20.json") 178 179 func TestERC(t *testing.T) { 180 run(t, ercJsonPath) 181 } 182 183 func FromUInt64(n uint64) (out []byte) { 184 more := true 185 for more { 186 b := byte(n & 0x7F) 187 n >>= 7 188 if n == 0 { 189 more = false 190 } else { 191 b = b | 0x80 192 } 193 out = append(out, b) 194 } 195 return 196 }