From 930335477108f3b6607b6260bd0141cd8c8f2555 Mon Sep 17 00:00:00 2001 From: Lucas Willems Date: Sat, 30 Nov 2024 11:33:58 +0100 Subject: [PATCH] Allow to customize the proxy URL --- website/app/data-converter/pageClient.tsx | 18 +++++--- website/pages/interact-contract.mdx | 2 +- xsuite/src/data/constants.ts | 15 +++++++ xsuite/src/data/index.ts | 14 ++++++ xsuite/src/interact/envChain.ts | 53 ++++++++++++---------- xsuite/src/interact/index.test.ts | 54 ++++++++++++++++++++--- xsuite/src/proxy/proxy.ts | 28 ++++++------ xsuite/src/world/world.ts | 28 ++++++------ 8 files changed, 149 insertions(+), 63 deletions(-) create mode 100644 xsuite/src/data/constants.ts diff --git a/website/app/data-converter/pageClient.tsx b/website/app/data-converter/pageClient.tsx index 1a6afe96..a9f5e5c9 100644 --- a/website/app/data-converter/pageClient.tsx +++ b/website/app/data-converter/pageClient.tsx @@ -31,7 +31,7 @@ import React, { import { FaPlus, FaArrowLeft, FaArrowRight, FaArrowDown } from "react-icons/fa"; import { FaXmark } from "react-icons/fa6"; import { LuClipboard } from "react-icons/lu"; -import { e, d, B64 } from "xsuite/data"; +import { e, d, B64, mainnetMvxProxyUrl } from "xsuite/data"; import { Proxy } from "xsuite/proxy"; export default function PageClient() { @@ -47,9 +47,10 @@ export default function PageClient() { const DataConverter = () => { const converters = useConverters(); const [address, setAddress] = useState(""); + const [proxyUrl, setProxyUrl] = useState(mainnetMvxProxyUrl); const query = useQuery({ - queryKey: ["address", address], - queryFn: () => proxy.getSerializableAccount(address), + queryKey: ["proxyUrl", proxyUrl, "address", address], + queryFn: () => new Proxy(proxyUrl).getSerializableAccount(address), }); const addressMainnetState = JSON.stringify(query.data, null, 2); @@ -78,12 +79,19 @@ const DataConverter = () => { ))} - Get address mainnet state + Get address state setAddress(e.currentTarget.value)} /> + + setProxyUrl(e.currentTarget.value)} + /> @@ -399,8 +407,6 @@ const genId = () => Math.random(); const queryClient = new QueryClient(); -const proxy = Proxy.newMainnet(); - const dataTypes = { hex: "Hex", base64: "Base64", diff --git a/website/pages/interact-contract.mdx b/website/pages/interact-contract.mdx index ea746abf..8dc842a2 100644 --- a/website/pages/interact-contract.mdx +++ b/website/pages/interact-contract.mdx @@ -123,7 +123,7 @@ const loadWallet = () => world.newWalletFromFile("wallet.json"); const program = new Command(); ``` -Here we define the `world` object using `World.new()` which takes the `proxyUrl`, `chainId` and `gasPrice` as arguments. Using the `envChain` utility we can easily get the appropriate `publicProxyUrl` (i.e. https://devnet-gateway.multiversx.com/ for Devnet) and chain `id` depending on the current `CHAIN` environment variable value that is set in the `scripts` section of our `package.json` file. +Here we define the `world` object using `World.new()` which takes the `proxyUrl`, `chainId` and `gasPrice` as arguments. Using the `envChain` utility we can easily get the appropriate `mvxProxyUrl` (i.e. https://devnet-gateway.multiversx.com/ for Devnet) and chain `id` depending on the current `CHAIN` environment variable value that is set in the `scripts` section of our `package.json` file. We then declare the function `loadWallet` to load a wallet from a `.json` file using the `world.newWalletFromFile()` function. The reason we don't call this directly is that we will get a password prompt after running this function, and we only want to trigger that after `commander` handles our custom command. diff --git a/xsuite/src/data/constants.ts b/xsuite/src/data/constants.ts new file mode 100644 index 00000000..56f9e531 --- /dev/null +++ b/xsuite/src/data/constants.ts @@ -0,0 +1,15 @@ +export const devnetChainId = "D"; +export const testnetChainId = "T"; +export const mainnetChainId = "1"; + +export const devnetMvxProxyUrl = "https://devnet-gateway.multiversx.com"; +export const testnetMvxProxyUrl = "https://testnet-gateway.multiversx.com"; +export const mainnetMvxProxyUrl = "https://gateway.multiversx.com"; + +export const devnetMinGasPrice = 1_000_000_000; +export const testnetMinGasPrice = 1_000_000_000; +export const mainnetMinGasPrice = 1_000_000_000; + +export const devnetMvxExplorerUrl = "https://devnet-explorer.multiversx.com"; +export const testnetMvxExplorerUrl = "https://testnet-explorer.multiversx.com"; +export const mainnetMvxExplorerUrl = "https://explorer.multiversx.com"; diff --git a/xsuite/src/data/index.ts b/xsuite/src/data/index.ts index c7cf80cb..f2a694d9 100644 --- a/xsuite/src/data/index.ts +++ b/xsuite/src/data/index.ts @@ -3,5 +3,19 @@ export { AddressLike } from "./addressLike"; export { b64, B64 } from "./b64"; export { Bytes } from "./bytes"; export { BytesLike } from "./bytesLike"; +export { + devnetChainId, + testnetChainId, + mainnetChainId, + devnetMvxProxyUrl, + testnetMvxProxyUrl, + mainnetMvxProxyUrl, + devnetMinGasPrice, + testnetMinGasPrice, + mainnetMinGasPrice, + devnetMvxExplorerUrl, + testnetMvxExplorerUrl, + mainnetMvxExplorerUrl, +} from "./constants"; export { Encodable, e } from "./encoding"; export { Decoder, d, InferDecoderOutput } from "./decoding"; diff --git a/xsuite/src/interact/envChain.ts b/xsuite/src/interact/envChain.ts index b9f506df..8a3a0da9 100644 --- a/xsuite/src/interact/envChain.ts +++ b/xsuite/src/interact/envChain.ts @@ -1,18 +1,17 @@ -export const devnetChainId = "D"; -export const testnetChainId = "T"; -export const mainnetChainId = "1"; - -export const devnetPublicProxyUrl = "https://devnet-gateway.multiversx.com"; -export const testnetPublicProxyUrl = "https://testnet-gateway.multiversx.com"; -export const mainnetPublicProxyUrl = "https://gateway.multiversx.com"; - -export const devnetMinGasPrice = 1_000_000_000; -export const testnetMinGasPrice = 1_000_000_000; -export const mainnetMinGasPrice = 1_000_000_000; - -export const devnetExplorerUrl = "https://devnet-explorer.multiversx.com"; -export const testnetExplorerUrl = "https://testnet-explorer.multiversx.com"; -export const mainnetExplorerUrl = "https://explorer.multiversx.com"; +import { + devnetChainId, + devnetMinGasPrice, + devnetMvxExplorerUrl, + devnetMvxProxyUrl, + mainnetChainId, + mainnetMinGasPrice, + mainnetMvxExplorerUrl, + mainnetMvxProxyUrl, + testnetChainId, + testnetMinGasPrice, + testnetMvxExplorerUrl, + testnetMvxProxyUrl, +} from "../data/constants"; export const envChain = { name: (): ChainName => { @@ -38,11 +37,11 @@ export const envChain = { testnet: testnetChainId, mainnet: mainnetChainId, } as const), - publicProxyUrl: () => + mvxProxyUrl: () => envChain.select({ - devnet: devnetPublicProxyUrl, - testnet: testnetPublicProxyUrl, - mainnet: mainnetPublicProxyUrl, + devnet: devnetMvxProxyUrl, + testnet: testnetMvxProxyUrl, + mainnet: mainnetMvxProxyUrl, } as const), minGasPrice: () => envChain.select({ @@ -50,12 +49,20 @@ export const envChain = { testnet: testnetMinGasPrice, mainnet: mainnetMinGasPrice, } as const), - explorerUrl: () => + mvxExplorerUrl: () => envChain.select({ - devnet: devnetExplorerUrl, - testnet: testnetExplorerUrl, - mainnet: mainnetExplorerUrl, + devnet: devnetMvxExplorerUrl, + testnet: testnetMvxExplorerUrl, + mainnet: mainnetMvxExplorerUrl, } as const), + /** + * @deprecated Use `.mvxProxyUrl` instead. + */ + publicProxyUrl: () => envChain.mvxProxyUrl(), + /** + * @deprecated Use `.mvxExplorerUrl` instead. + */ + explorerUrl: () => envChain.mvxExplorerUrl(), }; const isChainName = (chain: any): chain is ChainName => { diff --git a/xsuite/src/interact/index.test.ts b/xsuite/src/interact/index.test.ts index 42b78b3e..3a41f9e6 100644 --- a/xsuite/src/interact/index.test.ts +++ b/xsuite/src/interact/index.test.ts @@ -55,21 +55,65 @@ test("envChain.id - mainnet", () => { expect(envChain.id()).toEqual("1"); }); -test("envChain.publicProxyUrl - devnet", () => { +test("envChain.mvxProxyUrl - devnet", () => { process.env.CHAIN = "devnet"; - expect(envChain.publicProxyUrl()).toEqual( + expect(envChain.mvxProxyUrl()).toEqual( "https://devnet-gateway.multiversx.com", ); }); -test("envChain.publicProxyUrl - testnet", () => { +test("envChain.mvxProxyUrl - testnet", () => { process.env.CHAIN = "testnet"; - expect(envChain.publicProxyUrl()).toEqual( + expect(envChain.mvxProxyUrl()).toEqual( "https://testnet-gateway.multiversx.com", ); }); -test("envChain.publicProxyUrl - mainnet", () => { +test("envChain.mvxProxyUrl - mainnet", () => { + process.env.CHAIN = "mainnet"; + expect(envChain.mvxProxyUrl()).toEqual("https://gateway.multiversx.com"); +}); + +test("envChain.minGasPrice - devnet", () => { + process.env.CHAIN = "devnet"; + expect(envChain.minGasPrice()).toEqual(1_000_000_000); +}); + +test("envChain.minGasPrice - testnet", () => { + process.env.CHAIN = "testnet"; + expect(envChain.minGasPrice()).toEqual(1_000_000_000); +}); + +test("envChain.minGasPrice - mainnet", () => { + process.env.CHAIN = "mainnet"; + expect(envChain.minGasPrice()).toEqual(1_000_000_000); +}); + +test("envChain.mvxExplorerUrl - devnet", () => { + process.env.CHAIN = "devnet"; + expect(envChain.mvxExplorerUrl()).toEqual( + "https://devnet-explorer.multiversx.com", + ); +}); + +test("envChain.mvxExplorerUrl - testnet", () => { + process.env.CHAIN = "testnet"; + expect(envChain.mvxExplorerUrl()).toEqual( + "https://testnet-explorer.multiversx.com", + ); +}); + +test("envChain.mvxExplorerUrl - mainnet", () => { + process.env.CHAIN = "mainnet"; + expect(envChain.mvxExplorerUrl()).toEqual("https://explorer.multiversx.com"); +}); + +test("envChain.publicProxyUrl", () => { process.env.CHAIN = "mainnet"; expect(envChain.publicProxyUrl()).toEqual("https://gateway.multiversx.com"); }); + +test("envChain.publicExplorerUrl", () => { + process.env.CHAIN = "mainnet"; + expect(envChain.explorerUrl()).toEqual("https://explorer.multiversx.com"); +}); diff --git a/xsuite/src/proxy/proxy.ts b/xsuite/src/proxy/proxy.ts index 002249c6..79848ef0 100644 --- a/xsuite/src/proxy/proxy.ts +++ b/xsuite/src/proxy/proxy.ts @@ -6,6 +6,14 @@ import { addressLikeToHex, } from "../data/addressLike"; import { BytesLike, bytesLikeToHex } from "../data/bytesLike"; +import { + devnetMvxExplorerUrl, + devnetMvxProxyUrl, + mainnetMvxExplorerUrl, + mainnetMvxProxyUrl, + testnetMvxExplorerUrl, + testnetMvxProxyUrl, +} from "../data/constants"; import { Encodable, EncodableCodeMetadata, @@ -14,14 +22,6 @@ import { import { Kvs } from "../data/kvs"; import { base64ToHex, u8aToHex } from "../data/utils"; import { Prettify } from "../helpers"; -import { - devnetExplorerUrl, - devnetPublicProxyUrl, - mainnetExplorerUrl, - mainnetPublicProxyUrl, - testnetExplorerUrl, - testnetPublicProxyUrl, -} from "../interact/envChain"; export class Proxy { proxyUrl: string; @@ -47,24 +47,24 @@ export class Proxy { static newDevnet(params: ProxyNewRealnetParams = {}) { return this.new({ - proxyUrl: devnetPublicProxyUrl, - explorerUrl: devnetExplorerUrl, + proxyUrl: devnetMvxProxyUrl, + explorerUrl: devnetMvxExplorerUrl, ...params, }); } static newTestnet(params: ProxyNewRealnetParams = {}) { return this.new({ - proxyUrl: testnetPublicProxyUrl, - explorerUrl: testnetExplorerUrl, + proxyUrl: testnetMvxProxyUrl, + explorerUrl: testnetMvxExplorerUrl, ...params, }); } static newMainnet(params: ProxyNewRealnetParams = {}) { return this.new({ - proxyUrl: mainnetPublicProxyUrl, - explorerUrl: mainnetExplorerUrl, + proxyUrl: mainnetMvxProxyUrl, + explorerUrl: mainnetMvxExplorerUrl, ...params, }); } diff --git a/xsuite/src/world/world.ts b/xsuite/src/world/world.ts index 4c664869..29033f3c 100644 --- a/xsuite/src/world/world.ts +++ b/xsuite/src/world/world.ts @@ -1,21 +1,21 @@ import { d, e } from "../data"; import { AddressLike, addressLikeToBech } from "../data/addressLike"; import { BytesLike } from "../data/bytesLike"; -import { Optional, Prettify, Replace } from "../helpers"; import { devnetChainId, - devnetExplorerUrl, + devnetMvxExplorerUrl, devnetMinGasPrice, - devnetPublicProxyUrl, + devnetMvxProxyUrl, testnetChainId, - testnetExplorerUrl, + testnetMvxExplorerUrl, testnetMinGasPrice, - testnetPublicProxyUrl, + testnetMvxProxyUrl, mainnetChainId, - mainnetExplorerUrl, + mainnetMvxExplorerUrl, mainnetMinGasPrice, - mainnetPublicProxyUrl, -} from "../interact/envChain"; + mainnetMvxProxyUrl, +} from "../data/constants"; +import { Optional, Prettify, Replace } from "../helpers"; import { CallContractTx, DeployContractTx, @@ -56,17 +56,17 @@ export class World { static new({ chainId, proxyUrl, gasPrice, explorerUrl }: WorldNewParams) { if (chainId === "D") { - proxyUrl ??= devnetPublicProxyUrl; + proxyUrl ??= devnetMvxProxyUrl; gasPrice ??= devnetMinGasPrice; - explorerUrl ??= devnetExplorerUrl; + explorerUrl ??= devnetMvxExplorerUrl; } else if (chainId === "T") { - proxyUrl ??= testnetPublicProxyUrl; + proxyUrl ??= testnetMvxProxyUrl; gasPrice ??= testnetMinGasPrice; - explorerUrl ??= testnetExplorerUrl; + explorerUrl ??= testnetMvxExplorerUrl; } else if (chainId === "1") { - proxyUrl ??= mainnetPublicProxyUrl; + proxyUrl ??= mainnetMvxProxyUrl; gasPrice ??= mainnetMinGasPrice; - explorerUrl ??= mainnetExplorerUrl; + explorerUrl ??= mainnetMvxExplorerUrl; } if (proxyUrl === undefined) { throw new Error("proxyUrl is not defined.");