Skip to content

Commit

Permalink
Added AddDeployAcnt Mock and Test
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Jul 27, 2023
1 parent f58ad09 commit 666a958
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
3 changes: 3 additions & 0 deletions rpcv02/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rpcv02
import (
"context"
"encoding/json"
"fmt"
)

type callCloser interface {
Expand All @@ -12,7 +13,9 @@ type callCloser interface {

func do(ctx context.Context, call callCloser, method string, data interface{}, args ...interface{}) error {
var raw json.RawMessage
fmt.Println("-- here pre call context")
err := call.CallContext(ctx, &raw, method, args...)
fmt.Println("-- here post call context")
if err != nil {
return err
}
Expand Down
26 changes: 26 additions & 0 deletions rpcv02/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func (r *rpcMock) CallContext(ctx context.Context, result interface{}, method st
return mock_starknet_call(result, method, args...)
case "starknet_addDeclareTransaction":
return mock_starknet_addDeclareTransaction(result, method, args...)
case "starknet_addDeployAccountTransaction":
return mock_starknet_addDeployAccountTransaction(result, method, args...)
case "starknet_addInvokeTransaction":
return mock_starknet_addInvokeTransaction(result, method, args...)
case "starknet_estimateFee":
Expand Down Expand Up @@ -397,6 +399,30 @@ func mock_starknet_addDeclareTransaction(result interface{}, method string, args
return nil
}

func mock_starknet_addDeployAccountTransaction(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok {
return errWrongType
}
_, ok = args[0].(BroadcastedDeployAccountTransaction)
if !ok {
fmt.Printf("args[0] should be BroadcastedDeployAccountTransaction, got %T\n", args[0])
return errWrongArgs
}

deadbeefFelt, err := utils.HexToFelt("0xdeadbeef")
if err != nil {
return err
}
output := AddDeployAccountTransactionResponse{
TransactionHash: deadbeefFelt,
ContractAddress: deadbeefFelt,
}
outputContent, _ := json.Marshal(output)
json.Unmarshal(outputContent, r)
return nil
}

func mock_starknet_estimateFee(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok {
Expand Down
6 changes: 6 additions & 0 deletions rpcv02/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ type AddDeployTransactionOutput struct {
ContractAddress *felt.Felt `json:"contract_address"`
}

// AddDeployAccountTransactionOutput provides the output for AddDeployTransaction.
type AddDeployAccountTransactionResponse struct {
TransactionHash *felt.Felt `json:"transaction_hash"`
ContractAddress *felt.Felt `json:"contract_address"`
}

// FunctionCall function call information
type FunctionCall struct {
ContractAddress *felt.Felt `json:"contract_address"`
Expand Down
65 changes: 65 additions & 0 deletions rpcv02/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rpcv02
import (
"context"
"encoding/json"
"fmt"
"os"
"testing"

Expand Down Expand Up @@ -59,3 +60,67 @@ func TestDeclareTransaction(t *testing.T) {

}
}

// TestDeclareTransaction tests starknet_addDeclareTransaction
func TestDeployAccountTransaction(t *testing.T) {

testConfig := beforeEach(t)

type testSetType struct {
TransactionHash *felt.Felt
ContractAddress *felt.Felt
ExpectedError string
}
testSet := map[string][]testSetType{
"devnet": {},
"mainnet": {},
"mock": {{
TransactionHash: utils.TestHexToFelt(t, "0xdeadbeef"),
ContractAddress: utils.TestHexToFelt(t, "0xdeadbeef"),
ExpectedError: "",
}},
"testnet": {{
TransactionHash: utils.TestHexToFelt(t, "0x55b094dc5c84c2042e067824f82da90988674314d37e45cb0032aca33d6e0b9"),
ContractAddress: utils.TestHexToFelt(t, "0xdeadbeef"),
ExpectedError: "Invalid Params",
}},
}[testEnv]

for _, test := range testSet {
pub := new(felt.Felt).SetUint64(123)
predeployedClassHash, err := utils.HexToFelt("0x2794ce20e5f2ff0d40e632cb53845b9f4e526ebd8471983f7dbd355b721d5a")
require.Nil(t, err, "felt error")

// Should fail since not funded
deployAcntTx := BroadcastedDeployAccountTransaction{
BroadcastedTxnCommonProperties: BroadcastedTxnCommonProperties{
Nonce: &felt.Zero, // Contract accounts start with nonce zero.
MaxFee: new(felt.Felt).SetUint64(1),
Type: TransactionType_DeployAccount,
Version: TransactionV1, // ???
Signature: []*felt.Felt{&felt.Zero, &felt.Zero}, // ???
},
ClassHash: predeployedClassHash,
ContractAddressSalt: pub,
ConstructorCalldata: []*felt.Felt{pub},
}

spy := NewSpy(testConfig.provider.c)
testConfig.provider.c = spy

fmt.Println("Precall")
resp, err := testConfig.provider.AddDeployAccountTransaction(context.Background(), deployAcntTx)
fmt.Println("Postcall")
if err != nil {
require.Equal(t, err.Error(), test.ExpectedError)
continue
}
if resp.TransactionHash.String() != test.TransactionHash.String() {
t.Fatalf("TransactionHash does not match expected, current: %s", resp.TransactionHash) // todo fix
}

if resp.ContractAddress.String() != test.ContractAddress.String() {
t.Fatalf("ContractAddress does not match expected, current: %s", resp.ContractAddress) // todo fix
}
}
}

0 comments on commit 666a958

Please sign in to comment.