From 553fc625fc374b5397dc1e51d085970f08dcba6f Mon Sep 17 00:00:00 2001 From: rianhughes Date: Tue, 16 Jan 2024 17:54:26 +0200 Subject: [PATCH] example to send two transactions in the same block --- examples/simpleInvoke/go.mod | 5 ++- examples/simpleInvoke/main.go | 65 +++++++++++++++++++++++++---------- 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/examples/simpleInvoke/go.mod b/examples/simpleInvoke/go.mod index e5089ee9..45dd7071 100644 --- a/examples/simpleInvoke/go.mod +++ b/examples/simpleInvoke/go.mod @@ -3,7 +3,7 @@ module account go 1.21 require ( - github.com/NethermindEth/starknet.go v0.4.6-0.20231005024141-742a82479868 + github.com/NethermindEth/starknet.go v0.6.0 github.com/ethereum/go-ethereum v1.10.26 github.com/joho/godotenv v1.4.0 ) @@ -12,6 +12,7 @@ require ( github.com/NethermindEth/juno v0.3.1 // indirect github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect github.com/bits-and-blooms/bitset v1.7.0 // indirect + github.com/consensys/bavard v0.1.13 // indirect github.com/consensys/gnark-crypto v0.11.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/deckarep/golang-set v1.8.0 // indirect @@ -19,6 +20,7 @@ require ( github.com/go-ole/go-ole v1.2.1 // indirect github.com/go-stack/stack v1.8.0 // indirect github.com/gorilla/websocket v1.4.2 // indirect + github.com/mmcloughlin/addchain v0.4.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect github.com/stretchr/testify v1.8.1 // indirect @@ -30,4 +32,5 @@ require ( golang.org/x/sys v0.3.0 // indirect gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect gopkg.in/yaml.v3 v3.0.1 // indirect + rsc.io/tmplfunc v0.0.3 // indirect ) diff --git a/examples/simpleInvoke/main.go b/examples/simpleInvoke/main.go index d74b0b72..ba83420f 100644 --- a/examples/simpleInvoke/main.go +++ b/examples/simpleInvoke/main.go @@ -2,10 +2,12 @@ package main import ( "context" + "encoding/json" "fmt" "math/big" "os" + "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/starknet.go/account" "github.com/NethermindEth/starknet.go/rpc" "github.com/NethermindEth/starknet.go/utils" @@ -16,10 +18,10 @@ import ( // NOTE : Please add in your keys only for testing purposes, in case of a leak you would potentially lose your funds. var ( name string = "testnet" //env."name" - account_addr string = "0x06f36e8a0fc06518125bbb1c63553e8a7d8597d437f9d56d891b8c7d3c977716" //Replace it with your account address + account_addr string = "0x04c34f000a86f5e5fbfececdcae233209fa032fdd8e84e6dac7ce7ef4d858c73" //Replace it with your account address account_cairo_version = 0 //Replace with the cairo version of your account - privateKey string = "0x0687bf84896ee63f52d69e6de1b41492abeadc0dc3cb7bd351d0a52116915937" //Replace it with your account private key - public_key string = "0x58b0824ee8480133cad03533c8930eda6888b3c5170db2f6e4f51b519141963" //Replace it with your account public key + privateKey string = "0x05e6c9dbf900e2106c2a1977f632dfbaa0354983e98d3401c4817627a025b2e8" //Replace it with your account private key + public_key string = "0x5b6a747863f1efa0047c5917b20ca6d802ca55db775780114fb0965ea2035b9" //Replace it with your account public key someContract string = "0x4c1337d55351eac9a0b74f3b8f0d3928e2bb781e5084686a892e66d49d510d" //Replace it with the contract that you want to invoke contractMethod string = "increase_value" //Replace it with the function name that you want to invoke ) @@ -52,15 +54,8 @@ func main() { panic(err.Error()) } ks.Put(public_key, fakePrivKeyBI) - fmt.Println("Established connection with the client") - // Here we are setting the maxFee - maxfee, err := utils.HexToFelt("0x9184e72a000") - if err != nil { - panic(err.Error()) - } - // Initializing the account accnt, err := account.NewAccount(clientv02, account_address, public_key, ks, account_cairo_version) if err != nil { @@ -72,7 +67,44 @@ func main() { if err != nil { panic(err.Error()) } + fmt.Println("Nonce : ", nonce) + + //// Transaction 1 (eg 0x468000b61988138f75e4ffb4b21c5553c681d512fb14d12837b737f19de84bf) + nonceTxn1 := new(felt.Felt).Add(nonce, new(felt.Felt).SetUint64(0)) + fmt.Println("nonceTxn1 : ", nonceTxn1) + + txn1, err := prepareTx(accnt, clientv02, account_address, ks, nonceTxn1) + if err != nil { + panic(err.Error()) + } + + txn1Hash, _ := accnt.TransactionHashInvoke(txn1) + fmt.Println("txn1Hash : ", txn1Hash) + + resp1, err := accnt.AddInvokeTransaction(context.Background(), txn1) + fmt.Println("resp1, err", resp1, err) + + //// Transaction 2 (eg 0x7bfd7e6870ee114097285f2ea5d9797a0fe3b168c36d5417b68d41218c4b5a6) + nonceTxn2 := new(felt.Felt).Add(nonce, new(felt.Felt).SetUint64(1)) + fmt.Println("nonceTxn2 : ", nonceTxn2) + txn2, err2 := prepareTx(accnt, clientv02, account_address, ks, nonceTxn2) + if err2 != nil { + panic(err.Error()) + } + txn2Hash, _ := accnt.TransactionHashInvoke(txn2) + fmt.Println("txn2Hash : ", txn2Hash) + resp2, err := accnt.AddInvokeTransaction(context.Background(), txn2) + fmt.Println("resp2, err", resp2, err) + +} + +func prepareTx(accnt *account.Account, clientv02 *rpc.Provider, account_address *felt.Felt, ks account.Keystore, nonce *felt.Felt) (rpc.InvokeTxnV1, error) { + // Here we are setting the maxFee + maxfee, err := utils.HexToFelt("0x9184e72a101") + if err != nil { + panic(err.Error()) + } // Building the InvokeTx struct InvokeTx := rpc.InvokeTxnV1{ MaxFee: maxfee, @@ -105,13 +137,10 @@ func main() { if err != nil { panic(err.Error()) } + return InvokeTx, nil +} - // After the signing we finally call the AddInvokeTransaction in order to invoke the contract function - resp, err := accnt.AddInvokeTransaction(context.Background(), InvokeTx) - if err != nil { - panic(err.Error()) - } - // This returns us with the transaction hash - fmt.Println("Transaction hash response : ", resp.TransactionHash) - +func printTxn(txn any) { + qwe, _ := json.MarshalIndent(txn, "", "") + fmt.Println(string(qwe)) }