Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/soroswap/frontend into feat…
Browse files Browse the repository at this point in the history
…/SDEX-quote-on-swap
  • Loading branch information
MattPoblete committed Jun 21, 2024
2 parents b4ecff4 + 5e4d0e4 commit 3471364
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 54 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"react-virtualized-auto-sizer": "^1.0.20",
"react-window": "^1.8.9",
"redux": "^4.2.1",
"soroswap-router-sdk": "1.2.6",
"soroswap-router-sdk": "^1.2.8",
"swr": "^2.2.0",
"typescript": "5.3.3",
"use-resize-observer": "^9.1.0"
Expand Down
40 changes: 16 additions & 24 deletions src/functions/generateRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useFactory } from 'hooks';
import { useContext, useMemo } from 'react';
import { Currency, CurrencyAmount, Networks, Percent, Protocols, Route, Router, Token, TradeType } from 'soroswap-router-sdk';
import { AppContext } from 'contexts';
import { fetchAllPhoenixPairs, fetchAllSoroswapPairs } from 'services/pairs';
import axios from 'axios';
import { TokenType } from 'interfaces';
import { getBestPath, getHorizonBestPath } from 'helpers/horizon/getHorizonPath';
Expand Down Expand Up @@ -37,6 +38,8 @@ const queryNetworkDict: { [x: string]: 'MAINNET' | 'TESTNET' } = {
[Networks.TESTNET]: 'TESTNET',
};

const shouldUseBackend = process.env.NEXT_PUBLIC_SOROSWAP_BACKEND_ENABLED === 'true';

export const useRouterSDK = () => {
const sorobanContext = useSorobanReact();
const { factory } = useFactory(sorobanContext);
Expand All @@ -48,31 +51,20 @@ export const useRouterSDK = () => {

const router = useMemo(() => {
return new Router({
getPairsFn: async () => {
let queryNetwork = queryNetworkDict[network];

const { data } = await axios.get<
{
tokenA: TokenType;
tokenB: TokenType;
reserveA: string;
reserveB: string;
}[]
>('https://info.soroswap.finance/api/pairs', {
params: { network: queryNetwork },
});

return data.map((pair) => {
return {
tokenA: pair.tokenA.contract,
tokenB: pair.tokenB.contract,
reserveA: pair.reserveA,
reserveB: pair.reserveB,
};
});
},
getPairsFns: shouldUseBackend
? [
{
protocol: Protocols.SOROSWAP,
fn: async () => fetchAllSoroswapPairs(network),
},
// {
// protocol: Protocols.PHOENIX,
// fn: async () => fetchAllPhoenixPairs(network),
// },
]
: undefined,
pairsCacheInSeconds: 60,
protocols: [Protocols.SOROSWAP],
protocols: [Protocols.SOROSWAP], //, Protocols.PHOENIX],
network,
maxHops,
});
Expand Down
25 changes: 14 additions & 11 deletions src/functions/getLpTokens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { SorobanContextType } from '@soroban-react/core';
import { tokenBalance } from 'hooks';
import { TokenMapType, TokenType } from 'interfaces';
import BigNumber from 'bignumber.js';
import { getTotalShares } from './LiquidityPools';

export type LpTokensObj = {
token_0: TokenType | undefined;
Expand Down Expand Up @@ -35,38 +36,40 @@ const getLpResultsFromBackendPairs = async (

for (const element of pairsBackend) {
const pairLpTokens = await tokenBalance(
element.contractId,
element.address,
sorobanContext.address,
sorobanContext,
);

if (pairLpTokens != 0) {
const token_0 = await findToken(element.token0, tokensAsMap, sorobanContext);
const token_1 = await findToken(element.token1, tokensAsMap, sorobanContext);
const token_0 = await findToken(element.tokenA, tokensAsMap, sorobanContext);
const token_1 = await findToken(element.tokenB, tokensAsMap, sorobanContext);

const totalShares = await getTotalShares(element.address, sorobanContext);

const lpPercentage = BigNumber(pairLpTokens as BigNumber)
.dividedBy(Number(element.totalShares))
.dividedBy(Number(totalShares))
.multipliedBy(100)
.decimalPlaces(7);

if (!token_0 || !token_1) return;

const myReserve0 = BigNumber(pairLpTokens as BigNumber)
?.multipliedBy(BigNumber(element.reserve0))
.dividedBy(Number(element.totalShares));
?.multipliedBy(BigNumber(element.reserveA))
.dividedBy(Number(totalShares));
const myReserve1 = BigNumber(pairLpTokens as BigNumber)
?.multipliedBy(BigNumber(element.reserve1))
.dividedBy(Number(element.totalShares));
?.multipliedBy(BigNumber(element.reserveB))
.dividedBy(Number(totalShares));

const toReturn = {
token_0,
token_1,
balance: pairLpTokens,
lpPercentage: lpPercentage.toString(),
status: 'Active',
reserve0: BigNumber(element.reserve0),
reserve1: BigNumber(element.reserve1),
totalShares: element.totalShares,
reserve0: BigNumber(element.reserveA),
reserve1: BigNumber(element.reserveB),
totalShares: totalShares,
myReserve0,
myReserve1,
};
Expand Down
35 changes: 22 additions & 13 deletions src/services/pairs.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import axios from 'axios';
import { Networks } from '@stellar/stellar-sdk';
interface SubscribedPair {
contractId: string;
token0: string;
token1: string;
reserve0: string;
reserve1: string;
totalShares: string;
protocol: string;

interface MercuryPair {
tokenA: string;
tokenB: string;
address: string;
reserveA: string;
reserveB: string;
}

const passphraseToBackendNetworkName: { [x: string]: string } = {
Expand All @@ -18,12 +17,22 @@ const passphraseToBackendNetworkName: { [x: string]: string } = {
export const fetchAllSoroswapPairs = async (networkPassphrase: string) => {
const networkName = passphraseToBackendNetworkName[networkPassphrase];

const { data } = await axios.post<SubscribedPair[]>(
`${process.env.NEXT_PUBLIC_SOROSWAP_BACKEND_URL}/pairs/all?network=${networkName}&protocols=soroswap`,
undefined,
const { data } = await axios.get<MercuryPair[]>(`https://info.soroswap.finance/api/pairs/plain`, {
params: {
network: networkName,
},
});
return data;
};

export const fetchAllPhoenixPairs = async (networkPassphrase: string) => {
const networkName = passphraseToBackendNetworkName[networkPassphrase];

const { data } = await axios.get<MercuryPair[]>(
`https://info.soroswap.finance/api/pairs/phoenix`,
{
headers: {
apiKey: process.env.NEXT_PUBLIC_SOROSWAP_BACKEND_API_KEY,
params: {
network: networkName,
},
},
);
Expand Down
29 changes: 24 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5135,6 +5135,17 @@ glob-parent@^6.0.2:
dependencies:
is-glob "^4.0.3"

[email protected]:
version "10.3.10"
resolved "https://registry.yarnpkg.com/glob/-/glob-10.3.10.tgz#0351ebb809fd187fe421ab96af83d3a70715df4b"
integrity sha512-fa46+tv1Ak0UPK1TOy/pZrIybNNt4HCv7SDzwyfiOZkvZLEbjsZkJBPtDHVshZjbecAoAGSC20MjLDG/qr679g==
dependencies:
foreground-child "^3.1.0"
jackspeak "^2.3.5"
minimatch "^9.0.1"
minipass "^5.0.0 || ^6.0.2 || ^7.0.0"
path-scurry "^1.10.1"

glob@^10.2.2, glob@^10.3.10, glob@^10.3.12:
version "10.3.14"
resolved "https://registry.yarnpkg.com/glob/-/glob-10.3.14.tgz#36501f871d373fe197fc5794588d0aa71e69ff68"
Expand Down Expand Up @@ -5764,7 +5775,7 @@ iterator.prototype@^1.1.2:
reflect.getprototypeof "^1.0.4"
set-function-name "^2.0.1"

jackspeak@^2.3.6:
jackspeak@^2.3.5, jackspeak@^2.3.6:
version "2.3.6"
resolved "https://registry.yarnpkg.com/jackspeak/-/jackspeak-2.3.6.tgz#647ecc472238aee4b06ac0e461acc21a8c505ca8"
integrity sha512-N3yCS/NegsOBokc8GAdM8UcmfsKiSS8cipheD/nivzr700H+nsMOxJjQnvwOcRYVuFkdH0wGUvW2WbXGmrZGbQ==
Expand Down Expand Up @@ -7251,6 +7262,14 @@ path-parse@^1.0.7:
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.7.tgz#fbc114b60ca42b30d9daf5858e4bd68bbedb6735"
integrity sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==

path-scurry@^1.10.1:
version "1.11.1"
resolved "https://registry.yarnpkg.com/path-scurry/-/path-scurry-1.11.1.tgz#7960a668888594a0720b12a911d1a742ab9f11d2"
integrity sha512-Xa4Nw17FS9ApQFJ9umLiJS4orGjm7ZzwUrwamcGQuHSzDyth9boKDaycYdDcZDuqYATXw4HFXgaqWTctW/v1HA==
dependencies:
lru-cache "^10.2.0"
minipass "^5.0.0 || ^6.0.2 || ^7.0.0"

path-scurry@^1.11.0:
version "1.11.0"
resolved "https://registry.yarnpkg.com/path-scurry/-/path-scurry-1.11.0.tgz#332d64e9726bf667fb348e5a1c71005c09ad741a"
Expand Down Expand Up @@ -8296,10 +8315,10 @@ [email protected]:
stellar-base v10.0.0-beta.4
urijs "^1.19.1"

[email protected].6:
version "1.2.6"
resolved "https://registry.yarnpkg.com/soroswap-router-sdk/-/soroswap-router-sdk-1.2.6.tgz#40633e8a9d028263a80a504b325b6ec068dea5b4"
integrity sha512-i2IkVoFWlf+gxsoR1egiXWEagKC8UJj0Wj+3Z/tBZvRgvbdyoMVTv2LefB4xWrRoCC4nPiLQxcej4qPLqcrNAQ==
soroswap-router-sdk@^1.2.8:
version "1.2.8"
resolved "https://registry.yarnpkg.com/soroswap-router-sdk/-/soroswap-router-sdk-1.2.8.tgz#17d5a6e69b39ce61aa6ca312f4f0f6dd28d8ec73"
integrity sha512-m8kHw5EGStNlYyE4eTBP5o5475yCNTAE+zyGUWW3uScQsLWinm7yRxq6U2biWMocMmj5Eo2rDQUmhJyrUpDanw==
dependencies:
"@stellar/stellar-sdk" "^11.3.0"
axios "^1.6.5"
Expand Down

0 comments on commit 3471364

Please sign in to comment.