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

Merge changes in Rpcv04 branch #338

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 17 additions & 0 deletions rpc/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,20 @@ func (provider *Provider) EstimateFee(ctx context.Context, requests []Broadcaste
}
return raw, nil
}

// EstimateMessageFee estimates the L2 fee of a message sent on L1
func (provider *Provider) EstimateMessageFee(ctx context.Context, msg MsgFromL1, blockID BlockID) (*FeeEstimate, error) {
var raw FeeEstimate
if err := do(ctx, provider.c, "starknet_estimateMessageFee", &raw, msg, blockID); err != nil {
switch {
case errors.Is(err, ErrContractNotFound):
return nil, ErrContractNotFound
case errors.Is(err, ErrContractError):
return nil, ErrContractError
case errors.Is(err, ErrBlockNotFound):
return nil, ErrBlockNotFound
}
return nil, err
}
return &raw, nil
}
37 changes: 37 additions & 0 deletions rpc/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,40 @@ func TestNonce(t *testing.T) {
}
}
}

// TestEstimateMessageFee tests EstimateMesssageFee
func TestEstimateMessageFee(t *testing.T) {
testConfig := beforeEach(t)

type testSetType struct {
MsgFromL1
BlockID
ExpectedFeeEst FeeEstimate
}
testSet := map[string][]testSetType{
"mock": {
{
MsgFromL1: MsgFromL1{FromAddress: "0x0", ToAddress: &felt.Zero, Selector: &felt.Zero, Payload: []*felt.Felt{&felt.Zero}},
BlockID: BlockID{Tag: "latest"},
ExpectedFeeEst: FeeEstimate{
GasConsumed: NumAsHex("0x1"),
GasPrice: NumAsHex("0x2"),
OverallFee: NumAsHex("0x3"),
},
},
},
"testnet": {},
"mainnet": {},
}[testEnv]

for _, test := range testSet {
spy := NewSpy(testConfig.provider.c)
testConfig.provider.c = spy
value, err := testConfig.provider.EstimateMessageFee(context.Background(), test.MsgFromL1, test.BlockID)
if err != nil {
t.Fatal(err)
}
require.Equal(t, *value, test.ExpectedFeeEst)

}
}
32 changes: 32 additions & 0 deletions rpc/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func (r *rpcMock) CallContext(ctx context.Context, result interface{}, method st
return mock_starknet_addInvokeTransaction(result, method, args...)
case "starknet_estimateFee":
return mock_starknet_estimateFee(result, method, args...)
case "starknet_estimateMessageFee":
return mock_starknet_estimateMessageFee(result, method, args...)
case "starknet_getBlockWithTxHashes":
return mock_starknet_getBlockWithTxHashes(result, method, args...)
case "starknet_traceBlockTransactions":
Expand Down Expand Up @@ -445,6 +447,36 @@ func mock_starknet_estimateFee(result interface{}, method string, args ...interf
return nil
}

func mock_starknet_estimateMessageFee(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok {
return errWrongType
}
if len(args) != 2 {
fmt.Printf("args: %d\n", len(args))
return errWrongArgs
}
_, ok = args[0].(MsgFromL1)
if !ok {
fmt.Printf("args[0] should be MsgFromL1, got %T\n", args[0])
return errWrongArgs
}
_, ok = args[1].(BlockID)
if !ok {
fmt.Printf("args[1] should be *blockID, got %T\n", args[1])
return errWrongArgs
}

output := FeeEstimate{
GasConsumed: NumAsHex("0x1"),
GasPrice: NumAsHex("0x2"),
OverallFee: NumAsHex("0x3"),
}
outputContent, _ := json.Marshal(output)
json.Unmarshal(outputContent, r)
return nil
}

func mock_starknet_addInvokeTransaction(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok {
Expand Down
1 change: 1 addition & 0 deletions rpc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type api interface {
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 []BroadcastedTransaction, blockID BlockID) ([]FeeEstimate, error)
EstimateMessageFee(ctx context.Context, msg MsgFromL1, blockID BlockID) (*FeeEstimate, error)
Events(ctx context.Context, input EventsInput) (*EventChunk, error)
Nonce(ctx context.Context, blockID BlockID, contractAddress *felt.Felt) (*string, error)
StateUpdate(ctx context.Context, blockID BlockID) (*StateUpdateOutput, error)
Expand Down
11 changes: 11 additions & 0 deletions rpc/types_transaction_receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ type MsgToL1 struct {
Payload []*felt.Felt `json:"payload"`
}

type MsgFromL1 struct {
// FromAddress The address of the L1 contract sending the message
FromAddress string `json:"from_address"`
// ToAddress The target L2 address the message is sent to
ToAddress *felt.Felt `json:"to_address"`
// EntryPointSelector The selector of the l1_handler in invoke in the target contract
Selector *felt.Felt `json:"entry_point_selector"`
//Payload The payload of the message
Payload []*felt.Felt `json:"payload"`
}

type UnknownTransactionReceipt struct{ TransactionReceipt }

func (tr *UnknownTransactionReceipt) UnmarshalJSON(data []byte) error {
Expand Down