From 9bd5afd40e9a626e724cc0849c7967090e466a3d Mon Sep 17 00:00:00 2001 From: JulesCrown Date: Thu, 12 Sep 2024 16:57:58 +0200 Subject: [PATCH] add liquidations: wip subraph structure --- services/marketMaker/.gitignore | 1 + services/marketMaker/.graphclientrc.yml | 13 ++++ services/marketMaker/.graphql | 9 +++ services/marketMaker/liquidations.js | 92 +++++++++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 services/marketMaker/.graphclientrc.yml create mode 100644 services/marketMaker/.graphql create mode 100644 services/marketMaker/liquidations.js diff --git a/services/marketMaker/.gitignore b/services/marketMaker/.gitignore index 0e32cf0..ae71572 100644 --- a/services/marketMaker/.gitignore +++ b/services/marketMaker/.gitignore @@ -31,6 +31,7 @@ coverage/ # Ignore build output dist/ build/ +.graphclient/ # Ignore any other secret keys or sensitive information secret-keys.json diff --git a/services/marketMaker/.graphclientrc.yml b/services/marketMaker/.graphclientrc.yml new file mode 100644 index 0000000..9077c94 --- /dev/null +++ b/services/marketMaker/.graphclientrc.yml @@ -0,0 +1,13 @@ +# .graphclientrc.yml +sources: + - name: harberg + handler: + graphql: + endpoint: https://api.studio.thegraph.com/query/47986/harberg-base-sepolia/version/latest + transforms: + - autoPagination: + # You might want to disable schema validation for faster startup + validateSchema: true + +documents: + - ./.graphql diff --git a/services/marketMaker/.graphql b/services/marketMaker/.graphql new file mode 100644 index 0000000..a539e78 --- /dev/null +++ b/services/marketMaker/.graphql @@ -0,0 +1,9 @@ +query GetPositions { + positions { + id + share + lastTaxTime + taxRate + status + } +} \ No newline at end of file diff --git a/services/marketMaker/liquidations.js b/services/marketMaker/liquidations.js new file mode 100644 index 0000000..ad897a8 --- /dev/null +++ b/services/marketMaker/liquidations.js @@ -0,0 +1,92 @@ +require('dotenv').config(); +const { ethers } = require('ethers'); +const express = require('express'); +const { execute } = require('./.graphclient'); +const { bytesToUint256 } = require('harb-lib'); + +const myQuery = ` + query GetPositions { + positions(first: 5000, where: {status: "Active"}) { + id + share + lastTaxTime + taxRate + status + } + } +` + +// Load environment variables +const PROVIDER_URL = process.env.PROVIDER_URL; +const PRIVATE_KEY = process.env.PRIVATE_KEY; +const STAKE_CONTRACT_ADDRESS = process.env.STAKE_CONTRACT_ADDRESS; +const ABI = [ + // Add your contract's ABI here + {"inputs":[{"internalType":"uint256","name":"positionId","type":"uint256"}],"name":"payTax","outputs":[],"stateMutability":"nonpayable","type":"function"} +]; + +// Initialize the provider +const provider = new ethers.JsonRpcProvider(PROVIDER_URL); + +const wallet = new ethers.Wallet(PRIVATE_KEY, provider); +const contract = new ethers.Contract(STAKE_CONTRACT_ADDRESS, ABI, wallet); + +let startTime = new Date(); +let lastCallTime = null; + +async function checkPosition(position) { + let taxRate = parseFloat(position.taxRate); + let lastTaxTime = position.lastTaxTime; + let passed = (Date.now() / 1000) - lastTaxTime; + if (passed > 365 * 24 * 60 * 60 / taxRate ) { + const hexString = position.id; + const cleanHexString = hexString.startsWith('0x') ? hexString.substring(2) : hexString; + const bytes = new Uint8Array(Math.ceil(cleanHexString.length / 2)); + for (let i = 0, j = 0; i < cleanHexString.length; i += 2, j++) { + bytes[j] = parseInt(cleanHexString.slice(i, i + 2), 16); + } + let positionId = bytesToUint256(bytes); + console.log(`Calling payTax on ${positionId}`); + const tx = await contract.payTax(positionId); + await tx.wait(); + lastCallTime = new Date(); + } else { + console.log("not liquidated: "); + console.log(position); + } +} + + +async function main() { + console.log('Service started...'); + + while (true) { + // do stuff here + + // Wait for some time before checking again + await new Promise(resolve => setTimeout(resolve, 300000)); // 5 minute + } +} + +// Start the main loop +main().catch(async (error) => { + console.error('Fatal error:', error); +}); + +// Set up the Express server +const app = express(); +const PORT = process.env.PORT || 3000; + +app.get('/query', async (req, res) => { + const result = await execute(myQuery, {}); + res.status(200).json(result); + for (const position of result.data.positions) { + await checkPosition(position); + } +}); + + +app.listen(PORT, () => { + console.log(`HTTP server running on port ${PORT}`); +}); +