add more methods to integrator

This commit is contained in:
TheJuze
2023-09-28 17:36:08 +03:00
parent e868711374
commit 1441b51e0d
6 changed files with 107 additions and 7 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "@orionprotocol/sdk",
"version": "0.19.89-rc8",
"version": "0.19.89-rc9",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@orionprotocol/sdk",
"version": "0.19.89-rc8",
"version": "0.19.89-rc9",
"hasInstallScript": true,
"license": "ISC",
"dependencies": {

View File

@@ -1,6 +1,6 @@
{
"name": "@orionprotocol/sdk",
"version": "0.19.89-rc8",
"version": "0.19.89-rc9",
"description": "Orion Protocol SDK",
"main": "./lib/index.cjs",
"module": "./lib/index.js",

View File

@@ -1,11 +1,14 @@
import {
environmentResponseSchema,
getPoolResponseSchema,
listAmountResponseSchema,
listNFTOrderResponseSchema,
listPoolResponseSchema,
veORNInfoSchema,
veORNInfoResponseSchema,
votingInfoResponseSchema
} from './schemas/index.js';
import { fetchWithValidation } from 'simple-typed-fetch';
import { BigNumber } from 'bignumber.js';
type BasePayload = {
chainId: number
@@ -42,12 +45,29 @@ type VeORNInfoPayload = BasePayload & {
params: [string]
}
type ListAmountPayload = BasePayload & {
model: string
method: 'listAmount'
params: []
}
type GetAmountByORNPayload = BasePayload & {
amountToken: number
timeLock: number
}
type Payload =
| GetEnvironmentPayload
| ListNFTOrderPayload
| GetPoolInfoPayload
| ListPoolPayload
| VeORNInfoPayload;
| VeORNInfoPayload
| ListAmountPayload
| GetAmountByORNPayload;
const START_TIME = 1690848000;// Aug 01 2023 00:00:00 UTC
const DAY = 86400
const YEAR = 365 * DAY
class IntegratorService {
private readonly apiUrl: string;
@@ -67,6 +87,10 @@ class IntegratorService {
this.getPoolInfo = this.getPoolInfo.bind(this);
this.listPool = this.listPool.bind(this);
this.veORNInfo = this.veORNInfo.bind(this);
this.listAmount = this.listAmount.bind(this);
this.getAmountByORN = this.getAmountByORN.bind(this);
this.getAmountAtCurrent = this.getAmountAtCurrent.bind(this);
this.getVotingInfo = this.getVotingInfo.bind(this);
}
makeRPCPayload = (payload: Omit<Payload, 'chainId' | 'jsonrpc'>) => {
@@ -78,7 +102,7 @@ class IntegratorService {
};
veORNInfo = (address: string) => {
return fetchWithValidation(this.apiUrl, veORNInfoSchema, {
return fetchWithValidation(this.apiUrl, veORNInfoResponseSchema, {
method: 'POST',
body: this.makeRPCPayload({
model: 'veORN',
@@ -88,6 +112,13 @@ class IntegratorService {
})
}
getAmountAtCurrent = (amount: number) => {
const timestamp = Date.now() / 1000;
// sqrt
return BigNumber(amount).dividedBy(this.getK(timestamp));
}
private readonly getEnvironment = () => {
return fetchWithValidation(this.apiUrl, environmentResponseSchema, {
method: 'POST',
@@ -135,6 +166,47 @@ class IntegratorService {
}),
});
}
private readonly listAmount = (poolKey: string) => {
return fetchWithValidation(this.apiUrl, listAmountResponseSchema, {
method: 'POST',
body: this.makeRPCPayload({
model: poolKey,
method: 'listAmount',
params: [],
}),
});
}
private readonly getK = (time: number) => {
const currentTime = time < START_TIME ? START_TIME : time;
const deltaYears = BigNumber(currentTime).minus(START_TIME).dividedBy(YEAR);
return BigNumber(2).pow(BigNumber(deltaYears).pow(2));
}
private readonly getAmountByORN = (amountToken: number, timeLock: number) => {
const timestamp = Date.now() / 1000;
const deltaDays = BigNumber(timeLock).minus(timestamp).dividedBy(DAY);
if (deltaDays.lt(0)) {
return 0;
}
// sqrt
return BigNumber(amountToken).multipliedBy(BigNumber(deltaDays).sqrt()).dividedBy(5);
}
private readonly getVotingInfo = (userAddress: number) => {
return fetchWithValidation(this.apiUrl, votingInfoResponseSchema, {
method: 'POST',
body: this.makeRPCPayload({
model: 'OrionVoting',
method: 'info',
params: [userAddress],
}),
});
}
}
export * as schemas from './schemas/index.js';

View File

@@ -2,4 +2,6 @@ export { default as environmentResponseSchema } from './environment-response-sch
export { default as listNFTOrderResponseSchema } from './list-nft-order-response-schema.js';
export { default as getPoolResponseSchema } from './get-pool-response-schema.js';
export { default as listPoolResponseSchema } from './list-pool-response-schema.js';
export { default as veORNInfoSchema } from './veORN-info-schema.js';
export { default as veORNInfoResponseSchema } from './veORN-info-schema.js';
export { default as listAmountResponseSchema } from './list-amount-schema.js';
export { default as votingInfoResponseSchema } from './voting-info-schema.js';

View File

@@ -0,0 +1,5 @@
import { z } from 'zod';
const listAmountSchema = z.record(z.number(), z.number())
export default listAmountSchema;

View File

@@ -0,0 +1,21 @@
import { z } from 'zod';
const poolSchema = z.object({
allVote: z.number(),
name: z.string(),
poolAddress: z.string(),
type: z.string(),
userVote: z.number()
})
const votingInfoSchema = z.object({
absoluteVeTokenInVoting: z.number(),
pools: z.array(poolSchema),
userVeTokenBalance: z.number(),
userVeTokenInVoting: z.number(),
veTokenAddress: z.string(),
votingAddress: z.string(),
weeklyReward: z.number()
})
export default votingInfoSchema;