test_lock.py (4091B)
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 ABIContractEncoder 7 from chainlib.eth.contract import ABIContractType 8 from chainlib.eth.tx import TxFormat 9 from chainlib.eth.tx import receipt 10 from eth_erc20 import ERC20 11 from giftable_erc20_token import GiftableToken 12 from hexathon import add_0x 13 14 # local imports 15 from erc20_vend.unittest import TestVendParams 16 from erc20_vend import Vend 17 18 19 logging.basicConfig(level=logging.DEBUG) 20 logg = logging.getLogger() 21 22 class TestVendLocked(TestVendParams): 23 24 def test_vend_token_lock(self): 25 nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn) 26 c = Vend(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 27 (tx_hash, o) = c.create(self.vend_address, self.accounts[0], 'foo vend', 'FOOVEND') 28 self.rpc.do(o) 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 vend_amount = 100 35 src_amount = vend_amount * (10 ** (self.token_decimals - 2)) 36 c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 37 (tx_hash, o) = c.mint_to(self.token_address, self.accounts[0], self.alice, src_amount) 38 self.rpc.do(o) 39 o = receipt(tx_hash) 40 r = self.rpc.do(o) 41 self.assertEqual(r['status'], 1) 42 43 nonce_oracle = RPCNonceOracle(self.alice, conn=self.conn) 44 c = Vend(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 45 (tx_hash, o) = c.mint_for(self.vend_address, self.alice, vended_token_address) 46 self.rpc.do(o) 47 o = receipt(tx_hash) 48 r = self.rpc.do(o) 49 self.assertEqual(r['status'], 0) 50 51 c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 52 (tx_hash, o) = c.approve(self.token_address, self.alice, self.vend_address, src_amount) 53 self.rpc.do(o) 54 o = receipt(tx_hash) 55 r = self.rpc.do(o) 56 self.assertEqual(r['status'], 1) 57 58 c = Vend(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 59 (tx_hash, o) = c.mint_for(self.vend_address, self.alice, vended_token_address) 60 self.rpc.do(o) 61 o = receipt(tx_hash) 62 r = self.rpc.do(o) 63 self.assertEqual(r['status'], 1) 64 65 c = ERC20(self.chain_spec) 66 o = c.balance_of(vended_token_address, self.alice, sender_address=self.accounts[0]) 67 r = self.rpc.do(o) 68 balance = c.parse_balance(r) 69 self.assertEqual(balance, vend_amount) 70 71 o = c.balance_of(self.token_address, self.alice, sender_address=self.accounts[0]) 72 r = self.rpc.do(o) 73 balance = c.parse_balance(r) 74 self.assertEqual(balance, 0) 75 76 c = Vend(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 77 (tx_hash, o) = c.withdraw_for(self.vend_address, self.alice, vended_token_address) 78 self.rpc.do(o) 79 o = receipt(tx_hash) 80 r = self.rpc.do(o) 81 self.assertEqual(r['status'], 0) 82 83 c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 84 (tx_hash, o) = c.approve(vended_token_address, self.alice, self.vend_address, vend_amount) 85 self.rpc.do(o) 86 87 c = Vend(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 88 (tx_hash, o) = c.withdraw_for(self.vend_address, self.alice, vended_token_address) 89 self.rpc.do(o) 90 o = receipt(tx_hash) 91 r = self.rpc.do(o) 92 self.assertEqual(r['status'], 1) 93 94 c = ERC20(self.chain_spec) 95 o = c.balance_of(vended_token_address, self.alice, sender_address=self.accounts[0]) 96 r = self.rpc.do(o) 97 balance = c.parse_balance(r) 98 self.assertEqual(balance, 0) 99 100 o = c.balance_of(self.token_address, self.alice, sender_address=self.accounts[0]) 101 r = self.rpc.do(o) 102 balance = c.parse_balance(r) 103 self.assertEqual(balance, src_amount) 104 105 106 if __name__ == '__main__': 107 unittest.main()