From 2f49c73868453bc95e2a89c597a7afffdd21ef5b Mon Sep 17 00:00:00 2001 From: leifu Date: Wed, 19 Jul 2023 14:25:06 +0300 Subject: [PATCH 1/2] Added aws bucket support --- packages/sdk/src/constants/files.ts | 2 ++ packages/sdk/src/services/kwentaToken.ts | 14 +++++++------- packages/sdk/src/services/system.ts | 3 ++- packages/sdk/src/utils/files.ts | 19 ++++++++++++++----- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/packages/sdk/src/constants/files.ts b/packages/sdk/src/constants/files.ts index 8974ffb664..eccb225ebd 100644 --- a/packages/sdk/src/constants/files.ts +++ b/packages/sdk/src/constants/files.ts @@ -1,3 +1,5 @@ export const FLEEK_STORAGE_BUCKET = '25710180-23d8-43f4-b0c9-5b7f55f63165-bucket' export const FLEEK_BASE_URL = 'https://storage.kwenta.io' + +export const TRADING_REWARDS_AWS_BUCKET = 'https://trading-rewards-snapshots.s3.amazonaws.com/' diff --git a/packages/sdk/src/services/kwentaToken.ts b/packages/sdk/src/services/kwentaToken.ts index 3f2924840f..0bfab1ce20 100644 --- a/packages/sdk/src/services/kwentaToken.ts +++ b/packages/sdk/src/services/kwentaToken.ts @@ -19,7 +19,7 @@ import { import { ContractName } from '../contracts' import { ClaimParams, EpochData, EscrowData } from '../types/kwentaToken' import { formatTruncatedDuration } from '../utils/date' -import { client } from '../utils/files' +import { getClient } from '../utils/files' import { weiFromWei } from '../utils/number' import { getFuturesAggregateStats, getFuturesTrades } from '../utils/subgraph' import { calculateFeesForAccount, calculateTotalFees } from '../utils' @@ -503,11 +503,12 @@ export default class KwentaTokenService { public async getEstimatedRewards() { const { networkId, walletAddress } = this.sdk.context const fileNames = ['', '-op'].map( - (i) => `trading-rewards-snapshots/${networkId === 420 ? 'goerli-' : ''}epoch-current${i}.json` + (i) => `${networkId === 420 ? 'goerli-' : ''}epoch-current${i}.json` ) const responses: EpochData[] = await Promise.all( fileNames.map(async (fileName) => { + const client = getClient(true) const response = await client.get(fileName) return { ...response.data } }) @@ -541,14 +542,12 @@ export default class KwentaTokenService { : periods.slice(TRADING_REWARDS_CUTOFF_EPOCH) const fileNames = adjustedPeriods.map( - (i) => - `trading-rewards-snapshots/${ - this.sdk.context.networkId === 420 ? `goerli-` : '' - }epoch-${i}.json` + (i) => `${this.sdk.context.networkId === 420 ? `goerli-` : ''}epoch-${i}.json` ) const responses: EpochData[] = await Promise.all( fileNames.map(async (fileName, index) => { + const client = getClient(true) const response = await client.get(fileName) const period = isOldDistributor ? index >= 5 @@ -629,7 +628,7 @@ export default class KwentaTokenService { const fileNames = adjustedPeriods.map( (i) => - `trading-rewards-snapshots/${this.sdk.context.networkId === 420 ? `goerli-` : ''}epoch-${ + `${this.sdk.context.networkId === 420 ? `goerli-` : ''}epoch-${ isSnx ? i - OP_REWARDS_CUTOFF_EPOCH : i }${isOp ? (isSnx ? '-snx-op' : '-op') : ''}.json` ) @@ -637,6 +636,7 @@ export default class KwentaTokenService { const responses: EpochData[] = await Promise.all( fileNames.map(async (fileName, index) => { try { + const client = getClient(true) const response = await client.get(fileName) const period = isOldDistributor ? index >= 5 diff --git a/packages/sdk/src/services/system.ts b/packages/sdk/src/services/system.ts index 419393445e..2eaeb861a2 100644 --- a/packages/sdk/src/services/system.ts +++ b/packages/sdk/src/services/system.ts @@ -1,7 +1,7 @@ import KwentaSDK from '..' import { UNSUPPORTED_NETWORK } from '../common/errors' import { KwentaStatus } from '../types/system' -import { client } from '../utils/files' +import { getClient } from '../utils/files' import { StatusMap } from '../utils/system' export default class SystemService { @@ -27,6 +27,7 @@ export default class SystemService { } public async getKwentaStatus(): Promise { + const client = getClient() const response = await client.get('kwenta-status.json', { headers: { 'Cache-Control': 'no-cache' }, }) diff --git a/packages/sdk/src/utils/files.ts b/packages/sdk/src/utils/files.ts index 7f782b5d99..60811fb8eb 100644 --- a/packages/sdk/src/utils/files.ts +++ b/packages/sdk/src/utils/files.ts @@ -1,8 +1,17 @@ import axios from 'axios' -import { FLEEK_BASE_URL, FLEEK_STORAGE_BUCKET } from '../constants/files' +import { + FLEEK_BASE_URL, + FLEEK_STORAGE_BUCKET, + TRADING_REWARDS_AWS_BUCKET, +} from '../constants/files' -export const client = axios.create({ - baseURL: `${FLEEK_BASE_URL}/${FLEEK_STORAGE_BUCKET}/data/`, - timeout: 10000, -}) +export const getClient = (useAWS: boolean = false) => { + const baseURL = useAWS + ? TRADING_REWARDS_AWS_BUCKET + : `${FLEEK_BASE_URL}/${FLEEK_STORAGE_BUCKET}/data/` + return axios.create({ + baseURL, + timeout: 5000, + }) +} From 7825b4c763d6c8d611631b96536955a25a558c3a Mon Sep 17 00:00:00 2001 From: leifu Date: Wed, 19 Jul 2023 15:00:13 +0300 Subject: [PATCH 2/2] Create two clients instead of initialising each time --- packages/sdk/src/services/kwentaToken.ts | 11 ++++------- packages/sdk/src/services/system.ts | 5 ++--- packages/sdk/src/utils/files.ts | 9 +++++---- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/packages/sdk/src/services/kwentaToken.ts b/packages/sdk/src/services/kwentaToken.ts index 0bfab1ce20..b73173ddd4 100644 --- a/packages/sdk/src/services/kwentaToken.ts +++ b/packages/sdk/src/services/kwentaToken.ts @@ -19,7 +19,7 @@ import { import { ContractName } from '../contracts' import { ClaimParams, EpochData, EscrowData } from '../types/kwentaToken' import { formatTruncatedDuration } from '../utils/date' -import { getClient } from '../utils/files' +import { awsClient } from '../utils/files' import { weiFromWei } from '../utils/number' import { getFuturesAggregateStats, getFuturesTrades } from '../utils/subgraph' import { calculateFeesForAccount, calculateTotalFees } from '../utils' @@ -508,8 +508,7 @@ export default class KwentaTokenService { const responses: EpochData[] = await Promise.all( fileNames.map(async (fileName) => { - const client = getClient(true) - const response = await client.get(fileName) + const response = await awsClient.get(fileName) return { ...response.data } }) ) @@ -547,8 +546,7 @@ export default class KwentaTokenService { const responses: EpochData[] = await Promise.all( fileNames.map(async (fileName, index) => { - const client = getClient(true) - const response = await client.get(fileName) + const response = await awsClient.get(fileName) const period = isOldDistributor ? index >= 5 ? index >= 10 @@ -636,8 +634,7 @@ export default class KwentaTokenService { const responses: EpochData[] = await Promise.all( fileNames.map(async (fileName, index) => { try { - const client = getClient(true) - const response = await client.get(fileName) + const response = await awsClient.get(fileName) const period = isOldDistributor ? index >= 5 ? index >= 10 diff --git a/packages/sdk/src/services/system.ts b/packages/sdk/src/services/system.ts index 2eaeb861a2..63846bdbb5 100644 --- a/packages/sdk/src/services/system.ts +++ b/packages/sdk/src/services/system.ts @@ -1,7 +1,7 @@ import KwentaSDK from '..' import { UNSUPPORTED_NETWORK } from '../common/errors' import { KwentaStatus } from '../types/system' -import { getClient } from '../utils/files' +import { fleekClient } from '../utils/files' import { StatusMap } from '../utils/system' export default class SystemService { @@ -27,8 +27,7 @@ export default class SystemService { } public async getKwentaStatus(): Promise { - const client = getClient() - const response = await client.get('kwenta-status.json', { + const response = await fleekClient.get('kwenta-status.json', { headers: { 'Cache-Control': 'no-cache' }, }) diff --git a/packages/sdk/src/utils/files.ts b/packages/sdk/src/utils/files.ts index 60811fb8eb..411a2624cc 100644 --- a/packages/sdk/src/utils/files.ts +++ b/packages/sdk/src/utils/files.ts @@ -6,12 +6,13 @@ import { TRADING_REWARDS_AWS_BUCKET, } from '../constants/files' -export const getClient = (useAWS: boolean = false) => { - const baseURL = useAWS - ? TRADING_REWARDS_AWS_BUCKET - : `${FLEEK_BASE_URL}/${FLEEK_STORAGE_BUCKET}/data/` +const createClient = (baseURL: string) => { return axios.create({ baseURL, timeout: 5000, }) } + +export const awsClient = createClient(TRADING_REWARDS_AWS_BUCKET) + +export const fleekClient = createClient(`${FLEEK_BASE_URL}/${FLEEK_STORAGE_BUCKET}/data/`)