Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactor RPC starkNet_getTransactions with StarkScan DataClient #453

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
77f4b7b
feat: add stark scan client
stanleyyconsensys Sep 2, 2024
6b7a5e2
chore: add starkscan config
stanleyyconsensys Sep 2, 2024
3c9a523
chore: lint
stanleyyconsensys Sep 2, 2024
576f302
chore: add interface
stanleyyconsensys Sep 2, 2024
19fdf56
Merge branch 'main' into feat/add-stark-scan-client
stanleyyconsensys Sep 5, 2024
5d446cb
chore: support multiple txn
stanleyyconsensys Sep 5, 2024
3dbdf32
chore: update starkscan
stanleyyconsensys Sep 5, 2024
924ea45
chore: update stark scan client
stanleyyconsensys Sep 6, 2024
bcf34c7
chore: update contract func name
stanleyyconsensys Sep 6, 2024
d1ad70c
chore: fix test
stanleyyconsensys Sep 6, 2024
1a061d3
chore: update data client
stanleyyconsensys Sep 6, 2024
3bda85f
Merge branch 'main' into feat/add-stark-scan-client
stanleyyconsensys Sep 6, 2024
114ea6d
Merge branch 'main' into feat/add-stark-scan-client
stanleyyconsensys Oct 22, 2024
7539056
Merge branch 'main' into feat/add-stark-scan-client
stanleyyconsensys Dec 2, 2024
ed699ff
chore: re-structure starkscan type
stanleyyconsensys Dec 2, 2024
93adb36
chore: add test coverage
stanleyyconsensys Dec 2, 2024
dadb493
chore: factory and config
stanleyyconsensys Dec 2, 2024
b21a5d2
chore: add backward compatibility for transactions type
stanleyyconsensys Dec 2, 2024
07f0232
chore: add comment
stanleyyconsensys Dec 2, 2024
7a26c70
chore: lint
stanleyyconsensys Dec 2, 2024
804a2bd
chore: resolve review comment
stanleyyconsensys Dec 4, 2024
8e9e163
chore: change dataVersion to enum
stanleyyconsensys Dec 4, 2024
b09361f
chore: lint
stanleyyconsensys Dec 4, 2024
7cbd66f
chore: update test helper and refactor ContractAddressFilter
stanleyyconsensys Dec 5, 2024
f657422
chore: lint
stanleyyconsensys Dec 5, 2024
4ee01bc
chore: add test for dataVersion filter
stanleyyconsensys Dec 5, 2024
3a99e76
chore: update txn state mgr test
stanleyyconsensys Dec 5, 2024
38e5d63
chore: update search condition
stanleyyconsensys Dec 5, 2024
63e3030
chore: update starkscan to handle missing selector_name
stanleyyconsensys Dec 5, 2024
6391e81
Merge branch 'feat/add-stark-scan-client' into chore/refactor-txn-mgr
stanleyyconsensys Dec 5, 2024
873a1de
Merge branch 'main' into chore/refactor-txn-mgr
khanti42 Dec 5, 2024
97f0e05
Merge branch 'main' into chore/revamp-list-transactions
stanleyyconsensys Dec 5, 2024
956b10e
chore: apply starkscan for list transaction
stanleyyconsensys Dec 6, 2024
29f7c5f
chore: update list transactions handle
stanleyyconsensys Dec 6, 2024
9dba1a9
chore: resolve comment
stanleyyconsensys Dec 9, 2024
ca474d0
chore: update function name
stanleyyconsensys Dec 10, 2024
60e2689
Merge branch 'main' into chore/revamp-list-transactions
stanleyyconsensys Dec 10, 2024
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
2 changes: 2 additions & 0 deletions packages/starknet-snap/src/__tests__/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ function getTransactionTemplate() {
finalityStatus: '',
accountCalls: null,
version: 1,
maxFee: null,
actualFee: null,
dataVersion: TransactionDataVersion.V2,
};
}
Expand Down
267 changes: 267 additions & 0 deletions packages/starknet-snap/src/chain/transaction-service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
import { TransactionFinalityStatus, TransactionType } from 'starknet';

import { generateAccounts, generateTransactions } from '../__tests__/helper';
import { mockTransactionStateManager } from '../state/__tests__/helper';
import type { Network, Transaction } from '../types/snapState';
import { TransactionDataVersion } from '../types/snapState';
import {
ETHER_SEPOLIA_TESTNET,
STARKNET_SEPOLIA_TESTNET_NETWORK,
STRK_SEPOLIA_TESTNET,
} from '../utils/constants';
import type { IDataClient } from './data-client';
import { TransactionService } from './transaction-service';

describe('TransactionService', () => {
class MockTransactionService extends TransactionService {
async *getTransactionsOnChain(
address: string,
contractAddress: string,
tillToInDays: number,
) {
yield* super.getTransactionsOnChain(
address,
contractAddress,
tillToInDays,
);
}

async *getTransactionsOnState(address: string, contractAddress: string) {
yield* super.getTransactionsOnState(address, contractAddress);
}

async *filterTransactionsByContractAddress(
transactions: Transaction[],
contractAddress: string,
) {
yield* super.filterTransactionsByContractAddress(
transactions,
contractAddress,
);
}

hasMatchingContractOrIsDeploy(tx: Transaction, contractAddress: string) {
return super.hasMatchingContractOrIsDeploy(tx, contractAddress);
}
}

const mockDataClient = () => {
const getTransactionsSpy = jest.fn();

const dataClient: IDataClient = {
getTransactions: getTransactionsSpy,
getDeployTransaction: jest.fn(),
};

return {
dataClient,
getTransactionsSpy,
};
};

const mockTransactionService = (
network: Network,
dataClient: IDataClient,
) => {
const service = new MockTransactionService({
dataClient,
network,
});

return service;
};

const mockAddress = async (network: Network) => {
const [{ address }] = await generateAccounts(network.chainId, 1);
return address;
};

const generateEthAndStrkContractTransactions = ({ address, chainId }) => {
const ethContractAddress = ETHER_SEPOLIA_TESTNET.address;
const strkContractAddress = STRK_SEPOLIA_TESTNET.address;

// generate transactions for eth contract, include deploy and invoke transactions
const mockedEthTrasactions = generateTransactions({
cnt: 10,
address,
txnTypes: [TransactionType.DEPLOY_ACCOUNT, TransactionType.INVOKE],
chainId,
contractAddresses: [ethContractAddress],
});

const lastTx = mockedEthTrasactions[mockedEthTrasactions.length - 1];
const lastTxHashInBigInt = BigInt(lastTx.txnHash);

// generate transactions for strk contract, include invoke transactions only
const mockedStrkTrasactions = generateTransactions({
cnt: 10,
address,
chainId,
txnTypes: [TransactionType.INVOKE],
contractAddresses: [strkContractAddress],
// make sure the txnHash is unique for the transactions in strk contract
baseTxnHashInBigInt: lastTxHashInBigInt + BigInt(1),
});

return mockedEthTrasactions
.concat(mockedStrkTrasactions)
.sort((tx1, tx2) => tx2.timestamp - tx1.timestamp);
};

const prepareGetTransactions = async () => {
const network = STARKNET_SEPOLIA_TESTNET_NETWORK;
const address = await mockAddress(network);
const transactionsFromDataClientOrState =
generateEthAndStrkContractTransactions({
address,
chainId: network.chainId,
});
// the given contract address
const contractAddress = ETHER_SEPOLIA_TESTNET.address;

const { findTransactionsSpy, removeTransactionsSpy } =
mockTransactionStateManager();
const { getTransactionsSpy, dataClient } = mockDataClient();
removeTransactionsSpy.mockReturnThis();
findTransactionsSpy.mockResolvedValue(transactionsFromDataClientOrState);
getTransactionsSpy.mockResolvedValue(transactionsFromDataClientOrState);

const service = mockTransactionService(network, dataClient);

const filteredTransactions = transactionsFromDataClientOrState.filter(
(tx) => service.hasMatchingContractOrIsDeploy(tx, contractAddress),
);

return {
removeTransactionsSpy,
findTransactionsSpy,
getTransactionsSpy,
service,
transactionsFromDataClientOrState,
filteredTransactions,
network,
address,
contractAddress,
};
};

describe('getTransactionsOnChain', () => {
it('returns transactions on chain', async () => {
const {
service,
getTransactionsSpy,
filteredTransactions,
address,
contractAddress,
} = await prepareGetTransactions();

const transactions: Transaction[] = [];

for await (const tx of service.getTransactionsOnChain(
address,
contractAddress,
10,
)) {
transactions.push(tx);
}

expect(getTransactionsSpy).toHaveBeenCalledWith(
address,
expect.any(Number),
);
expect(transactions).toStrictEqual(filteredTransactions);
});
});

describe('getTransactionsOnState', () => {
it('returns transactions on state', async () => {
const {
service,
findTransactionsSpy,
filteredTransactions,
network,
address,
contractAddress,
} = await prepareGetTransactions();

const transactions: Transaction[] = [];
for await (const tx of service.getTransactionsOnState(
address,
contractAddress,
)) {
transactions.push(tx);
}

expect(findTransactionsSpy).toHaveBeenCalledWith({
senderAddress: [address],
chainId: [network.chainId],
finalityStatus: [TransactionFinalityStatus.RECEIVED],
dataVersion: [TransactionDataVersion.V2],
});
expect(transactions).toStrictEqual(filteredTransactions);
});
});

describe('getTransactions', () => {
it('returns and merge the transactions from chain and state', async () => {
const {
service,
filteredTransactions: transactionsFromChain,
findTransactionsSpy,
network,
address,
contractAddress,
} = await prepareGetTransactions();

const lastTransactionFromChain =
transactionsFromChain[transactionsFromChain.length - 1];
const lastTransactionHashInBigInt = BigInt(
lastTransactionFromChain.txnHash,
);
const transactionFromState = generateTransactions({
cnt: 5,
address,
chainId: network.chainId,
txnTypes: [TransactionType.INVOKE],
// make sure the contract address is match to the given contract address, so we can merge it with the transactions from chain
contractAddresses: [contractAddress],
// make sure the txnHash is different with the transaction from chain
baseTxnHashInBigInt: lastTransactionHashInBigInt * BigInt(2),
});
findTransactionsSpy.mockResolvedValue(transactionFromState);

const result = await service.getTransactions(
address,
contractAddress,
10,
);

const expectedResult = transactionFromState.concat(transactionsFromChain);

expect(result).toStrictEqual(expectedResult);
});

it('remove the transactions that are already on chain', async () => {
const {
service,
filteredTransactions: transactionsFromChain,
removeTransactionsSpy,
address,
contractAddress,
findTransactionsSpy,
} = await prepareGetTransactions();

const duplicatedTransactions = [
transactionsFromChain[transactionsFromChain.length - 1],
];

findTransactionsSpy.mockResolvedValue(duplicatedTransactions);

await service.getTransactions(address, contractAddress, 10);

expect(removeTransactionsSpy).toHaveBeenCalledWith({
txnHash: [duplicatedTransactions[0].txnHash],
});
});
});
});
Loading
Loading