P
P
Protokol
Search…
Complementary Examples
Scenarios Showing Transaction Creation And Broadcasting Best Practices
You can find runnable example in the following link

Prerequisites

Before we get started we need to make sure that all of the required dependencies are installed.
We recommend the usage of Yarn, but you can choose any other package manager you want!

yarn

1
yarn add @arkecosystem/crypto
2
yarn add @protokol/nameservice-crypto
3
yarn add @protokol/client
Copied!

pnpm

1
pnpm add @arkecosystem/crypto
2
pnpm add @protokol/nameservice-crypto
3
pnpm add @protokol/client
Copied!

npm

1
npm install @arkecosystem/crypto
2
npm install @protokol/nameservice-crypto
3
npm install @protokol/client
Copied!

How To Create And Broadcast Nameservice Transactions

Nameservice - Creating and Broadcasting

1
import { Identities, Managers, Transactions, Utils } from "@arkecosystem/crypto";
2
import { ProtokolConnection } from "@protokol/client";
3
import { Builders, Transactions as NameserviceTransactions } from "@protokol/nameservice-crypto";
4
5
export const Nameservice = async (): Promise<void> => {
6
// Configure our API client
7
const client = new ProtokolConnection("http://localhost:4003/api");
8
const passphrase = "clay harbor enemy utility margin pretty hub comic piece aerobic umbrella acquire";
9
10
// Configure manager and register transaction type
11
const configs = await client.api("node").crypto();
12
const {
13
body: {
14
data: {
15
block: { height },
16
},
17
},
18
} = await client.get("blockchain");
19
20
Managers.configManager.setConfig({ ...configs.body.data } as any);
21
Managers.configManager.setHeight(height);
22
Transactions.TransactionRegistry.registerTransactionType(NameserviceTransactions.NameserviceTransaction);
23
24
// Step 1: Retrieve the nonce of the sender wallet
25
const senderWallet = await client.api("wallets").get(Identities.Address.fromPassphrase(passphrase));
26
const senderNonce = Utils.BigNumber.make(senderWallet.body.data.nonce).plus(1);
27
28
// Step 2: Create the transaction
29
const transaction = new Builders.NameserviceBuilder()
30
.Nameservice({
31
name: "zan",
32
})
33
.nonce(senderNonce.toFixed())
34
.sign(passphrase);
35
36
// Step 3: Broadcast the transaction
37
const broadcastResponse = await client.api("transactions").create({ transactions: [transaction.build().toJson()] });
38
39
// Step 4: Log the response
40
console.log(JSON.stringify(broadcastResponse.body, null, 4));
41
};
42
43
Nameservice()
44
.then(() => process.exit(0))
45
.catch((error) => {
46
console.error(error);
47
process.exit(1);
48
});
Copied!
Last modified 1mo ago