add liquidations: wip subraph structure

This commit is contained in:
JulesCrown 2024-09-12 16:57:58 +02:00
parent 1ea5bc0403
commit 9bd5afd40e
4 changed files with 115 additions and 0 deletions

View file

@ -31,6 +31,7 @@ coverage/
# Ignore build output
dist/
build/
.graphclient/
# Ignore any other secret keys or sensitive information
secret-keys.json

View file

@ -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

View file

@ -0,0 +1,9 @@
query GetPositions {
positions {
id
share
lastTaxTime
taxRate
status
}
}

View file

@ -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}`);
});