Add capped interface, python interface and test for capped, expire
This commit is contained in:
1
python/eth_expire/__init__.py
Normal file
1
python/eth_expire/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .expire import EthExpire
|
||||
37
python/eth_expire/expire.py
Normal file
37
python/eth_expire/expire.py
Normal file
@@ -0,0 +1,37 @@
|
||||
# external imports
|
||||
from chainlib.eth.tx import TxFormat
|
||||
from chainlib.eth.tx import TxFactory
|
||||
from chainlib.jsonrpc import JSONRPCRequest
|
||||
from chainlib.eth.constant import ZERO_ADDRESS
|
||||
from chainlib.eth.contract import ABIContractEncoder
|
||||
from chainlib.eth.contract import ABIContractType
|
||||
from chainlib.eth.contract import abi_decode_single
|
||||
from hexathon import add_0x
|
||||
|
||||
class EthExpire(TxFactory):
|
||||
|
||||
def apply_expiry(self, method, contract_address, sender_address, tx_format=TxFormat.JSONRPC):
|
||||
enc = ABIContractEncoder()
|
||||
enc.method('applyExpiry')
|
||||
data = enc.get()
|
||||
tx = self.template(sender_address, contract_address, use_nonce=True)
|
||||
tx = self.set_code(tx, data)
|
||||
tx = self.finalize(tx, tx_format)
|
||||
return tx
|
||||
|
||||
|
||||
def expires(self, contract_address, sender_address=ZERO_ADDRESS, id_generator=None):
|
||||
j = JSONRPCRequest(id_generator)
|
||||
o = j.template()
|
||||
o['method'] = 'eth_call'
|
||||
enc = ABIContractEncoder()
|
||||
enc.method('expires')
|
||||
data = add_0x(enc.get())
|
||||
tx = self.template(sender_address, contract_address)
|
||||
tx = self.set_code(tx, data)
|
||||
o['params'].append(self.normalize(tx))
|
||||
return o
|
||||
|
||||
|
||||
def parse_expires(self, v):
|
||||
return abi_decode_single(ABIContractType.UINT256, v)
|
||||
1
python/eth_expire/unittest/__init__.py
Normal file
1
python/eth_expire/unittest/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .interface import TestEthExpireInterface
|
||||
69
python/eth_expire/unittest/base.py
Normal file
69
python/eth_expire/unittest/base.py
Normal file
@@ -0,0 +1,69 @@
|
||||
# standard imports
|
||||
import os
|
||||
import logging
|
||||
import datetime
|
||||
|
||||
# external imports
|
||||
from chainlib.eth.unittest.ethtester import EthTesterCase
|
||||
from chainlib.connection import RPCConnection
|
||||
from chainlib.eth.nonce import RPCNonceOracle
|
||||
from chainlib.eth.tx import receipt
|
||||
from chainlib.eth.tx import TxFactory
|
||||
from chainlib.eth.tx import TxFormat
|
||||
from chainlib.eth.contract import ABIContractEncoder
|
||||
from chainlib.eth.contract import ABIContractType
|
||||
|
||||
# local imports
|
||||
from eth_capped import EthCapped
|
||||
|
||||
script_dir = os.path.dirname(os.path.realpath(__file__))
|
||||
contract_dir = os.path.join(script_dir, '..', '..', 'solidity')
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TestEthExpire(EthTesterCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestEthExpire, self).setUp()
|
||||
|
||||
self.set_method = None
|
||||
|
||||
self.conn = RPCConnection.connect(self.chain_spec, 'default')
|
||||
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
|
||||
|
||||
f = open(os.path.join(contract_dir, 'ExpireTest.bin'))
|
||||
code = f.read()
|
||||
f.close()
|
||||
|
||||
txf = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
tx = txf.template(self.accounts[0], None, use_nonce=True)
|
||||
|
||||
date_expire = datetime.datetime.utcnow() + datetime.timedelta(seconds=10000)
|
||||
self.expire = int(date_expire.timestamp())
|
||||
enc = ABIContractEncoder()
|
||||
enc.uint256(self.expire)
|
||||
args = enc.get()
|
||||
|
||||
tx = txf.set_code(tx, code + args)
|
||||
(tx_hash_hex, o) = txf.build(tx)
|
||||
self.conn.do(o)
|
||||
o = receipt(tx_hash_hex)
|
||||
r = self.conn.do(o)
|
||||
self.assertEqual(r['status'], 1)
|
||||
self.address = r['contract_address']
|
||||
logg.debug('published expire test contract with hash {}'.format(r))
|
||||
|
||||
|
||||
def set_expiry(self, contract_address, sender_address, v, tx_format=TxFormat.JSONRPC):
|
||||
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
|
||||
txf = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
enc = ABIContractEncoder()
|
||||
enc.method('setExpiry')
|
||||
enc.typ(ABIContractType.UINT256)
|
||||
enc.uint256(v)
|
||||
data = enc.get()
|
||||
tx = txf.template(sender_address, contract_address, use_nonce=True)
|
||||
tx = txf.set_code(tx, data)
|
||||
tx = txf.finalize(tx, tx_format)
|
||||
return tx
|
||||
37
python/eth_expire/unittest/interface.py
Normal file
37
python/eth_expire/unittest/interface.py
Normal file
@@ -0,0 +1,37 @@
|
||||
# external imports
|
||||
from chainlib.eth.tx import receipt
|
||||
|
||||
# local imports
|
||||
from eth_expire import EthExpire
|
||||
|
||||
|
||||
class TestEthExpireInterface:
|
||||
|
||||
def __init__(self):
|
||||
self.set_method = None
|
||||
|
||||
|
||||
def test_expire(self):
|
||||
if self.expire == 0:
|
||||
return
|
||||
c = EthExpire(self.chain_spec)
|
||||
o = c.expires(self.address, sender_address=self.accounts[0])
|
||||
r = self.rpc.do(o)
|
||||
self.assertEqual(self.expire, int(r, 16))
|
||||
|
||||
|
||||
def test_expire_change(self):
|
||||
if self.set_method == None:
|
||||
return
|
||||
|
||||
self.expire += 43200
|
||||
(tx_hash_hex, o) = self.set_method(self.address, self.accounts[0], self.expire)
|
||||
self.rpc.do(o)
|
||||
o = receipt(tx_hash_hex)
|
||||
r = self.conn.do(o)
|
||||
self.assertEqual(r['status'], 1)
|
||||
|
||||
c = EthExpire(self.chain_spec)
|
||||
o = c.expires(self.address, sender_address=self.accounts[0])
|
||||
r = self.rpc.do(o)
|
||||
self.assertEqual(self.expire, int(r, 16))
|
||||
Reference in New Issue
Block a user