Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cyan-queens-create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stakekit/widget": patch
---

feat(externalProvider): expose actionType and txType in sendTransaction txMeta
5 changes: 5 additions & 0 deletions .changeset/thirty-beans-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stakekit/widget": patch
---

feat(externalProvider): add solana and ton support
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"knip": "^5.50.3",
"turbo": "^2.5.0"
},
"packageManager": "pnpm@10.8.0",
"packageManager": "pnpm@10.9.0",
"pnpm": {
"overrides": {
"@types/react": "19.0.10",
Expand Down
14 changes: 13 additions & 1 deletion packages/widget/src/domain/types/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,22 @@ export type SubstrateChainsMap = {
};
};

export const isEvmChain = (chain: string): chain is SupportedEvmChain => {
return supportedEVMChainsSet.has(chain as SupportedEvmChain);
};

export const isSolanaChain = (chain: string): chain is SupportedMiscChains => {
return chain === MiscNetworks.Solana;
};

export const isTonChain = (chain: string): chain is SupportedMiscChains => {
return chain === MiscNetworks.Ton;
};

export const isSupportedChain = (chain: string): chain is SupportedSKChains => {
return (
isEvmChain(chain) ||
supportedCosmosChainsSet.has(chain as SupportedCosmosChains) ||
supportedEVMChainsSet.has(chain as SupportedEvmChain) ||
supportedMiscChainsSet.has(chain as SupportedMiscChains) ||
supportedSubstrateChainsSet.has(chain as SupportedSubstrateChains)
);
Expand Down
13 changes: 9 additions & 4 deletions packages/widget/src/domain/types/external-providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ActionDto, TransactionDto } from "@stakekit/api-hooks";
import { EitherAsync, Left } from "purify-ts";
import type { RefObject } from "react";
import type { SKExternalProviders } from "./wallets";
import type { EVMTx } from "./wallets/generic-wallet";
import type { SKTx } from "./wallets/generic-wallet";

export class ExternalProvider {
constructor(private variantProvider: RefObject<SKExternalProviders>) {}
Expand All @@ -12,8 +12,13 @@ export class ExternalProvider {
}

sendTransaction(
tx: EVMTx,
txMeta: { txId: TransactionDto["id"]; actionId: ActionDto["id"] }
tx: SKTx,
txMeta: {
txId: TransactionDto["id"];
actionId: ActionDto["id"];
actionType: ActionDto["type"];
txType: TransactionDto["type"];
}
) {
const _sendTransaction =
this.variantProvider.current.provider.sendTransaction;
Expand All @@ -28,7 +33,7 @@ export class ExternalProvider {
});
}

switchChain({ chainId }: { chainId: string }) {
switchChain({ chainId }: { chainId: number }) {
return EitherAsync(() =>
this.variantProvider.current.provider.switchChain(chainId)
).mapLeft((e) => {
Expand Down
9 changes: 8 additions & 1 deletion packages/widget/src/domain/types/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Account } from "@ledgerhq/wallet-api-client";
import type {
ActionDto,
AddressWithTokenDtoAdditionalAddresses,
Networks,
TransactionDto,
} from "@stakekit/api-hooks";
import type { EitherAsync } from "purify-ts";
Expand All @@ -20,8 +21,14 @@ export type SKWallet = {
disconnect: () => Promise<void>;
signTransaction: (args: {
tx: NonNullable<TransactionDto["unsignedTransaction"]>;
txMeta: { txId: TransactionDto["id"]; actionId: ActionDto["id"] };
txMeta: {
txId: TransactionDto["id"];
actionId: ActionDto["id"];
actionType: ActionDto["type"];
txType: TransactionDto["type"];
};
ledgerHwAppId: Nullable<string>;
network: Networks;
}) => EitherAsync<
TransactionDecodeError | SendTransactionError,
{ signedTx: SignedTxOrMessage; broadcasted: boolean }
Expand Down
62 changes: 42 additions & 20 deletions packages/widget/src/domain/types/wallets/generic-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,57 @@
import type { ActionDto, TransactionDto } from "@stakekit/api-hooks";
import type { Hex } from "viem";

type Base64String = string;

export enum TxType {
Legacy = "0x1",
EIP1559 = "0x2",
}

export type EVMTx = {
data: Hex;
from: Hex;
to: Hex;
value: Hex | undefined;
nonce: Hex;
gas: Hex;
chainId: Hex;
type: Hex;
} & (
| {
type: TxType.EIP1559; // EIP-1559
maxFeePerGas: Hex | undefined;
maxPriorityFeePerGas: Hex | undefined;
}
| { type: TxType.Legacy } // Legacy
);
type: "evm";
tx: {
data: Hex;
from: Hex;
to: Hex;
value: Hex | undefined;
nonce: Hex;
gas: Hex;
chainId: Hex;
type: Hex;
} & (
| {
type: TxType.EIP1559; // EIP-1559
maxFeePerGas: Hex | undefined;
maxPriorityFeePerGas: Hex | undefined;
}
| { type: TxType.Legacy } // Legacy
);
};

export type SolanaTx = { type: "solana"; tx: Base64String };

export type EVMWallet = {
export type TonTx = {
type: "ton";
tx: {
seqno: bigint;
message: Base64String;
};
};

export type SKTx = EVMTx | SolanaTx | TonTx;

export type SKWallet = {
signMessage: (message: string) => Promise<string>;
switchChain: (chainId: string) => Promise<void>;
switchChain: (chainId: number) => Promise<void>;
getTransactionReceipt?(txHash: string): Promise<{ transactionHash?: string }>;
sendTransaction(
tx: EVMTx,
txMeta: { txId: TransactionDto["id"]; actionId: ActionDto["id"] }
tx: SKTx,
txMeta: {
txId: TransactionDto["id"];
actionId: ActionDto["id"];
actionType: ActionDto["type"];
txType: TransactionDto["type"];
}
): Promise<string>;
};
4 changes: 2 additions & 2 deletions packages/widget/src/domain/types/wallets/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { TokenString } from "@sk-widget/domain/types";
import type { EVMWallet } from "./generic-wallet";
import type { SKWallet } from "./generic-wallet";

export type SKExternalProviders = {
currentChain?: number;
currentAddress: string;
initToken?: TokenString;
supportedChainIds?: number[];
type: "generic";
provider: EVMWallet;
provider: SKWallet;
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { Box } from "../../../../components/atoms/box";
import { Text } from "../../../../components/atoms/typography";
import { WarningBox } from "../../../../components/atoms/warning-box";
import type { RewardTokenDetails } from "../../../../components/molecules/reward-token-details";
import { useTrackPage } from "../../../../hooks/tracking/use-track-page";
import { AnimationPage } from "../../../../navigation/containers/animation-page";
import { PageContainer } from "../../../components";
import { MetaInfo } from "../../../components/meta-info";
Expand Down Expand Up @@ -53,8 +52,6 @@ export const ReviewPage = ({
feeConfigLoading = false,
...rest
}: ReviewPageProps) => {
useTrackPage("stakeReview");

const trackEvent = useTrackEvent();
const { t } = useTranslation();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ export const useStepsMachine = ({
transactions,
integrationId,
actionId,
actionType,
}: {
transactions: ActionDto["transactions"];
integrationId: ActionDto["integrationId"];
actionId: ActionDto["id"];
actionType: ActionDto["type"];
}) => {
const { signTransaction, signMessage, isLedgerLive } = useSKWallet();

Expand All @@ -81,6 +83,7 @@ export const useStepsMachine = ({
signMessage,
signTransaction,
actionId,
actionType,
});

return useMachine(useState(() => getMachine(machineParams))[0]);
Expand All @@ -96,6 +99,7 @@ const getMachine = (
signMessage: ReturnType<typeof useSKWallet>["signMessage"];
signTransaction: ReturnType<typeof useSKWallet>["signTransaction"];
actionId: ActionDto["id"];
actionType: ActionDto["type"];
}>
>
) => {
Expand Down Expand Up @@ -267,7 +271,10 @@ const getMachine = (
txMeta: {
actionId: ref.current.actionId,
txId: constructedTx.id,
actionType: ref.current.actionType,
txType: constructedTx.type,
},
network: constructedTx.network,
})
.map((val) => ({
...val,
Expand Down
1 change: 1 addition & 0 deletions packages/widget/src/pages/steps/hooks/use-steps.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const useSteps = ({
transactions: session.transactions,
integrationId: session.integrationId,
actionId: session.id,
actionType: session.type,
});

/**
Expand Down
25 changes: 12 additions & 13 deletions packages/widget/src/providers/external-provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export const externalProviderConnector = (
iconUrl: config.appIcon,
iconBackground: "#fff",
createConnector: () =>
createConnector<unknown, ExtraProps>((config) => {
createConnector<unknown, ExtraProps>((connectorConfig) => {
const $filteredChains = new BehaviorSubject(
Maybe.fromNullable(variant.current.supportedChainIds)
.map((val) => new Set(val))
.mapOrDefault(
(val) => config.chains.filter((c) => val.has(c.id)),
config.chains as [Chain, ...Chain[]]
(val) => connectorConfig.chains.filter((c) => val.has(c.id)),
connectorConfig.chains as [Chain, ...Chain[]]
)
);
const provider = new ExternalProvider(variant);
Expand All @@ -62,7 +62,7 @@ export const externalProviderConnector = (

const connect: ReturnType<CreateConnectorFn>["connect"] =
async () => {
config.emitter.emit("message", { type: "connecting" });
connectorConfig.emitter.emit("message", { type: "connecting" });

const [accounts, chainId] = await Promise.all([
getAccounts(),
Expand All @@ -78,13 +78,11 @@ export const externalProviderConnector = (
await EitherAsync.liftEither(
List.find(
(c) => c.id === chainId,
config.chains as unknown as Array<Chain>
connectorConfig.chains as unknown as Array<Chain>
).toEither(new Error("Chain not found"))
)
.chain((chain) =>
provider
.switchChain({ chainId: `0x${chainId.toString(16)}` })
.map(() => chain)
provider.switchChain({ chainId }).map(() => chain)
)
.ifRight((chain) => onChainChanged(chain.id.toString()))
).unsafeCoerce();
Expand All @@ -101,19 +99,19 @@ export const externalProviderConnector = (

const onDisconnect: ReturnType<CreateConnectorFn>["onDisconnect"] =
() => {
config.emitter.emit("disconnect");
connectorConfig.emitter.emit("disconnect");
};

const onChainChanged: ReturnType<CreateConnectorFn>["onChainChanged"] =
(chainId) => {
config.emitter.emit("change", {
connectorConfig.emitter.emit("change", {
chainId: skNormalizeChainId(chainId),
});
};

const onAccountsChanged: ReturnType<CreateConnectorFn>["onAccountsChanged"] =
(accounts) => {
config.emitter.emit("change", {
connectorConfig.emitter.emit("change", {
accounts: accounts.filter((a) => !!a).map((a) => getAddress(a)),
});
};
Expand All @@ -124,8 +122,9 @@ export const externalProviderConnector = (
Maybe.fromFalsy(!!supportedChainIds.length)
.map(() => new Set(supportedChainIds))
.mapOrDefault(
(val) => config.chains.filter((c) => val.has(c.id)),
config.chains as [Chain, ...Chain[]]
(val) =>
connectorConfig.chains.filter((c) => val.has(c.id)),
connectorConfig.chains as [Chain, ...Chain[]]
)
);

Expand Down
Loading