Skip to content

Commit

Permalink
Merge pull request #134 from etherspot/fix/prime-sdk-txs-history
Browse files Browse the repository at this point in the history
fixed prime sdk transactions
  • Loading branch information
poocart authored Mar 28, 2024
2 parents 72c62d5 + cd78ea8 commit bc5d46f
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 20 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## [0.11.0] - 2024-03-28

### Added Changes
- Added `chainId` param to `useEtherspotNfts` hook's `getTransactions` and `getTransaction` methods
- Fixed `useEtherspotNfts` hook's `getTransactions` method returned result to match back-end changes

### Breaking Changes
- Transactions returned by `useEtherspotNfts` hook's `getTransactions` method are now different type called `UserOpTransaction`

## [0.10.1] - 2024-03-20

### Added Changes
Expand Down
8 changes: 4 additions & 4 deletions __mocks__/@etherspot/prime-sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,18 @@ export class DataUtils {
];

if (chainId !== 1) {
return { items: [] };
return { transactions: [] };
}

if (account === defaultAccountAddress) {
return { items: accountTransactions };
return { transactions: accountTransactions };
}

if (account === otherAccountAddress) {
return { items: [{ hash: '0x69', value: '0' }] };
return { transactions: [{ hash: '0x69', value: '0' }] };
}

return { items: [] };
return { transactions: [] };
}
getTransaction({ hash, chainId }) {
if (hash !== '0x42' || chainId !== 1) return;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@etherspot/transaction-kit",
"description": "React Etherspot Transaction Kit",
"version": "0.10.1",
"version": "0.11.0",
"main": "dist/cjs/index.js",
"scripts": {
"rollup:build": "NODE_OPTIONS=--max-old-space-size=8192 rollup -c",
Expand Down
33 changes: 20 additions & 13 deletions src/hooks/useEtherspotHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,34 @@ import { useMemo } from 'react';
// hooks
import useEtherspot from './useEtherspot';

// types
import { UserOpTransaction } from '../types/EtherspotTransactionKit';

interface IEtherspotHistoryHook {
getAccountTransactions: (accountAddress?: string) => Promise<Transaction[]>;
getAccountTransaction: (hash: string) => Promise<Transaction | undefined>;
getAccountTransactions: (accountAddress?: string, chainId?: number) => Promise<UserOpTransaction[]>;
getAccountTransaction: (hash: string, chainId?: number) => Promise<Transaction | undefined>;
}

/**
* Hook to fetch Etherspot account transactions history
* @param chainId {number | undefined} - Chain ID
* @returns {IEtherspotHistoryHook} - hook methods to fetch Etherspot transactions history
*/
const useEtherspotHistory = (chainId: number): IEtherspotHistoryHook => {
const { getDataService, getSdk, chainId: defaultChainId } = useEtherspot();
const useEtherspotHistory = (chainId?: number): IEtherspotHistoryHook => {
const { getDataService, getSdk, chainId: etherspotChainId } = useEtherspot();

const historyChainId = useMemo(() => {
const defaultChainId = useMemo(() => {
if (chainId) return chainId;
return defaultChainId;
}, [chainId, defaultChainId]);
return etherspotChainId;
}, [chainId, etherspotChainId]);

const getAccountTransactions = async (accountAddress?: string): Promise<Transaction[]> => {
const getAccountTransactions = async (
accountAddress?: string,
historyChainId: number = defaultChainId,
): Promise<UserOpTransaction[]> => {
const sdkForChainId = await getSdk(historyChainId);

let transactions: Transaction[] = [];
let transactions: UserOpTransaction[] = [];

const transactionsForAccount = accountAddress ?? await sdkForChainId.getCounterFactualAddress();
if (!transactionsForAccount) {
Expand All @@ -35,9 +41,7 @@ const useEtherspotHistory = (chainId: number): IEtherspotHistoryHook => {

try {
const dataService = getDataService();
// TODO: fix once available on Prime SDK
// @ts-ignore
({ items: transactions } = await dataService.getTransactions({
({ transactions } = await dataService.getTransactions({
account: transactionsForAccount,
chainId: +historyChainId,
}));
Expand All @@ -53,7 +57,10 @@ const useEtherspotHistory = (chainId: number): IEtherspotHistoryHook => {
return transactions;
};

const getAccountTransaction = async (hash: string): Promise<Transaction | undefined> => {
const getAccountTransaction = async (
hash: string,
historyChainId: number = defaultChainId,
): Promise<Transaction | undefined> => {
try {
const dataService = getDataService();
return dataService.getTransaction({ hash, chainId: +historyChainId });
Expand Down
66 changes: 66 additions & 0 deletions src/types/EtherspotTransactionKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BigNumber, BigNumberish, BytesLike } from 'ethers';
import { Route } from '@lifi/types';
import { PaymasterApi } from '@etherspot/prime-sdk';
import { ExchangeOffer } from '@etherspot/prime-sdk/dist/sdk/data';
import { TransactionStatuses } from '@etherspot/prime-sdk/dist/sdk/data/constants';

export interface ITransaction {
id?: string;
Expand Down Expand Up @@ -116,4 +117,69 @@ interface UserOp {
signature: EtherspotPromiseOrValue<BytesLike>;
}

// TODO: remove once available on Prime SDK
interface EtherspotErc20TransfersEntity {
from: string;
to: string;
value: number;
asset?: string;
address: string;
decimal: number;
}

// TODO: remove once available on Prime SDK
interface EtherspotNativeTransfersEntity {
from: string;
to: string;
value: string;
asset?: string;
address: string;
decimal: number;
data: string;
}

// TODO: remove once available on Prime SDK
interface EtherspotNftTransfersEntity {
from: string;
to: string;
value: number;
tokenId: number;
asset?: string;
category: string;
address: string;
}

// TODO: remove once available on Prime SDK
export interface UserOpTransaction {
chainId: number;
sender: string;
target?: string | null;
transactionHash: string;
userOpHash: string;
actualGasCost: number;
actualGasUsed: number;
success: TransactionStatuses;
timestamp: number;
paymaster: string;
value: number;
blockExplorerUrl: string;
input: string;
nonce: number;
initCode?: string;
callData?: string;
accountGasLimits?: string;
gasFees?: string;
callGasLimit: BigNumber;
verificationGasLimit: BigNumber;
preVerificationGas: BigNumber;
maxFeePerGas: BigNumber;
maxPriorityFeePerGas: BigNumber;
paymasterAndData?: string;
signature?: string;
beneficiary?: string;
nativeTransfers?: EtherspotNativeTransfersEntity[];
erc20Transfers?: EtherspotErc20TransfersEntity[];
nftTransfers?: EtherspotNftTransfersEntity[];
}

export type AccountTemplate = 'etherspot' | 'zeroDev' | 'simpleAccount';

0 comments on commit bc5d46f

Please sign in to comment.