From 66fa35974d817255d630147331e994c309f413eb Mon Sep 17 00:00:00 2001 From: rabi-siddique Date: Wed, 18 Dec 2024 16:24:12 +0500 Subject: [PATCH] ci: validate liquidating vaults --- .github/scripts/changePrice.mjs | 3 +- .github/scripts/checkLiquidation.mjs | 157 +++++++++++++++++++++++++++ .github/workflows/liquidation.yml | 13 ++- 3 files changed, 170 insertions(+), 3 deletions(-) create mode 100644 .github/scripts/checkLiquidation.mjs diff --git a/.github/scripts/changePrice.mjs b/.github/scripts/changePrice.mjs index 707293b2..913988c7 100644 --- a/.github/scripts/changePrice.mjs +++ b/.github/scripts/changePrice.mjs @@ -24,9 +24,8 @@ const setAtomPrice = async (amount, containerName, agoricNet) => { }; const command = `${agops} oracle setPrice --keys gov1,gov2 --pair ATOM.USD --price ${amount} --keyring-backend=test`; - const { stdout, stderr } = await execa('docker', ['exec', containerName, command], { env, shell: true }); + const { stdout } = await execa('docker', ['exec', containerName, command], { env, shell: true }); - assert.strictEqual(stderr, '', `Expected stderr to be empty, but received: '${stderr}'`); console.log('Standard output:', stdout); } catch (error) { console.error('Error:', error); diff --git a/.github/scripts/checkLiquidation.mjs b/.github/scripts/checkLiquidation.mjs new file mode 100644 index 00000000..ed0723eb --- /dev/null +++ b/.github/scripts/checkLiquidation.mjs @@ -0,0 +1,157 @@ +// @ts-check +import fetch from 'node-fetch'; +import assert from 'assert'; +const delay = (ms) => { + return new Promise((resolve) => setTimeout(resolve, ms)); +}; + +const states = { + liquidated: 'liquidated', + liquidating: 'liquidating', +}; + +const expectedLiquidating = { + ids: [ + 'published.vaultFactory.managers.manager0.vaults.vault10-liquidating', + 'published.vaultFactory.managers.manager0.vaults.vault11-liquidating', + 'published.vaultFactory.managers.manager0.vaults.vault12-liquidating', + ], + debts: ['100500000', '103515000', '105525000'], + balance: ['15000000', '15000000', '15000000'], + denom: 'ATOM', +}; + +const expectedLiquidated = { + ids: [ + 'published.vaultFactory.managers.manager0.vaults.vault10-liquidated', + 'published.vaultFactory.managers.manager0.vaults.vault11-liquidated', + 'published.vaultFactory.managers.manager0.vaults.vault12-liquidated', + ], + debts: ['0', '0', '0'], + balance: ['3425146', '3077900', '2846403'], + denom: 'ATOM', +}; + +const validate = async ({ + apiUrl, + maxRetries, + retryDuration, + expectedIds, + expectedDebts, + expectedBalance, + expectedDenom, + filterState, +}) => { + console.log(`Checking ${filterState} vaults...`); + let retries = 0; + + while (retries < maxRetries) { + try { + const graphqlQuery = { + query: `query { + vaultLiquidations (filter: {state: {equalTo: "${filterState}"}}) { + nodes { + id + denom + debt + state + balance + } + } + }`, + }; + + const response = await fetch(apiUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Accept: 'application/json', + }, + body: JSON.stringify(graphqlQuery), + }); + + const jsonResponse = await response.json(); + console.log('Response:', JSON.stringify(jsonResponse)); + + const nodes = jsonResponse?.data?.vaultLiquidations?.nodes || []; + if (nodes.length !== 3) { + console.error(`Attempt ${retries + 1}: No data available`); + retries++; + await delay(retryDuration); + continue; + } + + nodes.sort((a, b) => a.id.localeCompare(b.id)); + console.log('Sorted Nodes:', JSON.stringify(nodes)); + + // Validate Ids + assert.strictEqual(nodes[0].id, expectedIds[0]); + assert.strictEqual(nodes[1].id, expectedIds[1]); + assert.strictEqual(nodes[2].id, expectedIds[2]); + + // Validate Debts + assert.strictEqual(nodes[0].debt, expectedDebts[0]); + assert.strictEqual(nodes[1].debt, expectedDebts[1]); + assert.strictEqual(nodes[2].debt, expectedDebts[2]); + + // Validate Denom + assert.strictEqual(nodes[0].denom, expectedDenom); + assert.strictEqual(nodes[1].denom, expectedDenom); + assert.strictEqual(nodes[2].denom, expectedDenom); + + // Validate Balance + assert.strictEqual(nodes[0].balance, expectedBalance[0]); + assert.strictEqual(nodes[1].balance, expectedBalance[1]); + assert.strictEqual(nodes[2].balance, expectedBalance[2]); + + console.log('All validations passed successfully.'); + return; + } catch (error) { + console.error(`Error on attempt ${retries + 1} fetching active vaults:`, error); + retries++; + await delay(retryDuration); + } + } + + console.error('Maximum retry attempts reached. Exiting...'); + process.exit(1); +}; + +const main = async () => { + console.log('Starting main process...'); + + const apiUrl = process.env.API_URL || 'http://localhost:3000/'; + console.log(`API URL set to: ${apiUrl}`); + + try { + console.log('Validating liquidating vaults...'); + await validate({ + apiUrl, + expectedIds: expectedLiquidating.ids, + expectedBalance: expectedLiquidating.balance, + expectedDenom: expectedLiquidating.denom, + expectedDebts: expectedLiquidating.debts, + maxRetries: 3, + retryDuration: 5 * 60 * 1000, + filterState: states.liquidating, + }); + console.log('Validation successful for liquidating vaults.'); + + console.log('Validating liquidated vaults...'); + await validate({ + apiUrl, + expectedIds: expectedLiquidated.ids, + expectedBalance: expectedLiquidated.balance, + expectedDenom: expectedLiquidated.denom, + expectedDebts: expectedLiquidated.debts, + maxRetries: 3, + retryDuration: 2 * 60 * 1000, + filterState: states.liquidated, + }); + console.log('Validation successful for liquidated vaults.'); + } catch (error) { + console.error('Validation failed:', error); + } +}; + +main(); diff --git a/.github/workflows/liquidation.yml b/.github/workflows/liquidation.yml index cb0f38d8..60e4aa38 100644 --- a/.github/workflows/liquidation.yml +++ b/.github/workflows/liquidation.yml @@ -48,7 +48,7 @@ jobs: run: node .github/scripts/getActiveVaults.mjs env: apiUrl: 'http://localhost:3000/' - expectedVaults: '7' + expectedVaults: 7 - name: Create Vault with 100 Minted and 15 Collateral run: node .github/scripts/createVault.mjs @@ -122,3 +122,14 @@ jobs: amount: 9.99 containerName: agd agoricNet: local + + - name: Check liquidation indexing + run: node .github/scripts/checkLiquidation.mjs + env: + apiUrl: 'http://localhost:3000/' + + - name: Get active vaults + run: node .github/scripts/getActiveVaults.mjs + env: + apiUrl: 'http://localhost:3000/' + expectedVaults: 7