forked from NethermindEth/starknet.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
101 lines (86 loc) · 3.25 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"context"
"fmt"
"log"
"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"
"github.com/joho/godotenv"
)
var (
network string = "testnet"
predeployedClassHash = "0x2794ce20e5f2ff0d40e632cb53845b9f4e526ebd8471983f7dbd355b721d5a"
accountAddress = "0xdeadbeef"
accountContractVersion = 0 //Replace with the cairo version of your account contract
)
// main initializes the client, sets up the account, deploys a contract, and sends a transaction to the network.
//
// It loads environment variables, dials the Ethereum RPC, creates a new account, casts the account address to a felt type,
// sets up the account using the client, converts the predeployed class hash to a felt type, creates transaction data,
// precomputes an address, prompts the user to add funds to the precomputed address, signs the transaction,
// and finally sends the transaction to the network.
//
// Parameters:
//
// none
//
// Returns:
//
// none
func main() {
// Initialise the client.
godotenv.Load(fmt.Sprintf(".env.%s", network))
url := os.Getenv("INTEGRATION_BASE")
clientv02, err := rpc.NewProvider(url)
if err != nil {
log.Fatal(fmt.Sprintf("Error dialing the RPC provider: %s", err))
}
// Get random keys for test purposes
ks, pub, _ := account.GetRandomKeys()
accountAddressFelt, err := new(felt.Felt).SetString(accountAddress)
if err != nil {
panic("Error casting accountAddress to felt")
}
// Set up account
acnt, err := account.NewAccount(clientv02, accountAddressFelt, pub.String(), ks, accountContractVersion)
if err != nil {
panic(err)
}
classHash, err := utils.HexToFelt(predeployedClassHash)
if err != nil {
panic(err)
}
// Create transaction data
tx := rpc.DeployAccountTxn{
Nonce: &felt.Zero, // Contract accounts start with nonce zero.
MaxFee: new(felt.Felt).SetUint64(4724395326064),
Type: rpc.TransactionType_DeployAccount,
Version: rpc.TransactionV1,
Signature: []*felt.Felt{},
ClassHash: classHash,
ContractAddressSalt: pub,
ConstructorCalldata: []*felt.Felt{pub},
}
precomputedAddress, err := acnt.PrecomputeAddress(&felt.Zero, pub, classHash, tx.ConstructorCalldata)
fmt.Println("precomputedAddress:", precomputedAddress)
// At this point you need to add funds to precomputed address to use it.
var input string
fmt.Println("The `precomputedAddress` account needs to have enough ETH to perform a transaction.")
fmt.Println("Use the starknet faucet to send ETH to your `precomputedAddress`")
fmt.Println("When your account has been funded by the faucet, press any key, then `enter` to continue : ")
fmt.Scan(&input)
// Sign the transaction
err = acnt.SignDeployAccountTransaction(context.Background(), &tx, precomputedAddress)
if err != nil {
panic(err)
}
// Send transaction to the network
resp, err := acnt.AddDeployAccountTransaction(context.Background(), rpc.BroadcastDeployAccountTxn{DeployAccountTxn: tx})
if err != nil {
panic(fmt.Sprintf("Error returned from AddDeployAccountTransaction: %s", err))
}
fmt.Println("AddDeployAccountTransaction response:", resp)
}