Skip to content

Commit

Permalink
wup
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Nov 30, 2023
1 parent 952fb72 commit 18d5a64
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 43 deletions.
4 changes: 2 additions & 2 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (account *Account) SignDeployAccountTransactionv3(ctx context.Context, tx r
if err != nil {
return nil, err
}
fmt.Println("=-=-")
fmt.Println("hash:", hash)
return account.Sign(ctx, hash)
// if err != nil {
// return err
Expand Down Expand Up @@ -236,7 +236,7 @@ func (account *Account) TransactionHashDeployAccountV3(txn rpc.DeployAccountTxnV
if err != nil {
return nil, err
}
// https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/transactions/#deploy_account_hash_calculation

return crypto.PoseidonArray(
PREFIX_DEPLOY_ACCOUNT,
txnVersionFelt,
Expand Down
16 changes: 8 additions & 8 deletions examples/deployAccountV3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import (
)

var (
network string = "integration"
predeployedClassHash = "0x01a736d6ed154502257f02b1ccdf4d9d1089f80811cd6acad48e6b6a9d1f2003"
network string = "integration"
// https://external.integration.starknet.io/feeder_gateway/get_transaction?transactionHash=0x29fd7881f14380842414cdfdd8d6c0b1f2174f8916edcfeb1ede1eb26ac3ef0
predeployedClassHash = "0x2338634f11772ea342365abd5be9d9dc8a6f44f159ad782fdebd3db5d969738"
accountAddress string = "0x043784df59268c02b716e20bf77797bd96c68c2f100b2a634e448c35e3ad363e"
pubKey string = "0x049f060d2dffd3bf6f2c103b710baf519530df44529045f92c3903097e8d861f"
privKey string = "0x043b7fe9d91942c98cd5fd37579bd99ec74f879c4c79d886633eecae9dad35fa"
Expand Down Expand Up @@ -55,7 +56,7 @@ func main() {
}

// https://goerli.voyager.online/tx/0x559d0c57b7651b7f8e1c25cc92ff2d0567bcecb62a36c62d7a3f3fb9319e140
guardian, _ := new(felt.Felt).SetString("0x0")
// guardian, _ := new(felt.Felt).SetString("0x0")

// Create transaction data
tx := rpc.DeployAccountTxnV3{
Expand All @@ -66,19 +67,18 @@ func main() {
ClassHash: classHash,
ContractAddressSalt: PubKey,
ConstructorCalldata: []*felt.Felt{
PubKey,
guardian},
PubKey},
ResourceBounds: rpc.ResourceBoundsMapping{
L1Gas: rpc.ResourceBounds{
MaxAmount: new(felt.Felt).SetUint64(12345678912345123123),
MaxPricePerUnit: new(felt.Felt).SetUint64(12345678912345123123),
MaxAmount: new(felt.Felt).SetUint64(4328000220728),
MaxPricePerUnit: new(felt.Felt).SetUint64(4328000220728),
},
L2Gas: rpc.ResourceBounds{
MaxAmount: new(felt.Felt).SetUint64(0),
MaxPricePerUnit: new(felt.Felt).SetUint64(0),
},
},
Tip: new(felt.Felt).SetUint64(12345678912345),
Tip: new(felt.Felt).SetUint64(0),
PayMasterData: []*felt.Felt{},
NonceDataMode: rpc.DAModeL1,
FeeMode: rpc.DAModeL1,
Expand Down
12 changes: 7 additions & 5 deletions examples/deployAccountqweV1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ var (
// deployV1 Worked on Goerli.
// Trying deployV1 on integration. 0x35ddd3a7141fc476d8fbe0f769545b74a682eb3dc2c73cfbd72de25ce16412c
// Then try V3 on integration.
predeployedClassHash = "0x01a736d6ed154502257f02b1ccdf4d9d1089f80811cd6acad48e6b6a9d1f2003" //"0x2794ce20e5f2ff0d40e632cb53845b9f4e526ebd8471983f7dbd355b721d5a"
// predeployedClassHash = "0x01a736d6ed154502257f02b1ccdf4d9d1089f80811cd6acad48e6b6a9d1f2003" //"0x2794ce20e5f2ff0d40e632cb53845b9f4e526ebd8471983f7dbd355b721d5a"
// FROM successful v3 tx on integration. V1 works
predeployedClassHash = "0x2338634f11772ea342365abd5be9d9dc8a6f44f159ad782fdebd3db5d969738" // -> addr 0x39180766dd93d979cd03e6ba93d3210fb7b7e56e01020942157e387a3613040
)

func main() {
Expand All @@ -36,6 +38,7 @@ func main() {
clientv02 := rpc.NewProvider(c)

// Get random keys for test purposes
// Account address should be 0x39180766dd93d979cd03e6ba93d3210fb7b7e56e01020942157e387a3613040
AccountAddress, _ := utils.HexToFelt("0x043784df59268c02b716e20bf77797bd96c68c2f100b2a634e448c35e3ad363e")

ks := account.NewMemKeystore()
Expand All @@ -60,19 +63,18 @@ func main() {

// https://goerli.voyager.online/tx/0x559d0c57b7651b7f8e1c25cc92ff2d0567bcecb62a36c62d7a3f3fb9319e140

guardian, _ := new(felt.Felt).SetString("0x0")
// guardian, _ := new(felt.Felt).SetString("0x0")
// Create transaction data
tx := rpc.DeployAccountTxn{
Nonce: &felt.Zero, // Contract accounts start with nonce zero.
MaxFee: new(felt.Felt).SetUint64(12345678912345),
Nonce: new(felt.Felt).SetUint64(0),
MaxFee: new(felt.Felt).SetUint64(4328000220728), // for second contrat (success one)
Type: rpc.TransactionType_DeployAccount,
Version: rpc.TransactionV1,
Signature: []*felt.Felt{},
ClassHash: classHash,
ContractAddressSalt: PubKey,
ConstructorCalldata: []*felt.Felt{
PubKey,
guardian,
},
}
fmt.Println("tx", tx.ClassHash)
Expand Down
8 changes: 8 additions & 0 deletions examples/getBalance_STRK/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Note: To run this example, you need a mainnet endpoint.

Steps to run this example on mainnet:
1. rename ".env.template" to ".env.mainnet"
2. uncomment, and set INTEGRATION_BASE to the mainnet url
3. make sure you are in the "simpleCall" directory
4. execute `go mod tidy`
5. execute `go run main.go`
35 changes: 35 additions & 0 deletions examples/getBalance_STRK/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module account

go 1.21

require (
github.com/NethermindEth/starknet.go v0.2.1-0.20220620163912-1db2ca279608
github.com/ethereum/go-ethereum v1.10.26
github.com/joho/godotenv v1.4.0
)

replace github.com/NethermindEth/starknet.go => ../../

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/gnark-crypto v0.11.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/fxamacker/cbor/v2 v2.4.0 // indirect
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/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
github.com/test-go/testify v1.1.4 // indirect
github.com/tklauser/go-sysconf v0.3.5 // indirect
github.com/tklauser/numcpus v0.2.2 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/crypto v0.2.0 // indirect
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
)
File renamed without changes.
79 changes: 79 additions & 0 deletions examples/getBalance_STRK/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"context"
"fmt"
"math/big"
"os"

"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/NethermindEth/starknet.go/utils"
ethrpc "github.com/ethereum/go-ethereum/rpc"
"github.com/joho/godotenv"
)

var (
name string = "integration"
someMainnetContract string = "0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d" // STRK
contractMethod string = "balanceOf"
)

// main entry point of the program.
//
// It initializes the environment and establishes a connection with the client.
// It then makes a contract call and prints the response.
//
// Parameters:
//
// none
//
// Returns:
//
// none
func main() {
fmt.Println("Starting simpeCall example")
godotenv.Load(fmt.Sprintf(".env.%s", name))
base := os.Getenv("INTEGRATION_BASE")
c, err := ethrpc.DialContext(context.Background(), base)
if err != nil {
fmt.Println("Failed to connect to the client, did you specify the url in the .env.mainnet?")
panic(err)
}
clientv02 := rpc.NewProvider(c)
fmt.Println("Established connection with the client")

contractAddress, err := utils.HexToFelt(someMainnetContract)
if err != nil {
panic(err)
}

// 0x043784df59268c02b716e20bf77797bd96c68c2f100b2a634e448c35e3ad363e
// 0x39180766dd93d979cd03e6ba93d3210fb7b7e56e01020942157e387a3613040 // Takes a good 10min to bridge..
// 0xffbc2e41bad21b80eea2590c38287718aced9db4c66ae693b5ed24320f6de7
// 0xc6e9dddcab4b464f2edc1cb4ef14163317a58ae64fd24ffe280baaf8eb5691

// 0x13d46bc231a47e29ee1e3c7fab338e6b13c935e002f2be57f7318e88cbb872e
// 0x397ffc31dde066cab3f4a9f6315f5641620b2ee0622c042b91ade554ca4bff5
// 0x1fee7cfefa90a2878826212bb46bccddfb2473f306b7b17e4791bd0a486d4c6
//
myAddr, _ := new(felt.Felt).SetString("0x043784df59268c02b716e20bf77797bd96c68c2f100b2a634e448c35e3ad363e")

// Make read contract call
tx := rpc.FunctionCall{
ContractAddress: contractAddress,
EntryPointSelector: utils.GetSelectorFromNameFelt(contractMethod),
Calldata: []*felt.Felt{myAddr},
}

fmt.Println("Making Call() request", myAddr)
callResp, err := clientv02.Call(context.Background(), tx, rpc.BlockID{Tag: "latest"})
if err != nil {
fmt.Println("+--=-")
panic(err.Error())
}
qwe := new(big.Int)
callResp[0].BigInt(qwe)
fmt.Println(qwe)
fmt.Println(fmt.Sprintf("Response to %s():%s ", contractMethod, callResp[0]))
}
2 changes: 1 addition & 1 deletion examples/simpleCall/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func main() {
// 0x397ffc31dde066cab3f4a9f6315f5641620b2ee0622c042b91ade554ca4bff5
// 0x1fee7cfefa90a2878826212bb46bccddfb2473f306b7b17e4791bd0a486d4c6
//
myAddr, _ := new(felt.Felt).SetString("0x6764832432bbe2e35a45bf7d5771e4551fcf760fa803d30afd3ec55140c57a7")
myAddr, _ := new(felt.Felt).SetString("0x39180766dd93d979cd03e6ba93d3210fb7b7e56e01020942157e387a3613040")

// Make read contract call
tx := rpc.FunctionCall{
Expand Down
2 changes: 0 additions & 2 deletions examples/simpleInvoke/.env.template

This file was deleted.

File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions examples/transfer/go.work
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
go 1.21

use (
.
../..
)
58 changes: 33 additions & 25 deletions examples/simpleInvoke/main.go → examples/transfer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ import (
"github.com/joho/godotenv"
)

// NOTE : Please add in your keys only for testing purposes, in case of a leak you would potentially lose your funds.
// Success invoke-v1 transfer on goerli 0x4f787b35642e3063630672c2cde21af86361b4ac505c95aff6d0d0da62833a6
// Failing to trasnfer on integration..
var (
name string = "testnet" //env."name"
account_addr string = "0x043784df59268c02b716e20bf77797bd96c68c2f100b2a634e448c35e3ad363e" //Replace it with your account address
privateKey string = "0x043b7fe9d91942c98cd5fd37579bd99ec74f879c4c79d886633eecae9dad35fa" //Replace it with your account private key
public_key string = "0x049f060d2dffd3bf6f2c103b710baf519530df44529045f92c3903097e8d861f" //Replace it with your account public key
someContract string = "0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf" // UDC
contractMethod string = "deployContract" //Replace it with the function name that you want to invoke
name string = "integration" //env."name"
// goerli 0x043784df59268c02b716e20bf77797bd96c68c2f100b2a634e448c35e3ad363e
// integration 0x39180766dd93d979cd03e6ba93d3210fb7b7e56e01020942157e387a3613040
account_addr string = "0x39180766dd93d979cd03e6ba93d3210fb7b7e56e01020942157e387a3613040" //Replace it with your account address
privateKey string = "0x043b7fe9d91942c98cd5fd37579bd99ec74f879c4c79d886633eecae9dad35fa" //Replace it with your account private key
public_key string = "0x049f060d2dffd3bf6f2c103b710baf519530df44529045f92c3903097e8d861f" //Replace it with your account public key
// ETH 0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7
// STRK 0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d
tokenAddress string = "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"
CairoContractVersion = 0
contractMethod string = "transfer"
reciepient string = "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7" //Replace it with the function name that you want to invoke
)

func main() {
Expand Down Expand Up @@ -69,42 +76,43 @@ func main() {
}

// Getting the nonce from the account
// nonce, err := accnt.Nonce(context.Background(), rpc.BlockID{Tag: "latest"}, accnt.AccountAddress)
// if err != nil {
// panic(err.Error())
// }
nonce, err := accnt.Nonce(context.Background(), rpc.BlockID{Tag: "latest"}, accnt.AccountAddress)
if err != nil {
panic(err.Error())
}

// Building the InvokeTx struct
InvokeTx := rpc.InvokeTxnV1{
MaxFee: maxfee,
Version: rpc.TransactionV1,
Nonce: new(felt.Felt).SetUint64(0),
Nonce: nonce,
Type: rpc.TransactionType_Invoke,
SenderAddress: accnt.AccountAddress,
}
fmt.Println("=====")
// Converting the contractAddress from hex to felt
contractAddress, err := utils.HexToFelt(someContract)
contractAddress, err := utils.HexToFelt(tokenAddress)
if err != nil {
panic(err.Error())
}

// recipient, _ := utils.HexToFelt("0x39180766dd93d979cd03e6ba93d3210fb7b7e56e01020942157e387a3613040")
// amount, _ := utils.HexToFelt("0x2a303fe4b530000")
classHash, _ := utils.HexToFelt("0x2a303fe4b530000")
unique, _ := utils.HexToFelt("0x2a303fe4b530000")
salt := new(felt.Felt).SetUint64(0)
callata, _ := utils.HexToFelt(account_addr)
// classHash, _ := utils.HexToFelt("0x2a303fe4b530000")
// unique, _ := utils.HexToFelt("0x2a303fe4b530000")
// salt := new(felt.Felt).SetUint64(0)
// callata, _ := utils.HexToFelt(account_addr)

reciepientFelt, _ := new(felt.Felt).SetString(reciepient)

// 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{recipient, amount},
Calldata: []*felt.Felt{reciepientFelt, new(felt.Felt).SetUint64(1)},
}
fmt.Println("=====")
// Mentioning the contract version
CairoContractVersion := 0

// Building the Calldata with the help of FmtCalldata where we pass in the FnCall struct along with the Cairo version
InvokeTx.Calldata, err = accnt.FmtCalldata([]rpc.FunctionCall{FnCall}, CairoContractVersion)
Expand All @@ -122,12 +130,12 @@ func main() {
fmt.Println(string(qwe))

// 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())
// }
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)
// This returns us with the transaction hash
fmt.Println("Transaction hash response : ", resp.TransactionHash)

}

0 comments on commit 18d5a64

Please sign in to comment.