Skip to content

Commit

Permalink
Fix starknet_EstimateFee (#500)
Browse files Browse the repository at this point in the history
* Fix starknet_EstimateFee

* small tidy
  • Loading branch information
rianhughes authored Dec 18, 2023
1 parent 7d4e5b4 commit 915109a
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 22 deletions.
4 changes: 2 additions & 2 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,8 +712,8 @@ func (account *Account) ClassHashAt(ctx context.Context, blockID rpc.BlockID, co
// Returns:
// - []rpc.FeeEstimate: An array of rpc.FeeEstimate objects representing the estimated fees.
// - error: An error object if any error occurred during the estimation process.
func (account *Account) EstimateFee(ctx context.Context, requests []rpc.BroadcastTxn, simulationFlag rpc.SimulationFlag, blockID rpc.BlockID) ([]rpc.FeeEstimate, error) {
return account.provider.EstimateFee(ctx, requests, simulationFlag, blockID)
func (account *Account) EstimateFee(ctx context.Context, requests []rpc.BroadcastTxn, simulationFlags []rpc.SimulationFlag, blockID rpc.BlockID) ([]rpc.FeeEstimate, error) {
return account.provider.EstimateFee(ctx, requests, simulationFlags, blockID)
}

// EstimateMessageFee estimates the fee for a given message in the context of an account.
Expand Down
28 changes: 16 additions & 12 deletions mocks/mock_account.go

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

8 changes: 4 additions & 4 deletions mocks/mock_rpc_provider.go

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

4 changes: 2 additions & 2 deletions rpc/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ func (provider *Provider) Nonce(ctx context.Context, blockID BlockID, contractAd
// Estimates the resources required by a given sequence of transactions when applied on a given state.
// If one of the transactions reverts or fails due to any reason (e.g. validation failure or an internal error),
// a TRANSACTION_EXECUTION_ERROR is returned. For v0-2 transactions the estimate is given in wei, and for v3 transactions it is given in fri.
func (provider *Provider) EstimateFee(ctx context.Context, requests []BroadcastTxn, simulationFlag SimulationFlag, blockID BlockID) ([]FeeEstimate, error) {
func (provider *Provider) EstimateFee(ctx context.Context, requests []BroadcastTxn, simulationFlags []SimulationFlag, blockID BlockID) ([]FeeEstimate, error) {
var raw []FeeEstimate
if err := do(ctx, provider.c, "starknet_estimateFee", &raw, requests, simulationFlag, blockID); err != nil {
if err := do(ctx, provider.c, "starknet_estimateFee", &raw, requests, simulationFlags, blockID); err != nil {
return nil, tryUnwrapToRPCErr(err, ErrTxnExec, ErrBlockNotFound)
}
return raw, nil
Expand Down
102 changes: 102 additions & 0 deletions rpc/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,105 @@ func TestEstimateMessageFee(t *testing.T) {

}
}

func TestEstimateFee(t *testing.T) {
testConfig := beforeEach(t)

testBlockNumber := uint64(15643)
type testSetType struct {
txs []BroadcastTxn
simFlags []SimulationFlag
blockID BlockID
expectedResp []FeeEstimate
expectedError error
}
testSet := map[string][]testSetType{
"mainnet": {
{
txs: []BroadcastTxn{
InvokeTxnV0{
Type: TransactionType_Invoke,
Version: TransactionV0,
MaxFee: utils.TestHexToFelt(t, "0x95e566845d000"),
FunctionCall: FunctionCall{
ContractAddress: utils.TestHexToFelt(t, "0x45e92c365ba0908382bc346159f896e528214470c60ae2cd4038a0fff747b1e"),
EntryPointSelector: utils.TestHexToFelt(t, "0x15d40a3d6ca2ac30f4031e42be28da9b056fef9bb7357ac5e85627ee876e5ad"),
Calldata: utils.TestHexArrToFelt(t, []string{
"0x1",
"0x4a3621276a83251b557a8140e915599ae8e7b6207b067ea701635c0d509801e",
"0x2f0b3c5710379609eb5495f1ecd348cb28167711b73609fe565a72734550354",
"0x0",
"0x3",
"0x3",
"0x697066733a2f2f516d57554c7a475135556a52616953514776717765347931",
"0x4731796f4757324e6a5a76564e77776a66514577756a",
"0x0",
"0x2"}),
},
Signature: []*felt.Felt{
utils.TestHexToFelt(t, "0x63e4618ca2e323a45b9f860f12a4f5c4984648f1d110aa393e79d596d82abcc"),
utils.TestHexToFelt(t, "0x2844257b088ad4f49e2fe3df1ea6a8530aa2d21d8990112b7e88c4bd0ce9d50"),
},
},
},
simFlags: []SimulationFlag{},
blockID: BlockID{Number: &testBlockNumber},
expectedError: nil,
expectedResp: []FeeEstimate{
{
GasConsumed: utils.TestHexToFelt(t, "0x39b8"),
GasPrice: utils.TestHexToFelt(t, "0x350da9915"),
OverallFee: utils.TestHexToFelt(t, "0xbf62c933b418"),
FeeUnit: UnitWei,
},
},
},
{

txs: []BroadcastTxn{
DeployAccountTxn{

Type: TransactionType_DeployAccount,
Version: TransactionV1,
MaxFee: utils.TestHexToFelt(t, "0xdec823b1380c"),
Nonce: utils.TestHexToFelt(t, "0x0"),
Signature: []*felt.Felt{
utils.TestHexToFelt(t, "0x41dbc4b41f6506502a09eb7aea85759de02e91f49d0565776125946e54a2ec6"),
utils.TestHexToFelt(t, "0x85dcf2bc8e3543071a6657947cc9c157a9f6ad7844a686a975b588199634a9"),
},
ContractAddressSalt: utils.TestHexToFelt(t, "0x74ddc51af144d1bd805eb4184d07453d7c4388660270a7851fec387e654a50e"),
ClassHash: utils.TestHexToFelt(t, "0x25ec026985a3bf9d0cc1fe17326b245dfdc3ff89b8fde106542a3ea56c5a918"),
ConstructorCalldata: utils.TestHexArrToFelt(t, []string{
"0x33434ad846cdd5f23eb73ff09fe6fddd568284a0fb7d1be20ee482f044dabe2",
"0x79dc0da7c54b95f10aa182ad0a46400db63156920adb65eca2654c0945a463",
"0x2",
"0x74ddc51af144d1bd805eb4184d07453d7c4388660270a7851fec387e654a50e",
"0x0",
}),
},
},
simFlags: []SimulationFlag{},
blockID: BlockID{Hash: utils.TestHexToFelt(t, "0x1b0df1bafcb826b1fc053495aef5cdc24d0345cbfa1259b15939d01b89dc6d9")},
expectedError: nil,
expectedResp: []FeeEstimate{
{
GasConsumed: utils.TestHexToFelt(t, "0x15be"),
GasPrice: utils.TestHexToFelt(t, "0x378f962c4"),
OverallFee: utils.TestHexToFelt(t, "0x4b803e316178"),
FeeUnit: UnitWei,
},
},
},
},
"mock": {},
"testnet": {},
}[testEnv]

for _, test := range testSet {
spy := NewSpy(testConfig.provider.c)
testConfig.provider.c = spy
resp, err := testConfig.provider.EstimateFee(context.Background(), test.txs, test.simFlags, test.blockID)
require.Equal(t, test.expectedError, err)
require.Equal(t, test.expectedResp, resp)
}
}
2 changes: 1 addition & 1 deletion rpc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type RpcProvider interface {
Class(ctx context.Context, blockID BlockID, classHash *felt.Felt) (ClassOutput, error)
ClassAt(ctx context.Context, blockID BlockID, contractAddress *felt.Felt) (ClassOutput, error)
ClassHashAt(ctx context.Context, blockID BlockID, contractAddress *felt.Felt) (*felt.Felt, error)
EstimateFee(ctx context.Context, requests []BroadcastTxn, simulationFlag SimulationFlag, blockID BlockID) ([]FeeEstimate, error)
EstimateFee(ctx context.Context, requests []BroadcastTxn, simulationFlags []SimulationFlag, blockID BlockID) ([]FeeEstimate, error)
EstimateMessageFee(ctx context.Context, msg MsgFromL1, blockID BlockID) (*FeeEstimate, error)
Events(ctx context.Context, input EventsInput) (*EventChunk, error)
GetTransactionStatus(ctx context.Context, transactionHash *felt.Felt) (*TxnStatusResp, error)
Expand Down
2 changes: 1 addition & 1 deletion rpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ type FeeEstimate struct {
OverallFee *felt.Felt `json:"overall_fee"`

// Units in which the fee is given
FeeUnit FeePaymentUnit `json:"fee_unit"`
FeeUnit FeePaymentUnit `json:"unit"`
}

type TxnExecutionStatus string
Expand Down

0 comments on commit 915109a

Please sign in to comment.