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/voting-crypto
3
yarn add @protokol/client
Copied!

pnpm

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

npm

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

How To Create And Broadcast Nameservice Transactions

Create Proposal - Creating and Broadcasting

1
import { Identities, Managers, Transactions, Utils } from "@arkecosystem/crypto";
2
import { ProtokolConnection } from "@protokol/client";
3
import { Builders, Transactions as VotingTransactions } from "@protokol/voting-crypto";
4
5
export const CreateProposal = 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(VotingTransactions.CreateProposalTransaction);
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.CreateProposalBuilder()
30
.createProposal({
31
duration: {
32
blockHeight: 123456,
33
},
34
content: "QmPfdpTRbhGVZZWKZDzNT5T4NB6C7fFo5wM9Xe8qmLCXWt",
35
})
36
.nonce(senderNonce.toFixed())
37
.sign(passphrase);
38
39
// Step 3: Broadcast the transaction
40
const broadcastResponse = await client.api("transactions").create({ transactions: [transaction.build().toJson()] });
41
42
// Step 4: Log the response
43
console.log(JSON.stringify(broadcastResponse.body, null, 4));
44
};
45
46
CreateProposal()
47
.then(() => process.exit(0))
48
.catch((error) => {
49
console.error(error);
50
process.exit(1);
51
});
52
Copied!

Cast Vote - Creating and Broadcasting

1
import { Identities, Managers, Transactions, Utils } from "@arkecosystem/crypto";
2
import { ProtokolConnection } from "@protokol/client";
3
import { Builders, Transactions as VotingTransactions } from "@protokol/voting-crypto";
4
5
export const CastVote = 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(VotingTransactions.CastVoteTransaction);
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.CastVoteBuilder()
30
.castVote({
31
proposalId: "c8f3fff0df73f1125d15a467b4f9401a7967e2915c93612737b7761cde7ada29",
32
decision: "yes",
33
})
34
.nonce(senderNonce.toFixed())
35
.sign(passphrase);
36
37
// Step 3: Broadcast the transaction
38
const broadcastResponse = await client.api("transactions").create({ transactions: [transaction.build().toJson()] });
39
40
// Step 4: Log the response
41
console.log(JSON.stringify(broadcastResponse.body, null, 4));
42
};
43
44
CastVote()
45
.then(() => process.exit(0))
46
.catch((error) => {
47
console.error(error);
48
process.exit(1);
49
});
50
Copied!
Last modified 1mo ago