test_base.py (4851B)
1 # standard imports 2 import unittest 3 import logging 4 import os 5 from chainlib.eth.nonce import RPCNonceOracle 6 from chainlib.eth.tx import receipt 7 from eth_erc20 import ERC20 8 from giftable_erc20_token import GiftableToken 9 10 # local imports 11 from erc20_vend.unittest import TestVend 12 from erc20_vend import Vend 13 14 15 logging.basicConfig(level=logging.DEBUG) 16 logg = logging.getLogger() 17 18 19 class TestVendBase(TestVend): 20 21 def test_create_token(self): 22 nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn) 23 c = Vend(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 24 (tx_hash, o) = c.create(self.vend_address, self.accounts[0], 'foo vend', 'FOOVEND') 25 self.rpc.do(o) 26 o = receipt(tx_hash) 27 r = self.rpc.do(o) 28 self.assertEqual(r['status'], 1) 29 30 o = c.get_token(self.vend_address, 0, sender_address=self.accounts[0]) 31 r = self.rpc.do(o) 32 vended_token_address = c.parse_token(r) 33 34 c = ERC20(self.chain_spec) 35 o = c.name(vended_token_address, sender_address=self.accounts[0]) 36 r = self.rpc.do(o) 37 name = c.parse_name(r) 38 self.assertEqual(name, 'foo vend') 39 40 o = c.symbol(vended_token_address, sender_address=self.accounts[0]) 41 r = self.rpc.do(o) 42 symbol = c.parse_symbol(r) 43 self.assertEqual(symbol, 'FOOVEND') 44 45 def test_vend_token(self): 46 nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn) 47 c = Vend(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 48 (tx_hash, o) = c.create(self.vend_address, self.accounts[0], 'foo vend', 'FOOVEND') 49 self.rpc.do(o) 50 51 o = c.get_token(self.vend_address, 0, sender_address=self.accounts[0]) 52 r = self.rpc.do(o) 53 vended_token_address = c.parse_token(r) 54 55 vend_amount = 100 56 src_amount = vend_amount * (10 ** (self.token_decimals)) 57 c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 58 (tx_hash, o) = c.mint_to(self.token_address, self.accounts[0], self.alice, src_amount) 59 self.rpc.do(o) 60 o = receipt(tx_hash) 61 r = self.rpc.do(o) 62 self.assertEqual(r['status'], 1) 63 64 nonce_oracle = RPCNonceOracle(self.alice, conn=self.conn) 65 c = Vend(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 66 (tx_hash, o) = c.deposit(self.vend_address, self.alice, vended_token_address) 67 self.rpc.do(o) 68 o = receipt(tx_hash) 69 r = self.rpc.do(o) 70 self.assertEqual(r['status'], 0) 71 72 c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 73 (tx_hash, o) = c.approve(self.token_address, self.alice, self.vend_address, src_amount) 74 self.rpc.do(o) 75 o = receipt(tx_hash) 76 r = self.rpc.do(o) 77 self.assertEqual(r['status'], 1) 78 79 c = Vend(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 80 (tx_hash, o) = c.deposit(self.vend_address, self.alice, vended_token_address) 81 self.rpc.do(o) 82 o = receipt(tx_hash) 83 r = self.rpc.do(o) 84 self.assertEqual(r['status'], 1) 85 86 c = ERC20(self.chain_spec) 87 o = c.balance_of(vended_token_address, self.alice, sender_address=self.accounts[0]) 88 r = self.rpc.do(o) 89 balance = c.parse_balance(r) 90 self.assertEqual(balance, vend_amount) 91 92 o = c.balance_of(self.token_address, self.alice, sender_address=self.accounts[0]) 93 r = self.rpc.do(o) 94 balance = c.parse_balance(r) 95 self.assertEqual(balance, 0) 96 97 c = Vend(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 98 (tx_hash, o) = c.withdraw(self.vend_address, self.alice, vended_token_address) 99 self.rpc.do(o) 100 o = receipt(tx_hash) 101 r = self.rpc.do(o) 102 self.assertEqual(r['status'], 0) 103 104 c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 105 (tx_hash, o) = c.approve(vended_token_address, self.alice, self.vend_address, vend_amount) 106 self.rpc.do(o) 107 108 c = Vend(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 109 (tx_hash, o) = c.withdraw(self.vend_address, self.alice, vended_token_address) 110 self.rpc.do(o) 111 o = receipt(tx_hash) 112 r = self.rpc.do(o) 113 self.assertEqual(r['status'], 1) 114 115 c = ERC20(self.chain_spec) 116 o = c.balance_of(vended_token_address, self.alice, sender_address=self.accounts[0]) 117 r = self.rpc.do(o) 118 balance = c.parse_balance(r) 119 self.assertEqual(balance, 0) 120 121 o = c.balance_of(self.token_address, self.alice, sender_address=self.accounts[0]) 122 r = self.rpc.do(o) 123 balance = c.parse_balance(r) 124 self.assertEqual(balance, src_amount) 125 126 127 128 if __name__ == '__main__': 129 unittest.main()