mirror of
git://holbrook.no/eth-address-index
synced 2024-11-16 14:36:46 +01:00
Move test to python unittest
This commit is contained in:
parent
cd4f4a7131
commit
ba2c5ad5a3
1
python/eth_token_endorser/data/GiftableToken.bin
Normal file
1
python/eth_token_endorser/data/GiftableToken.bin
Normal file
File diff suppressed because one or more lines are too long
1
python/eth_token_endorser/data/GiftableToken.json
Normal file
1
python/eth_token_endorser/data/GiftableToken.json
Normal file
@ -0,0 +1 @@
|
||||
[{"inputs":[{"internalType":"string","name":"_name","type":"string"},{"internalType":"string","name":"_symbol","type":"string"},{"internalType":"uint8","name":"_decimals","type":"uint8"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_owner","type":"address"},{"indexed":true,"internalType":"address","name":"_spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_from","type":"address"},{"indexed":true,"internalType":"address","name":"_to","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_from","type":"address"},{"indexed":true,"internalType":"address","name":"_to","type":"address"},{"indexed":true,"internalType":"address","name":"_spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"TransferFrom","type":"event"},{"inputs":[{"internalType":"address","name":"_minter","type":"address"}],"name":"addMinter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"allowances","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"mint","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_minter","type":"address"}],"name":"removeMinter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_from","type":"address"},{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]
|
133
python/tests/test_deploy.py
Normal file
133
python/tests/test_deploy.py
Normal file
@ -0,0 +1,133 @@
|
||||
import os
|
||||
import unittest
|
||||
import json
|
||||
import logging
|
||||
import hashlib
|
||||
|
||||
import web3
|
||||
import eth_tester
|
||||
import eth_abi
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logg = logging.getLogger()
|
||||
|
||||
logging.getLogger('web3').setLevel(logging.WARNING)
|
||||
logging.getLogger('eth.vm').setLevel(logging.WARNING)
|
||||
|
||||
testdir = os.path.dirname(__file__)
|
||||
|
||||
|
||||
class Test(unittest.TestCase):
|
||||
|
||||
contract = None
|
||||
|
||||
def setUp(self):
|
||||
eth_params = eth_tester.backends.pyevm.main.get_default_genesis_params({
|
||||
'gas_limit': 9000000,
|
||||
})
|
||||
|
||||
# create store of used accounts
|
||||
f = open(os.path.join(testdir, '../eth_token_endorser/data/TokenEndorser.bin'), 'r')
|
||||
bytecode = f.read()
|
||||
f.close()
|
||||
|
||||
f = open(os.path.join(testdir, '../eth_token_endorser/data/TokenEndorser.json'), 'r')
|
||||
self.abi = json.load(f)
|
||||
f.close()
|
||||
|
||||
|
||||
backend = eth_tester.PyEVMBackend(eth_params)
|
||||
self.eth_tester = eth_tester.EthereumTester(backend)
|
||||
provider = web3.Web3.EthereumTesterProvider(self.eth_tester)
|
||||
self.w3 = web3.Web3(provider)
|
||||
c = self.w3.eth.contract(abi=self.abi, bytecode=bytecode)
|
||||
tx_hash = c.constructor().transact({'from': self.w3.eth.accounts[0]})
|
||||
|
||||
r = self.w3.eth.getTransactionReceipt(tx_hash)
|
||||
|
||||
self.address = r.contractAddress
|
||||
|
||||
|
||||
# create token
|
||||
f = open(os.path.join(testdir, '../eth_token_endorser/data/GiftableToken.bin'), 'r')
|
||||
bytecode = f.read()
|
||||
f.close()
|
||||
|
||||
f = open(os.path.join(testdir, '../eth_token_endorser/data/GiftableToken.json'), 'r')
|
||||
self.abi_token = json.load(f)
|
||||
f.close()
|
||||
|
||||
t = self.w3.eth.contract(abi=self.abi_token, bytecode=bytecode)
|
||||
tx_hash = t.constructor('Foo Token', 'FOO', 18).transact({'from': self.w3.eth.accounts[0]})
|
||||
|
||||
r = self.w3.eth.getTransactionReceipt(tx_hash)
|
||||
|
||||
self.address_token_one = r.contractAddress
|
||||
|
||||
t = self.w3.eth.contract(abi=self.abi_token, bytecode=bytecode)
|
||||
tx_hash = t.constructor('Bar Token', 'BAR', 18).transact({'from': self.w3.eth.accounts[0]})
|
||||
|
||||
r = self.w3.eth.getTransactionReceipt(tx_hash)
|
||||
|
||||
self.address_token_two = r.contractAddress
|
||||
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
|
||||
def test_basic(self):
|
||||
c = self.w3.eth.contract(abi=self.abi, address=self.address)
|
||||
|
||||
d = '0x' + os.urandom(32).hex()
|
||||
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[0]})
|
||||
|
||||
with self.assertRaises(Exception):
|
||||
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[0]})
|
||||
|
||||
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[1]})
|
||||
c.functions.add(self.address_token_two, d).transact({'from': self.w3.eth.accounts[0]})
|
||||
c.functions.add(self.address_token_two, d).transact({'from': self.w3.eth.accounts[1]})
|
||||
|
||||
def test_endorsement(self):
|
||||
|
||||
c = self.w3.eth.contract(abi=self.abi, address=self.address)
|
||||
|
||||
d = '0x' + os.urandom(32).hex()
|
||||
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[0]})
|
||||
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[1]})
|
||||
|
||||
|
||||
h = hashlib.new('sha256')
|
||||
h.update(bytes.fromhex(self.address_token_one[2:]))
|
||||
h.update(bytes.fromhex(self.w3.eth.accounts[0][2:]))
|
||||
z = h.digest()
|
||||
|
||||
assert d[2:] == c.functions.endorsement(z.hex()).call().hex()
|
||||
|
||||
#another_token_address = web3.Web3.toChecksumAddress('0x' + os.urandom(20).hex())
|
||||
c.functions.add(self.address_token_two, d).transact({'from': self.w3.eth.accounts[0]})
|
||||
|
||||
assert c.functions.endorsers(self.w3.eth.accounts[0], 0).call() == 1
|
||||
assert c.functions.endorsers(self.w3.eth.accounts[1], 0).call() == 1
|
||||
assert c.functions.endorsers(self.w3.eth.accounts[0], 1).call() == 2
|
||||
|
||||
assert c.functions.tokens(1).call() == self.address_token_one
|
||||
assert c.functions.tokens(2).call() == self.address_token_two
|
||||
|
||||
assert c.functions.tokenIndex(self.address_token_one).call() == 1
|
||||
assert c.functions.tokenIndex(self.address_token_two).call() == 2
|
||||
|
||||
|
||||
|
||||
def test_symbol_index(self):
|
||||
c = self.w3.eth.contract(abi=self.abi, address=self.address)
|
||||
d = '0x' + os.urandom(32).hex()
|
||||
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[0]})
|
||||
c.functions.add(self.address_token_one, d).transact({'from': self.w3.eth.accounts[1]})
|
||||
c.functions.add(self.address_token_two, d).transact({'from': self.w3.eth.accounts[1]})
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
Reference in New Issue
Block a user