Skip to content

Commit

Permalink
feat: WIP kado plugin updates
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Nov 20, 2024
1 parent 4138983 commit af4bb99
Show file tree
Hide file tree
Showing 13 changed files with 285 additions and 148 deletions.
336 changes: 229 additions & 107 deletions packages/plugins/kado/src/plugin.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/plugins/radix/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function plugin({ getWallet }: SwapKitPluginParams) {
// await convertInstructionsToManifest({ network: RadixMainnet })(
// route.transaction as Instructions,
// )
// ).value as string;
// ).value as string;c
return wallet.signAndBroadcast({
manifest: route.transaction as string,
});
Expand Down
3 changes: 2 additions & 1 deletion packages/plugins/thorchain/src/tcPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
TCBscDepositABI,
TCEthereumVaultAbi,
type UTXOChain,
type WalletChain,
getMemoForLoan,
lowercasedContractAbiMapping,
} from "@swapkit/helpers";
Expand Down Expand Up @@ -158,7 +159,7 @@ function plugin({ getWallet, stagenet = false }: SwapKitPluginParams) {
getMemoForLoan(type === "open" ? MemoType.OPEN_LOAN : MemoType.CLOSE_LOAN, {
asset: assetValue.toString(),
minAmount: minAmount.toString(),
address: getWallet(assetValue.chain).address,
address: getWallet(assetValue.chain as WalletChain).address,
}),
});
}
Expand Down
19 changes: 10 additions & 9 deletions packages/swapkit/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export function SwapKit<
return plugin;
}

function addChain<T extends Chain>(connectWallet: ChainWallet<T>) {
function addChain<T extends WalletChain>(connectWallet: ChainWallet<T>) {
const currentWallet = getWallet(connectWallet.chain);

connectedWallets[connectWallet.chain] = { ...currentWallet, ...connectWallet };
Expand Down Expand Up @@ -163,15 +163,15 @@ export function SwapKit<
/**
* @Public
*/
function getWallet<T extends Chain>(chain: T) {
function getWallet<T extends WalletChain>(chain: T) {
return connectedWallets[chain];
}

function getAllWallets() {
return { ...connectedWallets };
}

function getAddress<T extends Chain>(chain: T) {
function getAddress<T extends WalletChain>(chain: T) {
return getWallet(chain)?.address || "";
}

Expand All @@ -183,7 +183,7 @@ export function SwapKit<
return approve({ assetValue, contractAddress, type: ApproveMode.CheckOnly });
}

function disconnectChain<T extends Chain>(chain: T) {
function disconnectChain<T extends WalletChain>(chain: T) {
const wallet = getWallet(chain);
wallet?.disconnect?.();
delete connectedWallets[chain];
Expand All @@ -195,7 +195,7 @@ export function SwapKit<
}
}

function getBalance<T extends Chain, R extends boolean>(
function getBalance<T extends WalletChain, R extends boolean>(
chain: T,
refresh?: R,
): ConditionalAssetValueReturn<R> {
Expand All @@ -216,7 +216,7 @@ export function SwapKit<
});
}

async function getWalletWithBalance<T extends Chain>(chain: T, potentialScamFilter = true) {
async function getWalletWithBalance<T extends WalletChain>(chain: T, potentialScamFilter = true) {
const defaultBalance = [AssetValue.from({ chain })];
const wallet = getWallet(chain);

Expand Down Expand Up @@ -254,14 +254,14 @@ export function SwapKit<
assetValue,
...params
}: UTXOTransferParams | EVMTransferParams | CosmosTransferParams) {
const chain = assetValue.chain as WalletChain;
const chain = assetValue.chain as Exclude<WalletChain, Chain.Radix>;
const wallet = getWallet(chain);
if (!wallet) throw new SwapKitError("core_wallet_connection_not_found");

return wallet.transfer({ ...params, assetValue });
}

function signMessage({ chain, message }: { chain: Chain; message: string }) {
function signMessage({ chain, message }: { chain: WalletChain; message: string }) {
const wallet = getWallet(chain);
if (!wallet) throw new SwapKitError("core_wallet_connection_not_found");

Expand Down Expand Up @@ -315,7 +315,8 @@ export function SwapKit<
const { assetValue } = params;
const { chain } = assetValue;

if (!getWallet(chain)) throw new SwapKitError("core_wallet_connection_not_found");
if (!getWallet(chain as WalletChain))
throw new SwapKitError("core_wallet_connection_not_found");

const baseValue = AssetValue.from({ chain });

Expand Down
9 changes: 8 additions & 1 deletion packages/swapkit/helpers/src/types/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export enum Chain {
Dash = "DASH",
Dogecoin = "DOGE",
Ethereum = "ETH",
Fiat = "FIAT",
Kujira = "KUJI",
Litecoin = "LTC",
Maya = "MAYA",
Expand All @@ -26,7 +27,7 @@ export enum StagenetChain {
Maya = "MAYA_STAGENET",
}

export type WalletChain = Exclude<Chain, Chain.Radix>;
export type WalletChain = Exclude<Chain, Chain.Fiat>;

export enum ChainId {
Arbitrum = "42161",
Expand All @@ -46,6 +47,7 @@ export enum ChainId {
Kujira = "kaiyo-1",
Ethereum = "1",
EthereumHex = "0x1",
Fiat = "fiat",
Litecoin = "litecoin",
Maya = "mayachain-mainnet-v1",
MayaStagenet = "mayachain-stagenet-v1",
Expand Down Expand Up @@ -76,6 +78,7 @@ export const ChainIdToChain: Record<ChainId, Chain> = {
[ChainId.Dash]: Chain.Dash,
[ChainId.Dogecoin]: Chain.Dogecoin,
[ChainId.EthereumHex]: Chain.Ethereum,
[ChainId.Fiat]: Chain.Fiat,
[ChainId.Kujira]: Chain.Kujira,
[ChainId.Ethereum]: Chain.Ethereum,
[ChainId.Litecoin]: Chain.Litecoin,
Expand Down Expand Up @@ -107,6 +110,7 @@ export const BaseDecimal: Record<Chain, number> = {
DOGE: 8,
DOT: 10,
ETH: 18,
FIAT: 2,
FLIP: 18,
GAIA: 6,
KUJI: 6,
Expand Down Expand Up @@ -190,6 +194,7 @@ export const RPC_URLS: Record<Chain | StagenetChain, string> = {
[Chain.Dash]: "https://dash-rpc.publicnode.com",
[Chain.Dogecoin]: "https://node-router.thorswap.net/dogecoin",
[Chain.Ethereum]: "https://ethereum-rpc.publicnode.com",
[Chain.Fiat]: "",
[Chain.Kujira]: "https://rpc-kujira.synergynodes.com/",
[Chain.Litecoin]: "https://node-router.thorswap.net/litecoin",
[Chain.Maya]: "https://tendermint.mayachain.info",
Expand Down Expand Up @@ -227,6 +232,7 @@ export const FALLBACK_URLS: Record<Chain | StagenetChain, string[]> = {
[Chain.Dash]: ["https://dash-rpc.publicnode.com"],
[Chain.Dogecoin]: ["https://doge.getblock.io/mainnet", "https://dogecoin.publicnode.com"],
[Chain.Ethereum]: ["https://eth.llamarpc.com", "https://rpc.ankr.com/eth"],
[Chain.Fiat]: [],
[Chain.Kujira]: ["https://kujira-rpc.polkachu.com", "https://kujira-rpc.ibs.team"],
[Chain.Litecoin]: ["https://ltc.getblock.io/mainnet", "https://litecoin.publicnode.com"],
[Chain.Maya]: ["https://tendermint.mayachain.info", "https://maya-tendermint.publicnode.com"],
Expand Down Expand Up @@ -255,6 +261,7 @@ export const EXPLORER_URLS: Record<Chain, string> = {
[Chain.Dash]: "https://blockchair.com/dash",
[Chain.Dogecoin]: "https://blockchair.com/dogecoin",
[Chain.Ethereum]: "https://etherscan.io",
[Chain.Fiat]: "",
[Chain.Kujira]: "https://finder.kujira.network/kaiyo-1",
[Chain.Litecoin]: "https://blockchair.com/litecoin",
[Chain.Maya]: "https://www.mayascan.org",
Expand Down
4 changes: 2 additions & 2 deletions packages/swapkit/helpers/src/types/commonTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { RadixNetwork } from "@swapkit/toolbox-radix";
import type { Chain } from "./chains";
import type { Chain, WalletChain } from "./chains";
import type { ChainApis } from "./sdk";
import type { ChainWallet } from "./wallet";

Expand Down Expand Up @@ -77,7 +77,7 @@ export type ConnectConfig = {
};

export type ConnectWalletParams<M = { [key in string]: any }> = {
addChain: <T extends Chain>(params: ChainWallet<T> & M) => void;
addChain: <T extends WalletChain>(params: ChainWallet<T> & M) => void;
apis: ChainApis;
config: ConnectConfig;
rpcUrls: { [chain in Chain]?: string };
Expand Down
3 changes: 2 additions & 1 deletion packages/swapkit/helpers/src/types/derivationPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export const NetworkDerivationPath: Record<Chain, DerivationPathArray> = {
THOR: [44, 931, 0, 0, 0],

// Polkadot and related network derivation path is not number based
XRD: [0, 0, 0, 0, 0],
DOT: [0, 0, 0, 0, 0],
FIAT: [0, 0, 0, 0, 0],
FLIP: [0, 0, 0, 0, 0],
XRD: [0, 0, 0, 0, 0],
};
8 changes: 4 additions & 4 deletions packages/swapkit/helpers/src/types/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { UTXOWallets } from "@swapkit/toolbox-utxo";
import type { Eip1193Provider } from "ethers";

import type { AssetValue } from "../modules/assetValue";
import type { Chain } from "./chains";
import type { Chain, WalletChain } from "./chains";
import type { ConnectWalletParams } from "./commonTypes";

declare global {
Expand Down Expand Up @@ -66,9 +66,9 @@ export type ChainWallet<T extends Chain> = {
signMessage?: (message: string) => Promise<string>;
};

export type EmptyWallet = { [key in Chain]?: unknown };
export type EmptyWallet = { [key in WalletChain]?: unknown };
export type BaseWallet<T extends EmptyWallet | Record<string, unknown>> = {
[key in Chain]: ChainWallet<key> & (T extends EmptyWallet ? T[key] : never);
[key in WalletChain]: ChainWallet<key> & (T extends EmptyWallet ? T[key] : never);
};

export type FullWallet = BaseWallet<
Expand All @@ -91,7 +91,7 @@ export type SwapKitWallet<ConnectParams extends any[]> = (
) => (...connectParams: ConnectParams) => boolean | Promise<boolean>;

export type SwapKitPluginParams<Config = {}> = {
getWallet: <T extends Chain>(chain: T) => FullWallet[T];
getWallet: <T extends WalletChain>(chain: T) => FullWallet[T];
stagenet?: boolean;
config: Config;
};
Expand Down
7 changes: 4 additions & 3 deletions packages/wallets/ledger/src/ledgerLive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import {
FeeOption,
SwapKitError,
SwapKitNumber,
type WalletChain,
WalletOption,
setRequestClientConfig,
} from "@swapkit/sdk";
} from "@swapkit/helpers";
import { ETHToolbox, getProvider } from "@swapkit/toolbox-evm";
import type { UTXOTransferParams } from "@swapkit/toolbox-utxo";
import { BigNumber as BigNumberJS } from "bignumber.js";
Expand All @@ -37,14 +38,14 @@ export enum LedgerLiveChain {
ATOM = "cosmos",
}

export const LEDGER_LIVE_SUPPORTED_CHAINS: Chain[] = [
export const LEDGER_LIVE_SUPPORTED_CHAINS = [
Chain.Bitcoin,
Chain.Ethereum,
Chain.Cosmos,
Chain.Litecoin,
Chain.Dogecoin,
Chain.BitcoinCash,
];
] as WalletChain[];

export const ChainToLedgerLiveChain: Partial<Record<Chain, LedgerLiveChain>> = {
[Chain.Arbitrum]: LedgerLiveChain.ARB,
Expand Down
14 changes: 8 additions & 6 deletions playgrounds/vite/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AssetValue, type Chain, type FullWallet } from "@swapkit/core";
import { AssetValue, type FullWallet, type WalletChain } from "@swapkit/core";
import { useCallback, useEffect, useMemo, useState } from "react";

import { WalletWidget } from "@swapkit/wallet-exodus";
Expand All @@ -14,7 +14,7 @@ import { getSwapKitClient } from "./swapKitClient";

const apiKeys = ["walletConnectProjectId"] as const;

type WalletDataType = FullWallet[Chain] | FullWallet[Chain][] | null;
type WalletDataType = FullWallet[WalletChain] | FullWallet[WalletChain][] | null;

const App = () => {
const [widgetType, setWidgetType] = useState<"swap" | "loan" | "earn">("swap");
Expand Down Expand Up @@ -62,7 +62,7 @@ const App = () => {
[inputAsset, outputAsset],
);

const disconnectChain = (chain: Chain) => {
const disconnectChain = (chain: WalletChain) => {
if (!skClient) return;
skClient.disconnectChain(chain);
setWallet(Object.values(skClient.getAllWallets()));
Expand Down Expand Up @@ -154,15 +154,17 @@ const App = () => {
key={`${walletData?.address}-${walletData?.balance?.[0]?.chain}`}
setAsset={setAsset}
walletData={walletData}
disconnect={() => disconnectChain(walletData?.balance?.[0]?.chain as Chain)}
disconnect={() =>
disconnectChain(walletData?.balance?.[0]?.chain as WalletChain)
}
/>
))
) : (
<Wallet
key={`${wallet?.address}-${wallet?.balance?.[0]?.chain}`}
setAsset={setAsset}
walletData={wallet as FullWallet[Chain]}
disconnect={() => disconnectChain(wallet?.balance?.[0]?.chain as Chain)}
walletData={wallet as FullWallet[WalletChain]}
disconnect={() => disconnectChain(wallet?.balance?.[0]?.chain as WalletChain)}
/>
)}
</>
Expand Down
18 changes: 10 additions & 8 deletions playgrounds/vite/src/Send/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AssetValue, WalletChain } from "@swapkit/core";
import type { AssetValue, Chain, WalletChain } from "@swapkit/core";
import { useCallback, useState } from "react";
import type { SwapKitClient } from "../swapKitClient";

Expand All @@ -22,13 +22,15 @@ export default function Send({
const handleSend = useCallback(async () => {
if (!(inputAsset && inputAssetValue?.gt(0) && skClient)) return;

const from = skClient.getAddress(inputAsset.chain);
const txHash = await skClient.getWallet(inputAssetValue.chain as WalletChain).transfer({
from,
assetValue: inputAssetValue,
memo: "",
recipient,
});
const from = skClient.getAddress(inputAsset.chain as WalletChain);
const txHash = await skClient
.getWallet(inputAssetValue.chain as Exclude<WalletChain, Chain.Radix>)
.transfer({
from,
assetValue: inputAssetValue,
memo: "",
recipient,
});

window.open(
`${skClient.getExplorerTxUrl({ chain: inputAssetValue.chain, txHash: txHash as string })}`,
Expand Down
6 changes: 3 additions & 3 deletions playgrounds/vite/src/Swap/SwapInputs.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use client";
import type { AssetValue, QuoteResponseRoute, SwapKit } from "@swapkit/sdk";
import type { AssetValue, QuoteResponseRoute, SwapKit, WalletChain } from "@swapkit/sdk";
import { ProviderName, SwapKitApi, SwapKitNumber } from "@swapkit/sdk";
import { useCallback, useState } from "react";

Expand Down Expand Up @@ -34,8 +34,8 @@ export const SwapInputs = ({ skClient, inputAsset, outputAsset, handleSwap }: Pr
setLoading(true);
setRoutes([]);

const sourceAddress = skClient.getAddress(inputAsset.chain);
const destinationAddress = skClient.getAddress(outputAsset.chain);
const sourceAddress = skClient.getAddress(inputAsset.chain as WalletChain);
const destinationAddress = skClient.getAddress(outputAsset.chain as WalletChain);
// const providers = Object.values(ProviderName);

try {
Expand Down
4 changes: 2 additions & 2 deletions playgrounds/vite/src/Wallet.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type Chain, type FullWallet, SwapKitApi } from "@swapkit/sdk";
import { type FullWallet, SwapKitApi, type WalletChain } from "@swapkit/sdk";

type Props = {
walletData: FullWallet[Chain];
walletData: FullWallet[WalletChain];
setAsset: (asset: any) => void;
disconnect: () => void;
};
Expand Down

0 comments on commit af4bb99

Please sign in to comment.