mirror of
git://holbrook.no/eth-address-index
synced 2024-11-05 18:06:46 +01:00
Add interface, contract token symbol external call
This commit is contained in:
parent
a60e9383a5
commit
60a1c3402a
1
python/eth_token_endorser/__init__.py
Normal file
1
python/eth_token_endorser/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from .token import TokenEndorser
|
File diff suppressed because one or more lines are too long
@ -1 +1 @@
|
||||
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_token","type":"address"},{"indexed":true,"internalType":"address","name":"_adder","type":"address"},{"indexed":true,"internalType":"uint256","name":"_index","type":"uint256"},{"indexed":false,"internalType":"bytes32","name":"_data","type":"bytes32"}],"name":"EndorsementAdded","type":"event"},{"inputs":[{"internalType":"address","name":"_token","type":"address"},{"internalType":"bytes32","name":"_data","type":"bytes32"}],"name":"add","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"endorsement","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"endorsers","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"tokenIndex","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"tokens","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"}]
|
||||
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_token","type":"address"},{"indexed":true,"internalType":"address","name":"_adder","type":"address"},{"indexed":true,"internalType":"uint256","name":"_index","type":"uint256"},{"indexed":false,"internalType":"bytes32","name":"_data","type":"bytes32"}],"name":"EndorsementAdded","type":"event"},{"inputs":[{"internalType":"address","name":"_token","type":"address"},{"internalType":"bytes32","name":"_data","type":"bytes32"}],"name":"add","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"endorsement","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"endorser","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"endorserTokenCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"tokenIndex","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"","type":"string"}],"name":"tokenSymbolIndex","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"tokens","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"}]
|
||||
|
79
python/eth_token_endorser/token.py
Normal file
79
python/eth_token_endorser/token.py
Normal file
@ -0,0 +1,79 @@
|
||||
# Author: Louis Holbrook <dev@holbrook.no> 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
# File-version: 1
|
||||
# Description: Python interface to abi and bin files for faucet contracts
|
||||
|
||||
# standard imports
|
||||
import logging
|
||||
import json
|
||||
import os
|
||||
import hashlib
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
moddir = os.path.dirname(__file__)
|
||||
datadir = os.path.join(moddir, 'data')
|
||||
|
||||
|
||||
class TokenEndorser:
|
||||
|
||||
__abi = None
|
||||
__bytecode = None
|
||||
__address = None
|
||||
|
||||
def __init__(self, w3, address, signer_address=None):
|
||||
abi = TokenEndorser.abi()
|
||||
TokenEndorser.bytecode()
|
||||
self.__address = address
|
||||
self.contract = w3.eth.contract(abi=abi, address=address)
|
||||
self.w3 = w3
|
||||
if signer_address != None:
|
||||
self.signer_address = signer_address
|
||||
else:
|
||||
if type(self.w3.eth.defaultAccount).__name__ == 'Empty':
|
||||
self.w3.eth.defaultAccount = self.w3.eth.accounts[0]
|
||||
self.signer_address = self.w3.eth.defaultAccount
|
||||
|
||||
|
||||
@staticmethod
|
||||
def abi():
|
||||
if TokenEndorser.__abi == None:
|
||||
f = open(os.path.join(datadir, 'TokenEndorser.json'), 'r')
|
||||
TokenEndorser.__abi = json.load(f)
|
||||
f.close()
|
||||
return TokenEndorser.__abi
|
||||
|
||||
|
||||
@staticmethod
|
||||
def bytecode():
|
||||
if TokenEndorser.__bytecode == None:
|
||||
f = open(os.path.join(datadir, 'TokenEndorser.bin'))
|
||||
TokenEndorser.__bytecode = f.read()
|
||||
f.close()
|
||||
return TokenEndorser.__bytecode
|
||||
|
||||
|
||||
def token_from_symbol(self, symbol):
|
||||
return self.contract.functions.tokenSymbolIndex(symbol).call()
|
||||
|
||||
|
||||
def endorsed_tokens(self, endorser_address):
|
||||
tokens = []
|
||||
for i in range(self.contract.functions.endorserTokenCount(endorser_address).call()):
|
||||
tidx = self.contract.functions.endorser(endorser_address, i).call()
|
||||
t = self.contract.functions.tokens(tidx).call()
|
||||
tokens.append(t)
|
||||
return tokens
|
||||
|
||||
|
||||
def add(self, token_address, data):
|
||||
self.contract.functions.add(token_address, data).transact({'from': self.signer_address})
|
||||
|
||||
|
||||
def to_endorsement_key(token_address_hex, endorser_address_hex):
|
||||
h = hashlib.new('sha256')
|
||||
h.update(bytes.fromhex(token_address_hex[2:]))
|
||||
h.update(bytes.fromhex(endorser_address_hex[2:]))
|
||||
return h.digest()
|
||||
|
||||
|
@ -89,8 +89,8 @@ class Test(unittest.TestCase):
|
||||
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):
|
||||
|
||||
def test_endorsement(self):
|
||||
c = self.w3.eth.contract(abi=self.abi, address=self.address)
|
||||
|
||||
d = '0x' + os.urandom(32).hex()
|
||||
@ -105,7 +105,6 @@ class Test(unittest.TestCase):
|
||||
|
||||
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
|
||||
@ -127,6 +126,8 @@ class Test(unittest.TestCase):
|
||||
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]})
|
||||
|
||||
self.assertEqual(c.functions.tokenSymbolIndex('FOO').call(), self.address_token_one);
|
||||
self.assertEqual(c.functions.tokenSymbolIndex('BAR').call(), self.address_token_two);
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
105
python/tests/test_inteface.py
Normal file
105
python/tests/test_inteface.py
Normal file
@ -0,0 +1,105 @@
|
||||
import os
|
||||
import unittest
|
||||
import json
|
||||
import logging
|
||||
|
||||
import web3
|
||||
import eth_tester
|
||||
import eth_abi
|
||||
|
||||
from eth_token_endorser import TokenEndorser
|
||||
|
||||
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
|
||||
|
||||
|
||||
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_two, d).transact({'from': self.w3.eth.accounts[0]})
|
||||
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
|
||||
def test_interface(self):
|
||||
i = TokenEndorser(self.w3, self.address) #, self.w3.eth.accounts[1])
|
||||
self.assertEqual(i.token_from_symbol('FOO'), self.address_token_one)
|
||||
|
||||
|
||||
def test_endorsed_tokens(self):
|
||||
i = TokenEndorser(self.w3, self.address) #, self.w3.eth.accounts[1])
|
||||
t = i.endorsed_tokens(self.w3.eth.accounts[0])
|
||||
self.assertEqual(t[0], self.address_token_one)
|
||||
self.assertEqual(t[1], self.address_token_two)
|
||||
|
||||
|
||||
def test_add(self):
|
||||
i = TokenEndorser(self.w3, self.address, self.w3.eth.accounts[1])
|
||||
d = '0x' + os.urandom(32).hex()
|
||||
i.add(self.address_token_one, d)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
File diff suppressed because one or more lines are too long
@ -0,0 +1 @@
|
||||
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_token","type":"address"},{"indexed":true,"internalType":"address","name":"_adder","type":"address"},{"indexed":true,"internalType":"uint256","name":"_index","type":"uint256"},{"indexed":false,"internalType":"bytes32","name":"_data","type":"bytes32"}],"name":"EndorsementAdded","type":"event"},{"inputs":[{"internalType":"address","name":"_token","type":"address"},{"internalType":"bytes32","name":"_data","type":"bytes32"}],"name":"add","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"endorsement","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"endorser","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"endorserTokenCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"tokenIndex","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"","type":"string"}],"name":"tokenSymbolIndex","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"tokens","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"}]
|
@ -7,7 +7,8 @@ contract TokenEndorsement {
|
||||
uint256 count;
|
||||
mapping ( bytes32 => bytes32 ) public endorsement;
|
||||
mapping ( address => uint256 ) public tokenIndex;
|
||||
mapping ( address => uint256[] ) public endorsers;
|
||||
mapping ( address => uint256[] ) public endorser;
|
||||
mapping ( address => uint256 ) public endorserTokenCount;
|
||||
mapping ( string => address ) public tokenSymbolIndex;
|
||||
address[] public tokens;
|
||||
|
||||
@ -22,8 +23,11 @@ contract TokenEndorsement {
|
||||
if (tokenIndex[_token] > 0) {
|
||||
return false;
|
||||
}
|
||||
string memory token_symbol = _token.call(abi.encodeWithSignature('symbol()'));
|
||||
require(tokenSymbolIndex[token_symbol] = address(0));
|
||||
(bool _ok, bytes memory _r) = _token.call(abi.encodeWithSignature('symbol()'));
|
||||
require(_ok);
|
||||
|
||||
string memory token_symbol = abi.decode(_r, (string));
|
||||
require(tokenSymbolIndex[token_symbol] == address(0));
|
||||
|
||||
tokens.push(_token);
|
||||
tokenIndex[_token] = count;
|
||||
@ -47,7 +51,8 @@ contract TokenEndorsement {
|
||||
k = sha256(signMaterial);
|
||||
require(endorsement[k] == bytes32(0x00));
|
||||
endorsement[k] = _data;
|
||||
endorsers[msg.sender].push(tokenIndex[_token]);
|
||||
endorser[msg.sender].push(tokenIndex[_token]);
|
||||
endorserTokenCount[msg.sender]++;
|
||||
emit EndorsementAdded(_token, msg.sender, count, _data);
|
||||
return true;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user