Skip to content

Commit

Permalink
Add network selector (#435)
Browse files Browse the repository at this point in the history
* Add network setter. Add test.

* Revert "Add network setter. Add test."

This reverts commit 5709376.

* Revert formatter. Update config.ts and config.test.ts

* Resolve comments

* Update setWhirlpoolsConfig according to comment

---------

Co-authored-by: calintje <[email protected]>
  • Loading branch information
calintje and calintje authored Oct 30, 2024
1 parent 5df6db6 commit 95052f9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
30 changes: 22 additions & 8 deletions ts-sdk/whirlpool/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import { getWhirlpoolsConfigExtensionAddress } from "@orca-so/whirlpools-client";
import type { Address, TransactionSigner } from "@solana/web3.js";
import { address, createNoopSigner } from "@solana/web3.js";
import { address, createNoopSigner, isAddress } from "@solana/web3.js";

/**
* The default (null) address.
*/
export const DEFAULT_ADDRESS = address("11111111111111111111111111111111");

/**
* The WhirlpoolsConfig addresses for various networks.
*/
export const DEFAULT_WHIRLPOOLS_CONFIG_ADDRESSES = {
solanaMainnet: address("2LecshUwdy9xi7meFgHtFJQNSKk4KdTrcpvaB56dP2NQ"),
solanaDevnet: address("FcrweFY1G9HJAHG5inkGB6pKg1HZ6x9UC2WioAfWrGkR"),
eclipseMainnet: address("FVG4oDbGv16hqTUbovjyGmtYikn6UBEnazz6RVDMEFwv"),
eclipseTestnet: address("FPydDjRdZu9sT7HVd6ANhfjh85KLq21Pefr5YWWMRPFp"),
};

/**
* The default WhirlpoolsConfig address.
*/
export const DEFAULT_WHIRLPOOLS_CONFIG_ADDRESS = address(
"2LecshUwdy9xi7meFgHtFJQNSKk4KdTrcpvaB56dP2NQ",
);
export const DEFAULT_WHIRLPOOLS_CONFIG_ADDRESS =
DEFAULT_WHIRLPOOLS_CONFIG_ADDRESSES.solanaMainnet;

/**
* The default WhirlpoolsConfigExtension address.
Expand All @@ -36,15 +45,20 @@ export let WHIRLPOOLS_CONFIG_EXTENSION_ADDRESS: Address =
/**
* Updates the WhirlpoolsConfig and WhirlpoolsConfigExtension addresses.
*
* @param {Address} whirlpoolsConfigAddress - A WhirlpoolsConfig address.
* @param {Address | keyof typeof NETWORK_ADDRESSES} config - A WhirlpoolsConfig address or a network name.
* @returns {Promise<void>} - Resolves when the addresses have been updated.
*/
export async function setWhirlpoolsConfig(
whirlpoolsConfigAddress: Address,
config: Address | keyof typeof DEFAULT_WHIRLPOOLS_CONFIG_ADDRESSES,
): Promise<void> {
WHIRLPOOLS_CONFIG_ADDRESS = whirlpoolsConfigAddress;
if (isAddress(config)) {
WHIRLPOOLS_CONFIG_ADDRESS = config;
} else {
WHIRLPOOLS_CONFIG_ADDRESS = DEFAULT_WHIRLPOOLS_CONFIG_ADDRESSES[config as keyof typeof DEFAULT_WHIRLPOOLS_CONFIG_ADDRESSES];
}

WHIRLPOOLS_CONFIG_EXTENSION_ADDRESS =
await getWhirlpoolsConfigExtensionAddress(whirlpoolsConfigAddress).then(
await getWhirlpoolsConfigExtensionAddress(WHIRLPOOLS_CONFIG_ADDRESS).then(
(x) => x[0],
);
}
Expand Down
13 changes: 13 additions & 0 deletions ts-sdk/whirlpool/tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
DEFAULT_SOL_WRAPPING_STRATEGY,
DEFAULT_WHIRLPOOLS_CONFIG_ADDRESS,
DEFAULT_WHIRLPOOLS_CONFIG_EXTENSION_ADDRESS,
DEFAULT_WHIRLPOOLS_CONFIG_ADDRESSES,
} from "../src/config";
import assert from "assert";
import {
Expand Down Expand Up @@ -43,6 +44,18 @@ describe("Configuration", () => {
);
});

it("Should be able to set whirlpools config based on network", async () => {
await setWhirlpoolsConfig("eclipseTestnet");
assert.strictEqual(
WHIRLPOOLS_CONFIG_ADDRESS,
DEFAULT_WHIRLPOOLS_CONFIG_ADDRESSES.eclipseTestnet,
);
assert.strictEqual(
WHIRLPOOLS_CONFIG_EXTENSION_ADDRESS,
"6gUEB962oFdZtwoVyXNya9TfGWnBEbYNYt8UdvzT6PSf",
);
});

it("Should be able to set default funder to an address", () => {
setDefaultFunder(DEFAULT_ADDRESS);
assert.strictEqual(FUNDER.address, DEFAULT_ADDRESS);
Expand Down

0 comments on commit 95052f9

Please sign in to comment.