github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/test/NRC20MultEvent.js (about) 1 // Copyright (C) 2017 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // the go-nebulas library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 19 'use strict'; 20 21 var Allowed = function (obj) { 22 this.allowed = {}; 23 this.parse(obj); 24 } 25 26 Allowed.prototype = { 27 toString: function () { 28 return JSON.stringify(this.allowed); 29 }, 30 31 parse: function (obj) { 32 if (typeof obj != "undefined") { 33 var data = JSON.parse(obj); 34 for (var key in data) { 35 this.allowed[key] = new BigNumber(data[key]); 36 } 37 } 38 }, 39 40 get: function (key) { 41 return this.allowed[key]; 42 }, 43 44 set: function (key, value) { 45 this.allowed[key] = new BigNumber(value); 46 } 47 } 48 49 var StandardToken = function () { 50 LocalContractStorage.defineProperties(this, { 51 _name: null, 52 _symbol: null, 53 _decimals: null, 54 _totalSupply: { 55 parse: function (value) { 56 return new BigNumber(value); 57 }, 58 stringify: function (o) { 59 return o.toString(10); 60 } 61 } 62 }); 63 64 LocalContractStorage.defineMapProperties(this, { 65 "balances": { 66 parse: function (value) { 67 return new BigNumber(value); 68 }, 69 stringify: function (o) { 70 return o.toString(10); 71 } 72 }, 73 "allowed": { 74 parse: function (value) { 75 return new Allowed(value); 76 }, 77 stringify: function (o) { 78 return o.toString(); 79 } 80 } 81 }); 82 }; 83 84 StandardToken.prototype = { 85 init: function (name, symbol, decimals, totalSupply) { 86 this._name = name; 87 this._symbol = symbol; 88 this._decimals = decimals | 0; 89 this._totalSupply = new BigNumber(totalSupply).mul(new BigNumber(10).pow(decimals)); 90 91 var from = Blockchain.transaction.from; 92 this.balances.set(from, this._totalSupply); 93 this.transferEvent(true, from, from, this._totalSupply); 94 }, 95 96 // Returns the name of the token 97 name: function () { 98 return this._name; 99 }, 100 101 // Returns the symbol of the token 102 symbol: function () { 103 return this._symbol; 104 }, 105 106 // Returns the number of decimals the token uses 107 decimals: function () { 108 return this._decimals; 109 }, 110 111 totalSupply: function () { 112 return this._totalSupply.toString(10); 113 }, 114 115 balanceOf: function (owner) { 116 var balance = this.balances.get(owner); 117 118 if (balance instanceof BigNumber) { 119 return balance.toString(10); 120 } else { 121 return "0"; 122 } 123 }, 124 125 transferforMultEvent: function (to, value) { 126 value = new BigNumber(value); 127 128 var from = Blockchain.transaction.from; 129 var balance = this.balances.get(from) || new BigNumber(0); 130 131 if (balance.lt(value)) { 132 throw new Error("transfer failed."); 133 } 134 135 this.balances.set(from, balance.sub(value)); 136 var toBalance = this.balances.get(to) || new BigNumber(0); 137 this.balances.set(to, toBalance.add(value)); 138 139 Event.Trigger(this.name(), { 140 Status: true, 141 Transfer: { 142 from: from, 143 to: to, 144 value: value 145 } 146 }); 147 Event.Trigger(this.name(), { 148 Status: true, 149 Transfer: { 150 from: from, 151 value: value 152 } 153 }); 154 Event.Trigger(this.name(), { 155 Status: true, 156 Transfer: { 157 to: to, 158 value: value 159 } 160 }); 161 Event.Trigger(this.name(), { 162 Status: true, 163 Transfer: { 164 value: value 165 } 166 }); 167 }, 168 transferEvent: function (status, from, to, value) { 169 Event.Trigger(this.name(), { 170 Status: status, 171 Transfer: { 172 from: from, 173 to: to, 174 value: value 175 } 176 }); 177 }, 178 //test c catch err 179 transferforMultEventStatus: function (to, value) { 180 value = new BigNumber(value); 181 182 var from = Blockchain.transaction.from; 183 var balance = this.balances.get(from) || new BigNumber(0); 184 185 if (balance.lt(value)) { 186 throw new Error("transfer failed."); 187 } 188 189 this.balances.set(from, balance); 190 var toBalance = this.balances.get(to) || new BigNumber(0); 191 this.balances.set(to, toBalance); 192 193 Event.Trigger(this.name(), { 194 Status: status, 195 Transfer: { 196 from: from, 197 to: to, 198 value: value 199 } 200 }); 201 Event.Trigger(this.name(), { 202 Status: true, 203 Transfer: status, 204 }); 205 Event.Trigger(this.name(), { 206 Status: "123", 207 Transfer: { 208 from: from, 209 to: to, 210 value: value 211 } 212 }); 213 }, 214 215 transferforMultEventTransfer: function (to, value) { 216 value = new BigNumber(value); 217 218 var from = Blockchain.transaction.from; 219 var balance = this.balances.get(from) || new BigNumber(0); 220 221 if (balance.lt(value)) { 222 throw new Error("transfer failed."); 223 } 224 225 this.balances.set(from, balance.sub(value)); 226 var toBalance = this.balances.get(to) || new BigNumber(0); 227 this.balances.set(to, toBalance.add(value)); 228 229 Event.Trigger(this.name(), { 230 Status: true, 231 }); 232 233 Event.Trigger(this.name(), { 234 Status: null, 235 Transfer: { 236 from: from, 237 to: to, 238 value: value 239 } 240 }); 241 }, 242 243 }; 244 245 module.exports = StandardToken;