Skip to content

Examples: Announces a transaction

Eleazar Garrido edited this page Oct 5, 2018 · 17 revisions

Transfer Transaction

Transfer transaction is used to send assets between two accounts. It can hold a message of length 1024.

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

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

const (
	// Catapult-api-rest server.
	host = "http://catapult.internal.proximax.io:3000"

	// Types of network.
	// MainNet:   104
	// TestNet:   152
	// Mijin:     96
	// MijinTest: 144
	networkType = sdk.MijinTest

	// A valid private key.
	privateKey = "5D9513282B65A12A1B68DCB67DB64245721F7AE7822BE441FE813173803C512C"
)

func main() {

	// Testnet config default
	conf, err := sdk.LoadTestnetConfig(host)
	if err != nil {
		panic(err)
	}

	// Create an account from a private key
	acc, err := sdk.NewAccount(privateKey, networkType)

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

	// Create a new transfer type transaction
	ttx, 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.Mosaics{sdk.Xem(10000000)},
		// The transaction message of 1024 characters.
		sdk.NewPlainMessage(""),
		networkType,
	)

	// Sign transaction
	stx, err := acc.Sign(ttx)
	if err != nil {
		panic(fmt.Errorf("TransaferTransaction signing returned error: %s", err))
	}

	// Announce transaction
	restTx, resp, err := client.Transaction.Announce(context.Background(), stx)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s\n", restTx)
	fmt.Printf("Response Status Code == %d\n\n", resp.StatusCode)
	fmt.Printf("Hash: \t\t%v\n", stx.Hash)
	fmt.Printf("Signer: \t%X\n", acc.KeyPair.PublicKey.Raw)
}

Register namespace transaction

Accounts can rent a namespace for an amount of blocks and after a this renew the contract. This is done via a NewRegisterRootNamespaceTransaction() and NewRegisterSubNamespaceTransaction().

  • Create a root namespace
    • Following parameters are required:
      • deadline - The deadline to include the transaction.
      • namespaceName - The namespace name.
      • duration - The duration of the namespace.
      • networkType - The network type.
rootNamespace, err := sdk.NewRegisterRootNamespaceTransaction(
	sdk.NewDeadline(time.Hour*1),
	"newnamespace",
	big.NewInt(10000),
	networkType,
)

// Sign transaction
stx, err := acc.Sign(rootNamespace)
if err != nil {
	panic(fmt.Errorf("RegisterRootNamespaceTransaction signing returned error: %s", err))
}
// Announce transaction
restTx, resp, err := client.Transaction.Announce(context.Background(), stx)
if err != nil {
	panic(err)
}
fmt.Printf("%s\n", restTx)
fmt.Printf("Response Status Code == %d\n\n", resp.StatusCode)
fmt.Printf("Hash: \t\t%v\n", stx.Hash)
fmt.Printf("Signer: \t%X\n", acc.KeyPair.PublicKey.Raw)
  • Create a sub namespace
    • Following parameters are required:
      • deadline - The deadline to include the transaction.
      • namespaceName - The namespace name.
      • parentNamespace - The parent namespace name.
      • networkType - The network type.
SubNamespace, err := sdk.NewRegisterSubNamespaceTransaction(
	sdk.NewDeadline(time.Hour*1),
	"subnamespace",
	sdk.NewNamespaceId(big.NewInt(4635294387305441662), "newnamespace"),
	networkType,
)

// Sign transaction
stx, err := acc.Sign(SubNamespace)
if err != nil {
	panic(fmt.Errorf("NewRegisterSubNamespaceTransaction signing returned error: %s", err))
}

// Announce transaction
restTx, resp, err := client.Transaction.Announce(context.Background(), stx)
if err != nil {
	panic(err)
}
fmt.Printf("%s\n", restTx)
fmt.Printf("Response Status Code == %d\n\n", resp.StatusCode)
fmt.Printf("Hash: \t\t%v\n", stx.Hash)
fmt.Printf("Signer: \t%X\n", acc.KeyPair.PublicKey.Raw)

Mosaic definition transaction

Mosaic definition transaction is used to create a new mosaic.

  • Name
    • Name of the mosaic, up to a size limit of 64 characters; must be unique under the domain name.
  • Namespace name
    • To be able to create a mosaic definition, an account must rent at least one root namespace which the mosaic definition can then refer to.
  • Mosaic properties
    • divisibility: Determines up to what decimal place the mosaic can be divided. Divisibility of 3 means that a mosaic can be divided into smallest parts of 0.001 mosaics. The divisibility must be in the range of 0 and 6.
    • duration: The number of confirmed blocks we would like to rent our namespace for. Should be inferior or equal to namespace duration.
    • supply: The amount of mosaic in circulation. The creator can specify an initial supply of mosaics when creating the definition. The initial supply must be in the range of 0 and 9,000,000,000.
    • supply mutable: The creator can choose between a definition that allows a mosaic supply change at a later point or a immutable supply. In the first case the creator is only allowed to decrease the supply within the limits of mosaics owned.
    • transferability: The creator can choose if the mosaic can be transferred to and from arbitrary accounts, or only allowing itself to be the recipient once transferred for the first time.

Note: the connections to the server api are asynchronous and do not return the status of the transaction, to know the status you must use the websocket module of nem2-sdk-go.