fix: Ensure domains are not expired in renewal batch

This commit is contained in:
2025-11-26 17:42:36 +11:00
parent e84186831c
commit 61a050af72

View File

@@ -1,81 +1,85 @@
import account import account
import requests import requests
import logging
logger = logging.getLogger()
# Plugin Data # Plugin Data
info = { info = {
"name": "Batch Renew Domains", "name": "Batch Renew Domains",
"description": "Renew the next 100 domains", "description": "Renew the next 100 domains",
"version": "1.0", "version": "1.0",
"author": "Nathan.Woodburn/" "author": "Nathan.Woodburn/",
} }
# Functions # Functions
functions = { functions = {
"main":{ "main": {
"name": "Renew", "name": "Renew",
"type": "default", "type": "default",
"description": "Renew the next 100 domains in one transaction. Please wait for at least 1 block confirmation before renewing the next 100 domains", "description": "Renew the next 100 domains in one transaction. Please wait for at least 1 block confirmation before renewing the next 100 domains",
"params": {}, "params": {},
"returns": { "returns": {
"status": "status": {"name": "Status of the function", "type": "text"},
{ "transaction": {"name": "Transaction ID", "type": "tx"},
"name": "Status of the function", },
"type": "text"
},
"transaction":
{
"name": "Transaction ID",
"type": "tx"
}
}
} }
} }
def main(params, authentication): def main(params, authentication):
password = authentication.split(":")[1] password = authentication.split(":")[1]
wallet = authentication.split(":")[0] wallet = authentication.split(":")[0]
domains = account.getDomains(wallet) domains = account.getDomains(wallet)
domains = sorted(domains, key=lambda k: k['renewal']) domains = sorted(domains, key=lambda k: k["renewal"])
# Remove any domains that are expired or don't have an expiry date
names = [] names = []
for domain in domains: for domain in domains:
if "stats" not in domain or "blocksUntilExpire" not in domain["stats"]:
continue
if domain["stats"]["blocksUntilExpire"] <= 0:
continue
name = domain["name"] name = domain["name"]
names.append(name) names.append(name)
logger.info(f"Renewing {len(names)} domains")
# Split names into batches of 100 # Split names into batches of 100
batches = [] batches = []
for i in range(0, len(names), 100): for i in range(0, len(names), 100):
batches.append(names[i:i+100]) batches.append(names[i : i + 100])
# Unlock wallet # Unlock wallet
KEY = account.HSD_API KEY = account.HSD_API
IP = account.HSD_IP IP = account.HSD_IP
PORT = account.HSD_WALLET_PORT PORT = account.HSD_WALLET_PORT
response = requests.post(f'http://x:{KEY}@{IP}:{PORT}/wallet/{wallet}/unlock', response = requests.post(
json={'passphrase': password, 'timeout': 600}) f"http://x:{KEY}@{IP}:{PORT}/wallet/{wallet}/unlock",
json={"passphrase": password, "timeout": 600},
)
if response.status_code != 200: if response.status_code != 200:
print("Failed to unlock wallet") print("Failed to unlock wallet")
print(f'Status code: {response.status_code}') print(f"Status code: {response.status_code}")
print(f'Response: {response.text}') print(f"Response: {response.text}")
return {"status": "Failed unlocking wallet", "transaction": "None"} return {"status": "Failed unlocking wallet", "transaction": "None"}
tx = "None" tx = "None"
for batch in batches: for batch in batches:
batch = [] batch = []
for domain in names: for domain in names:
batch.append(f'["RENEW", "{domain}"]') batch.append(f'["RENEW", "{domain}"]')
batchTX = "[" + ", ".join(batch) + "]" batchTX = "[" + ", ".join(batch) + "]"
responseContent = f'{{"method": "sendbatch","params":[ {batchTX} ]}}' responseContent = f'{{"method": "sendbatch","params":[ {batchTX} ]}}'
response = requests.post(f'http://x:{KEY}@{IP}:{PORT}', data=responseContent) response = requests.post(f"http://x:{KEY}@{IP}:{PORT}", data=responseContent)
if response.status_code != 200: if response.status_code != 200:
print("Failed to create batch",flush=True) print("Failed to create batch", flush=True)
print(f'Status code: {response.status_code}',flush=True) print(f"Status code: {response.status_code}", flush=True)
print(f'Response: {response.text}',flush=True) print(f"Response: {response.text}", flush=True)
return {"status": "Failed", "transaction": "None"} return {"status": "Failed", "transaction": "None"}
batch = response.json() batch = response.json()
@@ -83,13 +87,15 @@ def main(params, authentication):
print("Verifying tx...") print("Verifying tx...")
if batch["error"]: if batch["error"]:
if batch["error"] != "": if batch["error"] != "":
print("Failed to verify batch",flush=True) print("Failed to verify batch", flush=True)
print(batch["error"]["message"],flush=True) print(batch["error"]["message"], flush=True)
return {"status": f"Failed: {batch['error']['message']}", "transaction": "None"} return {
"status": f"Failed: {batch['error']['message']}",
if 'result' in batch: "transaction": "None",
if batch['result'] is not None: }
tx = batch['result']['hash']
if "result" in batch:
if batch["result"] is not None:
tx = batch["result"]["hash"]
return {"status": "Success", "transaction": tx} return {"status": "Success", "transaction": tx}
# Note only one batch can be sent at a time # Note only one batch can be sent at a time