Skip to content

Commit

Permalink
restructure overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
nissoh committed Jan 21, 2024
1 parent 5c6dec9 commit cbc0fab
Show file tree
Hide file tree
Showing 23 changed files with 200 additions and 1,845 deletions.
142 changes: 51 additions & 91 deletions const/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,13 @@
import { BASIS_POINTS_DIVISOR, IntervalTime } from "common-utils"
import { arbitrum, avalanche } from "viem/chains"
import { ARBITRUM_ADDRESS } from "./chain/arbitrum.js"
import { AVALANCHE_ADDRESS } from "./chain/avalanche.js"
import * as viem from "viem"

export enum CHAIN {
ETH = 1,
ETH_ROPSTEN = 3,
ETH_KOVAN = 42,
ETH_RINKBY = 4,
ETH_GOERLI = 5,

BSC = 56,
BSC_TESTNET = 97,

ARBITRUM = 42161,
ARBITRUM_RINKBY = 421611,
AVALANCHE = 43114
}

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

export const BASIS_POINTS_DIVISOR = 10000n

export const FACTOR_PERCISION = 30
export const USD_DECIMALS = 30
export const WEI_PRECISION = 10n ** BigInt(18)

export const MAX_LEVERAGE_FACTOR = 100n * BASIS_POINTS_DIVISOR
export const MIN_LEVERAGE_FACTOR = 11000n
export const DEDUCT_USD_FOR_GAS = 10n ** 30n * 2n

export const PRECISION = 10n ** BigInt(FACTOR_PERCISION)
export const LIQUIDATION_FEE = 10n ** 5n

export const TAX_BASIS_POINTS = 50n
Expand All @@ -43,23 +20,6 @@ export const MARGIN_FEE_BASIS_POINTS = 10n
export const FUNDING_RATE_PRECISION = 1000000n


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 const PRICEFEED_INTERVAL = [
Expand All @@ -72,59 +32,59 @@ export const PRICEFEED_INTERVAL = [
] as const

export const TRADE_CONTRACT_MAPPING = {
[CHAIN.ARBITRUM]: ARBITRUM_ADDRESS,
[CHAIN.AVALANCHE]: AVALANCHE_ADDRESS
[arbitrum.id]: ARBITRUM_ADDRESS,
[avalanche.id]: AVALANCHE_ADDRESS
} as const

export type ContractChain = keyof typeof TRADE_CONTRACT_MAPPING




export enum MarketEvent {
MarketPoolValueInfo = "0x4d61726b6574506f6f6c56616c7565496e666f",
PoolAmountUpdated = "0x506f6f6c416d6f756e7455706461746564",
SwapImpactPoolAmountUpdated = "0x53776170496d70616374506f6f6c416d6f756e7455706461746564",
PositionImpactPoolAmountUpdated = '0x506f736974696f6e496d70616374506f6f6c416d6f756e7455706461746564',
OpenInterestUpdated = '0x4f70656e496e74657265737455706461746564',
OpenInterestInTokensUpdated = '0x4f70656e496e746572657374496e546f6b656e7355706461746564',
CollateralSumUpdated = '0x436f6c6c61746572616c53756d55706461746564',
CumulativeBorrowingFactorUpdated = '0x43756d756c6174697665426f72726f77696e67466163746f7255706461746564',
FundingFeeAmountPerSizeUpdated = '0x46756e64696e67466565416d6f756e7450657253697a6555706461746564',
ClaimableFundingAmountPerSizeUpdated = '0x436c61696d61626c6546756e64696e67416d6f756e7450657253697a6555706461746564',
FundingFeesClaimed = '0x46756e64696e6746656573436c61696d6564',
ClaimableFundingUpdated = '0x436c61696d61626c6546756e64696e6755706461746564',
ClaimableCollateralUpdated = '0x436c61696d61626c65436f6c6c61746572616c55706461746564',
CollateralClaimed = '0x436f6c6c61746572616c436c61696d6564',
UiFeeFactorUpdated = '0x5569466565466163746f7255706461746564',
MarketCreated = '0x4d61726b657443726561746564',
}

export enum FeeEvent {
FeesClaimed = '0x46656573436c61696d6564',
UiFeesClaimed = '0x556946656573436c61696d6564',
}


export enum OracleEvent {
OraclePriceUpdate = '0x4f7261636c655072696365557064617465',
SignerAdded = '0x5369676e65724164646564',
SignerRemoved = '0x5369676e657252656d6f766564',
}

export enum OrderEvent {
OrderExecuted = '0x4f726465724578656375746564',
OrderUpdated = '0x4f7264657255706461746564',
OrderSizeDeltaAutoUpdated = '0x4f7264657253697a6544656c74614175746f55706461746564',
OrderCollateralDeltaAmountAutoUpdated = '0x4f72646572436f6c6c61746572616c44656c7461416d6f756e744175746f55706461746564',
OrderCancelled = '0x4f7264657243616e63656c6c6564',
OrderFrozen = '0x4f7264657246726f7a656e',
}

export enum PositionEvent {
PositionIncrease = '0x506f736974696f6e496e637265617365',
PositionDecrease = '0x506f736974696f6e4465637265617365',
InsufficientFundingFeePayment = '0x496e73756666696369656e7446756e64696e674665655061796d656e74',
PositionFeesInfo = '0x506f736974696f6e46656573496e666f',
PositionFeesCollected = '0x506f736974696f6e46656573436f6c6c6563746564',
}
// export enum MarketEvent {
// MarketPoolValueInfo = "0x4d61726b6574506f6f6c56616c7565496e666f",
// PoolAmountUpdated = "0x506f6f6c416d6f756e7455706461746564",
// SwapImpactPoolAmountUpdated = "0x53776170496d70616374506f6f6c416d6f756e7455706461746564",
// PositionImpactPoolAmountUpdated = '0x506f736974696f6e496d70616374506f6f6c416d6f756e7455706461746564',
// OpenInterestUpdated = '0x4f70656e496e74657265737455706461746564',
// OpenInterestInTokensUpdated = '0x4f70656e496e746572657374496e546f6b656e7355706461746564',
// CollateralSumUpdated = '0x436f6c6c61746572616c53756d55706461746564',
// CumulativeBorrowingFactorUpdated = '0x43756d756c6174697665426f72726f77696e67466163746f7255706461746564',
// FundingFeeAmountPerSizeUpdated = '0x46756e64696e67466565416d6f756e7450657253697a6555706461746564',
// ClaimableFundingAmountPerSizeUpdated = '0x436c61696d61626c6546756e64696e67416d6f756e7450657253697a6555706461746564',
// FundingFeesClaimed = '0x46756e64696e6746656573436c61696d6564',
// ClaimableFundingUpdated = '0x436c61696d61626c6546756e64696e6755706461746564',
// ClaimableCollateralUpdated = '0x436c61696d61626c65436f6c6c61746572616c55706461746564',
// CollateralClaimed = '0x436f6c6c61746572616c436c61696d6564',
// UiFeeFactorUpdated = '0x5569466565466163746f7255706461746564',
// MarketCreated = '0x4d61726b657443726561746564',
// }

// export enum FeeEvent {
// FeesClaimed = '0x46656573436c61696d6564',
// UiFeesClaimed = '0x556946656573436c61696d6564',
// }


// export enum OracleEvent {
// OraclePriceUpdate = '0x4f7261636c655072696365557064617465',
// SignerAdded = '0x5369676e65724164646564',
// SignerRemoved = '0x5369676e657252656d6f766564',
// }

// export enum OrderEvent {
// OrderExecuted = '0x4f726465724578656375746564',
// OrderUpdated = '0x4f7264657255706461746564',
// OrderSizeDeltaAutoUpdated = '0x4f7264657253697a6544656c74614175746f55706461746564',
// OrderCollateralDeltaAmountAutoUpdated = '0x4f72646572436f6c6c61746572616c44656c7461416d6f756e744175746f55706461746564',
// OrderCancelled = '0x4f7264657243616e63656c6c6564',
// OrderFrozen = '0x4f7264657246726f7a656e',
// }

// export enum PositionEvent {
// PositionIncrease = '0x506f736974696f6e496e637265617365',
// PositionDecrease = '0x506f736974696f6e4465637265617365',
// InsufficientFundingFeePayment = '0x496e73756666696369656e7446756e64696e674665655061796d656e74',
// PositionFeesInfo = '0x506f736974696f6e46656573496e666f',
// PositionFeesCollected = '0x506f736974696f6e46656573436f6c6c6563746564',
// }
10 changes: 5 additions & 5 deletions const/src/token.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { arbitrum, avalanche } from "viem/chains"
import { ARBITRUM_ADDRESS } from "./chain/arbitrum.js"
import { AVALANCHE_ADDRESS } from "./chain/avalanche.js"
import { CHAIN } from "./common.js"
import { TOKEN_SYMBOL } from "./symbol.js"
import { mapArrayBy } from "./utils.js"

Expand Down Expand Up @@ -180,13 +180,13 @@ export const TOKEN_DESCRIPTION_LIST = [
export const TOKEN_DESCRIPTION_MAP = mapArrayBy(TOKEN_DESCRIPTION_LIST, token => token.symbol, x => x)

export const CHAIN_ADDRESS_MAP = {
[CHAIN.AVALANCHE]: AVALANCHE_ADDRESS,
[CHAIN.ARBITRUM]: ARBITRUM_ADDRESS,
[avalanche.id]: AVALANCHE_ADDRESS,
[arbitrum.id]: ARBITRUM_ADDRESS,
}

export const CHAIN_NATIVE_DESCRIPTION = {
[CHAIN.AVALANCHE]: TOKEN_DESCRIPTION_MAP.AVAX,
[CHAIN.ARBITRUM]: TOKEN_DESCRIPTION_MAP.ETH,
[avalanche.id]: TOKEN_DESCRIPTION_MAP.AVAX,
[arbitrum.id]: TOKEN_DESCRIPTION_MAP.ETH,
} as const

export const TOKEN_ADDRESS_DESCRIPTION_MAP = {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"scripts": {
"build": "cd utils && bun run build"
},
"dependencies": {
"viem": "^2.4.0"
},
"devDependencies": {
"@changesets/cli": "^2.26.2",
"@typescript-eslint/eslint-plugin": "^6.7.4",
Expand Down
1 change: 1 addition & 0 deletions utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@aelea/dom": "^0.2.2",
"@most/core": "^1.6.1",
"gmx-middleware-const": "workspace:*",
"common-utils": "workspace:*",
"@most/disposable": "^1.3.0",
"@most/prelude": "^1.8.0",
"@most/scheduler": "^1.3.0",
Expand Down
35 changes: 0 additions & 35 deletions utils/src/comTypes.ts

This file was deleted.

89 changes: 1 addition & 88 deletions utils/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,95 +1,8 @@
import { map, periodic } from "@most/core"
import * as GMX from "gmx-middleware-const"
import { groupArrayByKey, unixTimestampNow } from "./utils.js"
import { groupArrayByKey } from "common-utils"
import { IMarketCreatedEvent } from "./types.js"


const intervals = [
{ 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 getTimeSince(time: number, suffix = 'ago') {
const timeDelta = unixTimestampNow() - time
const interval = intervals.find(i => i.seconds < timeDelta)

if (!interval) {
return 'now'
}

const count = Math.floor(timeDelta / interval.seconds)
return `${count} ${interval.label}${count !== 1 ? 's' : ''} ${suffix}`
}

export const everySec = map(unixTimestampNow, periodic(1000))

export const displayDate = (unixTime: number | bigint) => {
return `${new Date(Number(unixTime) * 1000).toDateString()} ${new Date(Number(unixTime) * 1000).toLocaleTimeString()}`
}

export const countdown = (targetDate: number) => {
return map(now => countdownFn(targetDate, now), everySec)
}

export function countdownFn(targetDate: number | bigint, now: number | bigint) {
const distance = Number(targetDate) - Number(now)

const days = Math.floor(distance / (60 * 60 * 24))
const hours = Math.floor((distance % (60 * 60 * 24)) / (60 * 60))
const minutes = Math.floor((distance % (60 * 60)) / 60)
const seconds = Math.floor(distance % 60)

return `${days ? days + "d " : ''} ${hours ? hours + "h " : ''} ${minutes ? minutes + "m " : ''} ${seconds ? seconds + "s " : ''}`
}

export function getIntervalBasedOnTimeframe(maxColumns: number, from: number, to: number) {
const delta = to - from

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
}

function padZero(str: string | number, len = 2) {
const zeros = new Array(len).join('0')
return (zeros + str).slice(-len)
}

export function invertColor(hex: string, bw = true) {
if (hex.indexOf('#') === 0) {
hex = hex.slice(1)
}
// convert 3-digit hex to 6-digits.
if (hex.length === 3) {
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2]
}
if (hex.length !== 6) {
throw new Error('Invalid HEX color.')
}
const r = parseInt(hex.slice(0, 2), 16)
const g = parseInt(hex.slice(2, 4), 16)
const b = parseInt(hex.slice(4, 6), 16)

if (bw) {
// https://stackoverflow.com/a/3943023/112731
return (r * 0.299 + g * 0.587 + b * 0.114) > 186
? '#000000'
: '#FFFFFF'
}


// pad each with zeros and return
return "#" + padZero((255 - r).toString(16)) + padZero((255 - g).toString(16)) + padZero((255 - b).toString(16))
}
export const TEMP_MARKET_LIST: IMarketCreatedEvent[] = [
// {
// id: "0x0",
Expand Down
Loading

0 comments on commit cbc0fab

Please sign in to comment.