Skip to content

Instantly share code, notes, and snippets.

@disassembler
Created February 20, 2019 17:38
Show Gist options
  • Save disassembler/50b7b2efdcce8b4681c3ef08c5ea9b4b to your computer and use it in GitHub Desktop.
Save disassembler/50b7b2efdcce8b4681c3ef08c5ea9b4b to your computer and use it in GitHub Desktop.
def defragmentWallet(self, wallet_id, account_index, addr_target_amount, final_addr_split=15):
print("Defragmenting wallet to {} addresses with amount {} Ada").format(final_addr_split, self.lovelaceToAda(addr_target_amount))
target_total = addr_target_amount * final_addr_split
start_time = datetime.now(pytz.utc)
wallet = self.getAccount(wallet_id, account_index)
self.defragmentWalletRecursive(wallet, target_total)
end_time = datetime.now(pytz.utc)
elapsed = end_time - start_time
print("Wallet defragmented to combined target amount size of {} Ada in {} seconds").format(self.lovelaceToAda(target_total), elapsed)
print("Splitting combined amount into {} addresses").format(final_addr_split)
wallet = self.getAccount(wallet_id, account_index)
self.splitAmountAddresses(wallet, wallet["amount"], final_addr_split, 2000000)
def defragmentWalletRecursive(self, wallet, target_addr_amount):
start_time = datetime.now(pytz.utc)
addr_distribution = self.getAddressDistribution(wallet)
if addr_distribution["total"] < target_addr_amount:
print("Total in account {} exceeds requested address amount of {}").format(addr_distribution["total"], target_addr_amount)
exit(1)
index = -1
print(addr_distribution)
if not addr_distribution["distribution"]:
print("distribution empty")
return
else:
last = addr_distribution["distribution"][index]
transfer_amount = 0
if addr_distribution["max"] >= target_addr_amount:
print("Wallet successfully defragmented to largest address of: {}").format(self.lovelaceToAda(addr_distribution["max"]))
return
for i in range(0,30):
transfer_amount = transfer_amount + last["amount"]
last["count"] = last["count"] - 1
if last["count"] < 1:
index = index - 1
last = addr_distribution["distribution"][index]
if transfer_amount > target_addr_amount:
transfer_amount = target_addr_amount
dest_address = self.createAddress(wallet["walletId"], wallet["index"])
dest_address_info = self.getAddressInfo(dest_address)
destinations = [{"amount": transfer_amount, "address": dest_address}]
estimated_transaction = self.estimateTransactionFees(wallet, destinations, False)
if "error" in estimated_transaction:
if "diagnostic" in estimated_transaction["response"] and "needMore" in estimated_transaction["response"]["diagnostic"]:
print("Stopping defragmentation: Transaction fees exceed amount we can defragment.")
return
else:
transaction = self.sendTransaction(wallet["walletId"], wallet["index"], destinations)
tx_id = transaction["data"]["id"]
tx_amount = transaction["data"]["amount"]
self.waitForConfirmation(wallet["walletId"], tx_id)
end_time = datetime.now(pytz.utc)
elapsed = end_time - start_time
print("New largest address amount of {} Ada created in new address {} in {} seconds").format(self.lovelaceToAda(transfer_amount), dest_address, elapsed)
self.defragmentWalletRecursive(wallet, target_addr_amount)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment