|
| 1 | +// @ts-check |
| 2 | +import fetch from 'node-fetch'; |
| 3 | +import assert from 'assert'; |
| 4 | +const delay = (ms) => { |
| 5 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 6 | +}; |
| 7 | + |
| 8 | +const states = { |
| 9 | + liquidated: 'liquidated', |
| 10 | + liquidating: 'liquidating', |
| 11 | +}; |
| 12 | + |
| 13 | +const expectedLiquidating = { |
| 14 | + ids: [ |
| 15 | + 'published.vaultFactory.managers.manager0.vaults.vault10-liquidating', |
| 16 | + 'published.vaultFactory.managers.manager0.vaults.vault11-liquidating', |
| 17 | + 'published.vaultFactory.managers.manager0.vaults.vault12-liquidating', |
| 18 | + ], |
| 19 | + debts: ['100500000', '103515000', '105525000'], |
| 20 | + balance: ['15000000', '15000000', '15000000'], |
| 21 | + denom: 'ATOM', |
| 22 | +}; |
| 23 | + |
| 24 | +const expectedLiquidated = { |
| 25 | + ids: [ |
| 26 | + 'published.vaultFactory.managers.manager0.vaults.vault10-liquidated', |
| 27 | + 'published.vaultFactory.managers.manager0.vaults.vault11-liquidated', |
| 28 | + 'published.vaultFactory.managers.manager0.vaults.vault12-liquidated', |
| 29 | + ], |
| 30 | + debts: ['0', '0', '0'], |
| 31 | + balance: ['3425146', '3077900', '2846403'], |
| 32 | + denom: 'ATOM', |
| 33 | +}; |
| 34 | + |
| 35 | +const validate = async ({ |
| 36 | + apiUrl, |
| 37 | + maxRetries, |
| 38 | + retryDuration, |
| 39 | + expectedIds, |
| 40 | + expectedDebts, |
| 41 | + expectedBalance, |
| 42 | + expectedDenom, |
| 43 | + filterState, |
| 44 | +}) => { |
| 45 | + console.log(`Checking ${filterState} vaults...`); |
| 46 | + let retries = 0; |
| 47 | + |
| 48 | + while (retries < maxRetries) { |
| 49 | + try { |
| 50 | + const graphqlQuery = { |
| 51 | + query: `query { |
| 52 | + vaultLiquidations (filter: {state: {equalTo: "${filterState}"}}) { |
| 53 | + nodes { |
| 54 | + id |
| 55 | + denom |
| 56 | + debt |
| 57 | + state |
| 58 | + balance |
| 59 | + } |
| 60 | + } |
| 61 | + }`, |
| 62 | + }; |
| 63 | + |
| 64 | + const response = await fetch(apiUrl, { |
| 65 | + method: 'POST', |
| 66 | + headers: { |
| 67 | + 'Content-Type': 'application/json', |
| 68 | + Accept: 'application/json', |
| 69 | + }, |
| 70 | + body: JSON.stringify(graphqlQuery), |
| 71 | + }); |
| 72 | + |
| 73 | + const jsonResponse = await response.json(); |
| 74 | + console.log('Response:', JSON.stringify(jsonResponse)); |
| 75 | + |
| 76 | + const nodes = jsonResponse?.data?.vaultLiquidations?.nodes || []; |
| 77 | + if (nodes.length !== 3) { |
| 78 | + console.error(`Attempt ${retries + 1}: No data available`); |
| 79 | + retries++; |
| 80 | + await delay(retryDuration); |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + nodes.sort((a, b) => a.id.localeCompare(b.id)); |
| 85 | + console.log('Sorted Nodes:', JSON.stringify(nodes)); |
| 86 | + |
| 87 | + // Validate Ids |
| 88 | + assert.strictEqual(nodes[0].id, expectedIds[0]); |
| 89 | + assert.strictEqual(nodes[1].id, expectedIds[1]); |
| 90 | + assert.strictEqual(nodes[2].id, expectedIds[2]); |
| 91 | + |
| 92 | + // Validate Debts |
| 93 | + assert.strictEqual(nodes[0].debt, expectedDebts[0]); |
| 94 | + assert.strictEqual(nodes[1].debt, expectedDebts[1]); |
| 95 | + assert.strictEqual(nodes[2].debt, expectedDebts[2]); |
| 96 | + |
| 97 | + // Validate Denom |
| 98 | + assert.strictEqual(nodes[0].denom, expectedDenom); |
| 99 | + assert.strictEqual(nodes[1].denom, expectedDenom); |
| 100 | + assert.strictEqual(nodes[2].denom, expectedDenom); |
| 101 | + |
| 102 | + // Validate Balance |
| 103 | + assert.strictEqual(nodes[0].balance, expectedBalance[0]); |
| 104 | + assert.strictEqual(nodes[1].balance, expectedBalance[1]); |
| 105 | + assert.strictEqual(nodes[2].balance, expectedBalance[2]); |
| 106 | + |
| 107 | + console.log('All validations passed successfully.'); |
| 108 | + return; |
| 109 | + } catch (error) { |
| 110 | + console.error(`Error on attempt ${retries + 1} fetching active vaults:`, error); |
| 111 | + retries++; |
| 112 | + await delay(retryDuration); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + console.error('Maximum retry attempts reached. Exiting...'); |
| 117 | + process.exit(1); |
| 118 | +}; |
| 119 | + |
| 120 | +const main = async () => { |
| 121 | + console.log('Starting main process...'); |
| 122 | + |
| 123 | + const apiUrl = process.env.API_URL || 'http://localhost:3000/'; |
| 124 | + console.log(`API URL set to: ${apiUrl}`); |
| 125 | + |
| 126 | + try { |
| 127 | + console.log('Validating liquidating vaults...'); |
| 128 | + await validate({ |
| 129 | + apiUrl, |
| 130 | + expectedIds: expectedLiquidating.ids, |
| 131 | + expectedBalance: expectedLiquidating.balance, |
| 132 | + expectedDenom: expectedLiquidating.denom, |
| 133 | + expectedDebts: expectedLiquidating.debts, |
| 134 | + maxRetries: 3, |
| 135 | + retryDuration: 5 * 60 * 1000, |
| 136 | + filterState: states.liquidating, |
| 137 | + }); |
| 138 | + console.log('Validation successful for liquidating vaults.'); |
| 139 | + |
| 140 | + console.log('Validating liquidated vaults...'); |
| 141 | + await validate({ |
| 142 | + apiUrl, |
| 143 | + expectedIds: expectedLiquidated.ids, |
| 144 | + expectedBalance: expectedLiquidated.balance, |
| 145 | + expectedDenom: expectedLiquidated.denom, |
| 146 | + expectedDebts: expectedLiquidated.debts, |
| 147 | + maxRetries: 3, |
| 148 | + retryDuration: 2 * 60 * 1000, |
| 149 | + filterState: states.liquidated, |
| 150 | + }); |
| 151 | + console.log('Validation successful for liquidated vaults.'); |
| 152 | + } catch (error) { |
| 153 | + console.error('Validation failed:', error); |
| 154 | + } |
| 155 | +}; |
| 156 | + |
| 157 | +main(); |
0 commit comments