Last active
December 12, 2020 00:56
-
-
Save Vyryn/ea4ae93818c87a57ef63f4056a21de01 to your computer and use it in GitHub Desktop.
Generate an NFT claimlink on the WAX blockchain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """Helpers for creating actions on atomictoolsx contract. By Vyryn, licenesed under AGPL.""" | |
| from aioeos import types | |
| contract = 'atomictoolsx' | |
| def announcelink(creator: str, key: str, asset_ids: [int], memo: str, authorization=[]) -> types.EosAction: | |
| return types.EosAction( | |
| account=contract, | |
| name='announcelink', | |
| authorization=authorization, | |
| data={ | |
| 'creator': creator, | |
| 'key': key, | |
| 'asset_ids': asset_ids, | |
| 'memo': memo | |
| } | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """Example code for creating a claimlink on the WAX NFT Blockchain. By Vyryn, licensed under AGPL. """ | |
| import asyncio | |
| import aiohttp | |
| from aioeos import EosAccount, EosJsonRpc, EosTransaction, EosKey | |
| from aioeos.contracts import eosio_token | |
| from aioeos.exceptions import EosAssertMessageException | |
| import atomictoolsx | |
| async def create_claimlink(asset_ids: [int], memo=None, wait_for_confirmation=True) -> (int, str): | |
| if not memo: | |
| memo = 'Reward claimlink generated by the NFT Tip Bot.' | |
| keypair = EosKey() | |
| priv_key = keypair.to_wif() | |
| key = keypair.to_public() | |
| try: | |
| block = await rpc.get_head_block() | |
| except IndexError: | |
| return -1, 'There was an error fetching the network head block.' | |
| transaction = EosTransaction( | |
| ref_block_num=block['block_num'] & 65535, | |
| ref_block_prefix=block['ref_block_prefix'], | |
| actions=[ | |
| atomictoolsx.announcelink( | |
| creator=wax_ac.name, | |
| key=key, | |
| asset_ids=asset_ids, | |
| memo=memo, | |
| authorization=[wax_ac.authorization('active')]), | |
| atomicassets.transfer( | |
| from_addr=wax_ac.name, | |
| to_addr='atomictoolsx', | |
| asset_ids=asset_ids, | |
| memo='link', | |
| authorization=[wax_ac.authorization('active')]) | |
| ] | |
| ) | |
| try: | |
| result = await rpc.sign_and_push_transaction(transaction, keys=[wax_ac.key]) | |
| except EosAssertMessageException as e: | |
| error = str(e).split("message: ")[1].split("', 'file'")[0] | |
| return -1, f'There was a contract assertion error: {error}.' | |
| tx_id = result['transaction_id'] | |
| log(result, 'INFO') | |
| confirmed = False | |
| while wait_for_confirmation and not confirmed: | |
| try: | |
| async with session.get(wax_history_api + tx_id) as resp: | |
| response = await resp.json() | |
| log(response, 'INFO') | |
| if response['executed']: | |
| confirmed = True | |
| else: | |
| log('Not yet executed, waiting...', 'DBUG') | |
| await asyncio.sleep(2) | |
| except NameError: | |
| async with aiohttp.ClientSession() as session_: | |
| async with session_.get(wax_history_api + tx_id) as resp: | |
| try: | |
| response = await resp.json() | |
| except aiohttp.ContentTypeError: | |
| response = {'executed': False} | |
| log('ContentTypeError attempting to decode a json fetch for wax transaction history in ' | |
| 'confirming a claimlink creation.', 'WARN') | |
| if response['executed']: | |
| confirmed = True | |
| await session_.close() | |
| else: | |
| log('Not yet executed, waiting...', 'DBUG') | |
| await asyncio.sleep(2) | |
| link_id = str(result).split("link_id': ")[1].split(',')[0] | |
| link = f'https://wax.atomichub.io/trading/link/{link_id}?key={priv_key}' | |
| return 0, link |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment