Skip to content

Commit

Permalink
Allow to customize the proxy URL
Browse files Browse the repository at this point in the history
  • Loading branch information
lcswillems committed Nov 30, 2024
1 parent be0af45 commit 9303354
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 63 deletions.
18 changes: 12 additions & 6 deletions website/app/data-converter/pageClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);

Expand Down Expand Up @@ -78,12 +79,19 @@ const DataConverter = () => {
))}
</Box>
<Box mb="8" />
<Heading>Get address mainnet state</Heading>
<Heading>Get address state</Heading>
<Box mb="8" />
<Input
placeholder="Address"
value={address}
onChange={(e) => setAddress(e.currentTarget.value)}
/>
<Box mb="4" />
<Input
placeholder="Proxy URL"
value={proxyUrl}
onChange={(e) => setProxyUrl(e.currentTarget.value)}
/>
<Box mb="8" />
<Clipboard.Root value={addressMainnetState}>
<Clipboard.Trigger asChild>
Expand Down Expand Up @@ -399,8 +407,6 @@ const genId = () => Math.random();

const queryClient = new QueryClient();

const proxy = Proxy.newMainnet();

const dataTypes = {
hex: "Hex",
base64: "Base64",
Expand Down
2 changes: 1 addition & 1 deletion website/pages/interact-contract.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
15 changes: 15 additions & 0 deletions xsuite/src/data/constants.ts
Original file line number Diff line number Diff line change
@@ -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";
14 changes: 14 additions & 0 deletions xsuite/src/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
53 changes: 30 additions & 23 deletions xsuite/src/interact/envChain.ts
Original file line number Diff line number Diff line change
@@ -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 => {
Expand All @@ -38,24 +37,32 @@ 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({
devnet: devnetMinGasPrice,
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 => {
Expand Down
54 changes: 49 additions & 5 deletions xsuite/src/interact/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
28 changes: 14 additions & 14 deletions xsuite/src/proxy/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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,
});
}
Expand Down
28 changes: 14 additions & 14 deletions xsuite/src/world/world.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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.");
Expand Down

0 comments on commit 9303354

Please sign in to comment.