Skip to content

Transaction Transfer

Alexander Popov edited this page Aug 22, 2019 · 12 revisions

Transfer Transaction

Transfer transaction is used to send assets between two accounts. It can hold a message of length 1024. To create Transfer Transaction use NewTransferTransaction().

  • Following parameters required:
    • Recipient - The address of the recipient account.
    • Mosaics - The array of mosaics to be sent.
    • Message - The transaction message of 1024 characters.
package main

import (
    "context"
    "fmt"
    "time"
    "github.com/proximax-storage/go-xpx-catapult-sdk/sdk"
)

const (
    // Types of network.
    networkType = sdk.MijinTest
    // A valid private key
    privateKey = "3B9670B5CB19C893694FC49B461CE489BF9588BE16DBE8DC29CF06338133DEE6"
)

func main() {

    conf, err := sdk.NewConfig(context.Background(), []string{baseUrl})
    if err != nil {
        fmt.Printf("NewConfig returned error: %s", err)
        return
    }

    // Use the default http client
    client := sdk.NewClient(nil, conf)

    // Create an account from a private key
    account, err := sdk.NewAccountFromPrivateKey(privateKey, networkType, client.GenerationHash())
    if err != nil {
        fmt.Printf("NewAccountFromPrivateKey returned error: %s", err)
        return
    }

    // Create a new transfer type transaction
    transaction, err := sdk.NewTransferTransaction(
        // The maximum amount of time to include the transaction in the blockchain.
        sdk.NewDeadline(time.Hour * 1),
        // The address of the recipient account.
        sdk.NewAddress("SBILTA367K2LX2FEXG5TFWAS7GEFYAGY7QLFBYKC", networkType),
        // The array of mosaic to be sent.
        []*sdk.Mosaic{sdk.Xpx(10000000)},
        // The transaction message of 1024 characters.
        sdk.NewPlainMessage("Here you go"),
        networkType,
    )
    if err != nil {
        fmt.Printf("NewTransferTransaction returned error: %s", err)
        return
    }

    // Sign transaction
    signedTransaction, err := account.Sign(transaction)
    if err != nil {
        fmt.Printf("Sign returned error: %s", err)
        return
    }

    // Announce transaction
    _, err = client.Transaction.Announce(context.Background(), signedTransaction)
    if err != nil {
        fmt.Printf("Transaction.Announce returned error: %s", err)
        return
    }
}
Clone this wiki locally