mirror of
https://github.com/chaintool-py/eth-erc20.git
synced 2026-03-23 22:00:48 +01:00
Compare commits
13 Commits
sohail/hei
...
v0.6.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
99e82dd3bf
|
||
|
|
04f9b5868b
|
||
|
|
d7fb3f232c
|
||
|
|
ea7c106fa4
|
||
|
|
273cf23f21
|
||
|
|
15b6b4fa46
|
||
|
|
db30d4aaf4
|
||
|
|
7ab3cd14f5
|
||
|
|
fc9fa18e1c
|
||
|
|
d218d93fba
|
||
|
|
7183e01b72
|
||
| 5e7e539bb9 | |||
|
|
a87fbb13bc
|
@@ -1,3 +1,18 @@
|
||||
* 0.6.1
|
||||
- Add explicit burn (reduces supply) to giftable token
|
||||
* 0.6.0
|
||||
- Add token expiry to giftable token
|
||||
* 0.5.6
|
||||
- Remove name and symbol lookup
|
||||
- Remove decimals lookup for raw output
|
||||
* 0.5.5
|
||||
- Implement chainlib-gen for giftable token
|
||||
* 0.5.4
|
||||
- Enable setting sender address on contract read calls
|
||||
* 0.5.3
|
||||
- Fix giftable token cli commands
|
||||
* 0.5.2
|
||||
- Add block height to balance call
|
||||
* 0.5.1
|
||||
- Change license to AGPL3 and copyright waived to public domain
|
||||
* 0.5.0
|
||||
|
||||
@@ -1 +1 @@
|
||||
include **/data/ERC20.json **/data/GiftableToken.json **/data/GiftableToken.bin *requirements.txt CHANGELOG LICENSE WAIVER WAIVER.asc
|
||||
include **/data/ERC20.json **/data/GiftableToken.json **/data/GiftableToken.bin *requirements.txt CHANGELOG LICENSE WAIVER WAIVER.asc **/data/.chainlib
|
||||
|
||||
@@ -9,12 +9,14 @@ from chainlib.eth.contract import (
|
||||
ABIContractType,
|
||||
abi_decode_single,
|
||||
)
|
||||
from chainlib.eth.jsonrpc import to_blockheight_param
|
||||
from chainlib.eth.error import RequestMismatchException
|
||||
from chainlib.eth.tx import (
|
||||
TxFactory,
|
||||
TxFormat,
|
||||
)
|
||||
from chainlib.jsonrpc import JSONRPCRequest
|
||||
from chainlib.block import BlockSpec
|
||||
from hexathon import (
|
||||
add_0x,
|
||||
strip_0x,
|
||||
@@ -26,7 +28,7 @@ logg = logging.getLogger()
|
||||
class ERC20(TxFactory):
|
||||
|
||||
|
||||
def balance_of(self, contract_address, address, sender_address=ZERO_ADDRESS, id_generator=None):
|
||||
def balance_of(self, contract_address, address, sender_address=ZERO_ADDRESS, id_generator=None, height=BlockSpec.LATEST):
|
||||
j = JSONRPCRequest(id_generator)
|
||||
o = j.template()
|
||||
o['method'] = 'eth_call'
|
||||
@@ -38,7 +40,8 @@ class ERC20(TxFactory):
|
||||
tx = self.template(sender_address, contract_address)
|
||||
tx = self.set_code(tx, data)
|
||||
o['params'].append(self.normalize(tx))
|
||||
o['params'].append('latest')
|
||||
height = to_blockheight_param(height)
|
||||
o['params'].append(height)
|
||||
o = j.finalize(o)
|
||||
return o
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ logg = logging.getLogger()
|
||||
|
||||
arg_flags = ArgFlag()
|
||||
arg = Arg(arg_flags)
|
||||
flags = arg_flags.STD_READ | arg_flags.EXEC
|
||||
flags = arg_flags.STD_READ | arg_flags.EXEC | arg_flags.SENDER
|
||||
|
||||
argparser = chainlib.eth.cli.ArgumentParser()
|
||||
argparser = process_args(argparser, arg, flags)
|
||||
@@ -87,34 +87,39 @@ logg.debug('settings loaded:\n{}'.format(settings))
|
||||
def main():
|
||||
token_address = settings.get('EXEC')
|
||||
conn = settings.get('CONN')
|
||||
sender_address = settings.get('SENDER_ADDRESS')
|
||||
g = ERC20(
|
||||
chain_spec=settings.get('CHAIN_SPEC'),
|
||||
gas_oracle=settings.get('GAS_ORACLE'),
|
||||
)
|
||||
|
||||
# determine decimals
|
||||
decimals_o = g.decimals(token_address)
|
||||
r = conn.do(decimals_o)
|
||||
decimals = int(strip_0x(r), 16)
|
||||
logg.info('decimals {}'.format(decimals))
|
||||
if not config.get('_RAW'):
|
||||
decimals_o = g.decimals(token_address, sender_address=sender_address)
|
||||
r = conn.do(decimals_o)
|
||||
decimals = int(strip_0x(r), 16)
|
||||
logg.info('decimals {}'.format(decimals))
|
||||
|
||||
name_o = g.name(token_address)
|
||||
r = conn.do(name_o)
|
||||
token_name = g.parse_name(r)
|
||||
logg.info('name {}'.format(token_name))
|
||||
|
||||
symbol_o = g.symbol(token_address)
|
||||
r = conn.do(symbol_o)
|
||||
token_symbol = g.parse_symbol(r)
|
||||
logg.info('symbol {}'.format(token_symbol))
|
||||
# name_o = g.name(token_address, sender_address=sender_address)
|
||||
# r = conn.do(name_o)
|
||||
# token_name = g.parse_name(r)
|
||||
# logg.info('name {}'.format(token_name))
|
||||
#
|
||||
# symbol_o = g.symbol(token_address, sender_address=sender_address)
|
||||
# r = conn.do(symbol_o)
|
||||
# token_symbol = g.parse_symbol(r)
|
||||
# logg.info('symbol {}'.format(token_symbol))
|
||||
|
||||
# get balance
|
||||
balance_o = g.balance(token_address, settings.get('RECIPIENT'))
|
||||
balance_o = g.balance(token_address, settings.get('RECIPIENT'), sender_address=sender_address)
|
||||
r = conn.do(balance_o)
|
||||
|
||||
hx = strip_0x(r)
|
||||
balance_value = int(hx, 16)
|
||||
logg.debug('balance {} = {} decimals {}'.format(even(hx), balance_value, decimals))
|
||||
if config.get('_RAW'):
|
||||
logg.debug('balance {} = {}'.format(even(hx), balance_value))
|
||||
else:
|
||||
logg.debug('balance {} = {} decimals {}'.format(even(hx), balance_value, decimals))
|
||||
|
||||
balance_str = str(balance_value)
|
||||
balance_len = len(balance_str)
|
||||
|
||||
@@ -74,7 +74,7 @@ def process_config_local(config, arg, args, flags):
|
||||
|
||||
arg_flags = ArgFlag()
|
||||
arg = Arg(arg_flags)
|
||||
flags = arg_flags.STD_READ | arg_flags.EXEC | arg_flags.TAB
|
||||
flags = arg_flags.STD_READ | arg_flags.EXEC | arg_flags.TAB | arg_flags.SENDER
|
||||
|
||||
argparser = chainlib.eth.cli.ArgumentParser()
|
||||
argparser = process_args(argparser, arg, flags)
|
||||
@@ -96,6 +96,7 @@ logg.debug('settings loaded:\n{}'.format(settings))
|
||||
def main():
|
||||
token_address = config.get('_CONTRACT')
|
||||
conn = settings.get('CONN')
|
||||
sender_address = settings.get('SENDER_ADDRESS')
|
||||
g = ERC20(
|
||||
chain_spec=settings.get('CHAIN_SPEC'),
|
||||
gas_oracle=settings.get('GAS_ORACLE'),
|
||||
@@ -104,7 +105,7 @@ def main():
|
||||
outkeys = config.get('_OUTARG')
|
||||
|
||||
if not outkeys or 'address' in outkeys:
|
||||
name_o = g.name(token_address)
|
||||
name_o = g.name(token_address, sender_address=sender_address)
|
||||
r = conn.do(name_o)
|
||||
token_name = g.parse_name(r)
|
||||
s = ''
|
||||
@@ -114,7 +115,7 @@ def main():
|
||||
print(s)
|
||||
|
||||
if not outkeys or 'symbol' in outkeys:
|
||||
symbol_o = g.symbol(token_address)
|
||||
symbol_o = g.symbol(token_address, sender_address=sender_address)
|
||||
r = conn.do(symbol_o)
|
||||
token_symbol = g.parse_symbol(r)
|
||||
s = ''
|
||||
@@ -124,7 +125,7 @@ def main():
|
||||
print(s)
|
||||
|
||||
if not outkeys or 'decimals' in outkeys:
|
||||
decimals_o = g.decimals(token_address)
|
||||
decimals_o = g.decimals(token_address, sender_address=sender_address)
|
||||
r = conn.do(decimals_o)
|
||||
decimals = int(strip_0x(r), 16)
|
||||
s = ''
|
||||
@@ -134,7 +135,7 @@ def main():
|
||||
print(s)
|
||||
|
||||
if not outkeys or 'supply' in outkeys:
|
||||
supply_o = g.total_supply(token_address)
|
||||
supply_o = g.total_supply(token_address, sender_address=sender_address)
|
||||
r = conn.do(supply_o)
|
||||
supply = int(strip_0x(r), 16)
|
||||
s = ''
|
||||
|
||||
@@ -1 +1,4 @@
|
||||
from .factory import GiftableToken
|
||||
from .factory import bytecode
|
||||
from .factory import create
|
||||
from .factory import args
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -12,28 +12,41 @@ from chainlib.eth.contract import (
|
||||
ABIContractEncoder,
|
||||
ABIContractType,
|
||||
)
|
||||
from chainlib.eth.constant import ZERO_ADDRESS
|
||||
from chainlib.jsonrpc import JSONRPCRequest
|
||||
from hexathon import add_0x
|
||||
|
||||
# local imports
|
||||
from giftable_erc20_token.data import data_dir
|
||||
from eth_erc20 import ERC20
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class GiftableToken(TxFactory):
|
||||
class GiftableToken(ERC20):
|
||||
|
||||
__abi = None
|
||||
__bytecode = None
|
||||
|
||||
def constructor(self, sender_address, name, symbol, decimals, tx_format=TxFormat.JSONRPC):
|
||||
code = GiftableToken.bytecode()
|
||||
def constructor(self, sender_address, name, symbol, decimals, expire=0, tx_format=TxFormat.JSONRPC, version=None):
|
||||
code = self.cargs(name, symbol, decimals, expire=expire)
|
||||
tx = self.template(sender_address, None, use_nonce=True)
|
||||
tx = self.set_code(tx, code)
|
||||
return self.finalize(tx, tx_format)
|
||||
|
||||
|
||||
@staticmethod
|
||||
def cargs(name, symbol, decimals, expire=0, version=None):
|
||||
if expire == None:
|
||||
expire = 0
|
||||
code = GiftableToken.bytecode(version=version)
|
||||
enc = ABIContractEncoder()
|
||||
enc.string(name)
|
||||
enc.string(symbol)
|
||||
enc.uint256(decimals)
|
||||
enc.uint256(expire)
|
||||
code += enc.get()
|
||||
tx = self.template(sender_address, None, use_nonce=True)
|
||||
tx = self.set_code(tx, code)
|
||||
return self.finalize(tx, tx_format)
|
||||
return code
|
||||
|
||||
|
||||
@staticmethod
|
||||
@@ -51,7 +64,7 @@ class GiftableToken(TxFactory):
|
||||
|
||||
|
||||
@staticmethod
|
||||
def bytecode():
|
||||
def bytecode(version=None):
|
||||
if GiftableToken.__bytecode == None:
|
||||
f = open(os.path.join(data_dir, 'GiftableToken.bin'))
|
||||
GiftableToken.__bytecode = f.read()
|
||||
@@ -97,3 +110,59 @@ class GiftableToken(TxFactory):
|
||||
return tx
|
||||
|
||||
|
||||
def burn(self, contract_address, sender_address, value, tx_format=TxFormat.JSONRPC):
|
||||
enc = ABIContractEncoder()
|
||||
enc.method('burn')
|
||||
enc.typ(ABIContractType.UINT256)
|
||||
enc.uint256(value)
|
||||
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 burned(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('burned')
|
||||
data = add_0x(enc.get())
|
||||
tx = self.template(sender_address, contract_address)
|
||||
tx = self.set_code(tx, data)
|
||||
o['params'].append(self.normalize(tx))
|
||||
o['params'].append('latest')
|
||||
o = j.finalize(o)
|
||||
return o
|
||||
|
||||
|
||||
def total_minted(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('totalMinted')
|
||||
data = add_0x(enc.get())
|
||||
tx = self.template(sender_address, contract_address)
|
||||
tx = self.set_code(tx, data)
|
||||
o['params'].append(self.normalize(tx))
|
||||
o['params'].append('latest')
|
||||
o = j.finalize(o)
|
||||
return o
|
||||
|
||||
|
||||
def bytecode(**kwargs):
|
||||
return GiftableToken.bytecode(version=kwargs.get('version'))
|
||||
|
||||
|
||||
def create(**kwargs):
|
||||
return GiftableToken.cargs(kwargs['name'], kwargs['symbol'], kwargs['decimals'], expire=kwargs.get('expire'), version=kwargs.get('version'))
|
||||
|
||||
|
||||
def args(v):
|
||||
if v == 'create':
|
||||
return (['name', 'symbol', 'decimals'], ['expire', 'version'],)
|
||||
elif v == 'default' or v == 'bytecode':
|
||||
return ([], ['version'],)
|
||||
raise ValueError('unknown command: ' + v)
|
||||
|
||||
@@ -25,62 +25,80 @@ from hexathon import (
|
||||
strip_0x,
|
||||
add_0x,
|
||||
)
|
||||
from chainlib.settings import ChainSettings
|
||||
from chainlib.eth.cli.log import process_log
|
||||
from chainlib.eth.settings import process_settings
|
||||
from chainlib.eth.cli.arg import (
|
||||
Arg,
|
||||
ArgFlag,
|
||||
process_args,
|
||||
)
|
||||
from chainlib.eth.cli.config import (
|
||||
Config,
|
||||
process_config,
|
||||
)
|
||||
|
||||
# local imports
|
||||
from giftable_erc20_token import GiftableToken
|
||||
|
||||
logging.basicConfig(level=logging.WARNING)
|
||||
logg = logging.getLogger()
|
||||
|
||||
arg_flags = chainlib.eth.cli.argflag_std_write | chainlib.eth.cli.Flag.EXEC | chainlib.eth.cli.Flag.WALLET
|
||||
argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
|
||||
argparser.add_positional('amount', type=int, help='Token amount to gift')
|
||||
|
||||
def process_config_local(config, arg, args, flags):
|
||||
config.add(config.get('_POSARG'), '_VALUE', False)
|
||||
return config
|
||||
|
||||
|
||||
arg_flags = ArgFlag()
|
||||
arg = Arg(arg_flags)
|
||||
flags = arg_flags.STD_WRITE | arg_flags.WALLET | arg_flags.EXEC
|
||||
|
||||
argparser = chainlib.eth.cli.ArgumentParser()
|
||||
argparser = process_args(argparser, arg, flags)
|
||||
argparser.add_argument('value', type=str, help='Token value to send')
|
||||
args = argparser.parse_args()
|
||||
extra_args = {
|
||||
'amount': None,
|
||||
}
|
||||
config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_fee_limit=GiftableToken.gas())
|
||||
|
||||
wallet = chainlib.eth.cli.Wallet()
|
||||
wallet.from_config(config)
|
||||
logg = process_log(args, logg)
|
||||
|
||||
rpc = chainlib.eth.cli.Rpc(wallet=wallet)
|
||||
conn = rpc.connect_by_config(config)
|
||||
config = Config()
|
||||
config = process_config(config, arg, args, flags, positional_name='value')
|
||||
config = process_config_local(config, arg, args, flags)
|
||||
logg.debug('config loaded:\n{}'.format(config))
|
||||
|
||||
chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
|
||||
settings = ChainSettings()
|
||||
settings = process_settings(settings, config)
|
||||
logg.debug('settings loaded:\n{}'.format(settings))
|
||||
|
||||
|
||||
def main():
|
||||
signer = rpc.get_signer()
|
||||
signer_address = rpc.get_sender_address()
|
||||
token_address = settings.get('EXEC')
|
||||
signer_address = settings.get('SENDER_ADDRESS')
|
||||
recipient = settings.get('RECIPIENT')
|
||||
value = settings.get('VALUE')
|
||||
conn = settings.get('CONN')
|
||||
|
||||
gas_oracle = rpc.get_gas_oracle()
|
||||
nonce_oracle = rpc.get_nonce_oracle()
|
||||
c = GiftableToken(
|
||||
settings.get('CHAIN_SPEC'),
|
||||
signer=settings.get('SIGNER'),
|
||||
gas_oracle=settings.get('GAS_ORACLE'),
|
||||
nonce_oracle=settings.get('NONCE_ORACLE'),
|
||||
)
|
||||
|
||||
recipient_address_input = config.get('_RECIPIENT')
|
||||
if recipient_address_input == None:
|
||||
recipient_address_input = signer_address
|
||||
|
||||
recipient_address = add_0x(to_checksum_address(recipient_address_input))
|
||||
if not config.true('_UNSAFE') and recipient_address != add_0x(recipient_address_input):
|
||||
raise ValueError('invalid checksum address for recipient')
|
||||
|
||||
token_address = add_0x(to_checksum_address(config.get('_EXEC_ADDRESS')))
|
||||
if not config.true('_UNSAFE') and token_address != add_0x(config.get('_EXEC_ADDRESS')):
|
||||
raise ValueError('invalid checksum address for contract')
|
||||
|
||||
token_value = config.get('_AMOUNT')
|
||||
c = GiftableToken(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
|
||||
(tx_hash_hex, o) = c.mint_to(token_address, signer_address, recipient_address, token_value)
|
||||
if config.get('_RPC_SEND'):
|
||||
(tx_hash_hex, o) = c.mint_to(
|
||||
token_address,
|
||||
signer_address,
|
||||
recipient,
|
||||
value,
|
||||
)
|
||||
if settings.get('RPC_SEND'):
|
||||
conn.do(o)
|
||||
if config.get('_WAIT'):
|
||||
if settings.get('WAIT'):
|
||||
r = conn.wait(tx_hash_hex)
|
||||
if r['status'] == 0:
|
||||
sys.stderr.write('EVM revert. Wish I had more to tell you')
|
||||
sys.exit(1)
|
||||
|
||||
logg.info('mint to {} tx {}'.format(recipient_address, tx_hash_hex))
|
||||
logg.info('mint to {} tx {}'.format(recipient, tx_hash_hex))
|
||||
|
||||
print(tx_hash_hex)
|
||||
else:
|
||||
|
||||
@@ -72,7 +72,9 @@ logg.debug('settings loaded:\n{}'.format(settings))
|
||||
|
||||
|
||||
def main():
|
||||
token_address = settings.get('EXEC')
|
||||
signer_address = settings.get('SENDER_ADDRESS')
|
||||
conn = settings.get('CONN')
|
||||
|
||||
recipient_address_input = settings.get('RECIPIENT')
|
||||
if recipient_address_input == None:
|
||||
|
||||
@@ -45,6 +45,7 @@ def process_config_local(config, arg, args, flags):
|
||||
config.add(args.token_name, '_TOKEN_NAME', False)
|
||||
config.add(args.token_symbol, '_TOKEN_SYMBOL', False)
|
||||
config.add(args.token_decimals, '_TOKEN_DECIMALS', False)
|
||||
config.add(args.token_expire, '_TOKEN_EXPIRE', False)
|
||||
return config
|
||||
|
||||
|
||||
@@ -57,6 +58,7 @@ argparser = process_args(argparser, arg, flags)
|
||||
argparser.add_argument('--name', dest='token_name', required=True, type=str, help='Token name')
|
||||
argparser.add_argument('--symbol', dest='token_symbol', required=True, type=str, help='Token symbol')
|
||||
argparser.add_argument('--decimals', dest='token_decimals', default=18, type=int, help='Token decimals')
|
||||
argparser.add_argument('--expire', dest='token_expire', default=0, type=int, help='Token expiry timestamp (after which token cannot be traded)')
|
||||
args = argparser.parse_args()
|
||||
|
||||
logg = process_log(args, logg)
|
||||
@@ -73,6 +75,7 @@ logg.debug('settings loaded:\n{}'.format(settings))
|
||||
|
||||
def main():
|
||||
signer_address = settings.get('SENDER_ADDRESS')
|
||||
conn = settings.get('CONN')
|
||||
|
||||
c = GiftableToken(
|
||||
settings.get('CHAIN_SPEC'),
|
||||
@@ -86,6 +89,7 @@ def main():
|
||||
config.get('_TOKEN_NAME'),
|
||||
config.get('_TOKEN_SYMBOL'),
|
||||
config.get('_TOKEN_DECIMALS'),
|
||||
expire=config.get('_TOKEN_EXPIRE'),
|
||||
)
|
||||
if settings.get('RPC_SEND'):
|
||||
conn.do(o)
|
||||
1
python/giftable_erc20_token/unittest/__init__.py
Normal file
1
python/giftable_erc20_token/unittest/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .base import *
|
||||
51
python/giftable_erc20_token/unittest/base.py
Normal file
51
python/giftable_erc20_token/unittest/base.py
Normal file
@@ -0,0 +1,51 @@
|
||||
# standard imports
|
||||
import logging
|
||||
import time
|
||||
|
||||
# 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.address import to_checksum_address
|
||||
|
||||
# local imports
|
||||
from giftable_erc20_token import GiftableToken
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TestGiftableToken(EthTesterCase):
|
||||
|
||||
expire = 0
|
||||
|
||||
def setUp(self):
|
||||
super(TestGiftableToken, self).setUp()
|
||||
self.conn = RPCConnection.connect(self.chain_spec, 'default')
|
||||
nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn)
|
||||
c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
self.symbol = 'FOO'
|
||||
self.name = 'Foo Token'
|
||||
self.decimals = 16
|
||||
(tx_hash, o) = c.constructor(self.accounts[0], self.name, self.symbol, self.decimals, expire=self.expire)
|
||||
self.rpc.do(o)
|
||||
o = receipt(tx_hash)
|
||||
r = self.rpc.do(o)
|
||||
self.assertEqual(r['status'], 1)
|
||||
self.address = to_checksum_address(r['contract_address'])
|
||||
logg.debug('published on address {} with hash {}'.format(self.address, tx_hash))
|
||||
|
||||
self.initial_supply = 1 << 40
|
||||
(tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[0], self.initial_supply)
|
||||
r = self.conn.do(o)
|
||||
o = receipt(tx_hash)
|
||||
r = self.conn.do(o)
|
||||
self.assertEqual(r['status'], 1)
|
||||
|
||||
|
||||
class TestGiftableExpireToken(TestGiftableToken):
|
||||
|
||||
expire = int(time.time()) + 100000
|
||||
|
||||
def setUp(self):
|
||||
super(TestGiftableExpireToken, self).setUp()
|
||||
@@ -1,3 +1,4 @@
|
||||
confini~=0.6.1
|
||||
chainlib-eth~=0.4.2
|
||||
chainlib-eth~=0.4.15
|
||||
chainlib~=0.4.8
|
||||
potaahto~=0.1.1
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#!/bin/bash
|
||||
|
||||
export PYTHONPATH=${PYTHONPATH}:.
|
||||
|
||||
set -e
|
||||
set -x
|
||||
for f in `ls tests/*.py`; do
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
[metadata]
|
||||
name = eth-erc20
|
||||
version = 0.5.1
|
||||
version = 0.6.1
|
||||
description = ERC20 interface and simple contract with deployment script that lets any address mint and gift itself tokens.
|
||||
author = Louis Holbrook
|
||||
author_email = dev@holbrook.no
|
||||
url = https://git.defalsify.org/eth-erc20.git
|
||||
url = https://git.defalsify.org/eth-erc20
|
||||
keywords =
|
||||
dlt
|
||||
ethereum
|
||||
@@ -20,7 +20,7 @@ classifiers =
|
||||
License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)
|
||||
Topic :: Internet
|
||||
#Topic :: Blockchain :: EVM
|
||||
license = OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)
|
||||
license = AGPLv3+
|
||||
licence_files =
|
||||
LICENSE
|
||||
|
||||
@@ -30,6 +30,7 @@ python_requires = >= 3.7
|
||||
packages =
|
||||
giftable_erc20_token
|
||||
giftable_erc20_token.runnable
|
||||
giftable_erc20_token.unittest
|
||||
giftable_erc20_token.data
|
||||
eth_erc20
|
||||
eth_erc20.data
|
||||
@@ -46,9 +47,8 @@ packages =
|
||||
|
||||
[options.entry_points]
|
||||
console_scripts =
|
||||
giftable-token-deploy = giftable_erc20_token.runnable.deploy:main
|
||||
giftable-token-publish = giftable_erc20_token.runnable.publish:main
|
||||
giftable-token-gift = giftable_erc20_token.runnable.gift:main
|
||||
giftable-token-minter = giftable_erc20_token.runnable.minter:main
|
||||
erc20-transfer = eth_erc20.runnable.transfer:main
|
||||
erc20-balance = eth_erc20.runnable.balance:main
|
||||
erc20-info = eth_erc20.runnable.info:main
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -15,38 +15,14 @@ from chainlib.eth.address import to_checksum_address
|
||||
from hexathon import strip_0x
|
||||
|
||||
# local imports
|
||||
from giftable_erc20_token import GiftableToken
|
||||
from giftable_erc20_token.unittest import TestGiftableToken
|
||||
from eth_erc20 import ERC20
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logg = logging.getLogger(__name__)
|
||||
logg = logging.getLogger()
|
||||
|
||||
|
||||
class TestToken(EthTesterCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestToken, self).setUp()
|
||||
self.conn = RPCConnection.connect(self.chain_spec, 'default')
|
||||
nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn)
|
||||
c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
self.symbol = 'FOO'
|
||||
self.name = 'Foo Token'
|
||||
self.decimals = 16
|
||||
(tx_hash, o) = c.constructor(self.accounts[0], self.name, self.symbol, self.decimals)
|
||||
r = self.conn.do(o)
|
||||
logg.debug('deployed with hash {}'.format(r))
|
||||
|
||||
o = receipt(r)
|
||||
r = self.conn.do(o)
|
||||
self.address = to_checksum_address(r['contract_address'])
|
||||
|
||||
self.initial_supply = 1 << 40
|
||||
(tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[0], self.initial_supply)
|
||||
r = self.conn.do(o)
|
||||
o = receipt(tx_hash)
|
||||
r = self.conn.do(o)
|
||||
self.assertEqual(r['status'], 1)
|
||||
|
||||
class TestToken(TestGiftableToken):
|
||||
|
||||
def test_balance(self):
|
||||
c = ERC20(self.chain_spec)
|
||||
|
||||
123
python/tests/test_giftable.py
Normal file
123
python/tests/test_giftable.py
Normal file
@@ -0,0 +1,123 @@
|
||||
# standard imports
|
||||
import os
|
||||
import unittest
|
||||
import json
|
||||
import logging
|
||||
import datetime
|
||||
|
||||
# external imports
|
||||
from chainlib.eth.constant import ZERO_ADDRESS
|
||||
from chainlib.eth.nonce import RPCNonceOracle
|
||||
from chainlib.eth.tx import receipt
|
||||
from chainlib.eth.block import (
|
||||
block_latest,
|
||||
block_by_number,
|
||||
)
|
||||
|
||||
# local imports
|
||||
from giftable_erc20_token import GiftableToken
|
||||
from giftable_erc20_token.unittest import TestGiftableExpireToken
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logg = logging.getLogger()
|
||||
|
||||
testdir = os.path.dirname(__file__)
|
||||
|
||||
|
||||
class TestExpire(TestGiftableExpireToken):
|
||||
|
||||
def setUp(self):
|
||||
super(TestExpire, self).setUp()
|
||||
|
||||
|
||||
def test_expires(self):
|
||||
mint_amount = self.initial_supply
|
||||
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
||||
c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
|
||||
(tx_hash, o) = c.transfer(self.address, self.accounts[0], self.accounts[1], int(mint_amount / 2))
|
||||
r = self.rpc.do(o)
|
||||
o = receipt(tx_hash)
|
||||
r = self.rpc.do(o)
|
||||
self.assertEqual(r['status'], 1)
|
||||
|
||||
self.backend.time_travel(self.expire + 60)
|
||||
o = block_latest()
|
||||
r = self.rpc.do(o)
|
||||
o = block_by_number(r)
|
||||
r = self.rpc.do(o)
|
||||
self.assertGreaterEqual(r['timestamp'], self.expire)
|
||||
|
||||
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
||||
c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
(tx_hash, o) = c.transfer(self.address, self.accounts[0], self.accounts[1], 1)
|
||||
r = self.rpc.do(o)
|
||||
o = receipt(tx_hash)
|
||||
r = self.rpc.do(o)
|
||||
self.assertEqual(r['status'], 0)
|
||||
|
||||
nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
|
||||
c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
(tx_hash, o) = c.transfer(self.address, self.accounts[1], self.accounts[0], 1)
|
||||
r = self.rpc.do(o)
|
||||
o = receipt(tx_hash)
|
||||
r = self.rpc.do(o)
|
||||
self.assertEqual(r['status'], 0)
|
||||
|
||||
o = c.balance_of(self.address, self.accounts[0], sender_address=self.accounts[0])
|
||||
r = self.rpc.do(o)
|
||||
balance = c.parse_balance(r)
|
||||
self.assertEqual(balance, int(mint_amount / 2))
|
||||
|
||||
o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0])
|
||||
r = self.rpc.do(o)
|
||||
balance += c.parse_balance(r)
|
||||
self.assertEqual(balance, mint_amount)
|
||||
|
||||
o = c.total_supply(self.address, sender_address=self.accounts[0])
|
||||
r = self.rpc.do(o)
|
||||
supply = c.parse_balance(r)
|
||||
self.assertEqual(supply, mint_amount)
|
||||
|
||||
|
||||
def test_burn(self):
|
||||
mint_amount = self.initial_supply
|
||||
nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
|
||||
c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
(tx_hash, o) = c.burn(self.address, self.accounts[1], int(mint_amount / 4))
|
||||
self.rpc.do(o)
|
||||
o = receipt(tx_hash)
|
||||
r = self.rpc.do(o)
|
||||
self.assertEqual(r['status'], 0)
|
||||
|
||||
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
||||
c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
(tx_hash, o) = c.burn(self.address, self.accounts[0], int(mint_amount / 4))
|
||||
self.rpc.do(o)
|
||||
o = receipt(tx_hash)
|
||||
r = self.rpc.do(o)
|
||||
self.assertEqual(r['status'], 1)
|
||||
|
||||
o = c.burned(self.address, sender_address=self.accounts[0])
|
||||
r = self.rpc.do(o)
|
||||
burned = c.parse_balance(r)
|
||||
self.assertEqual(burned, int(mint_amount / 4))
|
||||
|
||||
o = c.balance_of(self.address, self.accounts[0], sender_address=self.accounts[0])
|
||||
r = self.rpc.do(o)
|
||||
balance = c.parse_balance(r)
|
||||
self.assertEqual(balance, mint_amount - burned)
|
||||
|
||||
o = c.total_supply(self.address, sender_address=self.accounts[0])
|
||||
r = self.rpc.do(o)
|
||||
balance = c.parse_balance(r)
|
||||
self.assertEqual(balance, mint_amount - burned)
|
||||
|
||||
o = c.total_minted(self.address, sender_address=self.accounts[0])
|
||||
r = self.rpc.do(o)
|
||||
balance = c.parse_balance(r)
|
||||
self.assertEqual(balance, mint_amount)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -5,8 +5,11 @@ pragma solidity >=0.6.11;
|
||||
|
||||
contract GiftableToken {
|
||||
|
||||
// Implements EIP173
|
||||
address public owner;
|
||||
mapping(address => bool) minters;
|
||||
|
||||
// Implements Writer
|
||||
mapping(address => bool) writer;
|
||||
|
||||
// Implements ERC20
|
||||
string public name;
|
||||
@@ -15,54 +18,109 @@ contract GiftableToken {
|
||||
// Implements ERC20
|
||||
uint8 public decimals;
|
||||
// Implements ERC20
|
||||
uint256 public totalSupply;
|
||||
// Implements ERC20
|
||||
mapping (address => uint256) public balanceOf;
|
||||
// Implements ERC20
|
||||
mapping (address => mapping (address => uint256)) public allowance;
|
||||
|
||||
// Implements Burner
|
||||
uint256 public totalMinted;
|
||||
// Implements Burner
|
||||
uint256 public burned;
|
||||
|
||||
// Implements expire
|
||||
uint256 public expires;
|
||||
bool expired;
|
||||
|
||||
// Implements ERC20
|
||||
event Transfer(address indexed _from, address indexed _to, uint256 _value);
|
||||
// Implements ERC20
|
||||
event TransferFrom(address indexed _from, address indexed _to, address indexed _spender, uint256 _value);
|
||||
// Implements ERC20
|
||||
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
|
||||
|
||||
// Implements Minter
|
||||
event Mint(address indexed _minter, address indexed _beneficiary, uint256 _value);
|
||||
|
||||
constructor(string memory _name, string memory _symbol, uint8 _decimals) public {
|
||||
// Implement Expire
|
||||
event Expired(uint256 _timestamp);
|
||||
|
||||
// Implements Writer
|
||||
event WriterAdded(address _writer);
|
||||
// Implements Writer
|
||||
event WriterRemoved(address _writer);
|
||||
|
||||
// Implements Burner
|
||||
event Burn(uint256 _value);
|
||||
|
||||
constructor(string memory _name, string memory _symbol, uint8 _decimals, uint256 _expireTimestamp) public {
|
||||
owner = msg.sender;
|
||||
name = _name;
|
||||
symbol = _symbol;
|
||||
decimals = _decimals;
|
||||
minters[msg.sender] = true;
|
||||
expires = _expireTimestamp;
|
||||
}
|
||||
|
||||
// Implements ERC20
|
||||
function totalSupply() public returns (uint256) {
|
||||
return totalMinted - burned;
|
||||
}
|
||||
|
||||
// Implements Minter
|
||||
mapping(address => bool) writers;
|
||||
function mintTo(address _to, uint256 _value) public returns (bool) {
|
||||
require(minters[msg.sender]);
|
||||
require(writers[msg.sender] || msg.sender == owner);
|
||||
|
||||
balanceOf[_to] += _value;
|
||||
totalSupply += _value;
|
||||
totalMinted += _value;
|
||||
|
||||
emit Mint(msg.sender, _to, _value);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function addMinter(address _minter) public returns (bool) {
|
||||
// Implements Writer
|
||||
function addWriter(address _minter) public returns (bool) {
|
||||
require(msg.sender == owner);
|
||||
|
||||
minters[_minter] = true;
|
||||
writers[_minter] = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function removeMinter(address _minter) public returns (bool) {
|
||||
// Implements Writer
|
||||
function removeWriter(address _minter) public returns (bool) {
|
||||
require(msg.sender == owner || msg.sender == _minter);
|
||||
|
||||
minters[_minter] = false;
|
||||
writers[_minter] = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Implements Writer
|
||||
function isWriter(address _minter) public view returns(bool) {
|
||||
return writers[_minter] || _minter == owner;
|
||||
}
|
||||
|
||||
// Implements Expire
|
||||
function applyExpiry() public returns(uint8) {
|
||||
if (expires == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (expired) {
|
||||
return 1;
|
||||
}
|
||||
if (block.timestamp >= expires) {
|
||||
expired = true;
|
||||
emit Expired(block.timestamp);
|
||||
return 2;
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
// Implements ERC20
|
||||
function transfer(address _to, uint256 _value) public returns (bool) {
|
||||
require(applyExpiry() == 0);
|
||||
require(balanceOf[msg.sender] >= _value);
|
||||
balanceOf[msg.sender] -= _value;
|
||||
balanceOf[_to] += _value;
|
||||
@@ -70,8 +128,20 @@ contract GiftableToken {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Implements Burner
|
||||
function burn(uint256 _value) public returns (bool) {
|
||||
require(msg.sender == owner, 'ERR_ACCESS');
|
||||
require(balanceOf[msg.sender] >= _value, 'ERR_FUNDS');
|
||||
|
||||
balanceOf[msg.sender] -= _value;
|
||||
burned += _value;
|
||||
|
||||
emit Burn(_value);
|
||||
}
|
||||
|
||||
// Implements ERC20
|
||||
function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
|
||||
require(applyExpiry() == 0);
|
||||
require(allowance[_from][msg.sender] >= _value);
|
||||
require(balanceOf[_from] >= _value);
|
||||
allowance[_from][msg.sender] = allowance[_from][msg.sender] - _value;
|
||||
@@ -83,6 +153,7 @@ contract GiftableToken {
|
||||
|
||||
// Implements ERC20
|
||||
function approve(address _spender, uint256 _value) public returns (bool) {
|
||||
require(applyExpiry() == 0);
|
||||
if (_value > 0) {
|
||||
require(allowance[msg.sender][_spender] == 0);
|
||||
}
|
||||
@@ -111,6 +182,12 @@ contract GiftableToken {
|
||||
if (_sum == 0x9493f8b2) { // EIP173
|
||||
return true;
|
||||
}
|
||||
if (_sum == 0xabe1f1f5) { // Writer
|
||||
return true;
|
||||
}
|
||||
if (_sum == 0xfccc2e79) { // Burner
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user