BLD: working on marketplace integration

This commit is contained in:
Frederic Fortier
2018-02-01 20:43:48 -05:00
parent 56bd3db7a2
commit 37980f3580
3 changed files with 52 additions and 52 deletions
+49 -50
View File
@@ -37,7 +37,6 @@ if sys.version_info.major < 3:
else: else:
import urllib.request as urllib import urllib.request as urllib
log = logbook.Logger('Marketplace', level=LOG_LEVEL) log = logbook.Logger('Marketplace', level=LOG_LEVEL)
@@ -48,9 +47,9 @@ class Marketplace:
if self.addresses[0]['pubAddr'] == '': if self.addresses[0]['pubAddr'] == '':
raise MarketplacePubAddressEmpty( raise MarketplacePubAddressEmpty(
filename=os.path.join( filename=os.path.join(
get_marketplace_folder(), 'addresses.json') get_marketplace_folder(), 'addresses.json')
) )
self.default_account = self.addresses[0]['pubAddr'] self.default_account = self.addresses[0]['pubAddr']
self.web3 = Web3(HTTPProvider(ETH_REMOTE_NODE)) self.web3 = Web3(HTTPProvider(ETH_REMOTE_NODE))
@@ -58,7 +57,7 @@ class Marketplace:
contract_url = urllib.urlopen(MARKETPLACE_CONTRACT) contract_url = urllib.urlopen(MARKETPLACE_CONTRACT)
self.mkt_contract_address = Web3.toChecksumAddress( self.mkt_contract_address = Web3.toChecksumAddress(
contract_url.readline().strip()) contract_url.readline().strip())
abi_url = urllib.urlopen(MARKETPLACE_CONTRACT_ABI) abi_url = urllib.urlopen(MARKETPLACE_CONTRACT_ABI)
abi = json.load(abi_url) abi = json.load(abi_url)
@@ -71,7 +70,7 @@ class Marketplace:
contract_url = urllib.urlopen(ENIGMA_CONTRACT) contract_url = urllib.urlopen(ENIGMA_CONTRACT)
self.eng_contract_address = Web3.toChecksumAddress( self.eng_contract_address = Web3.toChecksumAddress(
contract_url.readline().strip()) contract_url.readline().strip())
abi_url = urllib.urlopen(ENIGMA_CONTRACT_ABI) abi_url = urllib.urlopen(ENIGMA_CONTRACT_ABI)
abi = json.load(abi_url) abi = json.load(abi_url)
@@ -118,10 +117,10 @@ class Marketplace:
'this transaction: [default: 0] ') or 0) 'this transaction: [default: 0] ') or 0)
if not (0 <= address_i < len(self.addresses)): if not (0 <= address_i < len(self.addresses)):
print('Please choose a number between 0 and {}\n'.format( print('Please choose a number between 0 and {}\n'.format(
len(self.addresses)-1)) len(self.addresses) - 1))
else: else:
address = Web3.toChecksumAddress( address = Web3.toChecksumAddress(
self.addresses[address_i]['pubAddr']) self.addresses[address_i]['pubAddr'])
break break
return address, address_i return address, address_i
@@ -136,14 +135,14 @@ class Marketplace:
'Gas Limit:\t\t{gas}\n' 'Gas Limit:\t\t{gas}\n'
'Nonce:\t\t\t{nonce}\n' 'Nonce:\t\t\t{nonce}\n'
'Data:\t\t\t{data}\n'.format( 'Data:\t\t\t{data}\n'.format(
_from=from_address, _from=from_address,
to=tx['to'], to=tx['to'],
value=tx['value'], value=tx['value'],
gas=tx['gas'], gas=tx['gas'],
nonce=tx['nonce'], nonce=tx['nonce'],
data=tx['data'], data=tx['data'],
) )
) )
signed_tx = input('Copy and Paste the "Signed Transaction" ' signed_tx = input('Copy and Paste the "Signed Transaction" '
'field here:\n') 'field here:\n')
@@ -198,7 +197,7 @@ class Marketplace:
address = self.choose_pubaddr()[0] address = self.choose_pubaddr()[0]
dataset_info = self.mkt_contract.functions.getDataSource( dataset_info = self.mkt_contract.functions.getDataSource(
bytes32(dataset)).call() bytes32(dataset)).call()
price = dataset_info[1] price = dataset_info[1]
@@ -209,11 +208,11 @@ class Marketplace:
'{} ENG... '.format(address, price), end='') '{} ENG... '.format(address, price), end='')
balance = self.web3.eth.call({ balance = self.web3.eth.call({
'from': address, 'from': address,
'to': self.eng_contract_address, 'to': self.eng_contract_address,
'data': '0x70a08231000000000000000000000000{}'.format( 'data': '0x70a08231000000000000000000000000{}'.format(
address[2:]) address[2:])
}) })
balance = int(balance[2:], 16) // 10 ** 8 balance = int(balance[2:], 16) // 10 ** 8
@@ -231,7 +230,7 @@ class Marketplace:
agree_pay = input('Please confirm that you agree to pay {} ENG ' agree_pay = input('Please confirm that you agree to pay {} ENG '
'for a monthly subscription to the dataset "{}" ' 'for a monthly subscription to the dataset "{}" '
'starting today. [default: Y] '.format( 'starting today. [default: Y] '.format(
price, dataset)) or 'y' price, dataset)) or 'y'
if agree_pay.lower() not in ('y', 'n'): if agree_pay.lower() not in ('y', 'n'):
print("Please answer Y or N.") print("Please answer Y or N.")
else: else:
@@ -249,10 +248,10 @@ class Marketplace:
'desired dataset'.format(price)) 'desired dataset'.format(price))
tx = self.eng_contract.functions.approve( tx = self.eng_contract.functions.approve(
self.mkt_contract_address, self.mkt_contract_address,
price, price,
).buildTransaction( ).buildTransaction(
{'nonce': self.web3.eth.getTransactionCount(address)}) {'nonce': self.web3.eth.getTransactionCount(address)})
if 'ropsten' in ETH_REMOTE_NODE: if 'ropsten' in ETH_REMOTE_NODE:
tx['gas'] = min(int(tx['gas'] * 1.5), 4700000) tx['gas'] = min(int(tx['gas'] * 1.5), 4700000)
@@ -261,7 +260,7 @@ class Marketplace:
try: try:
tx_hash = '0x{}'.format(bin_hex( tx_hash = '0x{}'.format(bin_hex(
self.web3.eth.sendRawTransaction(signed_tx))) self.web3.eth.sendRawTransaction(signed_tx)))
print('\nThis is the TxHash for this transaction: ' print('\nThis is the TxHash for this transaction: '
'{}'.format(tx_hash)) '{}'.format(tx_hash))
@@ -271,7 +270,7 @@ class Marketplace:
if 'ropsten' in ETH_REMOTE_NODE: if 'ropsten' in ETH_REMOTE_NODE:
etherscan = 'https://ropsten.etherscan.io/tx/{}'.format( etherscan = 'https://ropsten.etherscan.io/tx/{}'.format(
tx_hash) tx_hash)
else: else:
etherscan = 'https://etherscan.io/tx/{}'.format(tx_hash) etherscan = 'https://etherscan.io/tx/{}'.format(tx_hash)
@@ -297,9 +296,9 @@ class Marketplace:
'Now processing second transaction.') 'Now processing second transaction.')
tx = self.mkt_contract.functions.subscribe( tx = self.mkt_contract.functions.subscribe(
bytes32(dataset), bytes32(dataset),
).buildTransaction( ).buildTransaction(
{'nonce': self.web3.eth.getTransactionCount(address)}) {'nonce': self.web3.eth.getTransactionCount(address)})
if 'ropsten' in ETH_REMOTE_NODE: if 'ropsten' in ETH_REMOTE_NODE:
tx['gas'] = min(int(tx['gas'] * 1.5), 4700000) tx['gas'] = min(int(tx['gas'] * 1.5), 4700000)
@@ -308,7 +307,7 @@ class Marketplace:
try: try:
tx_hash = '0x{}'.format(bin_hex( tx_hash = '0x{}'.format(bin_hex(
self.web3.eth.sendRawTransaction(signed_tx))) self.web3.eth.sendRawTransaction(signed_tx)))
print('\nThis is the TxHash for this transaction: ' print('\nThis is the TxHash for this transaction: '
'{}'.format(tx_hash)) '{}'.format(tx_hash))
@@ -318,7 +317,7 @@ class Marketplace:
if 'ropsten' in ETH_REMOTE_NODE: if 'ropsten' in ETH_REMOTE_NODE:
etherscan = 'https://ropsten.etherscan.io/tx/{}'.format( etherscan = 'https://ropsten.etherscan.io/tx/{}'.format(
tx_hash) tx_hash)
else: else:
etherscan = 'https://etherscan.io/tx/{}'.format(tx_hash) etherscan = 'https://etherscan.io/tx/{}'.format(tx_hash)
@@ -346,7 +345,7 @@ class Marketplace:
'You can now ingest this dataset anytime during the ' 'You can now ingest this dataset anytime during the '
'next month by running the following command:\n' 'next month by running the following command:\n'
'catalyst marketplace ingest --dataset={}'.format( 'catalyst marketplace ingest --dataset={}'.format(
dataset, address, dataset)) dataset, address, dataset))
def ingest(self, dataset, data_frequency=None, start=None, def ingest(self, dataset, data_frequency=None, start=None,
end=None, force_download=False): end=None, force_download=False):
@@ -356,17 +355,17 @@ class Marketplace:
address, address_i = self.choose_pubaddr() address, address_i = self.choose_pubaddr()
check_sub = self.mkt_contract.functions.checkAddressSubscription( check_sub = self.mkt_contract.functions.checkAddressSubscription(
address, bytes32(dataset)).call() address, bytes32(dataset)).call()
if check_sub[0] != address or b32_str(check_sub[1]) != dataset: if check_sub[0] != address or b32_str(check_sub[1]) != dataset:
raise MarketplaceContractDataNoMatch( raise MarketplaceContractDataNoMatch(
params='address: {}, dataset: {}'.format( params='address: {}, dataset: {}'.format(
address, dataset)) address, dataset))
if not check_sub[5]: if not check_sub[5]:
raise MarketplaceSubscriptionExpired( raise MarketplaceSubscriptionExpired(
dataset=dataset, dataset=dataset,
date=check_sub[4]) date=check_sub[4])
if 'key' in self.addresses[address_i]: if 'key' in self.addresses[address_i]:
key = self.addresses[address_i]['key'] key = self.addresses[address_i]['key']
@@ -500,11 +499,11 @@ class Marketplace:
address = self.choose_pubaddr()[0] address = self.choose_pubaddr()[0]
tx = self.mkt_contract.functions.register( tx = self.mkt_contract.functions.register(
bytes32(dataset), bytes32(dataset),
price, price,
address, address,
).buildTransaction( ).buildTransaction(
{'nonce': self.web3.eth.getTransactionCount(address)}) {'nonce': self.web3.eth.getTransactionCount(address)})
if 'ropsten' in ETH_REMOTE_NODE and tx['gas'] > 4700000: if 'ropsten' in ETH_REMOTE_NODE and tx['gas'] > 4700000:
tx['gas'] = 4700000 tx['gas'] = 4700000
@@ -512,14 +511,14 @@ class Marketplace:
signed_tx = self.sign_transaction(address, tx) signed_tx = self.sign_transaction(address, tx)
tx_hash = '0x{}'.format(bin_hex( tx_hash = '0x{}'.format(bin_hex(
self.web3.eth.sendRawTransaction(signed_tx))) self.web3.eth.sendRawTransaction(signed_tx)))
print('\nThis is the TxHash for this transaction: {}'.format(tx_hash)) print('\nThis is the TxHash for this transaction: {}'.format(tx_hash))
def publish(self, dataset, datadir, watch): def publish(self, dataset, datadir, watch):
datasource = self.mkt_contract.functions.getDataSource( datasource = self.mkt_contract.functions.getDataSource(
bytes32(dataset)).call() bytes32(dataset)).call()
if not datasource[4]: if not datasource[4]:
raise MarketplaceDatasetNotFound(dataset=dataset) raise MarketplaceDatasetNotFound(dataset=dataset)
@@ -529,11 +528,11 @@ class Marketplace:
if not match: if not match:
raise MarketplaceNoAddressMatch( raise MarketplaceNoAddressMatch(
dataset=dataset, dataset=dataset,
address=datasource[0]) address=datasource[0])
print('Using address: {} to publish this dataset.'.format( print('Using address: {} to publish this dataset.'.format(
datasource[0])) datasource[0]))
if 'key' in match: if 'key' in match:
key = match['key'] key = match['key']
+1 -1
View File
@@ -1 +1 @@
web3==4.0.0b5 web3==4.0.0b7
+2 -1
View File
@@ -11,7 +11,7 @@ class TestMarketplace(WithLogger, ZiplineTestCase):
def test_register(self): def test_register(self):
marketplace = Marketplace() marketplace = Marketplace()
marketplace.register('GitHub') marketplace.register()
pass pass
def test_ingest(self): def test_ingest(self):
@@ -30,3 +30,4 @@ class TestMarketplace(WithLogger, ZiplineTestCase):
marketplace = Marketplace() marketplace = Marketplace()
marketplace.clean('marketcap') marketplace.clean('marketcap')
pass pass