This commit is contained in:
JulesCrown 2024-04-04 07:40:12 +02:00
parent 5a8c80e838
commit 59533a64d2
6 changed files with 85 additions and 3 deletions

View file

@ -1,4 +1,5 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
maxWorkers: 1
};

View file

@ -1,6 +1,6 @@
{
"name": "harb-lib",
"version": "0.1.0",
"version": "0.1.1",
"description": "helper functions and snatch selection",
"main": "dist/index.js",
"types": "dist/index.d.ts",

37
harb-lib/src/helpers.ts Normal file
View file

@ -0,0 +1,37 @@
import { bytesToUint256LittleEndian } from "./subgraph";
import { Position, PositionStatus } from './__generated__/graphql';
const STAKE_TOTOAL_SUPPLY: bigint = 1000000000000000000n * 10000000n;
const sortAndFilterPositions = (positions) => {
return positions
.filter(position => position.status === PositionStatus.Active)
.sort((a, b) => parseFloat(a.taxRate) - parseFloat(b.taxRate));
};
export function getSnatchList(
positions: Position[], // all active positions
needed: bigint, // amount of stake requested by user
taxRate: number
): bigint[] {
const sortedActivePositions = sortAndFilterPositions(positions);
const rv: bigint[] = [];
let i = 0;
while (needed > 0 && i < positions.length) {
const available = (STAKE_TOTOAL_SUPPLY * BigInt(Math.round(sortedActivePositions[i].share * 100000))) / BigInt(100000);
if (sortedActivePositions[i].taxRate < taxRate) {
rv.push(bytesToUint256LittleEndian(sortedActivePositions[i].id));
console.log(rv, available);
needed -= available;
}
i++;
// code block to be executed
}
if (needed > 0)
throw new Error('Not enough capacity');
return rv;
}

View file

@ -1 +1,3 @@
export {bytesToUint256LittleEndian, uint256ToBytesLittleEndian} from './subgraph'
export {bytesToUint256LittleEndian, uint256ToBytesLittleEndian} from './subgraph'
export {getSnatchList} from './helpers'

View file

@ -1,4 +1,4 @@
import { bytesToUint256LittleEndian, uint256ToBytesLittleEndian } from "../subgraph"; // Adjust the import path to where your functions are defined
import { bytesToUint256LittleEndian, uint256ToBytesLittleEndian } from "../subgraph";
import { Position, PositionStatus } from '../__generated__/graphql';

View file

@ -0,0 +1,42 @@
import { uint256ToBytesLittleEndian } from "../subgraph";
import { getSnatchList } from "../helpers";
import { Position, PositionStatus } from '../__generated__/graphql';
describe('BigInt Conversion Functions', () => {
test('get a list of positions to snatch', async () => {
const mockPos: Position[] = [
{
__typename: 'Position',
id: uint256ToBytesLittleEndian(3n),
owner: '0x8db6b632d743aef641146dc943acb64957155388',
share: 0.000001,
creationTime: 1610000000,
taxRate: 0.05,
status: PositionStatus.Active, // Enum usage
},
{
__typename: 'Position',
id: uint256ToBytesLittleEndian(4n),
owner: '0x8db6b632d743aef641146dc943acb64957155388',
share: 0.000001,
creationTime: 1610000000,
taxRate: 0.01,
status: PositionStatus.Active, // Enum usage
},
{
__typename: 'Position',
id: uint256ToBytesLittleEndian(5n),
owner: '0x8db6b632d743aef641146dc943acb64957155388',
share: 0.000001,
creationTime: 1610000000,
taxRate: 0.02,
status: PositionStatus.Active, // Enum usage
}
];
expect(getSnatchList(mockPos, 20000000000000000000n, 0.03)).toEqual([4n, 5n]);
});
});