From 59533a64d2e648a1bea1237b706d962af57efb63 Mon Sep 17 00:00:00 2001 From: JulesCrown Date: Thu, 4 Apr 2024 07:40:12 +0200 Subject: [PATCH] wip --- harb-lib/jest.config.js | 1 + harb-lib/package.json | 2 +- harb-lib/src/helpers.ts | 37 ++++++++++++++++++++++++ harb-lib/src/index.ts | 4 ++- harb-lib/src/tests/functions.test.ts | 2 +- harb-lib/src/tests/helpers.test.ts | 42 ++++++++++++++++++++++++++++ 6 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 harb-lib/src/helpers.ts create mode 100644 harb-lib/src/tests/helpers.test.ts diff --git a/harb-lib/jest.config.js b/harb-lib/jest.config.js index 4a5b465..418c8d3 100644 --- a/harb-lib/jest.config.js +++ b/harb-lib/jest.config.js @@ -1,4 +1,5 @@ module.exports = { preset: 'ts-jest', testEnvironment: 'node', + maxWorkers: 1 }; diff --git a/harb-lib/package.json b/harb-lib/package.json index d1ab7e7..b880cf0 100644 --- a/harb-lib/package.json +++ b/harb-lib/package.json @@ -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", diff --git a/harb-lib/src/helpers.ts b/harb-lib/src/helpers.ts new file mode 100644 index 0000000..62197b3 --- /dev/null +++ b/harb-lib/src/helpers.ts @@ -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; +} \ No newline at end of file diff --git a/harb-lib/src/index.ts b/harb-lib/src/index.ts index 4f833ba..612e3ac 100644 --- a/harb-lib/src/index.ts +++ b/harb-lib/src/index.ts @@ -1 +1,3 @@ -export {bytesToUint256LittleEndian, uint256ToBytesLittleEndian} from './subgraph' \ No newline at end of file +export {bytesToUint256LittleEndian, uint256ToBytesLittleEndian} from './subgraph' + +export {getSnatchList} from './helpers' \ No newline at end of file diff --git a/harb-lib/src/tests/functions.test.ts b/harb-lib/src/tests/functions.test.ts index 222c336..351b5c4 100644 --- a/harb-lib/src/tests/functions.test.ts +++ b/harb-lib/src/tests/functions.test.ts @@ -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'; diff --git a/harb-lib/src/tests/helpers.test.ts b/harb-lib/src/tests/helpers.test.ts new file mode 100644 index 0000000..8cbe6ea --- /dev/null +++ b/harb-lib/src/tests/helpers.test.ts @@ -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]); + }); + +});