Skip to content

Commit

Permalink
fix(walletkit): fix custom service provider (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevzzsk committed Feb 6, 2023
1 parent 9c079d2 commit 9461e31
Show file tree
Hide file tree
Showing 5 changed files with 530 additions and 9 deletions.
13 changes: 7 additions & 6 deletions packages/walletkit-core/src/api/whale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,38 @@ import {
import { EnvironmentNetwork } from "./environment";

export function newOceanOptions(
network: EnvironmentNetwork
network: EnvironmentNetwork,
url?: string
): WhaleApiClientOptions {
switch (network) {
case EnvironmentNetwork.LocalPlayground:
return {
url: "http://localhost:19553",
url: url ?? "http://localhost:19553",
network: "regtest",
version: "v0",
};
case EnvironmentNetwork.RemotePlayground:
return {
url: "https://playground.jellyfishsdk.com",
url: url ?? "https://playground.jellyfishsdk.com",
network: "regtest",
version: "v0",
};
case EnvironmentNetwork.TestNet:
return {
url: "https://testnet.ocean.jellyfishsdk.com",
url: url ?? "https://testnet.ocean.jellyfishsdk.com",
network: "testnet",
version: "v0",
};
case EnvironmentNetwork.DevNet:
return {
url: "http://devnet.ocean.jellyfishsdk.com:3000",
url: url ?? "http://devnet.ocean.jellyfishsdk.com:3000",
network: "devnet",
version: "v0",
};
case EnvironmentNetwork.MainNet:
default:
return {
url: "https://ocean.defichain.com",
url: url ?? "https://ocean.defichain.com",
network: "mainnet",
version: "v0",
};
Expand Down
52 changes: 52 additions & 0 deletions packages/walletkit-core/src/api/whale.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,55 @@ describe("whale", () => {
);
});
});

describe("whale custom provider", () => {
const customProviderURL = "https://custom.provider.test.com";

it("should match custom provider URL for local playground", () => {
const oceanOptions = newOceanOptions(
EnvironmentNetwork.LocalPlayground,
customProviderURL
);
expect(oceanOptions).toMatchObject({
url: customProviderURL,
network: "regtest",
version: "v0",
});
});

it("should match custom provider URL for testnet", () => {
const oceanOptions = newOceanOptions(
EnvironmentNetwork.TestNet,
customProviderURL
);
expect(oceanOptions).toMatchObject({
url: customProviderURL,
network: "testnet",
version: "v0",
});
});

it("should match custom provider URL for mainnet", () => {
const oceanOptions = newOceanOptions(
EnvironmentNetwork.MainNet,
customProviderURL
);
expect(oceanOptions).toMatchObject({
url: customProviderURL,
network: "mainnet",
version: "v0",
});
});

it("should match custom provider URL for devnet", () => {
const oceanOptions = newOceanOptions(
EnvironmentNetwork.DevNet,
customProviderURL
);
expect(oceanOptions).toMatchObject({
url: customProviderURL,
network: "devnet",
version: "v0",
});
});
});
100 changes: 100 additions & 0 deletions packages/walletkit-ui/src/contexts/WhaleContext.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import { EnvironmentNetwork } from "@waveshq/walletkit-core";
import React from "react";

import * as NetworkContext from "./NetworkContext";
import * as ServiceProviderContext from "./StoreServiceProvider";
import { useWhaleApiClient, WhaleProvider } from "./WhaleContext";

const networkContext = jest.spyOn(NetworkContext, "useNetworkContext");
const serviceProviderContext = jest.spyOn(
ServiceProviderContext,
"useServiceProviderContext"
);

describe("Whale Context test", () => {
const networkDetails = [
Expand Down Expand Up @@ -49,6 +54,101 @@ describe("Whale Context test", () => {
network: networkDetail.name,
} as any)
);
serviceProviderContext.mockImplementation(
() =>
({
url: networkDetail.url,
} as any)
);
function WhaleProviderComponent(): JSX.Element {
const client = useWhaleApiClient() as any;
expect(Object.keys(client)).toEqual(
expect.arrayContaining([
"options",
"rpc",
"address",
"poolpairs",
"transactions",
"tokens",
"masternodes",
"blocks",
"oracles",
"prices",
"stats",
"rawtx",
"fee",
"loan",
])
);
const options = {
version: "v0",
timeout: 60000,
url: networkDetail.url,
network: networkDetail.network,
};
expect(client.options).toMatchObject(options);
return (
<div>
<span>{JSON.stringify(client.options)}</span>
</div>
);
}

const rendered = render(
<WhaleProvider>
<WhaleProviderComponent />
</WhaleProvider>
);
expect(rendered).toMatchSnapshot();
});
});
});
});

describe("Whale custom provider url test", () => {
const networkDetails = [
{
url: "https://custom.mainnet.com",
network: "mainnet",
name: EnvironmentNetwork.MainNet,
},
{
url: "https://custom.testnet.com",
network: "testnet",
name: EnvironmentNetwork.TestNet,
},
{
url: "https://custom.playground.com",
network: "regtest",
name: EnvironmentNetwork.RemotePlayground,
},
{
url: "https://custom.local.com",
network: "regtest",
name: EnvironmentNetwork.LocalPlayground,
},
{
url: "https://custom.devnet.com",
network: "devnet",
name: EnvironmentNetwork.DevNet,
},
];

networkDetails.forEach((networkDetail) => {
describe(`Whale custom provider url test for ${networkDetail.name}`, () => {
it(`should match custom provider url: ${networkDetail.url}`, () => {
networkContext.mockImplementation(
() =>
({
network: networkDetail.name,
} as any)
);
serviceProviderContext.mockImplementation(
() =>
({
url: networkDetail.url,
} as any)
);
function WhaleProviderComponent(): JSX.Element {
const client = useWhaleApiClient() as any;
expect(Object.keys(client)).toEqual(
Expand Down
9 changes: 6 additions & 3 deletions packages/walletkit-ui/src/contexts/WhaleContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import React, { createContext, useContext, useMemo } from "react";

import { useNetworkContext } from "./NetworkContext";
import { useServiceProviderContext } from "./StoreServiceProvider";

const WhaleApiClientContext = createContext<{
whaleAPI: WhaleApiClient;
Expand All @@ -25,12 +26,14 @@ export function WhaleProvider({
children,
}: React.PropsWithChildren<any>): JSX.Element | null {
const { network } = useNetworkContext();
const { url } = useServiceProviderContext();

const client = useMemo(
() => ({
whaleAPI: newWhaleAPIClient(newOceanOptions(network)),
whaleRPC: newWhaleRpcClient(newOceanOptions(network)),
whaleAPI: newWhaleAPIClient(newOceanOptions(network, url)),
whaleRPC: newWhaleRpcClient(newOceanOptions(network, url)),
}),
[network]
[network, url]
);

return (
Expand Down
Loading

0 comments on commit 9461e31

Please sign in to comment.