|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/NethermindEth/juno/core/felt" |
| 8 | + "github.com/NethermindEth/starknet.go/account" |
| 9 | + "github.com/NethermindEth/starknet.go/client" |
| 10 | + setup "github.com/NethermindEth/starknet.go/examples/internal" |
| 11 | + "github.com/NethermindEth/starknet.go/internal/utils" |
| 12 | + pm "github.com/NethermindEth/starknet.go/paymaster" |
| 13 | +) |
| 14 | + |
| 15 | +// OpenZeppelin account class hash that supports outside executions |
| 16 | +const OZAccountClassHash = "0x05b4b537eaa2399e3aa99c4e2e0208ebd6c71bc1467938cd52c798c601e43564" |
| 17 | + |
| 18 | +// An example of how to deploy a contract with a paymaster. |
| 19 | +func deployWithPaymaster() { |
| 20 | + fmt.Println("Starting paymaster example - deploying an account") |
| 21 | + |
| 22 | + // Load variables from '.env' file |
| 23 | + AVNUApiKey := setup.GetAVNUApiKey() |
| 24 | + |
| 25 | + // Since all accounts in Starknet are smart contracts, we need to deploy them first before we can use them. |
| 26 | + // And to do so, we need to calculate the address of the new account and fund it with |
| 27 | + // enough STRK tokens before deploying it. This tokens will be used to pay the fees for the `deploy` txn. |
| 28 | + // |
| 29 | + // Deploy an account with a paymaster using the `default` fee mode doesn't make much sense, as we will |
| 30 | + // need to send some tokens for the account anyway. So, we will use the `sponsored` fee mode now, |
| 31 | + // which will allow the paymaster to fully cover the fees for the `deploy` txn. This mode requires |
| 32 | + // an API key from an entity. You can only run this example with it. |
| 33 | + |
| 34 | + // Let's initialise the paymaster client, but now, we will also pass our API key to the client. |
| 35 | + // In the AVNU paymaster, the API key is a http header called `x-paymaster-api-key`. |
| 36 | + // In the current Starknet.go client, you can set a custom http header using the `client.WithHeader` option. |
| 37 | + paymaster, err := pm.New( |
| 38 | + context.Background(), |
| 39 | + AVNUPaymasterURL, |
| 40 | + client.WithHeader("x-paymaster-api-key", AVNUApiKey), |
| 41 | + ) |
| 42 | + if err != nil { |
| 43 | + panic(fmt.Errorf("error connecting to the paymaster provider with the API key: %w", err)) |
| 44 | + } |
| 45 | + |
| 46 | + fmt.Println("Established connection with the paymaster provider") |
| 47 | + fmt.Print("Step 1: Build the deploy transaction\n\n") |
| 48 | + |
| 49 | + // First, let's get all the data we need for deploy an account. |
| 50 | + _, pubKey, privK := account.GetRandomKeys() // Get random keys for the account |
| 51 | + fmt.Println("Public key:", pubKey) |
| 52 | + fmt.Println("Private key:", privK) |
| 53 | + classHash, _ := utils.HexToFelt( |
| 54 | + OZAccountClassHash, |
| 55 | + ) // It needs to be an SNIP-9 compatible account |
| 56 | + constructorCalldata := []*felt.Felt{ |
| 57 | + pubKey, |
| 58 | + } // The OZ account constructor requires the public key |
| 59 | + salt, _ := utils.HexToFelt("0xdeadbeef") // Just a random salt |
| 60 | + // Precompute the address of the new account based on the salt, class hash and constructor calldata |
| 61 | + precAddress := account.PrecomputeAccountAddress(salt, classHash, constructorCalldata) |
| 62 | + |
| 63 | + fmt.Println("Precomputed address:", precAddress) |
| 64 | + |
| 65 | + // Now we can create the deploy data for the transaction. |
| 66 | + deployData := &pm.AccountDeploymentData{ |
| 67 | + Address: precAddress, // The precomputed address of the new account |
| 68 | + ClassHash: classHash, |
| 69 | + Salt: salt, |
| 70 | + Calldata: constructorCalldata, |
| 71 | + SignatureData: []*felt.Felt{}, // Optional. For the OZ account, we don't need to add anything in the signature data. |
| 72 | + Version: pm.Cairo1, |
| 73 | + } |
| 74 | + |
| 75 | + // With the deploy data, we can build the transaction by calling the `paymaster_buildTransaction` method. |
| 76 | + // REMEMBER: this will only work if you have a valid API key configured. |
| 77 | + // |
| 78 | + // A full explanation about the paymaster_buildTransaction method can be found in the `main.go` file of this same example. |
| 79 | + builtTxn, err := paymaster.BuildTransaction(context.Background(), &pm.BuildTransactionRequest{ |
| 80 | + Transaction: pm.UserTransaction{ |
| 81 | + Type: pm.UserTxnDeploy, // we are building an `deploy` transaction |
| 82 | + Deployment: deployData, |
| 83 | + }, |
| 84 | + Parameters: pm.UserParameters{ |
| 85 | + Version: pm.UserParamV1, |
| 86 | + FeeMode: pm.FeeMode{ |
| 87 | + Mode: pm.FeeModeSponsored, // We then set the fee mode to `sponsored` |
| 88 | + Tip: &pm.TipPriority{ |
| 89 | + Priority: pm.TipPriorityNormal, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }) |
| 94 | + if err != nil { |
| 95 | + panic(fmt.Errorf("error building the deploy transaction: %w", err)) |
| 96 | + } |
| 97 | + fmt.Println("Transaction successfully built by the paymaster") |
| 98 | + PrettyPrint(builtTxn) |
| 99 | + |
| 100 | + // Since we are deploying an account, we don't need to sign the transaction, just execute it. |
| 101 | + |
| 102 | + fmt.Println("Step 2: Send the signed transaction") |
| 103 | + |
| 104 | + // With our built deploy transaction, we can send it to the paymaster by calling the `paymaster_executeTransaction` method. |
| 105 | + response, err := paymaster.ExecuteTransaction( |
| 106 | + context.Background(), |
| 107 | + &pm.ExecuteTransactionRequest{ |
| 108 | + Transaction: pm.ExecutableUserTransaction{ |
| 109 | + Type: pm.UserTxnDeploy, |
| 110 | + Deployment: builtTxn.Deployment, // The deployment data is the same. We can use our `deployData` variable, or |
| 111 | + // the `builtTxn.Deployment` value. |
| 112 | + }, |
| 113 | + Parameters: pm.UserParameters{ |
| 114 | + Version: pm.UserParamV1, |
| 115 | + |
| 116 | + // Using the same fee options as in the `paymaster_buildTransaction` method. |
| 117 | + FeeMode: pm.FeeMode{ |
| 118 | + Mode: pm.FeeModeSponsored, |
| 119 | + Tip: &pm.TipPriority{ |
| 120 | + Priority: pm.TipPriorityNormal, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + ) |
| 126 | + if err != nil { |
| 127 | + panic(fmt.Errorf("error executing the deploy transaction with the paymaster: %w", err)) |
| 128 | + } |
| 129 | + |
| 130 | + fmt.Println("Deploy transaction successfully executed by the paymaster") |
| 131 | + fmt.Println("Tracking ID:", response.TrackingID) |
| 132 | + fmt.Println("Transaction Hash:", response.TransactionHash) |
| 133 | +} |
0 commit comments