Skip to content

Commit

Permalink
middleware update
Browse files Browse the repository at this point in the history
  • Loading branch information
nissoh committed Jan 7, 2024
1 parent db66249 commit 4aeb114
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 40 deletions.
54 changes: 26 additions & 28 deletions const/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { encodePacked, hexToString, keccak256, stringToHex } from "viem"
import { ARBITRUM_ADDRESS } from "./chain/arbitrum.js"
import { AVALANCHE_ADDRESS } from "./chain/avalanche.js"

import * as viem from "viem"

export enum CHAIN {
ETH = 1,
Expand All @@ -18,8 +17,8 @@ export enum CHAIN {
AVALANCHE = 43114
}

export const ADDRESS_ZERO = "0x0000000000000000000000000000000000000000" as const
export const BYTES32_ZERO = "0x0000000000000000000000000000000000000000000000000000000000000000" as const
export const ADDRESS_ZERO = "0x0000000000000000000000000000000000000000" as viem.Address
export const BYTES32_ZERO = "0x0000000000000000000000000000000000000000000000000000000000000000" as viem.Hex

export const BASIS_POINTS_DIVISOR = 10000n

Expand All @@ -45,33 +44,32 @@ export const MARGIN_FEE_BASIS_POINTS = 10n
export const FUNDING_RATE_PRECISION = 1000000n


export const TIME_INTERVAL_MAP = {
SEC: 1,
MIN: 60,
MIN5: 300,
MIN15: 900,
MIN30: 1800,
MIN60: 3600,
HR2: 7200,
HR4: 14400,
HR6: 21600,
HR8: 28800,
HR24: 86400,
DAY7: 604800,
MONTH: 2628000,
MONTH2: 5256000,
YEAR: 31536000
} as const
export enum IntervalTime {
SEC = 1,
MIN = 60,
MIN5 = 300,
MIN15 = 900,
MIN30 = 1800,
MIN60 = 3600,
HR2 = 7200,
HR4 = 14400,
HR6 = 21600,
HR8 = 28800,
HR24 = 86400,
DAY7 = 604800,
MONTH = 2628000,
MONTH2 = 5256000,
YEAR = 31536000
}

export type IntervalTime = typeof TIME_INTERVAL_MAP[keyof typeof TIME_INTERVAL_MAP]

export const PRICEFEED_INTERVAL = [
TIME_INTERVAL_MAP.MIN5,
TIME_INTERVAL_MAP.MIN15,
TIME_INTERVAL_MAP.MIN60,
TIME_INTERVAL_MAP.HR6,
TIME_INTERVAL_MAP.HR24,
TIME_INTERVAL_MAP.DAY7,
IntervalTime.MIN5,
IntervalTime.MIN15,
IntervalTime.MIN60,
IntervalTime.HR6,
IntervalTime.HR24,
IntervalTime.DAY7,
] as const

export const TRADE_CONTRACT_MAPPING = {
Expand Down
24 changes: 12 additions & 12 deletions utils/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { IMarketCreatedEvent } from "./types.js"


const intervals = [
{ label: 'year', seconds: GMX.TIME_INTERVAL_MAP.MONTH * 12 },
{ label: 'month', seconds: GMX.TIME_INTERVAL_MAP.MONTH },
{ label: 'day', seconds: GMX.TIME_INTERVAL_MAP.HR24 },
{ label: 'hr', seconds: GMX.TIME_INTERVAL_MAP.MIN * 60 },
{ label: 'min', seconds: GMX.TIME_INTERVAL_MAP.MIN },
{ label: 'sec', seconds: GMX.TIME_INTERVAL_MAP.SEC }
{ label: 'year', seconds: GMX.IntervalTime.MONTH * 12 },
{ label: 'month', seconds: GMX.IntervalTime.MONTH },
{ label: 'day', seconds: GMX.IntervalTime.HR24 },
{ label: 'hr', seconds: GMX.IntervalTime.MIN * 60 },
{ label: 'min', seconds: GMX.IntervalTime.MIN },
{ label: 'sec', seconds: GMX.IntervalTime.SEC }
] as const

export function timeSince(time: number) {
Expand Down Expand Up @@ -49,12 +49,12 @@ export function countdownFn(targetDate: number | bigint, now: number | bigint) {
export function getIntervalBasedOnTimeframe(maxColumns: number, from: number, to: number) {
const delta = to - from

const interval = maxColumns < delta / GMX.TIME_INTERVAL_MAP.DAY7
? GMX.TIME_INTERVAL_MAP.DAY7 : maxColumns < delta / GMX.TIME_INTERVAL_MAP.HR24
? GMX.TIME_INTERVAL_MAP.HR24 : maxColumns < delta / GMX.TIME_INTERVAL_MAP.HR4
? GMX.TIME_INTERVAL_MAP.HR4 : maxColumns < delta / GMX.TIME_INTERVAL_MAP.MIN60
? GMX.TIME_INTERVAL_MAP.MIN60 : maxColumns < delta / GMX.TIME_INTERVAL_MAP.MIN15
? GMX.TIME_INTERVAL_MAP.MIN15 : GMX.TIME_INTERVAL_MAP.MIN5
const interval = maxColumns < delta / GMX.IntervalTime.DAY7
? GMX.IntervalTime.DAY7 : maxColumns < delta / GMX.IntervalTime.HR24
? GMX.IntervalTime.HR24 : maxColumns < delta / GMX.IntervalTime.HR4
? GMX.IntervalTime.HR4 : maxColumns < delta / GMX.IntervalTime.MIN60
? GMX.IntervalTime.MIN60 : maxColumns < delta / GMX.IntervalTime.MIN15
? GMX.IntervalTime.MIN15 : GMX.IntervalTime.MIN5

return interval
}
Expand Down
10 changes: 10 additions & 0 deletions utils/src/gmxUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,17 @@ export const abiParamParseMap = {



export function getShortHash(name: string, obj: any) {
const str = JSON.stringify(obj)
let hash = 0

for (let i = 0; i < str.length; i++) {
hash = ((hash << 5) - hash) + str.charCodeAt(i)
hash |= 0 // Convert to 32bit integer
}

return `${name}-${hash.toString(16)}`
}


export function createMovingAverageCalculator(windowValues: number[], windowSize: number, newValue: number) {
Expand Down

0 comments on commit 4aeb114

Please sign in to comment.