-
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.
refactor: revamp
starkNet_estimateFee
(#329)
* chore: revamp estimate fee and removed unused rpc calls * fix: verify if account need upgrade or deploy does not always throw * fix: address comments * refactor: revamp sign delcare transaction * feat: match starknet.js signature in estimate fee * feat: createStructWithAdditionalProperties superstruct factory * fix: improve error message in rpc input validation * refactor: add universal details and invocations in superstruct util * chore: lint + prettier * chore: add superstruct test * chore: update get-starknet interface * fix: address comments * chore: restrict tx version to v3 and v2 * fix: address further comments * fix: address further comments * refactor: rpc estimatefee test ts * chore: lint + prettier * feat: use define superstruct instead of union * fix: getEstimatedFees * chore: refine estimate fee (#337) * fix: address comments * chore: lint + prettier * fix: address comments * feat: remove transactionVersion, use invocationDetails * fix: last one --------- Co-authored-by: stanleyyuen <102275989+stanleyyconsensys@users.noreply.github.com>
- Loading branch information
1 parent
d7708da
commit e6d2518
Showing
19 changed files
with
5,814 additions
and
43 deletions.
There are no files selected for viewing
4,969 changes: 4,969 additions & 0 deletions
4,969
packages/starknet-snap/src/__tests__/fixture/contract-example.json
Large diffs are not rendered by default.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { InvalidParamsError } from '@metamask/snaps-sdk'; | ||
import type { Invocations } from 'starknet'; | ||
import { constants, TransactionType } from 'starknet'; | ||
import type { Infer } from 'superstruct'; | ||
|
||
import { FeeTokenUnit } from '../types/snapApi'; | ||
import { STARKNET_SEPOLIA_TESTNET_NETWORK } from '../utils/constants'; | ||
import * as starknetUtils from '../utils/starknetUtils'; | ||
import type { TxVersionStruct } from '../utils/superstruct'; | ||
import { mockAccount, prepareMockAccount } from './__tests__/helper'; | ||
import { estimateFee } from './estimateFee'; | ||
import type { EstimateFeeParams } from './estimateFee'; | ||
|
||
jest.mock('../utils/snap'); | ||
jest.mock('../utils/logger'); | ||
|
||
const prepareMockEstimateFee = ({ | ||
chainId, | ||
address, | ||
version, | ||
includeDeploy = false, | ||
}: { | ||
chainId: constants.StarknetChainId; | ||
address: string; | ||
version: Infer<typeof TxVersionStruct>; | ||
includeDeploy?: boolean; | ||
}) => { | ||
const invocations: Invocations = [ | ||
{ | ||
type: TransactionType.INVOKE, | ||
payload: { | ||
contractAddress: | ||
'0x00b28a089e7fb83debee4607b6334d687918644796b47d9e9e38ea8213833137', | ||
entrypoint: 'functionName', | ||
calldata: ['1', '1'], | ||
}, | ||
}, | ||
]; | ||
|
||
const request = { | ||
chainId, | ||
address, | ||
invocations, | ||
details: { version }, | ||
} as unknown as EstimateFeeParams; | ||
|
||
const estimateBulkFeeRespMock = { | ||
suggestedMaxFee: BigInt(1000000000000000).toString(10), | ||
overallFee: BigInt(1500000000000000).toString(10), | ||
unit: FeeTokenUnit.ETH, | ||
includeDeploy, | ||
}; | ||
|
||
const getEstimatedFeesSpy = jest.spyOn(starknetUtils, 'getEstimatedFees'); | ||
getEstimatedFeesSpy.mockResolvedValue(estimateBulkFeeRespMock); | ||
|
||
return { estimateBulkFeeRespMock, invocations, request, getEstimatedFeesSpy }; | ||
}; | ||
|
||
describe('estimateFee', () => { | ||
const state = { | ||
accContracts: [], | ||
erc20Tokens: [], | ||
networks: [STARKNET_SEPOLIA_TESTNET_NETWORK], | ||
transactions: [], | ||
}; | ||
|
||
it('estimates fee correctly', async () => { | ||
const chainId = constants.StarknetChainId.SN_SEPOLIA; | ||
const account = await mockAccount(chainId); | ||
prepareMockAccount(account, state); | ||
const { request, getEstimatedFeesSpy, estimateBulkFeeRespMock } = | ||
prepareMockEstimateFee({ | ||
includeDeploy: false, | ||
chainId, | ||
address: account.address, | ||
version: constants.TRANSACTION_VERSION.V2, | ||
}); | ||
|
||
const result = await estimateFee.execute(request); | ||
|
||
expect(getEstimatedFeesSpy).toHaveBeenCalledWith( | ||
STARKNET_SEPOLIA_TESTNET_NETWORK, | ||
account.address, | ||
account.privateKey, | ||
account.publicKey, | ||
request.invocations, | ||
{ | ||
version: constants.TRANSACTION_VERSION.V2, | ||
}, | ||
); | ||
expect(result).toStrictEqual(estimateBulkFeeRespMock); | ||
}); | ||
|
||
it('throws `InvalidParamsError` when request parameter is not correct', async () => { | ||
await expect( | ||
estimateFee.execute({} as unknown as EstimateFeeParams), | ||
).rejects.toThrow(InvalidParamsError); | ||
}); | ||
}); |
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,81 @@ | ||
import type { Json } from '@metamask/snaps-sdk'; | ||
import type { UniversalDetails } from 'starknet'; | ||
import type { Infer } from 'superstruct'; | ||
import { object, string, assign, boolean, optional, enums } from 'superstruct'; | ||
|
||
import { FeeTokenUnit } from '../types/snapApi'; | ||
import { | ||
AddressStruct, | ||
BaseRequestStruct, | ||
AccountRpcController, | ||
UniversalDetailsStruct, | ||
InvocationsStruct, | ||
} from '../utils'; | ||
import { getEstimatedFees } from '../utils/starknetUtils'; | ||
|
||
export const EstimateFeeRequestStruct = assign( | ||
object({ | ||
address: AddressStruct, | ||
invocations: InvocationsStruct, | ||
details: optional(UniversalDetailsStruct), | ||
}), | ||
BaseRequestStruct, | ||
); | ||
|
||
export const EstimateFeeResponseStruct = object({ | ||
suggestedMaxFee: string(), | ||
overallFee: string(), | ||
unit: enums(Object.values(FeeTokenUnit)), | ||
includeDeploy: boolean(), | ||
}); | ||
|
||
export type EstimateFeeParams = Infer<typeof EstimateFeeRequestStruct> & Json; | ||
|
||
export type EstimateFeeResponse = Infer<typeof EstimateFeeResponseStruct>; | ||
|
||
/** | ||
* The RPC handler to estimate fee of a transaction. | ||
*/ | ||
export class EstimateFeeRpc extends AccountRpcController< | ||
EstimateFeeParams, | ||
EstimateFeeResponse | ||
> { | ||
protected requestStruct = EstimateFeeRequestStruct; | ||
|
||
protected responseStruct = EstimateFeeResponseStruct; | ||
|
||
/** | ||
* Execute the bulk estimate transaction fee request handler. | ||
* | ||
* @param params - The parameters of the request. | ||
* @param params.address - The account address. | ||
* @param params.invocations - The invocations to estimate fee. Reference: https://starknetjs.com/docs/API/namespaces/types#invocations | ||
* @param params.details - The universal details associated to the invocations. Reference: https://starknetjs.com/docs/API/interfaces/types.EstimateFeeDetails | ||
* @param params.chainId - The chain id of the network. | ||
* @returns A promise that resolves to a EstimateFeeResponse object. | ||
*/ | ||
async execute(params: EstimateFeeParams): Promise<EstimateFeeResponse> { | ||
return super.execute(params); | ||
} | ||
|
||
protected async handleRequest( | ||
params: EstimateFeeParams, | ||
): Promise<EstimateFeeResponse> { | ||
const { address, invocations, details } = params; | ||
|
||
const estimateFeeResp = await getEstimatedFees( | ||
this.network, | ||
address, | ||
this.account.privateKey, | ||
this.account.publicKey, | ||
invocations, | ||
details as UniversalDetails, | ||
); | ||
|
||
return estimateFeeResp; | ||
} | ||
} | ||
|
||
export const estimateFee = new EstimateFeeRpc({ | ||
showInvalidAccountAlert: false, | ||
}); |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './signMessage'; | ||
export * from './displayPrivateKey'; | ||
export * from './estimateFee'; | ||
export * from './signMessage'; | ||
export * from './signTransaction'; | ||
export * from './sign-declare-transaction'; | ||
export * from './verify-signature'; |
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
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
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
Oops, something went wrong.