Skip to content
This repository has been archived by the owner on Aug 31, 2024. It is now read-only.

[WIP] add invoke function #18

Merged
merged 16 commits into from
Jan 31, 2024
4 changes: 4 additions & 0 deletions cairoVM/cairo.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,15 @@ func (c *Cairo) HandleDeclareTx(tx *core.DeclareTransaction, class core.Class) (
}

func (c *Cairo) HandleInvokeTx(tx *core.InvokeTransaction) (*vm.TransactionTrace, error) {
fmt.Println("---------- Invoke TX ----------")
tx.MaxFee = c.MaxFee
tx.SenderAddress = &felt.Zero
txnHash, err := core.TransactionHash(tx, c.cfg.Network)
if err != nil {
return nil, err
}
tx.TransactionHash = txnHash

sig, err := c.acc.Sign(context.Background(), txnHash)
if err != nil {
return nil, err
Expand Down
14 changes: 13 additions & 1 deletion cairoVM/cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
"github.com/NethermindEth/juno/core/felt"
"github.com/davecgh/go-spew/spew"
"testbot/cairoVM"

"github.com/NethermindEth/juno/core/felt"
// "github.com/NethermindEth/juno/core"
)

Expand Down Expand Up @@ -35,4 +36,15 @@ func main() {
}
spew.Dump(trace)

invokeTx, err := cairoVM.NewInvoke()
if err != nil {
panic(err)
}

trace, err = vm.HandleInvokeTx(invokeTx) // Assuming there is a HandleInvokeTx function
if err != nil {
panic(err)
}
spew.Dump(trace)

}
50 changes: 50 additions & 0 deletions cairoVM/invoke.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cairoVM

import (
"fmt"
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/starknet.go/account"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/NethermindEth/starknet.go/utils"
)

var (
contractMethod string = "set_value"
)

func NewInvoke() (*core.InvokeTransaction, error) {
//InvokeTx := rpc.InvokeTxnV1{
// Version: rpc.TransactionV1,
// Type: rpc.TransactionType_Invoke,
//}

// Converting the contractAddress from hex to felt
contractAddress := new(felt.Felt).SetUint64(2)

params := new(felt.Felt).SetUint64(111)
// Building the functionCall struct, where :
FnCall := rpc.FunctionCall{
ContractAddress: contractAddress, //contractAddress is the contract that we want to call
EntryPointSelector: utils.GetSelectorFromNameFelt(contractMethod), //this is the function that we want to call
Calldata: []*felt.Felt{params}, //this is the data that we want to pass to the function
}

txCallData := account.FmtCallDataCairo2([]rpc.FunctionCall{FnCall})

fmt.Println("invoke calldata = ", txCallData)

nonce := new(felt.Felt).SetUint64(1)
tx := core.InvokeTransaction{
Nonce: nonce,
MaxFee: &felt.Zero,
Version: new(core.TransactionVersion).SetUint64(1),
ContractAddress: contractAddress,
EntryPointSelector: utils.GetSelectorFromNameFelt(contractMethod),
CallData: txCallData,
// CallData: []*felt.Felt{randata},
}

return &tx, nil

}
Loading
Loading