Skip to content

Commit

Permalink
Merge pull request #1112 from TempleDAO/stage
Browse files Browse the repository at this point in the history
stage -> main
  • Loading branch information
frontier159 authored Nov 1, 2024
2 parents 60e4d7c + b439c20 commit ff74bc7
Show file tree
Hide file tree
Showing 30 changed files with 42,690 additions and 301,502 deletions.
3 changes: 2 additions & 1 deletion apps/dapp/.env.local.example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
VITE_ENV=local
VITE_ENV=local
VITE_BALANCER_SUBGRAPH_API_KEY=123 # From https://thegraph.com/studio
1 change: 1 addition & 0 deletions apps/dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"ethereumjs-util": "^7.1.5",
"ethers": "5.7.0",
"events": "^3.3.0",
"exponential-backoff": "^3.1.1",
"lottie-web": "^5.9.4",
"millify": "^4.0.0",
"polished": "^4.1.3",
Expand Down
17 changes: 12 additions & 5 deletions apps/dapp/src/components/Pages/Core/DappPages/Borrow/TLC/Repay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,25 @@ export const Repay: React.FC<IProps> = ({
setState({
...state,
repayValue: accountPosition
? formatToken(accountPosition.currentDebt, state.outputToken)
? accountPosition.currentDebt.gt(state.outputTokenBalance)
? formatToken(state.outputTokenBalance, state.outputToken)
: formatToken(accountPosition.currentDebt, state.outputToken)
: '0',
});
}}
min={0}
// Max is total debt
hint={`Max: ${formatToken(
accountPosition ? accountPosition.currentDebt : ZERO,
accountPosition
? accountPosition.currentDebt.gt(state.outputTokenBalance)
? state.outputTokenBalance
: accountPosition.currentDebt
: ZERO,
state.outputToken
)}`}
width="100%"
/>
{fromAtto(state.outputTokenBalance) < Number(state.repayValue) && (
{fromAtto(state.outputTokenBalance).toFixed(2) <
Number(state.repayValue).toFixed(2) && (
<Warning>
<InfoCircle>
<p>i</p>
Expand Down Expand Up @@ -164,7 +170,8 @@ export const Repay: React.FC<IProps> = ({
// Disable if repay amount is lte zero, or gt wallet balance
disabled={
Number(state.repayValue) <= 0 ||
fromAtto(state.outputTokenBalance) < Number(state.repayValue)
fromAtto(state.outputTokenBalance).toFixed(2) <
Number(state.repayValue).toFixed(2)
}
style={{ width: 'auto' }}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const BorrowPage = () => {
templePrice: data.tokens.filter((t: any) => t.symbol == 'TEMPLE')[0]
.price,
daiPrice: data.tokens.filter((t: any) => t.symbol == 'DAI')[0].price,
tpi: data.treasuryReservesVaults[0].treasuryPriceIndex,
tpi: Number(data.treasuryReservesVaults[0].treasuryPriceIndex),
});
}, []);

Expand Down Expand Up @@ -718,7 +718,9 @@ export const BorrowPage = () => {
<BrandParagraph>Current Borrow APY </BrandParagraph>
</MetricContainer>
<MetricContainer>
<LeadMetric>{showLoading ? '...' : prices.tpi}</LeadMetric>
<LeadMetric>
{showLoading ? '...' : prices.tpi.toFixed(2)}
</LeadMetric>
<BrandParagraph>Current TPI</BrandParagraph>
</MetricContainer>
</Metrics>
Expand Down
16 changes: 5 additions & 11 deletions apps/dapp/src/constants/env/local.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ADDRESS_ZERO } from 'utils/bigNumber';
import { Environment } from './types';

const ENV = import.meta.env;
const BALANCER_SUBGRAPH_API_KEY = import.meta.env
.VITE_BALANCER_SUBGRAPH_API_KEY;

const env: Environment = {
alchemyId: '-nNWThz_YpX1cGffGiz-lbSMu7dmp4GK',
Expand Down Expand Up @@ -37,6 +38,7 @@ const env: Environment = {
vaultEarlyExit: '',
ramos: '',
ramosPoolHelper: '',
templeDaiBalancerPool: '',
balancerHelpers: '',
strategies: {
dsrBaseStrategy: '',
Expand All @@ -51,26 +53,18 @@ const env: Environment = {
},
infuraId: '4cd22916292d4fb6be156454978c326b',
subgraph: {
// TODO: These need updated to the templedao organization subgraphs once they are deployed
templeCore: 'http://localhost:8000/subgraphs/name/templedao-core',
protocolMetrics:
'https://subgraph.satsuma-prod.com/a912521dd162/templedao/temple-metrics/api',
protocolMetricsArbitrum:
'https://api.studio.thegraph.com/query/76011/temple-metrics-arbitrum/version/latest',
// TODO: This is not used anymore and should be removed
balancerV2:
'https://api.thegraph.com/subgraphs/name/templedao/templedao-balancer-v2',
// TODO: Will be deprecated
balancerV2: `https://gateway.thegraph.com/api/${BALANCER_SUBGRAPH_API_KEY}/subgraphs/id/C4ayEZP2yTXRAB8vSaTrgN4m9anTe9Mdm2ViyiAuV9TV`,
ramos:
'https://api.studio.thegraph.com/query/76011/temple-ramos/version/latest',
// templeV2: 'https://api.studio.thegraph.com/query/520/v2-sepolia/version/latest',
'https://subgraph.satsuma-prod.com/a912521dd162/templedao/temple-ramos/api',
templeV2:
'https://subgraph.satsuma-prod.com/a912521dd162/templedao/temple-v2-mainnet/api',
templeV2Balances:
'https://subgraph.satsuma-prod.com/a912521dd162/templedao/temple-v2-balances/api',

// Original Balancer Subgraph
// balancerV2: 'https://api.thegraph.com/subgraphs/name/balancer-labs/balancer-goerli-v2',
},
intervals: {
ascendData: 30_000,
Expand Down
14 changes: 6 additions & 8 deletions apps/dapp/src/constants/env/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { ADDRESS_ZERO } from 'utils/bigNumber';
import { Environment } from './types';

const BALANCER_SUBGRAPH_API_KEY = import.meta.env
.VITE_BALANCER_SUBGRAPH_API_KEY;

const env: Environment = {
alchemyId: 'AorwfDdHDsEjIX4HPwS70zkVjWqjv5vZ',
rpcUrl: 'https://rpc.ankr.com/eth',
Expand Down Expand Up @@ -35,6 +38,7 @@ const env: Environment = {
vaultEarlyExit: '',
ramos: '0x82ce000a51E8474378f7b555bcC4de5992052452',
ramosPoolHelper: '0xbfC24c9d7D57C413618CE11cea1e313a2E8D9e1d',
templeDaiBalancerPool: '',
balancerHelpers: '0xdAE7e32ADc5d490a43cCba1f0c736033F2b4eFca',
strategies: {
dsrBaseStrategy: '0x472C7cDb6E730ff499E118dE6260c6b44c61d7bf',
Expand All @@ -48,25 +52,19 @@ const env: Environment = {
templeCircuitBreaker: '0x8f783c4A3d90712A794d5660b632AC67611852aF',
},
subgraph: {
// TODO: These need updated to the templedao organization subgraphs once they are deployed
templeCore:
'https://api.studio.thegraph.com/query/76011/temple-core/version/latest',
protocolMetrics:
'https://subgraph.satsuma-prod.com/a912521dd162/templedao/temple-metrics/api',
protocolMetricsArbitrum:
'https://api.studio.thegraph.com/query/76011/temple-metrics-arbitrum/version/latest',
// TODO: This is not used anymore and should be removed
balancerV2:
'https://api.thegraph.com/subgraphs/name/templedao/templedao-balancer-v2',
// TODO: Will be deprecated
balancerV2: `https://gateway.thegraph.com/api/${BALANCER_SUBGRAPH_API_KEY}/subgraphs/id/C4ayEZP2yTXRAB8vSaTrgN4m9anTe9Mdm2ViyiAuV9TV`,
ramos:
'https://api.studio.thegraph.com/query/76011/temple-ramos/version/latest',
'https://subgraph.satsuma-prod.com/a912521dd162/templedao/temple-ramos/api',
templeV2:
'https://subgraph.satsuma-prod.com/a912521dd162/templedao/temple-v2-mainnet/api',
templeV2Balances:
'https://subgraph.satsuma-prod.com/a912521dd162/templedao/temple-v2-balances/api',
// Original Balancer Subgraph
// balancerV2: 'https://api.thegraph.com/subgraphs/name/balancer-labs/balancer-goerli-v2',
},
gas: {
swapFraxForTemple: 300000,
Expand Down
15 changes: 7 additions & 8 deletions apps/dapp/src/constants/env/production.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { ADDRESS_ZERO } from 'utils/bigNumber';
import { Environment } from './types';

const BALANCER_SUBGRAPH_API_KEY = import.meta.env
.VITE_BALANCER_SUBGRAPH_API_KEY;

const env: Environment = {
alchemyId: 'XiIZxWykHU5AOFBwxKgxseXWN984Mp8F',
rpcUrl: 'https://rpc.ankr.com/eth',
Expand Down Expand Up @@ -174,6 +177,8 @@ const env: Environment = {
vaultEarlyExit: '0x24719d3AF60e1B622a29317d29E5Ce283617DeEC',
ramos: '0xDdF499e726Bfde29Ce035F6B355e55757F08B5EF',
ramosPoolHelper: '0xe32089bf9724aF09C026BeC36a7d8a81500cd58A',
templeDaiBalancerPool:
'0x8bd4a1e74a27182d23b98c10fd21d4fbb0ed4ba00002000000000000000004ed',
balancerHelpers: '0x5aDDCCa35b7A0D07C74063c48700C8590E87864E',
strategies: {
dsrBaseStrategy: '0x8b9e20D9970Af54fbaFe64049174e24d6DE0C412',
Expand Down Expand Up @@ -253,25 +258,19 @@ const env: Environment = {
network: 1,
etherscan: 'https://etherscan.io',
subgraph: {
// TODO: These need updated to the templedao organization subgraphs once they are deployed
templeCore:
'https://api.studio.thegraph.com/query/76011/temple-core/version/latest',
protocolMetrics:
'https://subgraph.satsuma-prod.com/a912521dd162/templedao/temple-metrics/api',
protocolMetricsArbitrum:
'https://api.studio.thegraph.com/query/76011/temple-metrics-arbitrum/version/latest',
// TODO: This is not used anymore and should be removed
balancerV2:
'https://api.thegraph.com/subgraphs/name/templedao/templedao-balancer-v2',
// TODO: Will be deprecated
balancerV2: `https://gateway.thegraph.com/api/${BALANCER_SUBGRAPH_API_KEY}/subgraphs/id/C4ayEZP2yTXRAB8vSaTrgN4m9anTe9Mdm2ViyiAuV9TV`,
ramos:
'https://api.studio.thegraph.com/query/76011/temple-ramos/version/latest',
'https://subgraph.satsuma-prod.com/a912521dd162/templedao/temple-ramos/api',
templeV2:
'https://subgraph.satsuma-prod.com/a912521dd162/templedao/temple-v2-mainnet/api',
templeV2Balances:
'https://subgraph.satsuma-prod.com/a912521dd162/templedao/temple-v2-balances/api',
// Original Balancer Subgraph
// balancerV2: 'https://api.thegraph.com/subgraphs/name/balancer-labs/balancer-v2-beta',
},
featureFlags: {
enableAscend: false,
Expand Down
1 change: 1 addition & 0 deletions apps/dapp/src/constants/env/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ interface Contracts {
fohmoGnosisStrategy: string;
};
ramosPoolHelper: string;
templeDaiBalancerPool: string;
balancerHelpers: string;
daiCircuitBreaker: string;
templeCircuitBreaker: string;
Expand Down
60 changes: 36 additions & 24 deletions apps/dapp/src/providers/SwapProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,26 @@ import { BalancerSDK, Network, SwapType, SwapInfo } from '@balancer-labs/sdk';
import VaultABI from 'data/abis/balancerVault.json';
import { formatToken } from 'utils/formatter';
import { ADDRESS_ZERO } from 'utils/bigNumber';
import { backOff } from 'exponential-backoff';
import { MAINNET_CHAIN } from 'utils/envChainMapping';

// Initialize balancer SOR
const maxPools = 4;
const balancer = new BalancerSDK({
network: Network.MAINNET,
rpcUrl: env.rpcUrl,
customSubgraphUrl: env.subgraph.balancerV2,
enableLogging: true,
});
const sor = balancer.sor;
const { swaps: balancerSwaps } = balancer; // Swaps module is abstracting SOR

const INITIAL_STATE: SwapService = {
buy: asyncNoop,
sell: asyncNoop,
getSellQuote: asyncNoop,
getBuyQuote: asyncNoop,
error: null,
sor: balancer.sor,
balancerSwaps,
};

// Build batchSwap transaction details
Expand Down Expand Up @@ -109,6 +113,8 @@ const buildSingleTransaction = (

const SwapContext = createContext(INITIAL_STATE);

class FetchPoolsError extends Error {}

// eslint-disable-next-line @typescript-eslint/ban-types
export const SwapProvider = (props: PropsWithChildren<{}>) => {
const [error, setError] = useState<Error | null>(null);
Expand All @@ -117,11 +123,19 @@ export const SwapProvider = (props: PropsWithChildren<{}>) => {

useEffect(() => {
const onMount = async () => {
try {
await sor.fetchPools();
} catch (e) {
console.log('failed to fetch sor pools');
}
return backOff(async () => {
const success = await balancerSwaps.fetchPools({
chainId: MAINNET_CHAIN.id,
where: {
id: {
eq: env.contracts.templeDaiBalancerPool,
},
},
});
if (!success) {
throw new FetchPoolsError();
}
});
};
onMount();
}, []);
Expand Down Expand Up @@ -309,14 +323,13 @@ export const SwapProvider = (props: PropsWithChildren<{}>) => {
const gasPrice = signer ? await signer?.getGasPrice() : BigNumber.from(0);

// Find swapInfo for best trade given pair and amount
const swapInfo: SwapInfo = await sor.getSwaps(
tokenInInfo.address,
tokenOutInfo.address,
0,
amountIn,
{ gasPrice, maxPools },
false
);
const swapInfo = await balancerSwaps.findRouteGivenIn({
tokenIn: tokenInInfo.address,
tokenOut: tokenOutInfo.address,
amount: amountIn,
gasPrice,
maxPools,
});
console.debug('swapInfo', swapInfo);
return swapInfo;
};
Expand All @@ -331,14 +344,13 @@ export const SwapProvider = (props: PropsWithChildren<{}>) => {
const gasPrice = signer ? await signer?.getGasPrice() : BigNumber.from(0);

// Find swapInfo for best trade given pair and amount
const swapInfo: SwapInfo = await sor.getSwaps(
tokenInInfo.address,
tokenOutInfo.address,
0,
amountToSell,
{ gasPrice, maxPools },
false
);
const swapInfo = await balancerSwaps.findRouteGivenIn({
tokenIn: tokenInInfo.address,
tokenOut: tokenOutInfo.address,
amount: amountToSell,
gasPrice,
maxPools,
});
return swapInfo;
};

Expand All @@ -350,7 +362,7 @@ export const SwapProvider = (props: PropsWithChildren<{}>) => {
getBuyQuote,
getSellQuote,
error,
sor,
balancerSwaps: balancerSwaps,
}}
>
{props.children}
Expand Down
12 changes: 3 additions & 9 deletions apps/dapp/src/providers/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { Network } from '@ethersproject/providers';
import {
BigNumber,
ContractReceipt,
Signer,
ContractTransaction,
} from 'ethers';
import { BigNumber, ContractReceipt, Signer } from 'ethers';
import { Nullable } from 'types/util';
import { TransactionReceipt } from '@ethersproject/abstract-provider';
import { TICKER_SYMBOL } from 'enums/ticker-symbol';
import { Sor, SwapInfo } from '@balancer-labs/sdk';
import { Swaps, SwapInfo } from '@balancer-labs/sdk';

export enum RitualKind {
OFFERING_STAKING = 'OFFERING_STAKING',
Expand Down Expand Up @@ -115,7 +109,7 @@ export interface SwapService {

error: Error | null;

sor: Sor;
balancerSwaps: Swaps;
}

export interface WalletState {
Expand Down
5 changes: 5 additions & 0 deletions apps/dapp/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10042,6 +10042,11 @@ expand-brackets@^2.1.4:
snapdragon "^0.8.1"
to-regex "^3.0.1"

exponential-backoff@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/exponential-backoff/-/exponential-backoff-3.1.1.tgz#64ac7526fe341ab18a39016cd22c787d01e00bf6"
integrity sha512-dX7e/LHVJ6W3DE1MHWi9S1EYzDESENfLrYohG2G++ovZrYOkm4Knwa0mc1cn84xJOR4KEU0WSchhLbd0UklbHw==

express@^4.14.0:
version "4.19.2"
resolved "https://registry.yarnpkg.com/express/-/express-4.19.2.tgz#e25437827a3aa7f2a827bc8171bbbb664a356465"
Expand Down
Loading

0 comments on commit ff74bc7

Please sign in to comment.