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/vast-ants-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stakekit/widget": patch
---

refactor: remove unused multi-sign transaction logic and clean up wallet type definitions
3 changes: 0 additions & 3 deletions packages/widget/src/domain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,6 @@ export const transactionsForConstructOnlySet = new Set<TransactionType>([
"LUGANODES_EXIT_REQUEST",
]);

export const getTransactionsForMultiSign = (txs: TransactionDto[]) =>
txs.filter((tx) => !transactionsForConstructOnlySet.has(tx.type));

export const skNormalizeChainId = (chainId: string) => {
const cId = normalizeChainId(chainId);

Expand Down
4 changes: 0 additions & 4 deletions packages/widget/src/domain/types/connectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,3 @@ export const shouldShowDisconnect = (connector: Connector) =>
!isExternalProviderConnector(connector) &&
!isLedgerLiveConnector(connector) &&
!isSafeConnector(connector);

export const shouldMultiSend: (conn: Connector) => boolean = (
connector: Connector
) => isSafeConnector(connector);
6 changes: 0 additions & 6 deletions packages/widget/src/domain/types/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ export type SKWallet = {
TransactionDecodeError | SendTransactionError,
{ signedTx: SignedTxOrMessage; broadcasted: boolean }
>;
signMultipleTransactions: (args: {
txs: NonNullable<TransactionDto["unsignedTransaction"]>[];
}) => EitherAsync<
TransactionDecodeError | SendTransactionError,
{ signedTx: SignedTxOrMessage; broadcasted: boolean }
>;
signMessage: (message: string) => EitherAsync<Error, SignedTxOrMessage>;
additionalAddresses: AddressWithTokenDtoAdditionalAddresses | null;
isLedgerLive: boolean;
Expand Down
108 changes: 5 additions & 103 deletions packages/widget/src/pages/steps/hooks/use-steps-machine.hook.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { shouldMultiSend } from "@sk-widget/domain/types/connectors";
import { useSavedRef } from "@sk-widget/hooks";
import type { ActionDto, TransactionDto } from "@stakekit/api-hooks";
import {
Expand All @@ -15,11 +14,7 @@ import { type RefObject, useMemo, useState } from "react";
import { assign, emit, setup } from "xstate";
import { getAverageGasMode } from "../../../common/get-gas-mode-value";
import { withRequestErrorRetry } from "../../../common/utils";
import {
getTransactionsForMultiSign,
isTxError,
transactionsForConstructOnlySet,
} from "../../../domain";
import { isTxError } from "../../../domain";
import { useTrackEvent } from "../../../hooks/tracking/use-track-event";
import { useSKWallet } from "../../../providers/sk-wallet";
import type { GetStakeSessionError, SendTransactionError } from "./errors";
Expand Down Expand Up @@ -67,21 +62,10 @@ export const useStepsMachine = ({
transactions: ActionDto["transactions"];
integrationId: ActionDto["integrationId"];
}) => {
const {
signTransaction,
signMultipleTransactions,
signMessage,
connector,
isLedgerLive,
} = useSKWallet();
const { signTransaction, signMessage, isLedgerLive } = useSKWallet();

const trackEvent = useTrackEvent();

const multiSend = useMemo(
() => Maybe.fromNullable(connector).map(shouldMultiSend).orDefault(false),
[connector]
);

const sortedTransactions = useMemo(
() => transactions.sort((a, b) => a.stepIndex - b.stepIndex),
[transactions]
Expand All @@ -90,10 +74,8 @@ export const useStepsMachine = ({
const machineParams = useSavedRef({
transactions: sortedTransactions,
integrationId,
multiSend,
isLedgerLive,
trackEvent,
signMultipleTransactions,
signMessage,
signTransaction,
});
Expand All @@ -106,12 +88,8 @@ const getMachine = (
RefObject<{
transactions: ActionDto["transactions"];
integrationId: ActionDto["integrationId"];
multiSend: boolean;
isLedgerLive: boolean;
trackEvent: ReturnType<typeof useTrackEvent>;
signMultipleTransactions: ReturnType<
typeof useSKWallet
>["signMultipleTransactions"];
signMessage: ReturnType<typeof useSKWallet>["signMessage"];
signTransaction: ReturnType<typeof useSKWallet>["signTransaction"];
}>
Expand All @@ -126,8 +104,7 @@ const getMachine = (

const initContext = getInitContext(
ref.current.transactions,
ref.current.integrationId,
ref.current.multiSend
ref.current.integrationId
);

return setup({
Expand Down Expand Up @@ -241,80 +218,6 @@ const getMachine = (
.toEither(new Error("missing tx"))
)
.chain<Error, SignRes>((tx) => {
const txs = ref.current.transactions;

/**
* Multi sign transactions
*/
if (ref.current.multiSend) {
return EitherAsync.liftEither(
Right(
txs.find((tx) =>
transactionsForConstructOnlySet.has(tx.type)
)
)
)
.chain((constructOnlyTx) => {
if (!constructOnlyTx) {
return EitherAsync.liftEither(Right(null));
}

return getAverageGasMode({
network: constructOnlyTx.network,
})
.chainLeft(async () => Right(null))
.chain((gas) =>
txConstruct(constructOnlyTx.id, {
gasArgs: gas?.gasArgs,
ledgerWalletAPICompatible: ref.current.isLedgerLive,
}).mapLeft(() => new TransactionConstructError())
)
.chain(() =>
withRequestErrorRetry({
fn: () =>
transactionGetTransactionStatusFromId(
constructOnlyTx.id
),
retryTimes: 10,
retryWaitForMs: () => 5000,
}).mapLeft(
() =>
new Error(
`failed to get ${constructOnlyTx.id} tx status`
)
)
);
})
.map(() => getTransactionsForMultiSign(txs))
.chain((txs) =>
getAverageGasMode({ network: tx.network }).chain((gas) =>
EitherAsync.sequence(
txs.map((tx) =>
txConstruct(tx.id, {
gasArgs: gas?.gasArgs,
ledgerWalletAPICompatible: ref.current.isLedgerLive,
}).mapLeft(() => new TransactionConstructError())
)
)
)
)
.map((txs) =>
txs
.map((tx) => tx.unsignedTransaction)
.filter((tx) => tx !== null)
)
.chain((txs) => {
if (!txs.length) {
return EitherAsync.liftEither(
Left(new TransactionConstructError())
);
}

return ref.current.signMultipleTransactions({ txs });
})
.map((val) => ({ type: "regular", data: val }));
}

/**
* Single sign transactions
*/
Expand Down Expand Up @@ -667,8 +570,7 @@ const getMachine = (

const getInitContext = (
transactions: ActionDto["transactions"],
integrationId: ActionDto["integrationId"],
shouldMultiSend: boolean
integrationId: ActionDto["integrationId"]
) => {
if (!transactions.length) {
return {
Expand Down Expand Up @@ -700,7 +602,7 @@ const getInitContext = (

return {
enabled: true,
txStates: shouldMultiSend ? [txStates[currentTxIdx]] : txStates,
txStates,
currentTxMeta,
yieldId: integrationId,
};
Expand Down
108 changes: 50 additions & 58 deletions packages/widget/src/providers/sk-wallet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type { SKWallet } from "../../domain/types";
import { useTrackEvent } from "../../hooks/tracking/use-track-event";
import { useIsomorphicEffect } from "../../hooks/use-isomorphic-effect";
import {
NotSupportedFlowError,
type NotSupportedFlowError,
SendTransactionError,
TransactionDecodeError,
} from "../../pages/steps/hooks/errors";
Expand Down Expand Up @@ -260,66 +260,25 @@ export const SKWalletProvider = ({ children }: PropsWithChildren) => {
}

/**
* EVM connector
* Safe connector
*/
return EitherAsync.liftEither(
Either.encase(() => JSON.parse(tx))
.chain((val) => unsignedEVMTransactionCodec.decode(val))
.mapLeft((e) => {
console.log(e);
return new TransactionDecodeError();
})
).chain((val) =>
EitherAsync(() =>
/**
* Params need to be in strict format, don't spread the object(val)!
*/
sendTransactionAsync({
data: val.data,
to: val.to,
value: val.value,
nonce: val.nonce,
maxFeePerGas: val.maxFeePerGas,
maxPriorityFeePerGas: val.maxPriorityFeePerGas,
chainId: val.chainId,
gas: val.gasLimit,
type: val.maxFeePerGas ? "eip1559" : "legacy",
})
)
.mapLeft(() => new SendTransactionError())
.map((val) => ({ signedTx: val, broadcasted: true }))
);
}),
[connectorDetails, cosmosCW, ledgerCurrentAccountId, sendTransactionAsync]
);

const signMultipleTransactions = useCallback<
SKWallet["signMultipleTransactions"]
>(
({ txs }) =>
connectorDetails.chain<
TransactionDecodeError | SendTransactionError | NotSupportedFlowError,
{ signedTx: string; broadcasted: boolean }
>(({ conn, address }) => {
if (isSafeConnector(conn)) {
return EitherAsync.liftEither(
Either.sequence(
txs.map((tx) =>
Either.encase(() => JSON.parse(tx))
.chain((val) => unsignedEVMTransactionCodec.decode(val))
.map((val) => prepareEVMTx({ address, decodedTx: val }))
.mapLeft(() => new TransactionDecodeError())
)
)
Either.encase(() => JSON.parse(tx))
.chain((val) => unsignedEVMTransactionCodec.decode(val))
.map((val) => prepareEVMTx({ address, decodedTx: val }))
.mapLeft(() => new TransactionDecodeError())
)
.chain((val) =>
conn
.sendTransactions({
txs: val.map((v) => ({
data: v.data,
to: v.to,
value: v.value ?? "0",
})),
txs: [
{
data: val.data,
to: val.to,
value: val.value ?? "0",
},
],
})
.map((res) => res.safeTxHash)
)
Expand Down Expand Up @@ -361,9 +320,44 @@ export const SKWalletProvider = ({ children }: PropsWithChildren) => {
.map((val) => ({ signedTx: val as Hash, broadcasted: true }));
}

return EitherAsync.liftEither(Left(new NotSupportedFlowError()));
/**
* EVM connector
*/
return EitherAsync.liftEither(
Either.encase(() => JSON.parse(tx))
.chain((val) => unsignedEVMTransactionCodec.decode(val))
.mapLeft((e) => {
console.log(e);
return new TransactionDecodeError();
})
).chain((val) =>
EitherAsync(() =>
/**
* Params need to be in strict format, don't spread the object(val)!
*/
sendTransactionAsync({
data: val.data,
to: val.to,
value: val.value,
nonce: val.nonce,
maxFeePerGas: val.maxFeePerGas,
maxPriorityFeePerGas: val.maxPriorityFeePerGas,
chainId: val.chainId,
gas: val.gasLimit,
type: val.maxFeePerGas ? "eip1559" : "legacy",
})
)
.mapLeft(() => new SendTransactionError())
.map((val) => ({ signedTx: val, broadcasted: true }))
);
}),
[connectorDetails, checkIsUnmounted]
[
connectorDetails,
cosmosCW,
ledgerCurrentAccountId,
sendTransactionAsync,
checkIsUnmounted,
]
);

const signMessage = useCallback<SKWallet["signMessage"]>(
Expand Down Expand Up @@ -400,7 +394,6 @@ export const SKWalletProvider = ({ children }: PropsWithChildren) => {
const common = {
disconnect,
signTransaction,
signMultipleTransactions,
signMessage,
connectorChains,
isLedgerLive,
Expand Down Expand Up @@ -451,7 +444,6 @@ export const SKWalletProvider = ({ children }: PropsWithChildren) => {
network,
onLedgerAccountChange,
signTransaction,
signMultipleTransactions,
signMessage,
]);

Expand Down