Implementing the Transaction Builder

Builder class handles versioning, serde process, milestones, dynamic-fee logic and all cryptography related items (sign, multisign, second-sign, sign with and without WIF, sequential nonce logic). The following code-snippet shows the actual implementation of the Builder class for the BusinessRegistration Transaction.

Success

Builder is something you will reuse in your client applications for creating new transaction payloads.

1import { Interfaces, Transactions, Utils } from "@arkecosystem/crypto";
2import { BusinessRegistrationTransaction } from "../transactions";
3 
4export class BusinessRegistrationBuilder extends Transactions.TransactionBuilder<BusinessRegistrationBuilder> {
5 constructor() {
6 super();
7 this.data.type = BusinessRegistrationTransaction.type;
8 this.data.typeGroup = BusinessRegistrationTransaction.typeGroup;
9 this.data.version = 2;
10 this.data.fee = Utils.BigNumber.make("5000000000");
11 this.data.amount = Utils.BigNumber.ZERO;
12 this.data.asset = { businessData: {} };
13 }
14 
15 public businessData(name: string, website: string): BusinessRegistrationBuilder {
16 this.data.asset.businessData = {
17 name,
18 website,
19 };
20 
21 return this;
22 }
23 
24 public getStruct(): Interfaces.ITransactionData {
25 const struct: Interfaces.ITransactionData = super.getStruct();
26 struct.amount = this.data.amount;
27 struct.asset = this.data.asset;
28 return struct;
29 }
30 
31 protected instance(): BusinessRegistrationBuilder {
32 return this;
33 }
34}

Now that we have implemented our builder class, we can use it to build new custom transaction payloads. The code snippet below shows us how to use the TransactionBuilder to create signed and serialized transaction payloads.

Warning

When using Transaction Builder on the client side (your mobile/web app) make sure to register your TransactionType inside the @arkecosystem/crypto package with the following code:

Transactions.TransactionRegistry.registerTransactionType(YOUR_TRANSACTION_CLASS);

Check the code-snippet below, line 8.

1import { Transactions } from "@arkecosystem/crypto";
2import { BusinessRegistrationBuilder } from "../src/builders"; // adapt to your directory structure
3import { BusinessRegistrationTransaction } from "../src/transactions"; // adapt to your directory structure
4 
5// Register your custom transaction (do it only once) to the crypto library,
6// This is important when using builder on the client side.
7 
8Transactions.TransactionRegistry.registerTransactionType(BusinessRegistrationTransaction);
9 
10const builder = new BusinessRegistrationBuilder();
11const actual = builder
12 .nonce("3")
13 .fee("100")
14 .businessAsset("google","www.google.com")
15 .sign("clay harbor enemy utility margin pretty hub comic piece aerobic umbrella acquire");
16console.log(actual.build().toJson());

Next step is to implement transaction handlers that enforce blockchain protocol validation and verification steps.

Last updated 2 years ago
Edit Page
Share: