|
| 1 | +const cron = require('node-cron'); |
| 2 | +const algosdk = require('algosdk'); |
| 3 | + |
| 4 | +class AutoReclaimWorker { |
| 5 | + constructor() { |
| 6 | + this.contractId = 'CAEGD57WVTVQSYWYB23AISBW334QO7WNA5XQ56S45GH6BP3D2AVHKUG4'; |
| 7 | + this.algodClient = null; |
| 8 | + this.isRunning = false; |
| 9 | + } |
| 10 | + |
| 11 | + async initialize() { |
| 12 | + require('dotenv').config(); |
| 13 | + |
| 14 | + const algodToken = process.env.ALGOD_TOKEN || ''; |
| 15 | + const algodServer = process.env.ALGOD_SERVER || 'https://testnet-api.algonode.cloud'; |
| 16 | + const algodPort = parseInt(process.env.ALGOD_PORT) || 443; |
| 17 | + |
| 18 | + this.algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort); |
| 19 | + console.log('AutoReclaimWorker initialized'); |
| 20 | + } |
| 21 | + |
| 22 | + async checkExpiredLeases() { |
| 23 | + if (!this.algodClient) { |
| 24 | + throw new Error('Worker not initialized'); |
| 25 | + } |
| 26 | + |
| 27 | + try { |
| 28 | + const appInfo = await this.algodClient.getApplicationByID(parseInt(this.contractId)).do(); |
| 29 | + |
| 30 | + const globalState = appInfo.params['global-state'] || []; |
| 31 | + const leases = this.extractLeasesFromGlobalState(globalState); |
| 32 | + |
| 33 | + const expiredLeases = leases.filter(lease => |
| 34 | + lease.renter_balance && lease.renter_balance <= 0 |
| 35 | + ); |
| 36 | + |
| 37 | + console.log(`Found ${expiredLeases.length} expired leases out of ${leases.length} total leases`); |
| 38 | + |
| 39 | + return expiredLeases; |
| 40 | + } catch (error) { |
| 41 | + console.error('Error checking expired leases:', error); |
| 42 | + throw error; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + extractLeasesFromGlobalState(globalState) { |
| 47 | + const leases = []; |
| 48 | + |
| 49 | + globalState.forEach(state => { |
| 50 | + const key = Buffer.from(state.key, 'base64').toString('utf8'); |
| 51 | + |
| 52 | + if (key.startsWith('lease_')) { |
| 53 | + const leaseData = this.parseLeaseData(state.value); |
| 54 | + leases.push({ |
| 55 | + id: key.replace('lease_', ''), |
| 56 | + ...leaseData |
| 57 | + }); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + return leases.filter(lease => lease.renter_balance !== undefined); |
| 62 | + } |
| 63 | + |
| 64 | + parseLeaseData(value) { |
| 65 | + if (value.type === 1) { |
| 66 | + const intValue = parseInt(value.uint); |
| 67 | + return { renter_balance: intValue }; |
| 68 | + } |
| 69 | + |
| 70 | + if (value.type === 2) { |
| 71 | + const byteValue = Buffer.from(value.bytes, 'base64').toString('utf8'); |
| 72 | + try { |
| 73 | + return JSON.parse(byteValue); |
| 74 | + } catch { |
| 75 | + return { renter_balance: 0 }; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return { renter_balance: 0 }; |
| 80 | + } |
| 81 | + |
| 82 | + async executeReclaim(leaseId) { |
| 83 | + try { |
| 84 | + const senderAccount = this.getOwnerAccount(); |
| 85 | + |
| 86 | + const suggestedParams = await this.algodClient.getTransactionParams().do(); |
| 87 | + |
| 88 | + const appCallTxn = algosdk.makeApplicationCallTxnFromObject({ |
| 89 | + from: senderAccount.addr, |
| 90 | + appIndex: parseInt(this.contractId), |
| 91 | + appArgs: [new Uint8Array(Buffer.from('reclaim'))], |
| 92 | + foreignAssets: [], |
| 93 | + accounts: [], |
| 94 | + appForeignApps: [], |
| 95 | + suggestedParams, |
| 96 | + onComplete: algosdk.OnApplicationComplete.NoOpOC |
| 97 | + }); |
| 98 | + |
| 99 | + const signedTxn = appCallTxn.signTxn(senderAccount.sk); |
| 100 | + const txId = appCallTxn.txID().toString(); |
| 101 | + |
| 102 | + await this.algodClient.sendRawTransaction(signedTxn).do(); |
| 103 | + |
| 104 | + const confirmedTxn = await algosdk.waitForConfirmation(this.algodClient, txId, 4); |
| 105 | + |
| 106 | + console.log(`Successfully reclaimed lease ${leaseId}. Transaction ID: ${txId}`); |
| 107 | + return confirmedTxn; |
| 108 | + |
| 109 | + } catch (error) { |
| 110 | + console.error(`Failed to reclaim lease ${leaseId}:`, error); |
| 111 | + throw error; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + getOwnerAccount() { |
| 116 | + const ownerMnemonic = process.env.OWNER_MNEMONIC; |
| 117 | + if (!ownerMnemonic) { |
| 118 | + throw new Error('OWNER_MNEMONIC environment variable not set'); |
| 119 | + } |
| 120 | + |
| 121 | + return algosdk.mnemonicToSecretKey(ownerMnemonic); |
| 122 | + } |
| 123 | + |
| 124 | + async runReclaimCycle() { |
| 125 | + if (this.isRunning) { |
| 126 | + console.log('Reclaim cycle already running, skipping...'); |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + this.isRunning = true; |
| 131 | + |
| 132 | + try { |
| 133 | + console.log('Starting auto-reclaim cycle...'); |
| 134 | + |
| 135 | + const expiredLeases = await this.checkExpiredLeases(); |
| 136 | + |
| 137 | + for (const lease of expiredLeases) { |
| 138 | + console.log(`Reclaiming lease ${lease.id} with balance ${lease.renter_balance}`); |
| 139 | + await this.executeReclaim(lease.id); |
| 140 | + } |
| 141 | + |
| 142 | + console.log('Auto-reclaim cycle completed'); |
| 143 | + |
| 144 | + } catch (error) { |
| 145 | + console.error('Error in reclaim cycle:', error); |
| 146 | + } finally { |
| 147 | + this.isRunning = false; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + start() { |
| 152 | + console.log('Starting Auto-Reclaim Worker (every 10 minutes)...'); |
| 153 | + |
| 154 | + cron.schedule('*/10 * * * *', async () => { |
| 155 | + await this.runReclaimCycle(); |
| 156 | + }); |
| 157 | + |
| 158 | + setTimeout(() => this.runReclaimCycle(), 5000); |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +module.exports = AutoReclaimWorker; |
0 commit comments