mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-19 19:25:51 +00:00
feat(_code-samples): WiP add go tx examples implementation
This commit is contained in:
24
_code-samples/freeze/go/go.mod
Normal file
24
_code-samples/freeze/go/go.mod
Normal file
@@ -0,0 +1,24 @@
|
||||
module github.com/XRPLF
|
||||
|
||||
go 1.23.0
|
||||
|
||||
toolchain go1.23.10
|
||||
|
||||
require github.com/Peersyst/xrpl-go v0.1.11
|
||||
|
||||
require (
|
||||
github.com/FactomProject/basen v0.0.0-20150613233007-fe3947df716e // indirect
|
||||
github.com/FactomProject/btcutilecc v0.0.0-20130527213604-d3a63a5752ec // indirect
|
||||
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
|
||||
github.com/decred/dcrd/crypto/ripemd160 v1.0.2 // indirect
|
||||
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
|
||||
github.com/gorilla/websocket v1.5.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/tyler-smith/go-bip32 v1.0.0 // indirect
|
||||
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
|
||||
github.com/ugorji/go/codec v1.2.11 // indirect
|
||||
golang.org/x/crypto v0.23.0 // indirect
|
||||
)
|
||||
58
_code-samples/freeze/go/go.sum
Normal file
58
_code-samples/freeze/go/go.sum
Normal file
@@ -0,0 +1,58 @@
|
||||
github.com/FactomProject/basen v0.0.0-20150613233007-fe3947df716e h1:ahyvB3q25YnZWly5Gq1ekg6jcmWaGj/vG/MhF4aisoc=
|
||||
github.com/FactomProject/basen v0.0.0-20150613233007-fe3947df716e/go.mod h1:kGUqhHd//musdITWjFvNTHn90WG9bMLBEPQZ17Cmlpw=
|
||||
github.com/FactomProject/btcutilecc v0.0.0-20130527213604-d3a63a5752ec h1:1Qb69mGp/UtRPn422BH4/Y4Q3SLUrD9KHuDkm8iodFc=
|
||||
github.com/FactomProject/btcutilecc v0.0.0-20130527213604-d3a63a5752ec/go.mod h1:CD8UlnlLDiqb36L110uqiP2iSflVjx9g/3U9hCI4q2U=
|
||||
github.com/Peersyst/xrpl-go v0.1.11 h1:P6r/gHxRnbAtAdPmzNHz/7zpsdfvwh0SS+QI2JNT44w=
|
||||
github.com/Peersyst/xrpl-go v0.1.11/go.mod h1:CBRM3/soqNeeL2Jx6USVUtECqulZVUoq3UxZKMz9hdw=
|
||||
github.com/btcsuite/btcd/btcec/v2 v2.3.4 h1:3EJjcN70HCu/mwqlUsGK8GcNVyLVxFDlWurTXGPFfiQ=
|
||||
github.com/btcsuite/btcd/btcec/v2 v2.3.4/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04=
|
||||
github.com/cmars/basen v0.0.0-20150613233007-fe3947df716e h1:0XBUw73chJ1VYSsfvcPvVT7auykAJce9FpRr10L6Qhw=
|
||||
github.com/cmars/basen v0.0.0-20150613233007-fe3947df716e/go.mod h1:P13beTBKr5Q18lJe1rIoLUqjM+CB1zYrRg44ZqGuQSA=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5ilcvdfma9wOH6Y=
|
||||
github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo=
|
||||
github.com/decred/dcrd/crypto/ripemd160 v1.0.2 h1:TvGTmUBHDU75OHro9ojPLK+Yv7gDl2hnUvRocRCjsys=
|
||||
github.com/decred/dcrd/crypto/ripemd160 v1.0.2/go.mod h1:uGfjDyePSpa75cSQLzNdVmWlbQMBuiJkvXw/MNKRY4M=
|
||||
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 h1:rpfIENRNNilwHwZeG5+P150SMrnNEcHYvcCuK6dPZSg=
|
||||
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
|
||||
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
|
||||
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
|
||||
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.1.5-0.20170601210322-f6abca593680/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/tyler-smith/go-bip32 v1.0.0 h1:sDR9juArbUgX+bO/iblgZnMPeWY1KZMUC2AFUJdv5KE=
|
||||
github.com/tyler-smith/go-bip32 v1.0.0/go.mod h1:onot+eHknzV4BVPwrzqY5OoVpyCvnwD7lMawL5aQupE=
|
||||
github.com/tyler-smith/go-bip39 v1.1.0 h1:5eUemwrMargf3BSLRRCalXT93Ns6pQJIjYQN2nyfOP8=
|
||||
github.com/tyler-smith/go-bip39 v1.1.0/go.mod h1:gUYDtqQw1JS3ZJ8UWVcGTGqqr6YIN3CWg+kkNaLt55U=
|
||||
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
|
||||
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||
golang.org/x/crypto v0.0.0-20170613210332-850760c427c5/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
|
||||
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
launchpad.net/gocheck v0.0.0-20140225173054-000000000087 h1:Izowp2XBH6Ya6rv+hqbceQyw/gSGoXfH/UPoTGduL54=
|
||||
launchpad.net/gocheck v0.0.0-20140225173054-000000000087/go.mod h1:hj7XX3B/0A+80Vse0e+BUHsHMTEhd0O4cpUHr/e/BUM=
|
||||
347
_code-samples/freeze/go/rpc/main.go
Normal file
347
_code-samples/freeze/go/rpc/main.go
Normal file
@@ -0,0 +1,347 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Peersyst/xrpl-go/pkg/crypto"
|
||||
"github.com/Peersyst/xrpl-go/xrpl/currency"
|
||||
"github.com/Peersyst/xrpl-go/xrpl/faucet"
|
||||
"github.com/Peersyst/xrpl-go/xrpl/rpc"
|
||||
transactions "github.com/Peersyst/xrpl-go/xrpl/transaction"
|
||||
"github.com/Peersyst/xrpl-go/xrpl/transaction/types"
|
||||
"github.com/Peersyst/xrpl-go/xrpl/wallet"
|
||||
)
|
||||
|
||||
const (
|
||||
currencyCode = "USDA"
|
||||
)
|
||||
|
||||
type SubmittableTransaction interface {
|
||||
TxType() transactions.TxType
|
||||
Flatten() transactions.FlatTransaction // Ensures all transactions can be flattened
|
||||
}
|
||||
|
||||
func main() {
|
||||
client := getRpcClient()
|
||||
|
||||
// Configure wallets
|
||||
|
||||
// Issuer
|
||||
fmt.Println("⏳ Setting up issuer wallet...")
|
||||
issuer, err := wallet.New(crypto.ED25519())
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error creating issuer wallet: %s\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = client.FundWallet(&issuer)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error funding issuer wallet: %s\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("✅ Issuer wallet funded: %s\n", issuer.ClassicAddress)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Holder 1
|
||||
fmt.Println("⏳ Setting up holder 1 wallet...")
|
||||
holderWallet1, err := wallet.New(crypto.ED25519())
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error creating holder wallet 1: %s\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = client.FundWallet(&holderWallet1)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error funding holder wallet 1: %s\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("✅ Holder wallet 1 funded: %s\n", holderWallet1.ClassicAddress)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Holder 2
|
||||
fmt.Println("⏳ Setting up holder 2 wallet...")
|
||||
holderWallet2, err := wallet.New(crypto.ED25519())
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error creating holder wallet 2: %s\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = client.FundWallet(&holderWallet2)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error funding holder wallet 2: %s\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("✅ Holder wallet 2 funded: %s\n", holderWallet2.ClassicAddress)
|
||||
fmt.Println()
|
||||
|
||||
fmt.Println("✅ Wallets setup complete!")
|
||||
fmt.Println()
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Configuring Issuing account
|
||||
fmt.Println("⏳ Configuring issuer address settings...")
|
||||
accountSet := &transactions.AccountSet{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(issuer.ClassicAddress),
|
||||
},
|
||||
Domain: types.Domain("697373756572"), // issuer
|
||||
}
|
||||
|
||||
accountSet.SetAsfDefaultRipple()
|
||||
submitAndWait(client, accountSet, issuer)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Trustline from the holder 1 to the issuer
|
||||
fmt.Println("⏳ Setting up trustline from holder 1 to the issuer...")
|
||||
trustSet := &transactions.TrustSet{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(holderWallet1.ClassicAddress),
|
||||
},
|
||||
LimitAmount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(issuer.ClassicAddress),
|
||||
Value: "1000000000",
|
||||
}}
|
||||
trustSet.SetSetNoRippleFlag()
|
||||
submitAndWait(client, trustSet, holderWallet1)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Trustline from the holder 2 to the issuer
|
||||
fmt.Println("⏳ Setting up trustline from holder 2 to the issuer...")
|
||||
trustSet = &transactions.TrustSet{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(holderWallet2.ClassicAddress),
|
||||
},
|
||||
LimitAmount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(issuer.ClassicAddress),
|
||||
Value: "1000000000",
|
||||
},
|
||||
}
|
||||
trustSet.SetSetNoRippleFlag()
|
||||
submitAndWait(client, trustSet, holderWallet2)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Minting to Holder 1
|
||||
fmt.Println("⏳ Minting to Holder 1...")
|
||||
payment := &transactions.Payment{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(issuer.ClassicAddress),
|
||||
},
|
||||
Destination: types.Address(holderWallet1.ClassicAddress),
|
||||
Amount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(issuer.ClassicAddress),
|
||||
Value: "50000",
|
||||
},
|
||||
}
|
||||
submitAndWait(client, payment, issuer)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Minting to Holder 2
|
||||
fmt.Println("⏳ Minting to Holder 2...")
|
||||
payment = &transactions.Payment{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(issuer.ClassicAddress),
|
||||
},
|
||||
Destination: types.Address(holderWallet2.ClassicAddress),
|
||||
Amount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(issuer.ClassicAddress),
|
||||
Value: "40000",
|
||||
},
|
||||
}
|
||||
submitAndWait(client, payment, issuer)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Sending payment from Holder 1 to Holder 2
|
||||
fmt.Println("⏳ Sending payment from Holder 1 to Holder 2...")
|
||||
payment = &transactions.Payment{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(holderWallet1.ClassicAddress),
|
||||
},
|
||||
Destination: types.Address(holderWallet2.ClassicAddress),
|
||||
Amount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(issuer.ClassicAddress),
|
||||
Value: "20",
|
||||
},
|
||||
}
|
||||
submitAndWait(client, payment, holderWallet1)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Freezing and Deep Freezing holder1
|
||||
fmt.Println("⏳ Freezing and Deep Freezing holder 1 trustline...")
|
||||
trustSet = &transactions.TrustSet{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(issuer.ClassicAddress),
|
||||
},
|
||||
LimitAmount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(holderWallet1.ClassicAddress),
|
||||
Value: "0",
|
||||
},
|
||||
}
|
||||
trustSet.SetSetFreezeFlag()
|
||||
trustSet.SetSetDeepFreezeFlag()
|
||||
|
||||
submitAndWait(client, trustSet, issuer)
|
||||
|
||||
// ------------------- SHOULD FAIL ⬇️ ------------------
|
||||
|
||||
// Sending payment from Holder 1 to Holder 2 (which should fail), Holder 1 can't decrease its balance
|
||||
fmt.Println("⏳ Sending payment from Holder 1 to Holder 2 (which should fail). Holder 1 can't decrease its balance...")
|
||||
payment = &transactions.Payment{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(holderWallet1.ClassicAddress),
|
||||
},
|
||||
Destination: types.Address(holderWallet2.ClassicAddress),
|
||||
Amount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(issuer.ClassicAddress),
|
||||
Value: "10",
|
||||
},
|
||||
}
|
||||
submitAndWait(client, payment, holderWallet1)
|
||||
|
||||
// ------------------- SHOULD FAIL ⬇️ ------------------
|
||||
|
||||
// Sending payment from Holder 2 to Holder 1 (which should fail), Holder 1 can't increase its balance
|
||||
fmt.Println("⏳ Sending payment from Holder 2 to Holder 1 (which should fail). Holder 1 can't increase its balance...")
|
||||
payment = &transactions.Payment{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(holderWallet2.ClassicAddress),
|
||||
},
|
||||
Destination: types.Address(holderWallet1.ClassicAddress),
|
||||
Amount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(issuer.ClassicAddress),
|
||||
Value: "10",
|
||||
},
|
||||
}
|
||||
submitAndWait(client, payment, holderWallet2)
|
||||
|
||||
// ------------------- SHOULD FAIL ⬇️ ------------------
|
||||
|
||||
// Creating OfferCreate transaction (which should fail), Holder 1 can't create an offer
|
||||
fmt.Println("⏳ Creating OfferCreate transaction (which should fail). Holder 1 can't create an offer...")
|
||||
offerCreate := &transactions.OfferCreate{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(holderWallet1.ClassicAddress),
|
||||
},
|
||||
TakerPays: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(issuer.ClassicAddress),
|
||||
Value: "10",
|
||||
},
|
||||
TakerGets: types.XRPCurrencyAmount(10),
|
||||
}
|
||||
submitAndWait(client, offerCreate, holderWallet1)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Unfreezing and Deep Unfreezing holder 1
|
||||
fmt.Println("⏳ Unfreezing and Deep Unfreezing holder 1 trustline...")
|
||||
trustSet = &transactions.TrustSet{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(issuer.ClassicAddress),
|
||||
},
|
||||
LimitAmount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(holderWallet1.ClassicAddress),
|
||||
Value: "0",
|
||||
},
|
||||
}
|
||||
trustSet.SetClearFreezeFlag()
|
||||
trustSet.SetClearDeepFreezeFlag()
|
||||
submitAndWait(client, trustSet, issuer)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Sending payment from Holder 1 to Holder 2 (which should succeed), Holder 1 can decrease its balance
|
||||
fmt.Println("⏳ Sending payment from Holder 1 to Holder 2 (which should succeed). Holder 1 can decrease its balance...")
|
||||
payment = &transactions.Payment{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(holderWallet1.ClassicAddress),
|
||||
},
|
||||
Destination: types.Address(holderWallet2.ClassicAddress),
|
||||
Amount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(issuer.ClassicAddress),
|
||||
Value: "10",
|
||||
},
|
||||
}
|
||||
submitAndWait(client, payment, holderWallet1)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Sending payment from Holder 2 to Holder 1 (which should succeed), Holder 1 can increase its balance
|
||||
fmt.Println("⏳ Sending payment from Holder 2 to Holder 1 (which should succeed). Holder 1 can increase its balance...")
|
||||
payment = &transactions.Payment{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(holderWallet2.ClassicAddress),
|
||||
},
|
||||
Destination: types.Address(holderWallet1.ClassicAddress),
|
||||
Amount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(issuer.ClassicAddress),
|
||||
Value: "10",
|
||||
},
|
||||
}
|
||||
submitAndWait(client, payment, holderWallet2)
|
||||
}
|
||||
|
||||
// getRpcClient returns a new rpc client
|
||||
func getRpcClient() *rpc.Client {
|
||||
cfg, err := rpc.NewClientConfig(
|
||||
// DeepFreeze only available on Devnet as of February 2025, change to testnet/mainnet once the amendment passes.
|
||||
"https://s.devnet.rippletest.net:51234",
|
||||
rpc.WithFaucetProvider(faucet.NewDevnetFaucetProvider()),
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return rpc.NewClient(cfg)
|
||||
}
|
||||
|
||||
// submitAndWait submits a transaction and waits for it to be included in a validated ledger
|
||||
func submitAndWait(client *rpc.Client, txn SubmittableTransaction, wallet wallet.Wallet) {
|
||||
fmt.Printf("⏳ Submitting %s transaction...\n", txn.TxType())
|
||||
|
||||
flattenedTx := txn.Flatten()
|
||||
|
||||
err := client.Autofill(&flattenedTx)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error autofilling %s transaction: %s\n", txn.TxType(), err)
|
||||
fmt.Println()
|
||||
return
|
||||
}
|
||||
|
||||
txBlob, _, err := wallet.Sign(flattenedTx)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error signing %s transaction: %s\n", txn.TxType(), err)
|
||||
fmt.Println()
|
||||
return
|
||||
}
|
||||
|
||||
response, err := client.SubmitTxBlobAndWait(txBlob, false)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error submitting %s transaction: %s\n", txn.TxType(), err)
|
||||
fmt.Println()
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("✅ %s transaction submitted\n", txn.TxType())
|
||||
fmt.Printf("🌐 Hash: %s\n", response.Hash.String())
|
||||
fmt.Println()
|
||||
}
|
||||
357
_code-samples/freeze/go/ws/main.go
Normal file
357
_code-samples/freeze/go/ws/main.go
Normal file
@@ -0,0 +1,357 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Peersyst/xrpl-go/pkg/crypto"
|
||||
"github.com/Peersyst/xrpl-go/xrpl/currency"
|
||||
"github.com/Peersyst/xrpl-go/xrpl/faucet"
|
||||
transactions "github.com/Peersyst/xrpl-go/xrpl/transaction"
|
||||
"github.com/Peersyst/xrpl-go/xrpl/transaction/types"
|
||||
"github.com/Peersyst/xrpl-go/xrpl/wallet"
|
||||
"github.com/Peersyst/xrpl-go/xrpl/websocket"
|
||||
)
|
||||
|
||||
const (
|
||||
currencyCode = "USDA"
|
||||
)
|
||||
|
||||
type SubmittableTransaction interface {
|
||||
TxType() transactions.TxType
|
||||
Flatten() transactions.FlatTransaction // Ensures all transactions can be flattened
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("⏳ Setting up client...")
|
||||
|
||||
client := getClient()
|
||||
fmt.Println("Connecting to server...")
|
||||
if err := client.Connect(); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("✅ Client configured!")
|
||||
fmt.Println()
|
||||
|
||||
fmt.Printf("Connection: %t", client.IsConnected())
|
||||
fmt.Println()
|
||||
|
||||
// Configure wallets
|
||||
|
||||
// Issuer
|
||||
fmt.Println("⏳ Setting up issuer wallet...")
|
||||
issuer, err := wallet.New(crypto.ED25519())
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error creating issuer wallet: %s\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = client.FundWallet(&issuer)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error funding issuer wallet: %s\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("✅ Issuer wallet funded: %s\n", issuer.ClassicAddress)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Holder 1
|
||||
fmt.Println("⏳ Setting up holder 1 wallet...")
|
||||
holderWallet1, err := wallet.New(crypto.ED25519())
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error creating holder wallet 1: %s\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = client.FundWallet(&holderWallet1)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error funding holder wallet 1: %s\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("✅ Holder wallet 1 funded: %s\n", holderWallet1.ClassicAddress)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Holder 2
|
||||
fmt.Println("⏳ Setting up holder 2 wallet...")
|
||||
holderWallet2, err := wallet.New(crypto.ED25519())
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error creating holder wallet 2: %s\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = client.FundWallet(&holderWallet2)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error funding holder wallet 2: %s\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("✅ Holder wallet 2 funded: %s\n", holderWallet2.ClassicAddress)
|
||||
fmt.Println()
|
||||
|
||||
fmt.Println("✅ Wallets setup complete!")
|
||||
fmt.Println()
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Configuring Issuing account
|
||||
fmt.Println("⏳ Configuring issuer address settings...")
|
||||
accountSet := &transactions.AccountSet{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(issuer.ClassicAddress),
|
||||
},
|
||||
Domain: types.Domain("697373756572"), // issuer
|
||||
}
|
||||
|
||||
accountSet.SetAsfDefaultRipple()
|
||||
submitAndWait(client, accountSet, issuer)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Trustline from the holder 1 to the issuer
|
||||
fmt.Println("⏳ Setting up trustline from holder 1 to the issuer...")
|
||||
trustSet := &transactions.TrustSet{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(holderWallet1.ClassicAddress),
|
||||
},
|
||||
LimitAmount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(issuer.ClassicAddress),
|
||||
Value: "1000000000",
|
||||
}}
|
||||
trustSet.SetSetNoRippleFlag()
|
||||
submitAndWait(client, trustSet, holderWallet1)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Trustline from the holder 2 to the issuer
|
||||
fmt.Println("⏳ Setting up trustline from holder 2 to the issuer...")
|
||||
trustSet = &transactions.TrustSet{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(holderWallet2.ClassicAddress),
|
||||
},
|
||||
LimitAmount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(issuer.ClassicAddress),
|
||||
Value: "1000000000",
|
||||
},
|
||||
}
|
||||
trustSet.SetSetNoRippleFlag()
|
||||
submitAndWait(client, trustSet, holderWallet2)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Minting to Holder 1
|
||||
fmt.Println("⏳ Minting to Holder 1...")
|
||||
payment := &transactions.Payment{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(issuer.ClassicAddress),
|
||||
},
|
||||
Destination: types.Address(holderWallet1.ClassicAddress),
|
||||
Amount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(issuer.ClassicAddress),
|
||||
Value: "50000",
|
||||
},
|
||||
}
|
||||
submitAndWait(client, payment, issuer)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Minting to Holder 2
|
||||
fmt.Println("⏳ Minting to Holder 2...")
|
||||
payment = &transactions.Payment{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(issuer.ClassicAddress),
|
||||
},
|
||||
Destination: types.Address(holderWallet2.ClassicAddress),
|
||||
Amount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(issuer.ClassicAddress),
|
||||
Value: "40000",
|
||||
},
|
||||
}
|
||||
submitAndWait(client, payment, issuer)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Sending payment from Holder 1 to Holder 2
|
||||
fmt.Println("⏳ Sending payment from Holder 1 to Holder 2...")
|
||||
payment = &transactions.Payment{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(holderWallet1.ClassicAddress),
|
||||
},
|
||||
Destination: types.Address(holderWallet2.ClassicAddress),
|
||||
Amount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(issuer.ClassicAddress),
|
||||
Value: "20",
|
||||
},
|
||||
}
|
||||
submitAndWait(client, payment, holderWallet1)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Freezing and Deep Freezing holder1
|
||||
fmt.Println("⏳ Freezing and Deep Freezing holder 1 trustline...")
|
||||
trustSet = &transactions.TrustSet{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(issuer.ClassicAddress),
|
||||
},
|
||||
LimitAmount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(holderWallet1.ClassicAddress),
|
||||
Value: "0",
|
||||
},
|
||||
}
|
||||
trustSet.SetSetFreezeFlag()
|
||||
trustSet.SetSetDeepFreezeFlag()
|
||||
|
||||
submitAndWait(client, trustSet, issuer)
|
||||
|
||||
// ------------------- SHOULD FAIL ⬇️ ------------------
|
||||
|
||||
// Sending payment from Holder 1 to Holder 2 (which should fail), Holder 1 can't decrease its balance
|
||||
fmt.Println("⏳ Sending payment from Holder 1 to Holder 2 (which should fail). Holder 1 can't decrease its balance...")
|
||||
payment = &transactions.Payment{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(holderWallet1.ClassicAddress),
|
||||
},
|
||||
Destination: types.Address(holderWallet2.ClassicAddress),
|
||||
Amount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(issuer.ClassicAddress),
|
||||
Value: "10",
|
||||
},
|
||||
}
|
||||
submitAndWait(client, payment, holderWallet1)
|
||||
|
||||
// ------------------- SHOULD FAIL ⬇️ ------------------
|
||||
|
||||
// Sending payment from Holder 2 to Holder 1 (which should fail), Holder 1 can't increase its balance
|
||||
fmt.Println("⏳ Sending payment from Holder 2 to Holder 1 (which should fail). Holder 1 can't increase its balance...")
|
||||
payment = &transactions.Payment{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(holderWallet2.ClassicAddress),
|
||||
},
|
||||
Destination: types.Address(holderWallet1.ClassicAddress),
|
||||
Amount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(issuer.ClassicAddress),
|
||||
Value: "10",
|
||||
},
|
||||
}
|
||||
submitAndWait(client, payment, holderWallet2)
|
||||
|
||||
// ------------------- SHOULD FAIL ⬇️ ------------------
|
||||
|
||||
// Creating OfferCreate transaction (which should fail), Holder 1 can't create an offer
|
||||
fmt.Println("⏳ Creating OfferCreate transaction (which should fail). Holder 1 can't create an offer...")
|
||||
offerCreate := &transactions.OfferCreate{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(holderWallet1.ClassicAddress),
|
||||
},
|
||||
TakerPays: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(issuer.ClassicAddress),
|
||||
Value: "10",
|
||||
},
|
||||
TakerGets: types.XRPCurrencyAmount(10),
|
||||
}
|
||||
submitAndWait(client, offerCreate, holderWallet1)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Unfreezing and Deep Unfreezing holder 1
|
||||
fmt.Println("⏳ Unfreezing and Deep Unfreezing holder 1 trustline...")
|
||||
trustSet = &transactions.TrustSet{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(issuer.ClassicAddress),
|
||||
},
|
||||
LimitAmount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(holderWallet1.ClassicAddress),
|
||||
Value: "0",
|
||||
},
|
||||
}
|
||||
trustSet.SetClearFreezeFlag()
|
||||
trustSet.SetClearDeepFreezeFlag()
|
||||
submitAndWait(client, trustSet, issuer)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Sending payment from Holder 1 to Holder 2 (which should succeed), Holder 1 can decrease its balance
|
||||
fmt.Println("⏳ Sending payment from Holder 1 to Holder 2 (which should succeed). Holder 1 can decrease its balance...")
|
||||
payment = &transactions.Payment{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(holderWallet1.ClassicAddress),
|
||||
},
|
||||
Destination: types.Address(holderWallet2.ClassicAddress),
|
||||
Amount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(issuer.ClassicAddress),
|
||||
Value: "10",
|
||||
},
|
||||
}
|
||||
submitAndWait(client, payment, holderWallet1)
|
||||
|
||||
// -----------------------------------------------------
|
||||
|
||||
// Sending payment from Holder 2 to Holder 1 (which should succeed), Holder 1 can increase its balance
|
||||
fmt.Println("⏳ Sending payment from Holder 2 to Holder 1 (which should succeed). Holder 1 can increase its balance...")
|
||||
payment = &transactions.Payment{
|
||||
BaseTx: transactions.BaseTx{
|
||||
Account: types.Address(holderWallet2.ClassicAddress),
|
||||
},
|
||||
Destination: types.Address(holderWallet1.ClassicAddress),
|
||||
Amount: types.IssuedCurrencyAmount{
|
||||
Currency: currency.ConvertStringToHex(currencyCode),
|
||||
Issuer: types.Address(issuer.ClassicAddress),
|
||||
Value: "10",
|
||||
},
|
||||
}
|
||||
submitAndWait(client, payment, holderWallet2)
|
||||
}
|
||||
|
||||
// getRpcClient returns a new rpc client
|
||||
func getClient() *websocket.Client {
|
||||
client := websocket.NewClient(
|
||||
websocket.NewClientConfig().
|
||||
WithHost("wss://s.devnet.rippletest.net:51233").
|
||||
WithFaucetProvider(faucet.NewDevnetFaucetProvider()),
|
||||
)
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
// submitAndWait submits a transaction and waits for it to be included in a validated ledger
|
||||
func submitAndWait(client *websocket.Client, txn SubmittableTransaction, wallet wallet.Wallet) {
|
||||
fmt.Printf("⏳ Submitting %s transaction...\n", txn.TxType())
|
||||
|
||||
flattenedTx := txn.Flatten()
|
||||
|
||||
err := client.Autofill(&flattenedTx)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error autofilling %s transaction: %s\n", txn.TxType(), err)
|
||||
fmt.Println()
|
||||
return
|
||||
}
|
||||
|
||||
txBlob, _, err := wallet.Sign(flattenedTx)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error signing %s transaction: %s\n", txn.TxType(), err)
|
||||
fmt.Println()
|
||||
return
|
||||
}
|
||||
|
||||
response, err := client.SubmitTxBlobAndWait(txBlob, false)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Error submitting %s transaction: %s\n", txn.TxType(), err)
|
||||
fmt.Println()
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("✅ %s transaction submitted\n", txn.TxType())
|
||||
fmt.Printf("🌐 Hash: %s\n", response.Hash.String())
|
||||
fmt.Println()
|
||||
}
|
||||
Reference in New Issue
Block a user