Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update soroswap-router-sdk and add get pairs from zephyr #458

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion .env.production.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,18 @@ NEXT_PUBLIC_DEFAULT_NETWORK=testnet
NEXT_PUBLIC_SOROSWAP_BACKEND_URL=https://backend.soroswap.finance
NEXT_PUBLIC_SOROSWAP_BACKEND_API_KEY=
NEXT_PUBLIC_TEST_TOKENS_ADMIN_SECRET_KEY=
NEXT_PUBLIC_TAG_ID=
NEXT_PUBLIC_TAG_ID=


#Mercury MAINNET
MERCURY_EMAIL_MAINNET=
MERCURY_PASSWORD_MAINNET=
MERCURY_BACKEND_ENDPOINT_MAINNET=https://mainnet.mercurydata.app:8443
MERCURY_GRAPHQL_ENDPOINT_MAINNET=https://mainnet.mercurydata.app:2083

#Mercury TESTNET
MERCURY_EMAIL_TESTNET=
MERCURY_PASSWORD_TESTNET=
MERCURY_BACKEND_ENDPOINT_TESTNET=https://api.mercurydata.app:8443
MERCURY_GRAPHQL_ENDPOINT_TESTNET=https://api.mercurydata.app:2083

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"i": "^0.3.7",
"lodash-es": "^4.17.21",
"luxon": "^3.4.4",
"mercury-sdk": "^0.1.22",
"next": "^14.2.3",
"npm": "^10.2.5",
"polished": "^4.2.2",
Expand All @@ -67,7 +68,7 @@
"react-virtualized-auto-sizer": "^1.0.20",
"react-window": "^1.8.9",
"redux": "^4.2.1",
"soroswap-router-sdk": "^1.2.4",
"soroswap-router-sdk": "^1.2.6",
"swr": "^2.2.0",
"typescript": "5.3.3",
"use-resize-observer": "^9.1.0"
Expand Down
107 changes: 107 additions & 0 deletions pages/api/pairs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { Mercury } from 'mercury-sdk';
import { NextApiRequest, NextApiResponse } from 'next';
import * as StellarSdk from '@stellar/stellar-sdk';

const testnetPairsTable = 'allZephyrAc4C7D40C78D2B474009391E80Aedb99S';
const mainnetPairsTable = 'allZephyr923625Cad8F2Bf73069B63583354Ba4As';

const parseScvalValue = (value: any) => {
const scval = StellarSdk.xdr.ScVal.fromXDR(value, 'base64');
return StellarSdk.scValToNative(scval);
};

const parseMercuryScvalResponse = (data: any) => {
return data.map((d: any) => {
let n: any = {};

for (let key in d) {
const value = parseScvalValue(d[key]);

if (typeof value === 'bigint' || typeof value === 'number') {
n[key] = value.toString();
} else {
n[key] = value;
}
}

return n;
});
};

const GET_ALL_PAIRS = (tableName: string) => `query Query {
events: ${tableName} {
data: nodes {
tokenA
tokenB
address
reserveA
reserveB
}
}
}`;

const getMercuryInstance = (network: QueryNetwork) => {
let backendEndpoint = null;
let graphqlEndpoint = null;
let email = null;
let password = null;

if (network === 'mainnet') {
email = process.env.MERCURY_EMAIL_MAINNET!;
password = process.env.MERCURY_PASSWORD_MAINNET!;
backendEndpoint = process.env.MERCURY_BACKEND_ENDPOINT_MAINNET!;
graphqlEndpoint = process.env.MERCURY_GRAPHQL_ENDPOINT_MAINNET!;
} else {
email = process.env.MERCURY_EMAIL_TESTNET!;
password = process.env.MERCURY_PASSWORD_TESTNET!;
backendEndpoint = process.env.MERCURY_BACKEND_ENDPOINT_TESTNET!;
graphqlEndpoint = process.env.MERCURY_GRAPHQL_ENDPOINT_TESTNET!;
}

return new Mercury({
backendEndpoint,
graphqlEndpoint,
email,
password,
});
};

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

type QueryNetwork = 'mainnet' | 'testnet';

async function handler(req: NextApiRequest, res: NextApiResponse) {
const queryParams = req.query;

const network = queryParams?.network as QueryNetwork;

if (!network || (network !== 'mainnet' && network !== 'testnet')) {
return res.status(400).json({ error: 'Invalid network' });
}

const mercuryInstance = getMercuryInstance(network);

const response = await mercuryInstance.getCustomQuery({
request: GET_ALL_PAIRS(network === 'mainnet' ? mainnetPairsTable : testnetPairsTable),
});

if (response.ok) {
const parsedData: MercuryPair[] = parseMercuryScvalResponse(response.data.events.data);

if (parsedData.length === 0) {
return res.status(500).json({ error: 'No pairs found' });
}

return res.json(parsedData);
}

return res.status(500).json({ error: 'Failed to fetch pairs from Mercury' });
}

export default handler;
29 changes: 15 additions & 14 deletions src/functions/generateRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { useFactory } from 'hooks';
import { useContext, useMemo } from 'react';
import { CurrencyAmount, Networks, Protocols, Router, Token, TradeType } from 'soroswap-router-sdk';
import { AppContext } from 'contexts';

const backendUrl = process.env.NEXT_PUBLIC_SOROSWAP_BACKEND_URL;
const backendApiKey = process.env.NEXT_PUBLIC_SOROSWAP_BACKEND_API_KEY;
const shouldUseBackend = process.env.NEXT_PUBLIC_SOROSWAP_BACKEND_ENABLED === 'true';
import axios from 'axios';

export interface GenerateRouteProps {
amountTokenAddress: string;
Expand All @@ -15,6 +12,11 @@ export interface GenerateRouteProps {
tradeType: TradeType;
}

const queryNetworkDict: { [x: string]: 'mainnet' | 'testnet' } = {
[Networks.PUBLIC]: 'mainnet',
[Networks.TESTNET]: 'testnet',
};

export const useRouterSDK = () => {
const sorobanContext = useSorobanReact();
const { factory } = useFactory(sorobanContext);
Expand All @@ -25,19 +27,19 @@ export const useRouterSDK = () => {
const network = sorobanContext.activeChain?.networkPassphrase as Networks;

const router = useMemo(() => {
if (!backendUrl || !backendApiKey) {
throw new Error(
'NEXT_PUBLIC_SOROSWAP_BACKEND_URL and NEXT_PUBLIC_SOROSWAP_BACKEND_API_KEY must be set in the environment variables.',
);
}

return new Router({
backendUrl,
backendApiKey,
getPairsFn: async () => {
let queryNetwork = queryNetworkDict[network];

const { data } = await axios.get('/api/pairs', {
params: { network: queryNetwork },
});

return data;
},
pairsCacheInSeconds: 60,
protocols: [Protocols.SOROSWAP],
network,
shouldUseBackend,
maxHops,
});
}, [network, maxHops]);
Expand All @@ -62,7 +64,6 @@ export const useRouterSDK = () => {
tradeType,
}: GenerateRouteProps) => {
if (!factory) throw new Error('Factory address not found');

const currencyAmount = fromAddressAndAmountToCurrencyAmount(amountTokenAddress, amount);
const quoteCurrency = fromAddressToToken(quoteTokenAddress);

Expand Down
54 changes: 43 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,11 @@
resolved "https://registry.yarnpkg.com/@floating-ui/utils/-/utils-0.2.2.tgz#d8bae93ac8b815b2bd7a98078cf91e2724ef11e5"
integrity sha512-J4yDIIthosAsRZ5CPYP/jQvUAQtlZTTD/4suA08/FEnlxqW3sKS9iAhgsa9VYLZ6vDHn/ixJgIqRQPotoBjxIw==

"@graphql-typed-document-node/core@^3.2.0":
version "3.2.0"
resolved "https://registry.yarnpkg.com/@graphql-typed-document-node/core/-/core-3.2.0.tgz#5f3d96ec6b2354ad6d8a28bf216a1d97b5426861"
integrity sha512-mB9oAsNCm9aM3/SOv4YtBMqZbYj10R7dkq8byBqxGY/ncFwhf2oQzMV+LCRlWoDSEBJ3COiR1yeDvMtsoOsuFQ==

"@humanwhocodes/config-array@^0.11.13":
version "0.11.14"
resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.11.14.tgz#d78e481a039f7566ecc9660b4ea7fe6b1fec442b"
Expand Down Expand Up @@ -880,11 +885,6 @@
dependencies:
glob "10.3.10"

"@next/[email protected]":
version "13.0.7"
resolved "https://registry.yarnpkg.com/@next/font/-/font-13.0.7.tgz#e0046376edb0ce592d9cfddea8f4ab321eb1515a"
integrity sha512-39SzuoMI6jbrIzPs3KtXdKX03OrVp6Y7kRHcoVmOg69spiBzruPJ5x5DQSfN+OXqznbvVBNZBXnmdnSqs3qXiA==

"@next/[email protected]":
version "14.2.3"
resolved "https://registry.yarnpkg.com/@next/swc-darwin-arm64/-/swc-darwin-arm64-14.2.3.tgz#db1a05eb88c0224089b815ad10ac128ec79c2cdb"
Expand Down Expand Up @@ -2316,7 +2316,7 @@
toml "^3.0.0"
urijs "^1.19.1"

"@stellar/[email protected]", "@stellar/stellar-sdk@^11.3.0":
"@stellar/[email protected]", "@stellar/stellar-sdk@^11.1.0", "@stellar/stellar-sdk@^11.3.0":
version "11.3.0"
resolved "https://registry.yarnpkg.com/@stellar/stellar-sdk/-/stellar-sdk-11.3.0.tgz#7cb010651846a07e1853e0fe30e430ece4da340b"
integrity sha512-i+heopibJNRA7iM8rEPz0AXphBPYvy2HDo8rxbDwWpozwCfw8kglP9cLkkhgJe8YicgLrdExz/iQZaLpqLC+6w==
Expand Down Expand Up @@ -4062,7 +4062,7 @@ [email protected]:
dependencies:
node-fetch "^2.6.12"

cross-fetch@^3.1.6:
cross-fetch@^3.1.5, cross-fetch@^3.1.6:
version "3.1.8"
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.1.8.tgz#0327eba65fd68a7d119f8fb2bf9334a1a7956f82"
integrity sha512-cvA+JwZoU0Xq+h6WkMvAUqPEYy92Obet6UdKLfW60qn99ftItKjB5T+BkyWOFWe2pUyfQ+IJHmpOTznqk1M6Kg==
Expand Down Expand Up @@ -5504,6 +5504,14 @@ graphemer@^1.4.0:
resolved "https://registry.yarnpkg.com/graphemer/-/graphemer-1.4.0.tgz#fb2f1d55e0e3a1849aeffc90c4fa0dd53a0e66c6"
integrity sha512-EtKwoO6kxCL9WO5xipiHTZlSzBm7WLT627TqC/uVRd0HKmq8NXyebnNYxDoBi7wt8eTWrUrKXCOVaFq9x1kgag==

graphql-request@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/graphql-request/-/graphql-request-6.1.0.tgz#f4eb2107967af3c7a5907eb3131c671eac89be4f"
integrity sha512-p+XPfS4q7aIpKVcgmnZKhMNqhltk20hfXtkaIkTfjjmiKMJ5xrt5c743cL03y/K7y1rg3WrIC49xGiEQ4mxdNw==
dependencies:
"@graphql-typed-document-node/core" "^3.2.0"
cross-fetch "^3.1.5"

graphql@^16.8.1:
version "16.8.1"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-16.8.1.tgz#1930a965bef1170603702acdb68aedd3f3cf6f07"
Expand Down Expand Up @@ -6718,6 +6726,20 @@ [email protected]:
resolved "https://registry.yarnpkg.com/memoize-one/-/memoize-one-5.2.1.tgz#8337aa3c4335581839ec01c3d594090cebe8f00e"
integrity sha512-zYiwtZUcYyXKo/np96AGZAckk+FWWsUdJ3cHGGmld7+AhvcWmQyGCYUh1hc4Q/pkOhb65dQR/pqCyK0cOaHz4Q==

mercury-sdk@^0.1.22:
version "0.1.22"
resolved "https://registry.yarnpkg.com/mercury-sdk/-/mercury-sdk-0.1.22.tgz#527fd3478c92229b07346ac1ff84600c9690cfed"
integrity sha512-Hae9cUIXPmURfcNxdzpaQWZU/l7C+fCUv+IYskB3ckEnbptc3iNwO+tC7W05WcGWjG6tUINPqS+t9R4J4XSK4A==
dependencies:
"@stellar/stellar-sdk" "^11.1.0"
axios "^1.6.2"
bigint-conversion "^2.4.3"
dotenv "^16.3.1"
graphql "^16.8.1"
graphql-request "^6.1.0"
typedoc-plugin-missing-exports "^2.2.0"
typescript "^5.3.2"

merge-stream@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60"
Expand Down Expand Up @@ -8760,10 +8782,10 @@ [email protected]:
stellar-base v10.0.0-beta.4
urijs "^1.19.1"

soroswap-router-sdk@^1.2.4:
version "1.2.4"
resolved "https://registry.yarnpkg.com/soroswap-router-sdk/-/soroswap-router-sdk-1.2.4.tgz#9287d56bfc389cd299bddbc46682ccd0ea5ca80a"
integrity sha512-DioKasFesXZFpmkadbOdl/SPWO0q0q3YYaD+6Tnqq1tycwV0egN2nxmV1UoXhCykgXvN9xYr3lmLZ305Whf1Dg==
soroswap-router-sdk@^1.2.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==
dependencies:
"@stellar/stellar-sdk" "^11.3.0"
axios "^1.6.5"
Expand Down Expand Up @@ -9454,11 +9476,21 @@ typed-array-length@^1.0.6:
is-typed-array "^1.1.13"
possible-typed-array-names "^1.0.0"

typedoc-plugin-missing-exports@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/typedoc-plugin-missing-exports/-/typedoc-plugin-missing-exports-2.2.0.tgz#e39a04bab5b0d7f7b28507d64c07a4c40b788648"
integrity sha512-2+XR1IcyQ5UwXZVJe9NE6HrLmNufT9i5OwoIuuj79VxuA3eYq+Y6itS9rnNV1D7UeQnUSH8kISYD73gHE5zw+w==

[email protected]:
version "5.3.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.3.3.tgz#b3ce6ba258e72e6305ba66f5c9b452aaee3ffe37"
integrity sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==

typescript@^5.3.2:
version "5.4.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.4.5.tgz#42ccef2c571fdbd0f6718b1d1f5e6e5ef006f611"
integrity sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==

ufo@^1.3.2:
version "1.5.3"
resolved "https://registry.yarnpkg.com/ufo/-/ufo-1.5.3.tgz#3325bd3c977b6c6cd3160bf4ff52989adc9d3344"
Expand Down