-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: revamp RPC
starkNet_getTransactionStatus
(#447)
* chore: add chain rpc controller * chore: relocate base rpc controller * chore: revamp getTransactionStatus API * chore: update get transaction status code * chore: remove mocha test * chore: remove get transactions test
- Loading branch information
1 parent
464d65c
commit 5d569a8
Showing
8 changed files
with
153 additions
and
384 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/starknet-snap/src/rpcs/get-transaction-status.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import type { constants } from 'starknet'; | ||
import { | ||
TransactionExecutionStatus, | ||
TransactionFinalityStatus, | ||
} from 'starknet'; | ||
|
||
import { mockNetworkStateManager } from '../state/__tests__/helper'; | ||
import type { Network } from '../types/snapState'; | ||
import { STARKNET_SEPOLIA_TESTNET_NETWORK } from '../utils/constants'; | ||
import { InvalidRequestParamsError } from '../utils/exceptions'; | ||
import * as starknetUtils from '../utils/starknetUtils'; | ||
import type { GetTransactionStatusParams } from './get-transaction-status'; | ||
import { getTransactionStatus } from './get-transaction-status'; | ||
|
||
jest.mock('../utils/snap'); | ||
jest.mock('../utils/logger'); | ||
|
||
describe('GetTransactionStatusRpc', () => { | ||
const prepareGetTransactionStatusTest = ({ | ||
network, | ||
status, | ||
}: { | ||
network: Network; | ||
status: { | ||
finalityStatus: TransactionFinalityStatus; | ||
executionStatus: TransactionExecutionStatus; | ||
}; | ||
}) => { | ||
const { getNetworkSpy } = mockNetworkStateManager(network); | ||
|
||
const getTransactionStatusSpy = jest.spyOn( | ||
starknetUtils, | ||
'getTransactionStatus', | ||
); | ||
|
||
getTransactionStatusSpy.mockResolvedValue(status); | ||
|
||
return { | ||
getTransactionStatusSpy, | ||
getNetworkSpy, | ||
}; | ||
}; | ||
|
||
it('returns transaction status', async () => { | ||
const network = STARKNET_SEPOLIA_TESTNET_NETWORK; | ||
const transactionHash = | ||
'0x06385d46da9fbed4a5798298b17df069ac5f786e4c9f8f6b81c665540aea245a'; | ||
const expectedResult = { | ||
finalityStatus: TransactionFinalityStatus.ACCEPTED_ON_L1, | ||
executionStatus: TransactionExecutionStatus.SUCCEEDED, | ||
}; | ||
const { getTransactionStatusSpy } = prepareGetTransactionStatusTest({ | ||
network, | ||
status: expectedResult, | ||
}); | ||
|
||
const result = await getTransactionStatus.execute({ | ||
chainId: network.chainId as unknown as constants.StarknetChainId, | ||
transactionHash, | ||
}); | ||
|
||
expect(result).toStrictEqual(expectedResult); | ||
expect(getTransactionStatusSpy).toHaveBeenCalledWith( | ||
transactionHash, | ||
network, | ||
); | ||
}); | ||
|
||
it('throws `InvalidRequestParamsError` when request parameter is not correct', async () => { | ||
await expect( | ||
getTransactionStatus.execute({} as unknown as GetTransactionStatusParams), | ||
).rejects.toThrow(InvalidRequestParamsError); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { HexStruct } from '@metamask/utils'; | ||
import { | ||
TransactionExecutionStatus, | ||
TransactionFinalityStatus, | ||
} from 'starknet'; | ||
import type { Infer } from 'superstruct'; | ||
import { assign, nonempty, object, enums, optional } from 'superstruct'; | ||
|
||
import { BaseRequestStruct } from '../utils'; | ||
import { getTransactionStatus as getTransactionStatusFn } from '../utils/starknetUtils'; | ||
import { ChainRpcController } from './abstract/chain-rpc-controller'; | ||
|
||
export const GetTransactionStatusRequestStruct = assign( | ||
object({ | ||
transactionHash: nonempty(HexStruct), | ||
}), | ||
BaseRequestStruct, | ||
); | ||
|
||
export const GetTransactionStatusResponseStruct = object({ | ||
executionStatus: optional(enums(Object.values(TransactionExecutionStatus))), | ||
finalityStatus: optional(enums(Object.values(TransactionFinalityStatus))), | ||
}); | ||
|
||
export type GetTransactionStatusParams = Infer< | ||
typeof GetTransactionStatusRequestStruct | ||
>; | ||
|
||
export type GetTransactionStatusResponse = Infer< | ||
typeof GetTransactionStatusResponseStruct | ||
>; | ||
|
||
/** | ||
* The RPC handler to get a transaction status by the given transaction hash. | ||
*/ | ||
export class GetTransactionStatusRpc extends ChainRpcController< | ||
GetTransactionStatusParams, | ||
GetTransactionStatusResponse | ||
> { | ||
protected requestStruct = GetTransactionStatusRequestStruct; | ||
|
||
protected responseStruct = GetTransactionStatusResponseStruct; | ||
|
||
/** | ||
* Execute the get transaction request handler. | ||
* | ||
* @param params - The parameters of the request. | ||
* @param params.transactionHash - The transaction hash to enquire the transaction status. | ||
* @param params.chainId - The chain id of the transaction. | ||
* @returns A promise that resolves to a GetTransactionStatusResponse object that contains executionStatus and finalityStatus. | ||
*/ | ||
async execute( | ||
params: GetTransactionStatusParams, | ||
): Promise<GetTransactionStatusResponse> { | ||
return super.execute(params); | ||
} | ||
|
||
protected async handleRequest( | ||
params: GetTransactionStatusParams, | ||
): Promise<GetTransactionStatusResponse> { | ||
const { transactionHash } = params; | ||
|
||
const resp = await getTransactionStatusFn(transactionHash, this.network); | ||
|
||
return resp; | ||
} | ||
} | ||
|
||
export const getTransactionStatus = new GetTransactionStatusRpc(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 0 additions & 69 deletions
69
packages/starknet-snap/test/src/getTransactionStatus.test.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.