|
| 1 | +var SpringToken = artifacts.require("./SPRINGToken.sol"); |
| 2 | + |
| 3 | +contract('SpringToken', function(accounts) { |
| 4 | + it("should have decimal place of 18", function() { |
| 5 | + return SpringToken.deployed().then(function(instance) { |
| 6 | + return instance.decimals.call(); |
| 7 | + }).then(function(decimals) { |
| 8 | + assert.equal(decimals.valueOf(), 18, "18 decimals not found"); |
| 9 | + }); |
| 10 | + }); |
| 11 | + |
| 12 | + it("token should have a name", function() { |
| 13 | + return SpringToken.deployed().then(function(instance) { |
| 14 | + return instance.name.call(); |
| 15 | + }).then(function(name) { |
| 16 | + assert.isDefined(name.valueOf(), 'token should have a name'); |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + it("token should have a symbol", function() { |
| 21 | + return SpringToken.deployed().then(function(instance) { |
| 22 | + return instance.symbol.call(); |
| 23 | + }).then(function(symbol) { |
| 24 | + assert.isDefined(symbol.valueOf(), 'token should have a symbol'); |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + it("token should have a maxSupply", function() { |
| 29 | + return SpringToken.deployed().then(function(instance) { |
| 30 | + return instance.maxSupply.call(); |
| 31 | + }).then(function(maxSupply) { |
| 32 | + assert.isDefined(maxSupply.valueOf(), 'token should have a maxSupply'); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + it("token default state should be unpaused", function() { |
| 37 | + return SpringToken.deployed().then(function(instance) { |
| 38 | + return instance.paused.call(); |
| 39 | + }).then(function(paused) { |
| 40 | + assert.isFalse(paused.valueOf(), 'token should have a maxSupply'); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it("mint should throw error when tried by non owner account", function() { |
| 45 | + return SpringToken.deployed().then(function(instance) { |
| 46 | + return instance.mint.call(10000,{from: accounts[1]}); |
| 47 | + }).then(function(minted) { |
| 48 | + assert.isFalse(minted, "transaction should have thrown error"); |
| 49 | + }).catch(function(err) { |
| 50 | + assert.isDefined(err, "transaction should have thrown error"); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + it("mint should throw error when tried to mint more than max supply", function() { |
| 55 | + var token; |
| 56 | + |
| 57 | + return SpringToken.deployed().then(function(instance) { |
| 58 | + token = instance; |
| 59 | + return token.maxSupply.call(); |
| 60 | + }).then(function(maxSupply) { |
| 61 | + return instance.mint(maxSupply.toNumber()+1); |
| 62 | + }).then(function(minted) { |
| 63 | + assert.isFalse(minted, "transaction should have thrown error"); |
| 64 | + }).catch(function(err) { |
| 65 | + assert.isDefined(err, "transaction should have thrown error"); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should mint 10000 when tried by owner account", function() { |
| 70 | + var token; |
| 71 | + var amount = 10000; |
| 72 | + var old_balance; |
| 73 | + var new_balance; |
| 74 | + |
| 75 | + return SpringToken.deployed().then(function(instance) { |
| 76 | + token = instance; |
| 77 | + return token.balanceOf.call(accounts[0]); |
| 78 | + }).then(function(balance) { |
| 79 | + old_balance = balance.toNumber(); |
| 80 | + return token.mint(amount); |
| 81 | + }).then(function(minted) { |
| 82 | + return token.balanceOf.call(accounts[0]); |
| 83 | + }).then(function(balance) { |
| 84 | + new_balance = balance.toNumber(); |
| 85 | + assert.equal(new_balance, old_balance+amount, "10000 tokens were not minted or not assigned to owner"); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should transfer 10 token correctly", function() { |
| 90 | + var token; |
| 91 | + |
| 92 | + // Get initial balances of first and second account. |
| 93 | + var account_one = accounts[0]; |
| 94 | + var account_two = accounts[1]; |
| 95 | + |
| 96 | + var account_one_starting_balance; |
| 97 | + var account_two_starting_balance; |
| 98 | + var account_one_ending_balance; |
| 99 | + var account_two_ending_balance; |
| 100 | + |
| 101 | + var amount = 10; |
| 102 | + |
| 103 | + return SpringToken.deployed().then(function(instance) { |
| 104 | + token = instance; |
| 105 | + return token.balanceOf.call(account_one); |
| 106 | + }).then(function(balance) { |
| 107 | + account_one_starting_balance = balance.toNumber(); |
| 108 | + return token.balanceOf.call(account_two); |
| 109 | + }).then(function(balance) { |
| 110 | + account_two_starting_balance = balance.toNumber(); |
| 111 | + return token.transfer(account_two, amount, {from: account_one}); |
| 112 | + }).then(function() { |
| 113 | + return token.balanceOf.call(account_one); |
| 114 | + }).then(function(balance) { |
| 115 | + account_one_ending_balance = balance.toNumber(); |
| 116 | + return token.balanceOf.call(account_two); |
| 117 | + }).then(function(balance) { |
| 118 | + account_two_ending_balance = balance.toNumber(); |
| 119 | + assert.equal(account_one_ending_balance, account_one_starting_balance - amount, "Amount wasn't correctly taken from the sender"); |
| 120 | + assert.equal(account_two_ending_balance, account_two_starting_balance + amount, "Amount wasn't correctly sent to the receiver"); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + it("should transfer should fail when low balance", function() { |
| 125 | + var token; |
| 126 | + |
| 127 | + // Get initial balances of first and second account. |
| 128 | + var account_one = accounts[2]; |
| 129 | + var account_two = accounts[3]; |
| 130 | + |
| 131 | + var account_one_starting_balance; |
| 132 | + var account_two_starting_balance; |
| 133 | + var account_one_ending_balance; |
| 134 | + var account_two_ending_balance; |
| 135 | + |
| 136 | + var amount = 10; |
| 137 | + |
| 138 | + return SpringToken.deployed().then(function(instance) { |
| 139 | + token = instance; |
| 140 | + return token.balanceOf.call(account_one); |
| 141 | + }).then(function(balance) { |
| 142 | + account_one_starting_balance = balance.toNumber(); |
| 143 | + return token.balanceOf.call(account_two); |
| 144 | + }).then(function(balance) { |
| 145 | + account_two_starting_balance = balance.toNumber(); |
| 146 | + return token.transfer(account_two, amount, {from: account_one}); |
| 147 | + }).then(function(result) { |
| 148 | + assert.isFalse(result, "transaction should have thrown error"); |
| 149 | + }).catch(function(err) { |
| 150 | + assert.isDefined(err, "transaction should have thrown error"); |
| 151 | + }); |
| 152 | + }); |
| 153 | + |
| 154 | + it("should approve 1 token correctly", function() { |
| 155 | + var token; |
| 156 | + |
| 157 | + var amount = 1; |
| 158 | + |
| 159 | + return SpringToken.deployed().then(function(instance) { |
| 160 | + token = instance; |
| 161 | + return token.approve(accounts[2],amount,{from:accounts[1]}); |
| 162 | + }).then(function() { |
| 163 | + return token.allowance.call(accounts[1],accounts[2]); |
| 164 | + }).then(function(allowance) { |
| 165 | + assert.equal(allowance.toNumber(), amount, "Amount wasn't correctly approved"); |
| 166 | + }); |
| 167 | + }); |
| 168 | + |
| 169 | + it("should transfer 1 token correctly using 3rd approved address", function() { |
| 170 | + var token; |
| 171 | + |
| 172 | + var amount = 10; |
| 173 | + |
| 174 | + var contract_owner = accounts[0]; |
| 175 | + var token_owner = accounts[3]; |
| 176 | + var spender = accounts[2]; |
| 177 | + var transfer_to = accounts[4]; |
| 178 | + |
| 179 | + var allowance_before_transfer; |
| 180 | + var allowance_after_transfer; |
| 181 | + |
| 182 | + var amount_transferred = 1; |
| 183 | + |
| 184 | + var account_one_starting_balance; |
| 185 | + var account_two_starting_balance; |
| 186 | + var account_one_ending_balance; |
| 187 | + var account_two_ending_balance; |
| 188 | + |
| 189 | + return SpringToken.deployed().then(function(instance) { |
| 190 | + token = instance; |
| 191 | + return token.transfer(token_owner, amount, {from: contract_owner}); |
| 192 | + }).then(function() { |
| 193 | + return token.balanceOf.call(token_owner); |
| 194 | + }).then(function(balance) { |
| 195 | + account_one_starting_balance = balance.toNumber(); |
| 196 | + return token.balanceOf.call(transfer_to); |
| 197 | + }).then(function(balance) { |
| 198 | + account_two_starting_balance = balance.toNumber(); |
| 199 | + return token.approve(spender,amount,{from:token_owner}); |
| 200 | + }).then(function() { |
| 201 | + return token.allowance.call(token_owner,spender); |
| 202 | + }).then(function(allowance) { |
| 203 | + allowance_before_transfer = allowance.toNumber(); |
| 204 | + return token.transferFrom(token_owner,transfer_to,amount_transferred, {from: spender}); |
| 205 | + }).then(function() { |
| 206 | + return token.allowance.call(token_owner,spender); |
| 207 | + }).then(function(allowance) { |
| 208 | + allowance_after_transfer = allowance.toNumber(); |
| 209 | + assert.equal(allowance_after_transfer, allowance_before_transfer - amount_transferred, "Allowance not updated correctly"); |
| 210 | + }).then(function() { |
| 211 | + return token.balanceOf.call(token_owner); |
| 212 | + }).then(function(balance) { |
| 213 | + account_one_ending_balance = balance.toNumber(); |
| 214 | + return token.balanceOf.call(transfer_to); |
| 215 | + }).then(function(balance) { |
| 216 | + account_two_ending_balance = balance.toNumber(); |
| 217 | + assert.equal(account_one_ending_balance, account_one_starting_balance - amount_transferred, "Amount wasn't correctly taken from the sender"); |
| 218 | + assert.equal(account_two_ending_balance, account_two_starting_balance + amount_transferred, "Amount wasn't correctly sent to the receiver"); |
| 219 | + }); |
| 220 | + }); |
| 221 | + |
| 222 | + it("should increase approval 1 token correctly", function() { |
| 223 | + var token; |
| 224 | + |
| 225 | + var amount = 1; |
| 226 | + |
| 227 | + var approval_starting_balance; |
| 228 | + var approval_ending_balance; |
| 229 | + |
| 230 | + return SpringToken.deployed().then(function(instance) { |
| 231 | + token = instance; |
| 232 | + return token.allowance.call(accounts[1],accounts[2]); |
| 233 | + }).then(function(balance) { |
| 234 | + approval_starting_balance = balance.toNumber(); |
| 235 | + return token.increaseApproval(accounts[2],amount,{from:accounts[1]}); |
| 236 | + }).then(function(allowance) { |
| 237 | + return token.allowance.call(accounts[1],accounts[2]); |
| 238 | + }).then(function(balance) { |
| 239 | + approval_ending_balance = balance.toNumber(); |
| 240 | + assert.equal(approval_ending_balance, approval_starting_balance + amount, "Amount wasn't correctly approved"); |
| 241 | + }); |
| 242 | + }); |
| 243 | + |
| 244 | + it("should decrease approval 1 token correctly", function() { |
| 245 | + var token; |
| 246 | + |
| 247 | + var amount = 1; |
| 248 | + |
| 249 | + var approval_starting_balance; |
| 250 | + var approval_ending_balance; |
| 251 | + |
| 252 | + return SpringToken.deployed().then(function(instance) { |
| 253 | + token = instance; |
| 254 | + return token.allowance.call(accounts[1],accounts[2]); |
| 255 | + }).then(function(balance) { |
| 256 | + approval_starting_balance = balance.toNumber(); |
| 257 | + return token.decreaseApproval(accounts[2],amount,{from:accounts[1]}); |
| 258 | + }).then(function(allowance) { |
| 259 | + return token.allowance.call(accounts[1],accounts[2]); |
| 260 | + }).then(function(balance) { |
| 261 | + approval_ending_balance = balance.toNumber(); |
| 262 | + assert.equal(approval_ending_balance, approval_starting_balance - amount, "Amount wasn't correctly approved"); |
| 263 | + }); |
| 264 | + }); |
| 265 | + |
| 266 | + it("should fail when try to approve 1 token when allowance already exists", function() { |
| 267 | + var token; |
| 268 | + |
| 269 | + var amount = 1; |
| 270 | + |
| 271 | + return SpringToken.deployed().then(function(instance) { |
| 272 | + token = instance; |
| 273 | + return token.approve(accounts[2],amount,{from:accounts[1]}); |
| 274 | + }).then(function(status) { |
| 275 | + assert.isFalse(status, "transaction should have thrown error"); |
| 276 | + }).catch(function(err) { |
| 277 | + assert.isDefined(err, "transaction should have thrown error"); |
| 278 | + }); |
| 279 | + }); |
| 280 | + |
| 281 | + it("should be able to change paused status", function() { |
| 282 | + return SpringToken.deployed().then(function(instance) { |
| 283 | + token = instance; |
| 284 | + return token.pause(); |
| 285 | + }).then(function() { |
| 286 | + return token.paused.call(); |
| 287 | + }).then(function(paused) { |
| 288 | + assert.isTrue(paused.valueOf(), 'should be able to change paused status'); |
| 289 | + }); |
| 290 | + }); |
| 291 | + |
| 292 | + it("transfer should fail when paused", function() { |
| 293 | + var token; |
| 294 | + |
| 295 | + // Get initial balances of first and second account. |
| 296 | + var account_one = accounts[0]; |
| 297 | + var account_two = accounts[1]; |
| 298 | + |
| 299 | + var account_one_starting_balance; |
| 300 | + var account_two_starting_balance; |
| 301 | + var account_one_ending_balance; |
| 302 | + var account_two_ending_balance; |
| 303 | + |
| 304 | + var amount = 1; |
| 305 | + |
| 306 | + return SpringToken.deployed().then(function(instance) { |
| 307 | + token = instance; |
| 308 | + return token.balanceOf.call(account_one); |
| 309 | + }).then(function(balance) { |
| 310 | + account_one_starting_balance = balance.toNumber(); |
| 311 | + return token.balanceOf.call(account_two); |
| 312 | + }).then(function(balance) { |
| 313 | + account_two_starting_balance = balance.toNumber(); |
| 314 | + return token.transfer(account_two, amount, {from: account_one}); |
| 315 | + }).then(function(result) { |
| 316 | + assert.isFalse(result, "transaction should have thrown error"); |
| 317 | + }).catch(function(err) { |
| 318 | + assert.isDefined(err, "transaction should have thrown error"); |
| 319 | + }); |
| 320 | + }); |
| 321 | + |
| 322 | + it("approval should fail when paused", function() { |
| 323 | + var token; |
| 324 | + |
| 325 | + var amount = 1; |
| 326 | + |
| 327 | + var approval_starting_balance; |
| 328 | + var approval_ending_balance; |
| 329 | + |
| 330 | + return SpringToken.deployed().then(function(instance) { |
| 331 | + token = instance; |
| 332 | + return token.allowance.call(accounts[1],accounts[2]); |
| 333 | + }).then(function(balance) { |
| 334 | + approval_starting_balance = balance.toNumber(); |
| 335 | + return token.increaseApproval(accounts[2],amount,{from:accounts[1]}); |
| 336 | + }).then(function(result) { |
| 337 | + assert.isFalse(result, "transaction should have thrown error"); |
| 338 | + }).catch(function(err) { |
| 339 | + assert.isDefined(err, "transaction should have thrown error"); |
| 340 | + }); |
| 341 | + }); |
| 342 | + |
| 343 | + it("decrease approval should fail when paused", function() { |
| 344 | + var token; |
| 345 | + |
| 346 | + var amount = 1; |
| 347 | + |
| 348 | + var approval_starting_balance; |
| 349 | + var approval_ending_balance; |
| 350 | + |
| 351 | + return SpringToken.deployed().then(function(instance) { |
| 352 | + token = instance; |
| 353 | + return token.allowance.call(accounts[1],accounts[2]); |
| 354 | + }).then(function(balance) { |
| 355 | + approval_starting_balance = balance.toNumber(); |
| 356 | + return token.decreaseApproval(accounts[2],amount,{from:accounts[1]}); |
| 357 | + }).then(function(result) { |
| 358 | + assert.isFalse(result, "transaction should have thrown error"); |
| 359 | + }).catch(function(err) { |
| 360 | + assert.isDefined(err, "transaction should have thrown error"); |
| 361 | + }); |
| 362 | + }); |
| 363 | +}); |
0 commit comments