|
| 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 PaymentTokenContractAddress = "0x.." |
| 19 | +const PaymentRecipientAddress = "0x.." |
| 20 | +const PaymentSponsorPolicyId = ".." |
| 21 | +const SponsorAPIEndpoint = "https://open-platform.nodereal.io/{Your_API_key}/eoa-paymaster-testnet" |
| 22 | +const UserPrivateKey = "..." |
| 23 | + |
| 24 | +func main() { |
| 25 | + receiver := common.HexToAddress(PaymentRecipientAddress) |
| 26 | + payAmount := big.NewInt(1e17) |
| 27 | + |
| 28 | + paymentGatewaySetUpPolicyRules(receiver) |
| 29 | + |
| 30 | + userDoGaslessPayment(receiver, payAmount) |
| 31 | +} |
| 32 | + |
| 33 | +func paymentGatewaySetUpPolicyRules(receiver common.Address) { |
| 34 | + sponsorClient, err := NewSponsorClient(SponsorAPIEndpoint) |
| 35 | + if err != nil { |
| 36 | + log.Fatal(err) |
| 37 | + } |
| 38 | + |
| 39 | + // sponsor the tx that interact with the stable coin ERC20 contract |
| 40 | + success, err := sponsorClient.AddToWhitelist(context.Background(), WhitelistParams{ |
| 41 | + PolicyUUID: PaymentSponsorPolicyId, |
| 42 | + WhitelistType: ToAccountWhitelist, |
| 43 | + Values: []string{PaymentTokenContractAddress}, |
| 44 | + }) |
| 45 | + if err != nil || !success { |
| 46 | + log.Fatal("failed to add token contract whitelist") |
| 47 | + } |
| 48 | + |
| 49 | + // sponsor the tx that call the "transfer" interface of ERC20 contract |
| 50 | + success, err = sponsorClient.AddToWhitelist(context.Background(), WhitelistParams{ |
| 51 | + PolicyUUID: PaymentSponsorPolicyId, |
| 52 | + WhitelistType: ContractMethodSigWhitelist, |
| 53 | + Values: []string{"0xa9059cbb"}, |
| 54 | + }) |
| 55 | + if err != nil || !success { |
| 56 | + log.Fatal("failed to add contract method whitelist") |
| 57 | + } |
| 58 | + |
| 59 | + // sponsor the tx that "transfer" stable coin to particular receiver account |
| 60 | + success, err = sponsorClient.AddToWhitelist(context.Background(), WhitelistParams{ |
| 61 | + PolicyUUID: PaymentSponsorPolicyId, |
| 62 | + WhitelistType: BEP20ReceiverWhiteList, |
| 63 | + Values: []string{receiver.String()}, |
| 64 | + }) |
| 65 | + |
| 66 | + if err != nil || !success { |
| 67 | + log.Fatal("failed to add payment receiver whitelist") |
| 68 | + } |
| 69 | + |
| 70 | + receiverWhitelist := GetWhitelistParams{ |
| 71 | + PolicyUUID: PaymentSponsorPolicyId, |
| 72 | + WhitelistType: BEP20ReceiverWhiteList, |
| 73 | + Offset: 0, |
| 74 | + Limit: 1000, |
| 75 | + } |
| 76 | + |
| 77 | + // get the receiver whitelist, the Payment Gateway may need to update the whitelist according to its need. |
| 78 | + result, err := sponsorClient.GetWhitelist(context.Background(), receiverWhitelist) |
| 79 | + if err != nil { |
| 80 | + log.Fatal(err) |
| 81 | + } |
| 82 | + |
| 83 | + fmt.Println("Whitelist addresses:") |
| 84 | + for _, address := range result { |
| 85 | + fmt.Println(address) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func userDoGaslessPayment(receiver common.Address, amount *big.Int) { |
| 90 | + // Connect to an Ethereum node (for transaction assembly) |
| 91 | + client, err := ethclient.Dial("https://bsc-testnet-dataseed.bnbchain.org") |
| 92 | + if err != nil { |
| 93 | + log.Fatalf("Failed to connect to the Ethereum network: %v", err) |
| 94 | + } |
| 95 | + // Create a PaymasterClient (for transaction sending) |
| 96 | + paymasterClient, err := NewPaymasterClient("https://bsc-paymaster-testnet.nodereal.io") |
| 97 | + if err != nil { |
| 98 | + log.Fatalf("Failed to create PaymasterClient: %v", err) |
| 99 | + } |
| 100 | + |
| 101 | + // Load your private key |
| 102 | + privateKey, err := crypto.HexToECDSA(UserPrivateKey) |
| 103 | + if err != nil { |
| 104 | + log.Fatalf("Failed to load private key: %v", err) |
| 105 | + } |
| 106 | + |
| 107 | + publicKey := privateKey.Public() |
| 108 | + publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey) |
| 109 | + if !ok { |
| 110 | + log.Fatal("Error casting public key to ECDSA") |
| 111 | + } |
| 112 | + fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA) |
| 113 | + |
| 114 | + // Token contract address |
| 115 | + tokenAddress := common.HexToAddress(PaymentTokenContractAddress) |
| 116 | + |
| 117 | + // Create ERC20 transfer data |
| 118 | + data, err := createERC20TransferData(receiver, amount) |
| 119 | + if err != nil { |
| 120 | + log.Fatalf("Failed to create ERC20 transfer data: %v", err) |
| 121 | + } |
| 122 | + |
| 123 | + // Get the latest nonce for the from address |
| 124 | + nonce, err := client.PendingNonceAt(context.Background(), fromAddress) |
| 125 | + if err != nil { |
| 126 | + log.Fatalf("Failed to get nonce: %v", err) |
| 127 | + } |
| 128 | + |
| 129 | + // Create the transaction |
| 130 | + gasPrice := big.NewInt(0) |
| 131 | + tx := types.NewTransaction(nonce, tokenAddress, big.NewInt(0), 300000, gasPrice, data) |
| 132 | + |
| 133 | + // Get the chain ID |
| 134 | + chainID, err := client.ChainID(context.Background()) |
| 135 | + if err != nil { |
| 136 | + log.Fatalf("Failed to get chain ID: %v", err) |
| 137 | + } |
| 138 | + |
| 139 | + // Sign the transaction |
| 140 | + signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey) |
| 141 | + if err != nil { |
| 142 | + log.Fatalf("Failed to sign transaction: %v", err) |
| 143 | + } |
| 144 | + |
| 145 | + // Convert to Transaction struct for IsSponsorable check |
| 146 | + gasLimit := tx.Gas() |
| 147 | + sponsorableTx := Transaction{ |
| 148 | + To: &tokenAddress, |
| 149 | + From: fromAddress, |
| 150 | + Value: (*hexutil.Big)(big.NewInt(0)), |
| 151 | + Gas: (*hexutil.Uint64)(&gasLimit), |
| 152 | + Data: (*hexutil.Bytes)(&data), |
| 153 | + } |
| 154 | + |
| 155 | + // Check if the transaction is sponsorable |
| 156 | + sponsorableInfo, err := paymasterClient.IsSponsorable(context.Background(), sponsorableTx) |
| 157 | + if err != nil { |
| 158 | + log.Fatalf("Error checking sponsorable status: %v", err) |
| 159 | + } |
| 160 | + |
| 161 | + jsonInfo, _ := json.MarshalIndent(sponsorableInfo, "", " ") |
| 162 | + fmt.Printf("Sponsorable Information:\n%s\n", string(jsonInfo)) |
| 163 | + |
| 164 | + if sponsorableInfo.Sponsorable { |
| 165 | + // Send the transaction using PaymasterClient |
| 166 | + err := paymasterClient.SendTransaction(context.Background(), signedTx) |
| 167 | + if err != nil { |
| 168 | + log.Fatalf("Failed to send sponsorable transaction: %v", err) |
| 169 | + } |
| 170 | + fmt.Printf("Sponsorable transaction sent: %s\n", signedTx.Hash()) |
| 171 | + } else { |
| 172 | + fmt.Println("Transaction is not sponsorable. You may need to send it as a regular transaction.") |
| 173 | + } |
| 174 | +} |
0 commit comments