|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/Peersyst/xrpl-go/pkg/crypto" |
| 7 | + "github.com/Peersyst/xrpl-go/xrpl/faucet" |
| 8 | + "github.com/Peersyst/xrpl-go/xrpl/rpc" |
| 9 | + "github.com/Peersyst/xrpl-go/xrpl/rpc/types" |
| 10 | + "github.com/Peersyst/xrpl-go/xrpl/transaction" |
| 11 | + txnTypes "github.com/Peersyst/xrpl-go/xrpl/transaction/types" |
| 12 | + "github.com/Peersyst/xrpl-go/xrpl/wallet" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + CreatePaymentTx = func(sender, receiver *wallet.Wallet, amount txnTypes.CurrencyAmount) *transaction.Payment { |
| 17 | + return &transaction.Payment{ |
| 18 | + BaseTx: transaction.BaseTx{ |
| 19 | + Account: sender.GetAddress(), |
| 20 | + TransactionType: transaction.PaymentTx, |
| 21 | + Flags: txnTypes.TfInnerBatchTxn, |
| 22 | + }, |
| 23 | + Amount: amount, |
| 24 | + Destination: receiver.GetAddress(), |
| 25 | + } |
| 26 | + } |
| 27 | +) |
| 28 | + |
| 29 | +func main() { |
| 30 | + // Configure the client |
| 31 | + cfg, err := rpc.NewClientConfig( |
| 32 | + "https://s.devnet.rippletest.net:51234/", |
| 33 | + rpc.WithFaucetProvider(faucet.NewDevnetFaucetProvider()), |
| 34 | + ) |
| 35 | + if err != nil { |
| 36 | + panic(err) |
| 37 | + } |
| 38 | + client := rpc.NewClient(cfg) |
| 39 | + |
| 40 | + // Create and fund wallets |
| 41 | + userWallet, err := wallet.New(crypto.ED25519()) |
| 42 | + if err != nil { |
| 43 | + fmt.Println(err) |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + user2Wallet, err := wallet.New(crypto.ED25519()) |
| 48 | + if err != nil { |
| 49 | + fmt.Println(err) |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + receiverWallet, err := wallet.New(crypto.ED25519()) |
| 54 | + if err != nil { |
| 55 | + fmt.Println(err) |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + fmt.Println("⏳ Funding wallets...") |
| 60 | + if err := client.FundWallet(&userWallet); err != nil { |
| 61 | + fmt.Println(err) |
| 62 | + return |
| 63 | + } |
| 64 | + if err := client.FundWallet(&user2Wallet); err != nil { |
| 65 | + fmt.Println(err) |
| 66 | + return |
| 67 | + } |
| 68 | + fmt.Println("💸 Wallets funded") |
| 69 | + |
| 70 | + // Check initial balances |
| 71 | + userBalance, err := client.GetXrpBalance(userWallet.ClassicAddress) |
| 72 | + if err != nil { |
| 73 | + userBalance = "0" |
| 74 | + } |
| 75 | + user2Balance, err := client.GetXrpBalance(user2Wallet.ClassicAddress) |
| 76 | + if err != nil { |
| 77 | + user2Balance = "0" |
| 78 | + } |
| 79 | + |
| 80 | + receiverBalance, err := client.GetXrpBalance(receiverWallet.ClassicAddress) |
| 81 | + if err != nil { |
| 82 | + receiverBalance = "0" |
| 83 | + } |
| 84 | + |
| 85 | + fmt.Printf("💳 User initial balance: %s XRP\n", userBalance) |
| 86 | + fmt.Printf("💳 User2 initial balance: %s XRP\n", user2Balance) |
| 87 | + fmt.Printf("💳 Receiver initial balance: %s XRP\n", receiverBalance) |
| 88 | + fmt.Println() |
| 89 | + |
| 90 | + fmt.Printf("Batch transaction test\n") |
| 91 | + |
| 92 | + // Create test batch transaction |
| 93 | + batchTx := &transaction.Batch{ |
| 94 | + BaseTx: transaction.BaseTx{ |
| 95 | + Account: txnTypes.Address(userWallet.ClassicAddress), |
| 96 | + TransactionType: transaction.BatchTx, |
| 97 | + }, |
| 98 | + RawTransactions: []txnTypes.RawTransaction{ |
| 99 | + {RawTransaction: CreatePaymentTx(&userWallet, &receiverWallet, txnTypes.XRPCurrencyAmount(5000000)).Flatten()}, |
| 100 | + {RawTransaction: CreatePaymentTx(&userWallet, &receiverWallet, txnTypes.XRPCurrencyAmount(5000000)).Flatten()}, |
| 101 | + }, |
| 102 | + } |
| 103 | + batchTx.SetAllOrNothingFlag() |
| 104 | + |
| 105 | + flattenedBatchTx := batchTx.Flatten() |
| 106 | + fmt.Println("⏳ Autofilling flattened batch transaction...") |
| 107 | + if err := client.Autofill(&flattenedBatchTx); err != nil { |
| 108 | + fmt.Println("Autofill error:", err) |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + fmt.Println("⏳ Signing batch transaction...") |
| 113 | + response, err := client.SubmitTxAndWait(flattenedBatchTx, &types.SubmitOptions{ |
| 114 | + Autofill: false, |
| 115 | + Wallet: &userWallet, |
| 116 | + }) |
| 117 | + if err != nil { |
| 118 | + fmt.Println(err) |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + fmt.Println("✅ Batch transaction submitted") |
| 123 | + fmt.Printf("🌐 Hash: %s\n", response.Hash.String()) |
| 124 | + fmt.Printf("🌐 Validated: %t\n", response.Validated) |
| 125 | + fmt.Println() |
| 126 | + |
| 127 | + // Check final balances |
| 128 | + finalUserBalance, err := client.GetXrpBalance(userWallet.ClassicAddress) |
| 129 | + if err != nil { |
| 130 | + finalUserBalance = "0" |
| 131 | + } |
| 132 | + finalReceiverBalance, err := client.GetXrpBalance(receiverWallet.ClassicAddress) |
| 133 | + if err != nil { |
| 134 | + finalReceiverBalance = "0" |
| 135 | + } |
| 136 | + |
| 137 | + fmt.Printf("💳 User final balance: %s XRP\n", finalUserBalance) |
| 138 | + |
| 139 | + fmt.Printf("💳 Receiver final balance: %s XRP\n", finalReceiverBalance) |
| 140 | + |
| 141 | + fmt.Println() |
| 142 | + fmt.Printf("Multisig Batch transaction test\n") |
| 143 | + |
| 144 | + // Create test batch transaction |
| 145 | + multiBatchTx := &transaction.Batch{ |
| 146 | + BaseTx: transaction.BaseTx{ |
| 147 | + Account: txnTypes.Address(userWallet.ClassicAddress), |
| 148 | + TransactionType: transaction.BatchTx, |
| 149 | + }, |
| 150 | + RawTransactions: []txnTypes.RawTransaction{ |
| 151 | + {RawTransaction: CreatePaymentTx(&userWallet, &receiverWallet, txnTypes.XRPCurrencyAmount(5000000)).Flatten()}, |
| 152 | + {RawTransaction: CreatePaymentTx(&user2Wallet, &receiverWallet, txnTypes.XRPCurrencyAmount(5000000)).Flatten()}, |
| 153 | + }, |
| 154 | + BatchSigners: []txnTypes.BatchSigner{ |
| 155 | + { |
| 156 | + BatchSigner: txnTypes.BatchSignerData{ |
| 157 | + Account: txnTypes.Address(user2Wallet.ClassicAddress), |
| 158 | + SigningPubKey: user2Wallet.PublicKey, |
| 159 | + }, |
| 160 | + }, |
| 161 | + }, |
| 162 | + } |
| 163 | + multiBatchTx.SetAllOrNothingFlag() |
| 164 | + |
| 165 | + flattenedMultiBatchTx := multiBatchTx.Flatten() |
| 166 | + fmt.Println("⏳ Autofilling flattened multi batch transaction...") |
| 167 | + if err := client.AutofillMultisigned(&flattenedMultiBatchTx, 1); err != nil { |
| 168 | + fmt.Println("Autofill error:", err) |
| 169 | + return |
| 170 | + } |
| 171 | + |
| 172 | + fmt.Println("⏳ Signing multi batch transaction...") |
| 173 | + if err := wallet.SignMultiBatch(user2Wallet, &flattenedMultiBatchTx, nil); err != nil { |
| 174 | + fmt.Println("Signing error:", err) |
| 175 | + return |
| 176 | + } |
| 177 | + |
| 178 | + response, err = client.SubmitTxAndWait(flattenedMultiBatchTx, &types.SubmitOptions{ |
| 179 | + Autofill: false, |
| 180 | + Wallet: &userWallet, |
| 181 | + }) |
| 182 | + if err != nil { |
| 183 | + fmt.Println(err) |
| 184 | + return |
| 185 | + } |
| 186 | + |
| 187 | + fmt.Println("✅ Multisig Batch transaction submitted") |
| 188 | + fmt.Printf("🌐 Hash: %s\n", response.Hash.String()) |
| 189 | + fmt.Printf("🌐 Validated: %t\n", response.Validated) |
| 190 | + fmt.Println() |
| 191 | + |
| 192 | + // Check final balances |
| 193 | + finalUser2Balance, err := client.GetXrpBalance(user2Wallet.ClassicAddress) |
| 194 | + if err != nil { |
| 195 | + finalUser2Balance = "0" |
| 196 | + } |
| 197 | + finalUserBalance, err = client.GetXrpBalance(userWallet.ClassicAddress) |
| 198 | + if err != nil { |
| 199 | + finalUserBalance = "0" |
| 200 | + } |
| 201 | + finalReceiverBalance, err = client.GetXrpBalance(receiverWallet.ClassicAddress) |
| 202 | + if err != nil { |
| 203 | + finalReceiverBalance = "0" |
| 204 | + } |
| 205 | + fmt.Printf("💳 User final balance: %s XRP\n", finalUserBalance) |
| 206 | + fmt.Printf("💳 User2 final balance: %s XRP\n", finalUser2Balance) |
| 207 | + fmt.Printf("💳 Receiver final balance: %s XRP\n", finalReceiverBalance) |
| 208 | +} |
0 commit comments