package main
import (
"context"
"fmt"
"log"
"time"
"github.com/stellar/go-stellar-sdk/clients/rpcclient"
"github.com/stellar/go-stellar-sdk/network"
protocol "github.com/stellar/go-stellar-sdk/protocols/rpc"
"github.com/stellar/go-stellar-sdk/strkey"
"github.com/stellar/go-stellar-sdk/txnbuild"
"github.com/stellar/go-stellar-sdk/xdr"
)
func must[T any](value T, err error) T {
if err != nil {
log.Fatal(err)
}
return value
}
func accountArg(address string) xdr.ScVal {
id := must(xdr.AddressToAccountId(address))
return xdr.ScVal{Type: xdr.ScValTypeScvAddress, Address: &xdr.ScAddress{
Type: xdr.ScAddressTypeScAddressTypeAccount, AccountId: &id,
}}
}
func main() {
// Replace with your MPCVault wallet and recipient addresses.
const source = "<string>"
const destination = "<string>"
const contractID = "CCW67TSZV3SSS2HXMBQ5JFGCKJNXKZM7UQUWUZPUTHXSTZLEO7SJMI75"
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
client := rpcclient.NewClient("https://mainnet.sorobanrpc.com", nil)
defer client.Close()
if must(client.GetNetwork(ctx)).Passphrase != network.PublicNetworkPassphrase {
log.Fatal("RPC network is not Stellar Mainnet")
}
account := must(client.LoadAccount(ctx, source))
decoded := must(strkey.Decode(strkey.VersionByteContract, contractID))
if len(decoded) != 32 {
log.Fatal("Invalid contract ID")
}
var contract xdr.ContractId
copy(contract[:], decoded)
amount := xdr.Int128Parts{Hi: 0, Lo: 100000} // 0.01 USDC
op := &txnbuild.InvokeHostFunction{HostFunction: xdr.HostFunction{
Type: xdr.HostFunctionTypeHostFunctionTypeInvokeContract,
InvokeContract: &xdr.InvokeContractArgs{
ContractAddress: xdr.ScAddress{Type: xdr.ScAddressTypeScAddressTypeContract, ContractId: &contract},
FunctionName: "transfer",
Args: xdr.ScVec{accountArg(source), accountArg(destination), {Type: xdr.ScValTypeScvI128, I128: &amount}},
},
}}
tx := must(txnbuild.NewTransaction(txnbuild.TransactionParams{
SourceAccount: account, IncrementSequenceNum: true,
Operations: []txnbuild.Operation{op}, BaseFee: 100,
Preconditions: txnbuild.Preconditions{TimeBounds: txnbuild.NewTimeout(900)},
}))
sim := must(client.SimulateTransaction(ctx, protocol.SimulateTransactionRequest{Transaction: must(tx.Base64())}))
if sim.Error != "" {
log.Fatal(sim.Error)
}
if sim.RestorePreamble != nil {
log.Fatal("RestoreFootprint is required")
}
if sim.TransactionDataXDR == "" || len(sim.Results) != 1 || sim.Results[0].AuthXDR == nil {
log.Fatal("Simulation did not return resource data and authorization")
}
var resources xdr.SorobanTransactionData
if err := xdr.SafeUnmarshalBase64(sim.TransactionDataXDR, &resources); err != nil {
log.Fatal(err)
}
envelope := tx.ToXDR()
envelope.V1.Tx.Ext = must(xdr.NewTransactionExt(1, resources))
if sim.MinResourceFee < 0 || uint64(sim.MinResourceFee)+uint64(envelope.V1.Tx.Fee) > uint64(^uint32(0)) {
log.Fatal("Invalid resource fee")
}
envelope.V1.Tx.Fee += xdr.Uint32(sim.MinResourceFee)
invoke := envelope.V1.Tx.Operations[0].Body.InvokeHostFunctionOp
for _, encoded := range *sim.Results[0].AuthXDR {
var entry xdr.SorobanAuthorizationEntry
if err := xdr.SafeUnmarshalBase64(encoded, &entry); err != nil {
log.Fatal(err)
}
if entry.Credentials.Type != xdr.SorobanCredentialsTypeSorobanCredentialsSourceAccount {
log.Fatal("This example requires source-account authorization")
}
invoke.Auth = append(invoke.Auth, entry)
}
fmt.Println("authEntryCount:", len(invoke.Auth))
fmt.Println("minResourceFeeStroops:", sim.MinResourceFee)
fmt.Println("feeStroops:", envelope.V1.Tx.Fee)
fmt.Println("txBase64:", must(xdr.MarshalBase64(envelope)))
}