|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "math/big" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/coinbase/coinbase-sdk-go/pkg/coinbase" |
| 10 | + "github.com/ethereum/go-ethereum/core/types" |
| 11 | + "github.com/ethereum/go-ethereum/crypto" |
| 12 | + "github.com/ethereum/go-ethereum/ethclient" |
| 13 | +) |
| 14 | + |
| 15 | +/* |
| 16 | + * This example code tops up an existing validator on the Hoodi network with some ETH. |
| 17 | + * Run the code with 'go run examples/ethereum/dedicated-eth-top-up/main.go <api_key_file_path> <funding_wallet_address> <top-up-validator-pubkey> <private_key> <rpc_url>' |
| 18 | + */ |
| 19 | + |
| 20 | +func main() { |
| 21 | + ctx := context.Background() |
| 22 | + |
| 23 | + client, err := coinbase.NewClient( |
| 24 | + coinbase.WithAPIKeyFromJSON(os.Args[1]), |
| 25 | + ) |
| 26 | + |
| 27 | + if err != nil { |
| 28 | + log.Fatalf("error creating coinbase client: %v", err) |
| 29 | + } |
| 30 | + |
| 31 | + address := coinbase.NewExternalAddress(coinbase.EthereumHoodi, os.Args[2]) |
| 32 | + |
| 33 | + options := []coinbase.StakingOperationOption{ |
| 34 | + coinbase.WithNativeStakingOperationMode(), |
| 35 | + coinbase.WithTopUpValidatorPublicKey(os.Args[3]), |
| 36 | + } |
| 37 | + |
| 38 | + topUpOperation, err := client.BuildStakeOperation( |
| 39 | + ctx, |
| 40 | + big.NewFloat(1), // Top Up 1 ETH |
| 41 | + coinbase.Eth, |
| 42 | + address, |
| 43 | + options..., |
| 44 | + ) |
| 45 | + if err != nil { |
| 46 | + log.Fatalf("error building staking operation: %v", err) |
| 47 | + } |
| 48 | + |
| 49 | + log.Printf("staking operation ID: %s\n", topUpOperation.ID()) |
| 50 | + |
| 51 | + if err := client.Wait(ctx, topUpOperation, coinbase.WithWaitTimeoutSeconds(600)); err != nil { |
| 52 | + log.Fatalf("error waiting for staking operation: %v", err) |
| 53 | + } |
| 54 | + |
| 55 | + // Load your wallet's private key from which you initiated the above top up operation. |
| 56 | + key, err := crypto.HexToECDSA(os.Args[4]) |
| 57 | + if err != nil { |
| 58 | + log.Fatal(err) |
| 59 | + } |
| 60 | + |
| 61 | + log.Printf("Signing top up operation ...") |
| 62 | + |
| 63 | + // Sign the transactions within staking operation resource with your private key. |
| 64 | + if err := topUpOperation.Sign(key); err != nil { |
| 65 | + log.Fatal(err) |
| 66 | + } |
| 67 | + |
| 68 | + // For Hoodi, publicly available RPC URL's can be found here https://chainlist.org/chain/560048 |
| 69 | + ethClient, err := ethclient.Dial(os.Args[5]) |
| 70 | + if err != nil { |
| 71 | + log.Fatal(err) |
| 72 | + } |
| 73 | + |
| 74 | + // Broadcast each of the signed transactions to the network. |
| 75 | + for index, transaction := range topUpOperation.Transactions() { |
| 76 | + log.Printf("Deposit tx %d: %s\n", index+1, transaction.UnsignedPayload()) |
| 77 | + |
| 78 | + rawTx, ok := transaction.Raw().(*types.Transaction) |
| 79 | + if !ok { |
| 80 | + log.Fatal("Failed to cast to *types.Transaction") |
| 81 | + } |
| 82 | + |
| 83 | + if err := ethClient.SendTransaction(context.Background(), rawTx); err != nil { |
| 84 | + log.Fatal(err) |
| 85 | + } |
| 86 | + |
| 87 | + log.Printf("Broadcasted transaction hash: %s", rawTx.Hash().Hex()) |
| 88 | + } |
| 89 | +} |
0 commit comments