async def nuls2_balance_getter(address, config=None): global DECIMALS if config is None: from aleph.web import app config = app['config'] server = get_server(config.nuls2.api_url.value) contract_address = get_server(config.nuls2.contract_address.value) chain_id = config.nuls2.chain_id.value if DECIMALS is None: response = await server.invokeView([chain_id, "decimals", "", []]) DECIMALS = int(response['result']) response = await server.invokeView([chain_id, "balanceOf", "", [address]]) return int(response['result'])/(10**DECIMALS)
async def rmain(config_file): with open(config_file, 'r') as stream: config = yaml.safe_load(stream) pri_key = bytes.fromhex(config['source_pkey']) privkey = PrivateKey(pri_key, raw=True) pub_key = privkey.pubkey.serialize() # address = await get_address(pub_key, config['network_id']) address = await get_address(pub_key, config['chain_id'], config['prefix']) print(address) server = get_server(config['api_server']) addresses = [] payload = json.load(open('./nuls1-aleph-holders-snapshot.json', 'r')) holders = {h['_id']: h['balance'] for h in payload['holders'][1:]} print(holders) for address, balance in holders.items(): print(address, nuls1to2(address, config['chain_id'], config['prefix']), balance / (10**10)) # for line in open('/home/jon/Documents/Aleph/snapshots/cached_unspent-testnet-20190527.json', 'r'): # val = json.loads(line) # if val['_id'].startswith('TT'): # unspent = val['unspent_value'] # if isinstance(unspent, dict): # unspent = int(unspent['$numberLong']) # if unspent >= 99500000: # addresses.append(val['_id']) # nutxo = None # for address in addresses[:50]: # nutxo = await nuls_packer([address], # method='transfer', gas_limit=20000, # gas_price=100, # utxo=nutxo) # print(address) distribution_list = [(nuls1to2(address, config['chain_id'], config['prefix']), balance) for address, balance in holders.items()] max_items = config.get('bulk_max_items') for i in range(math.ceil(len(distribution_list) / max_items)): step_items = distribution_list[max_items * i:max_items * (i + 1)] print([[i[0] for i in step_items], [str(int(i[1])) for i in step_items]]) txhash = await contract_call_packer( server, config['source_address'], config['contract_address'], 'bulkTransfer', [[i[0] for i in step_items], [str(int(i[1])) for i in step_items]], pri_key, remark=config['balances_remark'], chain_id=config['chain_id'], asset_id=config.get('asset_id', 1), gas_limit=len(step_items) * 30000) print("balances stage", i, len(step_items), "items", txhash)
async def rmain(config_file): with open(config_file, 'r') as stream: config = yaml.safe_load(stream) client = motor.motor_asyncio.AsyncIOMotorClient(config.get('mongodb_host', 'localhost'), config.get('mongodb_port', 27017)) db = client[config.get('mongodb_db', 'nuls2main')] to_distribute = await get_distribution_info(config['reward_address'], START_DATE, db) pprint(to_distribute) distributed = await get_sent_tokens(config['source_address'], config['contract_address'], db, remark=config['distribution_pre_active_remark']) pprint(distributed) # return to_distribute = { addr: value - distributed.get(addr, 0) for addr, value in to_distribute.items() } pprint(to_distribute) distribution_list = [ (address, value) for address, value in to_distribute.items() if value > (10**10) # distribute more than 1 aleph only. ] pri_key = bytes.fromhex(config['distribution_pkey']) # privkey = PrivateKey(pri_key, raw=True) # pub_key = privkey.pubkey.serialize() # address = await get_address(pub_key, config['chain_id'], config['prefix']) server = get_server(config['api_server']) pprint(distribution_list) # return # and the distribution. nonce = None max_items = config.get('bulk_max_items') if len(distribution_list): for i in range(math.ceil(len(distribution_list) / max_items)): step_items = distribution_list[max_items*i:max_items*(i+1)] nash = await contract_call_packer( server, config['distribution_address'], config['contract_address'], 'bulkTransferFrom', [[config['source_address'],], [i[0] for i in step_items], [str(int(i[1])) for i in step_items]], pri_key, nonce=nonce, remark=config['distribution_pre_active_remark'], chain_id=config['chain_id'], asset_id=config.get('asset_id', 1), gas_limit=len(step_items)*30000) nonce = nash[-16:] await asyncio.sleep(10) print("reward stage", i, len(step_items), "items")
async def nuls2_packer(config): server = get_server(config.nuls2.api_url.value) target_addr = config.nuls2.sync_address.value remark = config.nuls2.remark.value.encode("utf-8") pri_key = bytes.fromhex(config.nuls2.private_key.value) privkey = PrivateKey(pri_key) pub_key = privkey.public_key.format() chain_id = config.nuls2.chain_id.value address = get_address(pub_key, config.nuls2.chain_id.value) LOGGER.info("NULS2 Connector set up with address %s" % address) # utxo = await get_utxo(config, address) i = 0 nonce = await get_nonce(server, address, chain_id) while True: if (await pending_txs_count(chain=CHAIN_NAME)) or ( await pending_messages_count(source_chain=CHAIN_NAME)): await asyncio.sleep(30) continue if i >= 100: await asyncio.sleep(30) # wait three (!!) blocks nonce = await get_nonce(server, address, chain_id) # utxo = await get_utxo(config, address) i = 0 messages = [ message async for message in (await Message.get_unconfirmed_raw( limit=10000, for_chain=CHAIN_NAME)) ] if len(messages): content = await get_chaindata(messages) tx = await prepare_transfer_tx( address, [(target_addr, CHEAP_UNIT_FEE)], nonce, chain_id=chain_id, asset_id=1, raw_tx_data=content.encode("utf-8"), remark=remark, ) await tx.sign_tx(pri_key) tx_hex = (await tx.serialize(update_data=False)).hex() ret = await broadcast(server, tx_hex, chain_id=chain_id) LOGGER.info("Broadcasted %r on %s" % (ret["hash"], CHAIN_NAME)) nonce = ret["hash"][-16:] await asyncio.sleep(config.nuls2.commit_delay.value) i += 1
async def rmain(config_file, remark): with open(config_file, 'r') as stream: config = yaml.safe_load(stream) pri_key = bytes.fromhex(config['distribution_pkey']) # address = await get_address(pub_key, config['chain_id'], config['prefix']) # pri_key = bytes.fromhex(config['source_pkey']) privkey = PrivateKey(pri_key, raw=True) pub_key = privkey.pubkey.serialize() address = await get_address(pub_key, config['chain_id'], config['prefix']) server = get_server(config['api_server']) distribution_list = [] content = await fetch_aggregate(config['aleph_balances_oracle'], 'contract_'+config['old_contract_address'] , api_server=config['aleph_api_server']) distribution_list = [ (address, amount) for address, amount in content['holders'].items() if address != config['old_source_address'] ] print(distribution_list) nonce = None # return # for address in addresses[:50]: # nutxo = await nuls_packer([address], # method='transfer', gas_limit=20000, # gas_price=100, # utxo=nutxo) # print(address) max_items = config.get('bulk_max_items') if len(distribution_list): for i in range(math.ceil(len(distribution_list) / max_items)): step_items = distribution_list[max_items*i:max_items*(i+1)] nash = await contract_call_packer( server, config['distribution_address'], config['contract_address'], 'batchTransferFrom', [[config['source_address'],], [i[0] for i in step_items], [str(int(i[1])) for i in step_items]], pri_key, nonce=nonce, remark=remark, chain_id=config['chain_id'], asset_id=config.get('asset_id', 1), gas_limit=len(step_items)*30000) nonce = nash[-16:] await asyncio.sleep(10) print("distribution stage", i, len(step_items), "items")
async def rmain(config_file, amount=1000000): with open(config_file, 'r') as stream: config = yaml.safe_load(stream) pri_key = bytes.fromhex(config['source_pkey']) privkey = PrivateKey(pri_key, raw=True) pub_key = privkey.pubkey.serialize() address = await get_address(pub_key, config['chain_id'], config['prefix']) server = get_server(config['api_server']) ret = await contract_call_packer(server, address, config['contract_address'], 'increaseApproval', [[ config['distribution_address'], ], [ str(amount * (10**10)), ]], pri_key, chain_id=config['chain_id'], asset_id=config.get('asset_id', 1)) print(ret)
async def rmain(config_file): with open(config_file, 'r') as stream: config = yaml.safe_load(stream) client = motor.motor_asyncio.AsyncIOMotorClient( config.get('mongodb_host', 'localhost'), config.get('mongodb_port', 27017)) db = client[config.get('mongodb_db', 'nuls2main')] replacements = config.get('replacements', dict()) fixed_rewards = config.get('fixed_rewards', dict()) to_refund, to_distribute = await get_distribution_info( config['reward_address'], START_DATE, db, replacements=replacements, fixed_rewards=fixed_rewards) pprint(to_refund) pprint(to_distribute) refunded = await get_sent_nuls(config['distribution_address'], db, remark=config['refund_remark']) pprint(refunded) # we add the old address refunds to the new one balance for addr, replaces in replacements.items(): if addr in refunded.keys(): refunded[replaces] = refunded.get(replaces, 0) + refunded[addr] pprint(refunded) to_refund = { addr: value - refunded.get(addr, 0) for addr, value in to_refund.items() if (value - refunded.get(addr, 0)) > 10000000 } pprint(to_refund) distributed_old_token = await get_sent_tokens( config['old_source_address'], config['old_contract_address'], db, remark=config['distribution_remark']) print("on old") pprint(distributed_old_token) distributed = await get_sent_tokens(config['source_address'], config['contract_address'], db, remark=config['distribution_remark']) print("on new") pprint(distributed) total_distributed = Counter(distributed_old_token) + Counter(distributed) print("total") pprint(total_distributed) # we add the old address distributions to the new one balance for addr, replaces in replacements.items(): if addr in distributed.keys(): total_distributed[replaces] = total_distributed.get( replaces, 0) + total_distributed[addr] to_distribute = { addr: value - total_distributed.get(addr, 0) for addr, value in to_distribute.items() } pprint(to_distribute) pri_key = bytes.fromhex(config['distribution_pkey']) # address = await get_address(pub_key, config['chain_id'], config['prefix']) # pri_key = bytes.fromhex(config['source_pkey']) privkey = PrivateKey(pri_key, raw=True) pub_key = privkey.pubkey.serialize() address = await get_address(pub_key, config['chain_id'], config['prefix']) server = get_server(config['api_server']) nonce = None # nutxo = None if len(to_refund): # now let's do the refund. # for refund in to_refund.items(): nash = await transfer_packer(server, config['distribution_address'], list(to_refund.items()), pri_key, nonce=nonce, remark=config['refund_remark'], chain_id=config['chain_id'], asset_id=config.get('asset_id', 1)) nonce = nash[-16:] await asyncio.sleep(10) print("refund issued for", to_refund) distribution_list = [ (address, value) for address, value in to_distribute.items() if value > (10**10) # distribute more than 1 aleph only. ] # return pprint(to_distribute.keys()) # return print([str(v) for v in to_distribute.values()]) # # and the distribution. max_items = config.get('bulk_max_items') if len(distribution_list): for i in range(math.ceil(len(distribution_list) / max_items)): step_items = distribution_list[max_items * i:max_items * (i + 1)] nash = await contract_call_packer( server, config['distribution_address'], config['contract_address'], 'batchTransferFrom', [[ config['source_address'], ], [i[0] for i in step_items], [str(int(i[1])) for i in step_items]], pri_key, nonce=nonce, remark=config['distribution_remark'], chain_id=config['chain_id'], asset_id=config.get('asset_id', 1), gas_limit=len(step_items) * 30000) nonce = nash[-16:] await asyncio.sleep(10) print("reward stage", i, len(step_items), "items")