test_basic.py (14759B)
1 # standard imports 2 import os 3 import unittest 4 import json 5 import logging 6 import datetime 7 8 # external imports 9 from chainlib.eth.constant import ZERO_ADDRESS 10 from chainlib.eth.nonce import RPCNonceOracle 11 from chainlib.eth.tx import receipt 12 from chainlib.eth.block import ( 13 block_latest, 14 block_by_number, 15 ) 16 17 # local imports 18 from erc20_demurrage_token import DemurrageToken 19 20 # test imports 21 from erc20_demurrage_token.unittest import TestDemurrageDefault 22 23 logging.basicConfig(level=logging.DEBUG) 24 logg = logging.getLogger() 25 26 testdir = os.path.dirname(__file__) 27 28 29 class TestBasic(TestDemurrageDefault): 30 31 def test_hello(self): 32 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 33 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 34 o = c.actual_period(self.address, sender_address=self.accounts[0]) 35 r = self.rpc.do(o) 36 37 self.backend.time_travel(self.start_time + self.period_seconds + 1) 38 o = c.actual_period(self.address, sender_address=self.accounts[0]) 39 r = self.rpc.do(o) 40 41 42 def test_balance(self): 43 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 44 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 45 (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1024) 46 r = self.rpc.do(o) 47 o = receipt(tx_hash) 48 r = self.rpc.do(o) 49 self.assertEqual(r['status'], 1) 50 51 o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) 52 r = self.rpc.do(o) 53 balance = c.parse_balance_of(r) 54 self.assertEqual(balance, 1024) 55 56 57 def test_apply_demurrage_limited(self): 58 #modifier = (10 ** 28) 59 modifier = 1 60 61 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 62 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 63 64 o = c.demurrage_amount(self.address, sender_address=self.accounts[0]) 65 r = self.rpc.do(o) 66 demurrage_amount = c.parse_demurrage_amount(r) 67 self.assertEqual(modifier, demurrage_amount) 68 69 self.backend.time_travel(self.start_time + (60 * 43200)) 70 (tx_hash, o) = c.apply_demurrage(self.address, sender_address=self.accounts[0], limit=20000) 71 r = self.rpc.do(o) 72 o = receipt(tx_hash) 73 r = self.rpc.do(o) 74 self.assertEqual(r['status'], 1) 75 76 o = c.demurrage_amount(self.address, sender_address=self.accounts[0]) 77 r = self.rpc.do(o) 78 demurrage_amount = c.parse_demurrage_amount(r) 79 self.assert_equal_decimals(0.9906, demurrage_amount, 4) 80 81 82 def test_apply_demurrage(self): 83 #modifier = (10 ** 28) 84 modifier = 1 85 86 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 87 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 88 89 o = c.demurrage_amount(self.address, sender_address=self.accounts[0]) 90 r = self.rpc.do(o) 91 demurrage_amount = c.parse_demurrage_amount(r) 92 self.assertEqual(modifier, demurrage_amount) 93 94 o = block_latest() 95 r = self.rpc.do(o) 96 o = block_by_number(r) 97 b = self.rpc.do(o) 98 logg.debug('block {} start {}'.format(b['timestamp'], self.start_time)) 99 100 self.backend.time_travel(self.start_time + (60 * 43200)) 101 (tx_hash, o) = c.apply_demurrage(self.address, sender_address=self.accounts[0]) 102 r = self.rpc.do(o) 103 o = receipt(tx_hash) 104 r = self.rpc.do(o) 105 self.assertEqual(r['status'], 1) 106 107 o = c.demurrage_amount(self.address, sender_address=self.accounts[0]) 108 r = self.rpc.do(o) 109 demurrage_amount = c.parse_demurrage_amount(r) 110 self.assert_equal_decimals(0.98, demurrage_amount, 2) 111 112 self.backend.time_travel(self.start_time + (60 * 43200 * 2)) 113 (tx_hash, o) = c.apply_demurrage(self.address, sender_address=self.accounts[0]) 114 r = self.rpc.do(o) 115 o = receipt(tx_hash) 116 r = self.rpc.do(o) 117 self.assertEqual(r['status'], 1) 118 o = c.demurrage_amount(self.address, sender_address=self.accounts[0]) 119 r = self.rpc.do(o) 120 demurrage_amount = c.parse_demurrage_amount(r) 121 modifier_base = 1000000 - self.tax_level 122 modifier = int(modifier_base * (10 ** 22)) # 38 decimal places minus 6 (1000000) 123 self.assert_equal_decimals(0.9604, demurrage_amount, 4) 124 125 126 def test_mint_balance(self): 127 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 128 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 129 (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1024) 130 r = self.rpc.do(o) 131 o = receipt(tx_hash) 132 r = self.rpc.do(o) 133 self.assertEqual(r['status'], 1) 134 135 o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) 136 r = self.rpc.do(o) 137 balance = c.parse_balance_of(r) 138 self.assertEqual(balance, 1024) 139 140 (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 976) 141 r = self.rpc.do(o) 142 o = receipt(tx_hash) 143 r = self.rpc.do(o) 144 self.assertEqual(r['status'], 1) 145 o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) 146 r = self.rpc.do(o) 147 balance = c.parse_balance_of(r) 148 self.assertEqual(balance, 2000) 149 150 151 self.backend.time_travel(self.start_time + (60 * 43200)) 152 (tx_hash, o) = c.apply_demurrage(self.address, sender_address=self.accounts[0]) 153 r = self.rpc.do(o) 154 o = receipt(tx_hash) 155 r = self.rpc.do(o) 156 self.assertEqual(r['status'], 1) 157 158 o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) 159 r = self.rpc.do(o) 160 balance = c.parse_balance_of(r) 161 self.assertEqual(balance, int(2000 * 0.98)) 162 163 164 def test_minter_control(self): 165 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 166 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 167 168 (tx_hash, o) = c.mint_to(self.address, self.accounts[1], self.accounts[2], 1024) 169 self.rpc.do(o) 170 o = receipt(tx_hash) 171 r = self.rpc.do(o) 172 self.assertEqual(r['status'], 0) 173 174 (tx_hash, o) = c.add_minter(self.address, self.accounts[1], self.accounts[1]) 175 self.rpc.do(o) 176 o = receipt(tx_hash) 177 r = self.rpc.do(o) 178 self.assertEqual(r['status'], 0) 179 180 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 181 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 182 (tx_hash, o) = c.add_minter(self.address, self.accounts[0], self.accounts[1]) 183 self.rpc.do(o) 184 o = receipt(tx_hash) 185 r = self.rpc.do(o) 186 self.assertEqual(r['status'], 1) 187 188 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 189 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 190 (tx_hash, o) = c.mint_to(self.address, self.accounts[1], self.accounts[2], 1024) 191 self.rpc.do(o) 192 o = receipt(tx_hash) 193 r = self.rpc.do(o) 194 self.assertEqual(r['status'], 1) 195 196 (tx_hash, o) = c.add_minter(self.address, self.accounts[1], self.accounts[2]) 197 self.rpc.do(o) 198 o = receipt(tx_hash) 199 r = self.rpc.do(o) 200 self.assertEqual(r['status'], 0) 201 202 (tx_hash, o) = c.remove_minter(self.address, self.accounts[1], self.accounts[1]) 203 self.rpc.do(o) 204 o = receipt(tx_hash) 205 r = self.rpc.do(o) 206 self.assertEqual(r['status'], 1) 207 208 (tx_hash, o) = c.mint_to(self.address, self.accounts[1], self.accounts[2], 1024) 209 self.rpc.do(o) 210 o = receipt(tx_hash) 211 r = self.rpc.do(o) 212 self.assertEqual(r['status'], 0) 213 214 215 def test_base_amount(self): 216 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 217 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 218 219 (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1024) 220 self.rpc.do(o) 221 222 self.backend.time_travel(self.start_time + (60 * 43200)) 223 (tx_hash, o) = c.apply_demurrage(self.address, sender_address=self.accounts[0]) 224 r = self.rpc.do(o) 225 o = c.to_base_amount(self.address, 1000, sender_address=self.accounts[0]) 226 r = self.rpc.do(o) 227 amount = c.parse_to_base_amount(r) 228 self.assertEqual(amount, 1020) 229 230 231 def test_transfer(self): 232 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 233 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 234 235 (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1024) 236 self.rpc.do(o) 237 238 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 239 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 240 (tx_hash, o) = c.transfer(self.address, self.accounts[1], self.accounts[2], 500) 241 self.rpc.do(o) 242 o = receipt(tx_hash) 243 r = self.rpc.do(o) 244 self.assertEqual(r['status'], 1) 245 246 o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) 247 r = self.rpc.do(o) 248 balance = c.parse_balance_of(r) 249 self.assertEqual(balance, 524) 250 251 o = c.balance_of(self.address, self.accounts[2], sender_address=self.accounts[0]) 252 r = self.rpc.do(o) 253 balance = c.parse_balance_of(r) 254 self.assertEqual(balance, 500) 255 256 nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) 257 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 258 (tx_hash, o) = c.transfer(self.address, self.accounts[2], self.accounts[1], 500) 259 self.rpc.do(o) 260 o = receipt(tx_hash) 261 r = self.rpc.do(o) 262 self.assertEqual(r['status'], 1) 263 264 265 def test_approve(self): 266 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 267 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 268 (tx_hash, o) = c.approve(self.address, self.accounts[0], self.accounts[1], 500) 269 self.rpc.do(o) 270 o = receipt(tx_hash) 271 r = self.rpc.do(o) 272 self.assertEqual(r['status'], 1) 273 274 (tx_hash, o) = c.approve(self.address, self.accounts[0], self.accounts[1], 600) 275 self.rpc.do(o) 276 o = receipt(tx_hash) 277 r = self.rpc.do(o) 278 self.assertEqual(r['status'], 0) 279 280 (tx_hash, o) = c.approve(self.address, self.accounts[0], self.accounts[1], 0) 281 self.rpc.do(o) 282 o = receipt(tx_hash) 283 r = self.rpc.do(o) 284 self.assertEqual(r['status'], 1) 285 286 (tx_hash, o) = c.approve(self.address, self.accounts[0], self.accounts[1], 600) 287 self.rpc.do(o) 288 o = receipt(tx_hash) 289 r = self.rpc.do(o) 290 self.assertEqual(r['status'], 1) 291 292 (tx_hash, o) = c.increase_allowance(self.address, self.accounts[0], self.accounts[1], 200) 293 self.rpc.do(o) 294 o = receipt(tx_hash) 295 r = self.rpc.do(o) 296 self.assertEqual(r['status'], 1) 297 298 (tx_hash, o) = c.decrease_allowance(self.address, self.accounts[0], self.accounts[1], 800) 299 self.rpc.do(o) 300 o = receipt(tx_hash) 301 r = self.rpc.do(o) 302 self.assertEqual(r['status'], 1) 303 304 (tx_hash, o) = c.approve(self.address, self.accounts[0], self.accounts[1], 42) 305 self.rpc.do(o) 306 o = receipt(tx_hash) 307 r = self.rpc.do(o) 308 self.assertEqual(r['status'], 1) 309 310 311 def test_approve_max(self): 312 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 313 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 314 (tx_hash, o) = c.approve(self.address, self.accounts[0], self.accounts[1], int.from_bytes(b'\xff' * 32, byteorder='big')) 315 self.rpc.do(o) 316 o = receipt(tx_hash) 317 r = self.rpc.do(o) 318 self.assertEqual(r['status'], 1) 319 320 (tx_hash, o) = c.approve(self.address, self.accounts[0], self.accounts[1], 0) 321 self.rpc.do(o) 322 o = receipt(tx_hash) 323 r = self.rpc.do(o) 324 self.assertEqual(r['status'], 1) 325 326 self.backend.time_travel(self.start_time + (60 * 60 * 24 * 365 * 10)) 327 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 328 (tx_hash, o) = c.approve(self.address, self.accounts[0], self.accounts[1], int.from_bytes(b'\xff' * 32, byteorder='big')) 329 self.rpc.do(o) 330 o = receipt(tx_hash) 331 r = self.rpc.do(o) 332 self.assertEqual(r['status'], 1) 333 334 335 def test_transfer_from(self): 336 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 337 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 338 (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1024) 339 self.rpc.do(o) 340 341 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 342 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 343 (tx_hash, o) = c.approve(self.address, self.accounts[1], self.accounts[2], 500) 344 self.rpc.do(o) 345 o = receipt(tx_hash) 346 r = self.rpc.do(o) 347 self.assertEqual(r['status'], 1) 348 349 o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) 350 r = self.rpc.do(o) 351 balance = c.parse_balance_of(r) 352 self.assertEqual(balance, 1024) 353 354 nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) 355 c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 356 (tx_hash, o) = c.transfer_from(self.address, self.accounts[2], self.accounts[1], self.accounts[3], 500) 357 self.rpc.do(o) 358 o = receipt(tx_hash) 359 r = self.rpc.do(o) 360 self.assertEqual(r['status'], 1) 361 362 o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) 363 r = self.rpc.do(o) 364 balance = c.parse_balance_of(r) 365 self.assertEqual(balance, 524) 366 367 o = c.balance_of(self.address, self.accounts[3], sender_address=self.accounts[0]) 368 r = self.rpc.do(o) 369 balance = c.parse_balance_of(r) 370 self.assertEqual(balance, 500) 371 372 (tx_hash, o) = c.transfer_from(self.address, self.accounts[2], self.accounts[1], self.accounts[3], 1) 373 self.rpc.do(o) 374 o = receipt(tx_hash) 375 r = self.rpc.do(o) 376 self.assertEqual(r['status'], 0) 377 378 379 if __name__ == '__main__': 380 unittest.main()