Skip to content

Examples: Announces a transaction

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

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(0), "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.