|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/ecdsa" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "math/big" |
| 10 | + |
| 11 | + "github.com/ethereum/go-ethereum/common" |
| 12 | + "github.com/ethereum/go-ethereum/common/hexutil" |
| 13 | + "github.com/ethereum/go-ethereum/core/types" |
| 14 | + "github.com/ethereum/go-ethereum/crypto" |
| 15 | + "github.com/ethereum/go-ethereum/ethclient" |
| 16 | +) |
| 17 | + |
| 18 | +const TokenContractAddress = "0x.." |
| 19 | +const WithdrawRecipientAddress = "0x.." |
| 20 | +const SponsorPolicyId = ".." |
| 21 | +const SponsorAPIEndpoint = "https://open-platform.nodereal.io/{YOUR_API_KEY}/eoa-paymaster-testnet" |
| 22 | +const HotwalletPrivateKey = ".." |
| 23 | + |
| 24 | +func main() { |
| 25 | + sponsorSetUpPolicyRules() |
| 26 | + cexDoGaslessWithdrawl() |
| 27 | +} |
| 28 | + |
| 29 | +func sponsorSetUpPolicyRules() { |
| 30 | + sponsorClient, err := NewSponsorClient(SponsorAPIEndpoint) |
| 31 | + if err != nil { |
| 32 | + log.Fatal(err) |
| 33 | + } |
| 34 | + // sponsor the tx that interact with the stable coin ERC20 contract |
| 35 | + success, err := sponsorClient.AddToWhitelist(context.Background(), WhitelistParams{ |
| 36 | + PolicyUUID: SponsorPolicyId, |
| 37 | + WhitelistType: ToAccountWhitelist, |
| 38 | + Values: []string{TokenContractAddress}, |
| 39 | + }) |
| 40 | + if err != nil || !success { |
| 41 | + log.Fatal("failed to add token contract whitelist", err) |
| 42 | + } |
| 43 | + |
| 44 | + // sponsor the tx that from hotwallets |
| 45 | + fromAddress := getAddressFromPrivateKey(HotwalletPrivateKey) |
| 46 | + |
| 47 | + success, err = sponsorClient.AddToWhitelist(context.Background(), WhitelistParams{ |
| 48 | + PolicyUUID: SponsorPolicyId, |
| 49 | + WhitelistType: FromAccountWhitelist, |
| 50 | + Values: []string{fromAddress.String()}, |
| 51 | + }) |
| 52 | + if err != nil || !success { |
| 53 | + log.Fatal("failed to add contract method whitelist") |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func cexDoGaslessWithdrawl() { |
| 58 | + withdrawAmount := big.NewInt(1e17) |
| 59 | + // Connect to an Ethereum node (for transaction assembly) |
| 60 | + client, err := ethclient.Dial("https://bsc-testnet-dataseed.bnbchain.org") |
| 61 | + if err != nil { |
| 62 | + log.Fatalf("Failed to connect to the Ethereum network: %v", err) |
| 63 | + } |
| 64 | + // Create a PaymasterClient (for transaction sending) |
| 65 | + paymasterClient, err := NewPaymasterClient("https://bsc-paymaster-testnet.nodereal.io") |
| 66 | + if err != nil { |
| 67 | + log.Fatalf("Failed to create PaymasterClient: %v", err) |
| 68 | + } |
| 69 | + |
| 70 | + // Load your private key |
| 71 | + privateKey, err := crypto.HexToECDSA(HotwalletPrivateKey) |
| 72 | + if err != nil { |
| 73 | + log.Fatalf("Failed to load private key: %v", err) |
| 74 | + } |
| 75 | + |
| 76 | + fromAddress := getAddressFromPrivateKey(HotwalletPrivateKey) |
| 77 | + |
| 78 | + // Token contract address |
| 79 | + tokenAddress := common.HexToAddress(TokenContractAddress) |
| 80 | + |
| 81 | + // Create ERC20 transfer data |
| 82 | + data, err := createERC20TransferData(common.HexToAddress(WithdrawRecipientAddress), withdrawAmount) |
| 83 | + if err != nil { |
| 84 | + log.Fatalf("Failed to create ERC20 transfer data: %v", err) |
| 85 | + } |
| 86 | + |
| 87 | + // Get the latest nonce for the from address |
| 88 | + nonce, err := client.PendingNonceAt(context.Background(), fromAddress) |
| 89 | + if err != nil { |
| 90 | + log.Fatalf("Failed to get nonce: %v", err) |
| 91 | + } |
| 92 | + |
| 93 | + // Create the transaction |
| 94 | + gasPrice := big.NewInt(0) |
| 95 | + tx := types.NewTransaction(nonce, tokenAddress, big.NewInt(0), 300000, gasPrice, data) |
| 96 | + |
| 97 | + // Get the chain ID |
| 98 | + chainID, err := client.ChainID(context.Background()) |
| 99 | + if err != nil { |
| 100 | + log.Fatalf("Failed to get chain ID: %v", err) |
| 101 | + } |
| 102 | + |
| 103 | + // Sign the transaction |
| 104 | + signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey) |
| 105 | + if err != nil { |
| 106 | + log.Fatalf("Failed to sign transaction: %v", err) |
| 107 | + } |
| 108 | + |
| 109 | + // Convert to Transaction struct for IsSponsorable check |
| 110 | + gasLimit := tx.Gas() |
| 111 | + sponsorableTx := Transaction{ |
| 112 | + To: &tokenAddress, |
| 113 | + From: fromAddress, |
| 114 | + Value: (*hexutil.Big)(big.NewInt(0)), |
| 115 | + Gas: (*hexutil.Uint64)(&gasLimit), |
| 116 | + Data: (*hexutil.Bytes)(&data), |
| 117 | + } |
| 118 | + |
| 119 | + // Check if the transaction is sponsorable |
| 120 | + sponsorableInfo, err := paymasterClient.IsSponsorable(context.Background(), sponsorableTx) |
| 121 | + if err != nil { |
| 122 | + log.Fatalf("Error checking sponsorable status: %v", err) |
| 123 | + } |
| 124 | + |
| 125 | + jsonInfo, _ := json.MarshalIndent(sponsorableInfo, "", " ") |
| 126 | + fmt.Printf("Sponsorable Information:\n%s\n", string(jsonInfo)) |
| 127 | + |
| 128 | + if sponsorableInfo.Sponsorable { |
| 129 | + // Send the transaction using PaymasterClient |
| 130 | + err := paymasterClient.SendTransaction(context.Background(), signedTx) |
| 131 | + if err != nil { |
| 132 | + log.Fatalf("Failed to send sponsorable transaction: %v", err) |
| 133 | + } |
| 134 | + fmt.Printf("Sponsorable transaction sent: %s\n", signedTx.Hash()) |
| 135 | + } else { |
| 136 | + fmt.Println("Transaction is not sponsorable. You may need to send it as a regular transaction.") |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func getAddressFromPrivateKey(pk string) common.Address { |
| 141 | + // Load your private key |
| 142 | + privateKey, err := crypto.HexToECDSA(pk) |
| 143 | + if err != nil { |
| 144 | + log.Fatalf("Failed to load private key: %v", err) |
| 145 | + } |
| 146 | + |
| 147 | + publicKey := privateKey.Public() |
| 148 | + publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey) |
| 149 | + if !ok { |
| 150 | + log.Fatal("Error casting public key to ECDSA") |
| 151 | + } |
| 152 | + return crypto.PubkeyToAddress(*publicKeyECDSA) |
| 153 | +} |
0 commit comments