Skip to content

Commit

Permalink
Merge pull request #71 from akiraonstarknet/prod
Browse files Browse the repository at this point in the history
add redis price feed for backend
  • Loading branch information
akiraonstarknet authored Sep 20, 2024
2 parents 521316f + 98460fe commit 1f2c1bb
Show file tree
Hide file tree
Showing 5 changed files with 937 additions and 36 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"sharp": "0.33.4",
"starknet": "6.4.1",
"starknetkit": "1.1.9",
"strkfarm-sdk": "git+https://github.com/strkfarm/sdk-ts.git#main",
"swr": "2.2.5",
"wonka": "6.3.4"
},
Expand Down
13 changes: 3 additions & 10 deletions src/strategies/auto_strk.strat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import {
getERC20Balance,
getERC20BalanceAtom,
} from '@/store/balance.atoms';
import { getTokenInfoFromName } from '@/utils';
import axios from 'axios';
import { getPrice, getTokenInfoFromName } from '@/utils';

interface Step {
name: string;
Expand Down Expand Up @@ -133,10 +132,7 @@ export class AutoTokenStrategy extends IStrategy {
tokenInfo: this.token,
};
}
const priceInfo = await axios.get(
`https://api.coinbase.com/v2/prices/${this.token.name}-USDT/spot`,
);
const price = Number(priceInfo.data.data.amount);
const price = await getPrice(this.token);
console.log('getUserTVL autoc', price, balanceInfo.amount.toEtherStr());
return {
amount: balanceInfo.amount,
Expand All @@ -155,10 +151,7 @@ export class AutoTokenStrategy extends IStrategy {

const zTokenInfo = getTokenInfoFromName(this.lpTokenName);
const bal = await getERC20Balance(zTokenInfo, this.strategyAddress);
const priceInfo = await axios.get(
`https://api.coinbase.com/v2/prices/${this.token.name}-USDT/spot`,
);
const price = Number(priceInfo.data.data.amount);
const price = await getPrice(this.token);
return {
amount: bal.amount,
usdValue: Number(bal.amount.toEtherStr()) * price,
Expand Down
14 changes: 3 additions & 11 deletions src/strategies/delta_neutral_mm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ import DeltaNeutralAbi from '@/abi/deltraNeutral.abi.json';
import MyNumber from '@/utils/MyNumber';
import { Call, Contract, ProviderInterface, uint256 } from 'starknet';
import { nostraLending } from '@/store/nostralending.store';
import { getTokenInfoFromName, standariseAddress } from '@/utils';
import { getPrice, getTokenInfoFromName, standariseAddress } from '@/utils';
import {
DUMMY_BAL_ATOM,
getBalance,
getBalanceAtom,
getERC20Balance,
} from '@/store/balance.atoms';
import { atom } from 'jotai';
import axios from 'axios';

export class DeltaNeutralMM extends IStrategy {
riskFactor = 0.75;
Expand Down Expand Up @@ -298,10 +297,7 @@ export class DeltaNeutralMM extends IStrategy {
tokenInfo: this.token,
};
}
const priceInfo = await axios.get(
`https://api.coinbase.com/v2/prices/${balanceInfo.tokenInfo.name}-USDT/spot`,
);
const price = Number(priceInfo.data.data.amount);
const price = await getPrice(balanceInfo.tokenInfo);
console.log('getUserTVL dnmm', price, balanceInfo.amount.toEtherStr());
return {
amount: balanceInfo.amount,
Expand All @@ -328,11 +324,7 @@ export class DeltaNeutralMM extends IStrategy {
const discountFactor = this.stepAmountFactors[4];
const amount = bal.amount.operate('div', 1 + discountFactor);
console.log('getTVL1', amount.toString());
const priceInfo = await axios.get(
`https://api.coinbase.com/v2/prices/${mainTokenName}-USDT/spot`,
);
console.log('getTVL2', priceInfo);
const price = Number(priceInfo.data.data.amount);
const price = await getPrice(this.token);
return {
amount,
usdValue: Number(amount.toEtherStr()) * price,
Expand Down
42 changes: 42 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { MenuItemProps, MenuListProps } from '@chakra-ui/react';
import { num } from 'starknet';
import { TOKENS } from './constants';
import toast from 'react-hot-toast';
import { TokenInfo } from './strategies/IStrategy';
import axios from 'axios';

export function getUniqueStrings(arr: Array<string>) {
const _arr: string[] = [];
Expand Down Expand Up @@ -113,3 +115,43 @@ export function copyReferralLink(refCode: string) {
position: 'bottom-right',
});
}

// only meant for backend calls
let redisClient: any = null;
async function initRedis() {
if (typeof window === 'undefined') {
console.log('initRedis server');
// eslint-disable-next-line
const strkFarmSdk = require('strkfarm-sdk');
console.log('strkFarmSdk', strkFarmSdk);
const pricer = new strkFarmSdk.PricerRedis(null, []);
if (!process.env.REDIS_URL) {
console.warn('REDIS_URL not set');
return;
}
await pricer.initRedis(process.env.REDIS_URL);
redisClient = pricer;
}
}

initRedis();

export async function getPrice(tokenInfo: TokenInfo) {
if (redisClient) {
const priceInfo = await redisClient.getPrice(tokenInfo.name);
console.log('getPrice redis', priceInfo, tokenInfo.name);
const now = new Date().getTime();
const priceTime = new Date(priceInfo.timestamp).getTime();
if (now - priceTime < 1000 * 60 * 5) {
return priceInfo.price as number;
}
} else if (typeof window === 'undefined') {
initRedis();
}
console.log('getPrice coinbase', tokenInfo.name);
const priceInfo = await axios.get(
`https://api.coinbase.com/v2/prices/${tokenInfo.name}-USDT/spot`,
);
const price = Number(priceInfo.data.data.amount);
return price;
}
Loading

0 comments on commit 1f2c1bb

Please sign in to comment.