test_events.py (6154B)
1 # standard imports 2 import unittest 3 import logging 4 import os 5 from chainlib.eth.nonce import RPCNonceOracle 6 from chainlib.eth.contract import ABIContractLogDecoder 7 from chainlib.eth.contract import ABIContractType 8 from chainlib.eth.tx import receipt 9 from eth_erc20 import ERC20 10 from giftable_erc20_token import GiftableToken 11 from hexathon import same as same_hex 12 13 # local imports 14 from erc20_vend.unittest.base import TestVendCore 15 from erc20_vend import Vend 16 17 18 logging.basicConfig(level=logging.DEBUG) 19 logg = logging.getLogger() 20 21 22 class TestVendBase(TestVendCore): 23 24 def test_create_token_event(self): 25 self.publish(mint=True) 26 nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn) 27 c = Vend(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 28 (tx_hash, o) = c.create(self.vend_address, self.accounts[0], 'foo vend', 'FOOVEND') 29 self.rpc.do(o) 30 o = receipt(tx_hash) 31 r = self.rpc.do(o) 32 rlog = r['logs'][0] 33 34 o = c.get_token(self.vend_address, 0, sender_address=self.accounts[0]) 35 r = self.rpc.do(o) 36 vended_token_address = c.parse_token(r) 37 38 dec = ABIContractLogDecoder() 39 dec.topic('TokenCreated') 40 dec.typ(ABIContractType.UINT256) 41 dec.typ(ABIContractType.UINT256) 42 dec.typ(ABIContractType.ADDRESS) 43 dec.apply(rlog['topics'], [rlog['data']]) 44 self.assertEqual(int(dec.contents[0], 16), 0) 45 self.assertEqual(int(dec.contents[1], 16), 0) 46 self.assertEqual(int(dec.contents[2], 16), int(vended_token_address, 16)) 47 48 49 def test_mint_event(self): 50 self.publish(mint=True) 51 nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn) 52 c = Vend(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 53 (tx_hash, o) = c.create(self.vend_address, self.accounts[0], 'foo vend', 'FOOVEND') 54 self.rpc.do(o) 55 56 o = c.get_token(self.vend_address, 0, sender_address=self.accounts[0]) 57 r = self.rpc.do(o) 58 vended_token_address = c.parse_token(r) 59 60 vend_amount = 100 61 src_amount = vend_amount * (10 ** self.token_decimals) 62 c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 63 (tx_hash, o) = c.mint_to(self.token_address, self.accounts[0], self.alice, src_amount) 64 self.rpc.do(o) 65 66 nonce_oracle = RPCNonceOracle(self.alice, conn=self.conn) 67 c = Vend(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 68 (tx_hash, o) = c.deposit(self.vend_address, self.alice, vended_token_address) 69 self.rpc.do(o) 70 o = receipt(tx_hash) 71 r = self.rpc.do(o) 72 self.assertEqual(r['status'], 1) 73 rlog = r['logs'][0] 74 75 dec = ABIContractLogDecoder() 76 dec.topic('Mint') 77 dec.typ(ABIContractType.ADDRESS) 78 dec.typ(ABIContractType.ADDRESS) 79 dec.typ(ABIContractType.UINT256) 80 dec.apply(rlog['topics'], [rlog['data']]) 81 self.assertEqual(int(dec.contents[0], 16), int(self.vend_address, 16)) 82 self.assertEqual(int(dec.contents[1], 16), int(self.alice, 16)) 83 self.assertEqual(int(dec.contents[2], 16), vend_amount) 84 85 86 def test_create_token_nomint_event(self): 87 self.publish() 88 nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn) 89 c = Vend(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 90 (tx_hash, o) = c.create(self.vend_address, self.accounts[0], 'foo vend', 'FOOVEND') 91 self.rpc.do(o) 92 o = receipt(tx_hash) 93 r = self.rpc.do(o) 94 self.assertEqual(r['status'], 1) 95 rlog = r['logs'][1] 96 97 o = c.get_token(self.vend_address, 0, sender_address=self.accounts[0]) 98 r = self.rpc.do(o) 99 vended_token_address = c.parse_token(r) 100 101 dec = ABIContractLogDecoder() 102 dec.topic('TokenCreated') 103 dec.typ(ABIContractType.UINT256) 104 dec.typ(ABIContractType.UINT256) 105 dec.typ(ABIContractType.ADDRESS) 106 dec.apply(rlog['topics'], [rlog['data']]) 107 self.assertEqual(int(dec.contents[0], 16), 0) 108 self.assertEqual(int(dec.contents[1], 16), self.initial_supply) 109 self.assertEqual(int(dec.contents[2], 16), int(vended_token_address, 16)) 110 111 112 def test_mint_event(self): 113 self.publish() 114 nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn) 115 c = Vend(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 116 (tx_hash, o) = c.create(self.vend_address, self.accounts[0], 'foo vend', 'FOOVEND') 117 self.rpc.do(o) 118 119 o = c.get_token(self.vend_address, 0, sender_address=self.accounts[0]) 120 r = self.rpc.do(o) 121 vended_token_address = c.parse_token(r) 122 123 vend_amount = 100 124 src_amount = vend_amount * (10 ** self.token_decimals) 125 c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 126 (tx_hash, o) = c.mint_to(self.token_address, self.accounts[0], self.alice, src_amount) 127 self.rpc.do(o) 128 129 nonce_oracle = RPCNonceOracle(self.alice, conn=self.conn) 130 c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 131 (tx_hash, o) = c.approve(self.token_address, self.alice, self.vend_address, src_amount) 132 self.rpc.do(o) 133 134 c = Vend(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 135 (tx_hash, o) = c.deposit(self.vend_address, self.alice, vended_token_address) 136 self.rpc.do(o) 137 o = receipt(tx_hash) 138 r = self.rpc.do(o) 139 self.assertEqual(r['status'], 1) 140 rlog = r['logs'][1] 141 142 dec = ABIContractLogDecoder() 143 dec.topic('Transfer') 144 dec.typ(ABIContractType.ADDRESS) 145 dec.typ(ABIContractType.ADDRESS) 146 dec.typ(ABIContractType.UINT256) 147 dec.apply(rlog['topics'], [rlog['data']]) 148 self.assertEqual(int(dec.contents[0], 16), int(self.vend_address, 16)) 149 self.assertEqual(int(dec.contents[1], 16), int(self.alice, 16)) 150 self.assertEqual(int(dec.contents[2], 16), vend_amount) 151 152 153 if __name__ == '__main__': 154 unittest.main()