Skip to content

Commit

Permalink
chore: conditional balance on add chain and default to chain gas asset
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-chillios committed Feb 25, 2025
1 parent e2d9746 commit 3915d8f
Show file tree
Hide file tree
Showing 32 changed files with 85 additions and 79 deletions.
1 change: 1 addition & 0 deletions bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
"@swapkit/toolboxes": "workspace:*",
"@trezor/connect-web": "9.4.7",
"@walletconnect/modal-sign-html": "2.7.0",
"bitcoinjs-lib": "6.1.7",
"blakejs": "1.2.1",
"cosmjs-types": "0.9.0",
"sats-connect": "3.2.0",
Expand Down
11 changes: 8 additions & 3 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,15 @@ export function SwapKit<
return plugin;
}

function addChain<T extends Chain>(connectWallet: ChainWallet<T>) {
function addChain<T extends Chain>(
connectWallet: Omit<ChainWallet<T>, "balance"> & { balance?: AssetValue[] },
) {
const currentWallet = getWallet(connectWallet.chain);

connectedWallets[connectWallet.chain] = { ...currentWallet, ...connectWallet };
const balance = connectWallet.balance ||
currentWallet.balance || [AssetValue.from({ chain: connectWallet.chain })];

connectedWallets[connectWallet.chain] = { ...currentWallet, ...connectWallet, balance };
}

// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>
Expand Down Expand Up @@ -153,7 +158,7 @@ export function SwapKit<
* @Public
*/
function getWallet<T extends Chain>(chain: T) {
return connectedWallets[chain];
return connectedWallets[chain] || {};
}

function getAllWallets() {
Expand Down
3 changes: 2 additions & 1 deletion packages/helpers/src/api/swapkitApi/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { createHash } from "crypto";
import { ProviderName, RequestClient, SKConfig, SwapKitError, warnOnce } from "@swapkit/helpers";

import {
Expand Down Expand Up @@ -54,6 +53,8 @@ export const computeHash = (
? JSON.stringify(hashParams.payload)
: `${hashParams.url}${swapKit}`;

const { createHash } = require("crypto");

return createHash("sha256").update(data, "utf8").digest("hex");
};

Expand Down
2 changes: 1 addition & 1 deletion packages/helpers/src/types/commonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Chain } from "./chains";
import type { ChainWallet, CryptoChain } from "./wallet";

export type AddChainType<M = { [key in string]: any }> = <T extends CryptoChain>(
params: ChainWallet<T> & M,
params: Omit<ChainWallet<T>, "balance"> & M,
) => void;

export type Witness = {
Expand Down
2 changes: 0 additions & 2 deletions packages/toolboxes/src/utxo/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export { type Network, networks, Psbt, Transaction } from "bitcoinjs-lib";

/**
* Package
*/
Expand Down
1 change: 1 addition & 0 deletions packages/wallets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@swapkit/toolboxes": "workspace:*",
"@trezor/connect-web": "9.4.7",
"@walletconnect/modal-sign-html": "2.7.0",
"bitcoinjs-lib": "6.1.7",
"blakejs": "1.2.1",
"cosmjs-types": "0.9.0",
"sats-connect": "3.2.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/wallets/src/bitget/bitgetWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const bitgetWallet = createWallet({
filteredChains.map(async (chain) => {
const walletMethods = await getWalletMethods(chain);

addChain({ ...walletMethods, chain, balance: [], walletType });
addChain({ ...walletMethods, chain, walletType });
}),
);

Expand Down
6 changes: 4 additions & 2 deletions packages/wallets/src/bitget/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
switchEVMWalletNetwork,
} from "@swapkit/helpers";
import type { TransferParams } from "@swapkit/toolboxes/cosmos";
import type { Psbt, UTXOTransferParams } from "@swapkit/toolboxes/utxo";
import type { UTXOTransferParams } from "@swapkit/toolboxes/utxo";
import type { Psbt } from "bitcoinjs-lib";
import type { Eip1193Provider } from "ethers";

function cosmosTransfer() {
Expand Down Expand Up @@ -76,7 +77,8 @@ export async function getWalletMethods(chain: Chain) {
}
const { unisat: wallet } = bitget;

const { Psbt, BTCToolbox } = await import("@swapkit/toolboxes/utxo");
const { Psbt } = await import("bitcoinjs-lib");
const { BTCToolbox } = await import("@swapkit/toolboxes/utxo");
const [address] = await wallet.requestAccounts();
const toolbox = BTCToolbox();

Expand Down
2 changes: 1 addition & 1 deletion packages/wallets/src/coinbase/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const coinbaseWallet = createWallet({
filteredChains.map(async (chain) => {
const walletMethods = await getWalletMethods({ chain, coinbaseSdk });

addChain({ ...walletMethods, balance: [], chain, walletType });
addChain({ ...walletMethods, chain, walletType });
}),
);

Expand Down
2 changes: 1 addition & 1 deletion packages/wallets/src/ctrl/ctrlWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const ctrlWallet = createWallet({
const address = await getCtrlAddress(chain);
const walletMethods = await getWalletMethods(chain);

addChain({ ...walletMethods, address, balance: [], chain, walletType });
addChain({ ...walletMethods, address, chain, walletType });
});

await Promise.all(promises);
Expand Down
19 changes: 2 additions & 17 deletions packages/wallets/src/evm-extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,7 @@ export const evmWallet = createWallet({
const getBalance = async (potentialScamFilter = true) =>
walletMethods.getBalance(address, potentialScamFilter, getProvider(chain));

addChain({
...walletMethods,
address,
balance: [],
chain,
getBalance,
walletType,
});
addChain({ ...walletMethods, address, chain, getBalance, walletType });
return;
}

Expand All @@ -131,15 +124,7 @@ export const evmWallet = createWallet({
const disconnect = () =>
web3provider.send("wallet_revokePermissions", [{ eth_accounts: {} }]);

addChain({
...walletMethods,
address,
balance: [],
chain,
disconnect,
getBalance,
walletType,
});
addChain({ ...walletMethods, address, chain, disconnect, getBalance, walletType });
}),
);

Expand Down
6 changes: 3 additions & 3 deletions packages/wallets/src/exodus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
switchEVMWalletNetwork,
} from "@swapkit/helpers";
import type { UTXOTransferParams } from "@swapkit/toolboxes/utxo";
import type { Psbt } from "@swapkit/toolboxes/utxo";
import type { Psbt } from "bitcoinjs-lib";
import type { BrowserProvider, Eip1193Provider } from "ethers";
import {
AddressPurpose,
Expand All @@ -35,7 +35,8 @@ async function getWalletMethods({
}) {
switch (chain) {
case Chain.Bitcoin: {
const { BTCToolbox, Psbt } = await import("@swapkit/toolboxes/utxo");
const { Psbt } = await import("bitcoinjs-lib");
const { BTCToolbox } = await import("@swapkit/toolboxes/utxo");
const toolbox = BTCToolbox();

let address = "";
Expand Down Expand Up @@ -174,7 +175,6 @@ export const exodusWallet = createWallet({
chain,
address,
getBalance,
balance: [],
walletType: WalletOption.EXODUS,
});
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/wallets/src/keepkey-bex/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const keepkeyBexWallet = createWallet({
const address = await getKEEPKEYAddress(chain);
const walletMethods = await getWalletMethods(chain);

addChain({ ...walletMethods, address, balance: [], chain, walletType });
addChain({ ...walletMethods, address, chain, walletType });
}),
);

Expand Down
3 changes: 2 additions & 1 deletion packages/wallets/src/keepkey/chains/utxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
type UTXOChain,
derivationPathToString,
} from "@swapkit/helpers";
import type { BCHToolbox, Psbt, UTXOToolbox, UTXOTransferParams } from "@swapkit/toolboxes/utxo";
import type { BCHToolbox, UTXOToolbox, UTXOTransferParams } from "@swapkit/toolboxes/utxo";
import type { Psbt } from "bitcoinjs-lib";

import { ChainToKeepKeyName, bip32ToAddressNList } from "../coins";

Expand Down
7 changes: 1 addition & 6 deletions packages/wallets/src/keepkey/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,7 @@ export const keepkeyWallet = createWallet({
sdk: keepKeySdk,
});

addChain({
...walletMethods,
balance: [],
chain,
walletType: WalletOption.KEEPKEY,
});
addChain({ ...walletMethods, chain, walletType: WalletOption.KEEPKEY });
}),
);
return true;
Expand Down
1 change: 0 additions & 1 deletion packages/wallets/src/keplr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ export const keplrWallet = createWallet({
chain,
transfer,
address,
balance: [],
walletType,
});
}),
Expand Down
10 changes: 2 additions & 8 deletions packages/wallets/src/keystore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import {
} from "@swapkit/helpers";
import type { DepositParam, TransferParams } from "@swapkit/toolboxes/cosmos";
import type {
Psbt,
TransactionType,
UTXOTransferParams,
UTXOWalletTransferParams,
} from "@swapkit/toolboxes/utxo";
import type { Psbt } from "bitcoinjs-lib";
import { getWalletSupportedChains } from "../helpers";

type Params = {
Expand Down Expand Up @@ -217,13 +217,7 @@ export const keystoreWallet = createWallet({
phrase,
});

addChain({
...walletMethods,
address,
balance: [],
chain,
walletType: WalletOption.KEYSTORE,
});
addChain({ ...walletMethods, address, chain, walletType: WalletOption.KEYSTORE });
}),
);

Expand Down
5 changes: 3 additions & 2 deletions packages/wallets/src/ledger/clients/utxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
derivationPathToString,
getWalletFormatFor,
} from "@swapkit/helpers";
import type { Psbt, UTXOType } from "@swapkit/toolboxes/utxo";
import type { UTXOType } from "@swapkit/toolboxes/utxo";
import type { Psbt } from "bitcoinjs-lib";

import { getLedgerTransport } from "../helpers/getLedgerTransport";

Expand All @@ -21,7 +22,7 @@ const signUTXOTransaction = async (
{ psbt, inputUtxos, btcApp, derivationPath }: Params,
options?: Partial<CreateTransactionArg>,
) => {
const { Transaction } = await import("@swapkit/toolboxes/utxo");
const { Transaction } = await import("bitcoinjs-lib");

const inputs = inputUtxos.map((item) => {
const utxoTx = Transaction.fromHex(item.txHex || "");
Expand Down
2 changes: 1 addition & 1 deletion packages/wallets/src/ledger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const ledgerWallet = createWallet({

const walletMethods = await getWalletMethods({ chain, derivationPath });

addChain({ ...walletMethods, chain, balance: [], walletType: WalletOption.LEDGER });
addChain({ ...walletMethods, chain, walletType: WalletOption.LEDGER });

return true;
},
Expand Down
2 changes: 1 addition & 1 deletion packages/wallets/src/ledger/ledgerLive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ export const ledgerLiveWallet = createWallet({

const toolbox = await getLedgerLiveWallet({ chain, ledgerLiveAccount });

addChain({ ...toolbox, chain, balance: [], walletType });
addChain({ ...toolbox, chain, walletType });

return true;
},
Expand Down
7 changes: 4 additions & 3 deletions packages/wallets/src/okx/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
switchEVMWalletNetwork,
} from "@swapkit/helpers";
import type { GaiaToolbox } from "@swapkit/toolboxes/cosmos";
import type { BTCToolbox, Psbt, UTXOTransferParams } from "@swapkit/toolboxes/utxo";
import type { BTCToolbox, UTXOTransferParams } from "@swapkit/toolboxes/utxo";
import type { Eip1193Provider } from "ethers";

const cosmosTransfer =
Expand Down Expand Up @@ -95,13 +95,14 @@ export async function getWalletMethods(
if (!(window.okxwallet && "bitcoin" in window.okxwallet)) {
throw new Error("No bitcoin okxwallet found");
}
const { Psbt, BTCToolbox } = await import("@swapkit/toolboxes/utxo");
const { Psbt } = await import("bitcoinjs-lib");
const { BTCToolbox } = await import("@swapkit/toolboxes/utxo");

const { bitcoin: wallet } = window.okxwallet;
const address = (await wallet.connect()).address;
const toolbox = BTCToolbox();

const signTransaction = async (psbt: Psbt) => {
const signTransaction = async (psbt: InstanceType<typeof Psbt>) => {
const signedPsbt = await wallet.signPsbt(psbt.toHex(), { from: address, type: "list" });

return Psbt.fromHex(signedPsbt);
Expand Down
2 changes: 1 addition & 1 deletion packages/wallets/src/okx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const okxWallet = createWallet({
filteredChains.map(async (chain) => {
const walletMethods = await getWalletMethods(chain);

addChain({ ...walletMethods, chain, balance: [], walletType });
addChain({ ...walletMethods, chain, walletType });
}),
);

Expand Down
2 changes: 1 addition & 1 deletion packages/wallets/src/phantom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const phantomWallet = createWallet({
filteredChains.map(async (chain) => {
const { address, ...methods } = await getWalletMethods(chain);

addChain({ ...methods, chain, address, walletType, balance: [] });
addChain({ ...methods, chain, address, walletType });
}),
);

Expand Down
2 changes: 1 addition & 1 deletion packages/wallets/src/polkadotjs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const polkadotWallet = createWallet({
filteredChains.map(async (chain) => {
const { address, ...walletMethods } = await getWalletMethods(chain);

addChain({ ...walletMethods, chain, address, walletType, balance: [] });
addChain({ ...walletMethods, chain, address, walletType });
}),
);

Expand Down
2 changes: 1 addition & 1 deletion packages/wallets/src/radix/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const radixWallet = createWallet({
filteredChains.map(async (chain) => {
const walletMethods = await getWalletMethods();

addChain({ ...walletMethods, chain, balance: [], walletType });
addChain({ ...walletMethods, chain, walletType });
}),
);

Expand Down
2 changes: 1 addition & 1 deletion packages/wallets/src/talisman/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const talismanWallet = createWallet({
filteredChains.map(async (chain) => {
const { address, ...walletMethods } = await getWalletMethods(chain);

addChain({ ...walletMethods, address, balance: [], chain, walletType });
addChain({ ...walletMethods, address, chain, walletType });
}),
);

Expand Down
5 changes: 3 additions & 2 deletions packages/wallets/src/trezor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
derivationPathToString,
filterSupportedChains,
} from "@swapkit/helpers";
import type { Psbt, UTXOTransferParams, UTXOType } from "@swapkit/toolboxes/utxo";
import type { UTXOTransferParams, UTXOType } from "@swapkit/toolboxes/utxo";
import type { Psbt } from "bitcoinjs-lib";
import { getWalletSupportedChains } from "../helpers";

function getScriptType(derivationPath: DerivationPathArray) {
Expand Down Expand Up @@ -238,7 +239,7 @@ export const trezorWallet = createWallet({

const { address, walletMethods } = await getToolbox({ chain, derivationPath });

addChain({ ...walletMethods, address, balance: [], chain, walletType });
addChain({ ...walletMethods, address, chain, walletType });

return true;
},
Expand Down
1 change: 0 additions & 1 deletion packages/wallets/src/walletconnect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ export const walletconnectWallet = createWallet({
addChain({
...toolbox,
address,
balance: [],
chain,
disconnect: walletconnect.disconnect,
walletType: WalletOption.WALLETCONNECT,
Expand Down
3 changes: 1 addition & 2 deletions playgrounds/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"clean": "rm -rf .next node_modules",
"build-playground": "next build",
"start": "next start",
"lint": "biome check --write ./src",
"type-check": "tsc --noEmit"
"lint": "biome check --write ./src"
},
"dependencies": {
"@hookform/resolvers": "4.1.2",
Expand Down
Loading

0 comments on commit 3915d8f

Please sign in to comment.