2021-03-21 13:27:42 +01:00
|
|
|
# standard imports
|
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import hashlib
|
|
|
|
|
|
|
|
# external imports
|
|
|
|
from chainlib.eth.unittest.ethtester import EthTesterCase
|
|
|
|
from chainlib.eth.contract import (
|
|
|
|
ABIContractEncoder,
|
|
|
|
ABIContractType,
|
|
|
|
)
|
|
|
|
from chainlib.eth.nonce import RPCNonceOracle
|
|
|
|
from chainlib.eth.tx import receipt
|
|
|
|
from giftable_erc20_token import GiftableToken
|
2021-10-04 09:14:51 +02:00
|
|
|
from hexathon import (
|
|
|
|
add_0x,
|
|
|
|
strip_0x,
|
|
|
|
)
|
2021-03-21 13:27:42 +01:00
|
|
|
|
|
|
|
# local imports
|
2021-04-30 17:13:58 +02:00
|
|
|
from eth_address_declarator.declarator import AddressDeclarator
|
|
|
|
from eth_address_declarator import Declarator
|
2021-10-04 12:35:10 +02:00
|
|
|
from eth_address_declarator.unittest import TestAddressDeclaratorBase
|
2021-03-21 13:27:42 +01:00
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
|
|
|
testdir = os.path.dirname(__file__)
|
|
|
|
|
|
|
|
description = '0x{:<064s}'.format(b'foo'.hex())
|
|
|
|
|
2021-10-04 09:14:51 +02:00
|
|
|
|
2021-10-04 12:35:10 +02:00
|
|
|
class TestAddressDeclarator(TestAddressDeclaratorBase):
|
2021-03-21 13:27:42 +01:00
|
|
|
|
|
|
|
def setUp(self):
|
2021-10-04 09:14:51 +02:00
|
|
|
super(TestAddressDeclarator, self).setUp()
|
2021-03-21 13:27:42 +01:00
|
|
|
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
|
|
|
|
2021-10-04 09:14:51 +02:00
|
|
|
#c = GiftableToken(signer=self.signer, nonce_oracle=nonce_oracle, chain_id=self.chain_spec.chain_id())
|
2021-04-30 16:15:25 +02:00
|
|
|
c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
2021-03-21 13:27:42 +01:00
|
|
|
(tx_hash_hex, o) = c.constructor(self.accounts[0], 'BarToken', 'BAR', 6)
|
|
|
|
self.rpc.do(o)
|
|
|
|
|
|
|
|
o = receipt(tx_hash_hex)
|
|
|
|
r = self.rpc.do(o)
|
|
|
|
self.assertEqual(r['status'], 1)
|
|
|
|
|
|
|
|
self.bar_token_address = r['contract_address']
|
|
|
|
|
|
|
|
|
|
|
|
def test_basic(self):
|
|
|
|
|
|
|
|
d = add_0x(os.urandom(32).hex())
|
|
|
|
|
|
|
|
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
2021-04-30 17:13:58 +02:00
|
|
|
c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
2021-03-21 13:27:42 +01:00
|
|
|
(tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[0], self.foo_token_address, d)
|
|
|
|
self.rpc.do(o)
|
|
|
|
|
|
|
|
nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
|
2021-04-30 17:13:58 +02:00
|
|
|
c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
2021-03-21 13:27:42 +01:00
|
|
|
(tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d)
|
|
|
|
self.rpc.do(o)
|
|
|
|
|
|
|
|
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
2021-04-30 17:13:58 +02:00
|
|
|
c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
2021-03-21 13:27:42 +01:00
|
|
|
(tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[0], self.bar_token_address, d)
|
|
|
|
self.rpc.do(o)
|
|
|
|
|
|
|
|
o = c.declarator_count(self.address, self.foo_token_address, sender_address=self.accounts[0])
|
|
|
|
r = self.rpc.do(o)
|
|
|
|
self.assertEqual(c.parse_declarator_count(r), 2)
|
|
|
|
|
|
|
|
o = c.declarator_count(self.address, self.bar_token_address, sender_address=self.accounts[0])
|
|
|
|
r = self.rpc.do(o)
|
|
|
|
self.assertEqual(c.parse_declarator_count(r), 1)
|
|
|
|
|
|
|
|
|
2021-10-04 09:14:51 +02:00
|
|
|
def test_get_single_declaration(self):
|
|
|
|
d = add_0x(os.urandom(32).hex())
|
|
|
|
|
|
|
|
nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
|
|
|
|
c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
|
|
|
(tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d)
|
|
|
|
self.rpc.do(o)
|
|
|
|
o = receipt(tx_hash_hex)
|
|
|
|
r = self.rpc.do(o)
|
|
|
|
self.assertEqual(r['status'], 1)
|
|
|
|
|
|
|
|
o = c.declaration(self.address, self.accounts[1], self.foo_token_address, sender_address=self.accounts[0])
|
|
|
|
r = self.rpc.do(o)
|
|
|
|
proofs = c.parse_declaration(r)
|
|
|
|
self.assertEqual(proofs[0], strip_0x(d))
|
|
|
|
|
|
|
|
|
2021-03-21 13:27:42 +01:00
|
|
|
def test_declaration(self):
|
|
|
|
|
|
|
|
d = add_0x(os.urandom(32).hex())
|
|
|
|
d_two = add_0x(os.urandom(32).hex())
|
|
|
|
|
|
|
|
nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
|
2021-04-30 17:13:58 +02:00
|
|
|
c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
2021-03-21 13:27:42 +01:00
|
|
|
(tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d)
|
|
|
|
self.rpc.do(o)
|
|
|
|
|
2021-04-30 17:13:58 +02:00
|
|
|
c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
2021-03-21 13:27:42 +01:00
|
|
|
(tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d_two)
|
|
|
|
self.rpc.do(o)
|
|
|
|
|
|
|
|
nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc)
|
2021-04-30 17:13:58 +02:00
|
|
|
c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
2021-03-21 13:27:42 +01:00
|
|
|
(tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[2], self.foo_token_address, d)
|
|
|
|
self.rpc.do(o)
|
|
|
|
|
2021-04-30 17:13:58 +02:00
|
|
|
c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
2021-03-21 13:27:42 +01:00
|
|
|
(tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[2], self.bar_token_address, d)
|
|
|
|
self.rpc.do(o)
|
|
|
|
|
|
|
|
o = c.declaration(self.address, self.accounts[1], self.foo_token_address, sender_address=self.accounts[0])
|
|
|
|
r = self.rpc.do(o)
|
|
|
|
proofs = c.parse_declaration(r)
|
2021-10-04 09:14:51 +02:00
|
|
|
self.assertEqual(proofs[0], strip_0x(d))
|
|
|
|
self.assertEqual(proofs[1], strip_0x(d_two))
|
2021-03-21 13:27:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_declarator_to_subject(self):
|
|
|
|
d = add_0x(os.urandom(32).hex())
|
|
|
|
|
|
|
|
nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
|
2021-04-30 17:13:58 +02:00
|
|
|
c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
2021-03-21 13:27:42 +01:00
|
|
|
(tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d)
|
|
|
|
self.rpc.do(o)
|
|
|
|
|
|
|
|
nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc)
|
2021-04-30 17:13:58 +02:00
|
|
|
c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
2021-03-21 13:27:42 +01:00
|
|
|
(tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[2], self.foo_token_address, d)
|
|
|
|
self.rpc.do(o)
|
|
|
|
|
|
|
|
nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
|
2021-04-30 17:13:58 +02:00
|
|
|
c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
2021-03-21 13:27:42 +01:00
|
|
|
(tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.bar_token_address, d)
|
|
|
|
self.rpc.do(o)
|
|
|
|
|
|
|
|
o = c.declaration_address_at(self.address, self.accounts[1], 0, sender_address=self.accounts[0])
|
|
|
|
r = self.rpc.do(o)
|
2021-10-04 12:35:10 +02:00
|
|
|
self.assertEqual(c.parse_declaration_address_at(r), strip_0x(self.foo_token_address))
|
2021-03-21 13:27:42 +01:00
|
|
|
|
|
|
|
o = c.declaration_address_at(self.address, self.accounts[2], 0, sender_address=self.accounts[0])
|
|
|
|
r = self.rpc.do(o)
|
2021-10-04 12:35:10 +02:00
|
|
|
self.assertEqual(c.parse_declaration_address_at(r), strip_0x(self.foo_token_address))
|
2021-03-21 13:27:42 +01:00
|
|
|
|
|
|
|
o = c.declaration_address_at(self.address, self.accounts[1], 1, sender_address=self.accounts[0])
|
|
|
|
r = self.rpc.do(o)
|
2021-10-04 12:35:10 +02:00
|
|
|
self.assertEqual(c.parse_declaration_address_at(r), strip_0x(self.bar_token_address))
|
2021-03-21 13:27:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_subject_to_declarator(self):
|
|
|
|
d = '0x' + os.urandom(32).hex()
|
|
|
|
|
|
|
|
nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
|
2021-04-30 17:13:58 +02:00
|
|
|
c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
2021-03-21 13:27:42 +01:00
|
|
|
(tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.foo_token_address, d)
|
|
|
|
self.rpc.do(o)
|
|
|
|
|
|
|
|
nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc)
|
2021-04-30 17:13:58 +02:00
|
|
|
c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
2021-03-21 13:27:42 +01:00
|
|
|
(tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[2], self.foo_token_address, d)
|
|
|
|
self.rpc.do(o)
|
|
|
|
|
|
|
|
nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
|
2021-04-30 17:13:58 +02:00
|
|
|
c = Declarator(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
2021-03-21 13:27:42 +01:00
|
|
|
(tx_hash_hex, o) = c.add_declaration(self.address, self.accounts[1], self.bar_token_address, d)
|
|
|
|
self.rpc.do(o)
|
|
|
|
|
|
|
|
o = c.declarator_address_at(self.address, self.foo_token_address, 0, sender_address=self.accounts[0])
|
|
|
|
r = self.rpc.do(o)
|
2021-10-04 12:35:10 +02:00
|
|
|
self.assertEqual(c.parse_declaration_address_at(r), strip_0x(self.accounts[1]))
|
2021-03-21 13:27:42 +01:00
|
|
|
|
|
|
|
o = c.declarator_address_at(self.address, self.foo_token_address, 1, sender_address=self.accounts[0])
|
|
|
|
r = self.rpc.do(o)
|
2021-10-04 12:35:10 +02:00
|
|
|
self.assertEqual(c.parse_declaration_address_at(r), strip_0x(self.accounts[2]))
|
2021-03-21 13:27:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|