Skip to content

Commit 6f6d35b

Browse files
authored
v1.1.0-alpha.3
2 parents 9c69b7c + afae3c1 commit 6f6d35b

File tree

12 files changed

+216
-340
lines changed

12 files changed

+216
-340
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5+
## [1.1.0-alpha.3](https://github.com/thenewboston-developers/thenewboston-js/compare/v1.1.0-alpha.2...v1.1.0-alpha.3) (2021-04-12)
6+
7+
8+
### Features
9+
10+
* Added support for memo ([#140](https://github.com/thenewboston-developers/thenewboston-js/issues/140)) ([ad5ed54](https://github.com/thenewboston-developers/thenewboston-js/commit/ad5ed54efc1f09b0ec2525fc0255ab39ab1df61d))
11+
512
## [1.1.0-alpha.2](https://github.com/thenewboston-developers/thenewboston-js/compare/v1.1.0-alpha.1...v1.1.0-alpha.2) (2021-04-10)
613

714

docs/account-payment-handler.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ await paymentHandler.init();
2929
const recipientAccount = new Account("fakeSigningKey");
3030
const amount = 1000;
3131

32-
await sendCoins(recipientAccount, 1000);
32+
// You can use this method to send memos as well
33+
await sendCoins(recipientAccount, amount, "memo");
3334
```
3435

3536
## Send Bulk Payments
@@ -58,6 +59,7 @@ await paymentHandler.init();
5859
/* Note
5960
The sender cannot be listed as a recipient
6061
A recipient cannot be listed more than once
62+
You must follow this order of the fields
6163
*/
6264

6365
const txs = [
@@ -67,6 +69,7 @@ const txs = [
6769
},
6870
{
6971
amount: 100,
72+
memo: "hi",
7073
recipient: "fakeAccountNumber2",
7174
},
7275
{

docs/primary-validator.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const transactions = [
1919
},
2020
{
2121
amount: 1,
22+
memo: "hi",
2223
recipient: "fakeAccountNumber",
2324
},
2425
{

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "thenewboston",
3-
"version": "1.1.0-alpha.2",
3+
"version": "1.1.0-alpha.3",
44
"description": "JavaScript library for thenewboston.",
55
"author": {
66
"name": "thenewboston-developers",

src/account-payment-handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export class AccountPaymentHandler {
1616
await this.client.init();
1717
}
1818

19-
async sendCoins(recipient: Account | string, amount: number) {
20-
await this.client.sendCoins(new TransferDetails(this.account, recipient, amount));
19+
async sendCoins(recipient: Account | string, amount: number, memo = "") {
20+
await this.client.sendCoins(new TransferDetails(this.account, recipient, amount, memo));
2121
}
2222

2323
async sendBulkTransactions(transactions: Transaction[]) {

src/models/transaction.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/** The model for thenewboston transactions. */
22
export interface Transaction {
33
amount: number;
4-
recipient: string;
54
fee?: string;
5+
memo?: string;
6+
recipient: string;
67
}

src/payment-handler.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ export class PaymentHandler {
4848
*
4949
*/
5050
async createTransaction(sender: Account, txs: Transaction[]) {
51+
txs = txs.map((tx) => {
52+
if (tx.memo) {
53+
tx.memo?.trim();
54+
if (!/^[a-zA-Z0-9_ ]*$/.test(tx.memo))
55+
throwError("Invalid memo", "Memo can only contain alphanumeric characters, spaces, and underscores");
56+
if (tx.memo.length > 64) throwError("Invalid memo", "Memo cannot exceed 64 characters");
57+
}
58+
if (tx.memo === "") delete tx.memo;
59+
return tx;
60+
});
61+
5162
const { balance_lock: balanceLock } = await this.primaryValidator!.getAccountBalanceLock(
5263
sender.accountNumberHex
5364
).catch((err) =>
@@ -81,9 +92,9 @@ export class PaymentHandler {
8192
* Sends a specific amount of coins to a given account from the sender.
8293
* @param transferDetails The object with transfer details like sender, recipient and amount
8394
*/
84-
async sendCoins({ sender, recipient, amount }: TransferDetails) {
95+
async sendCoins({ sender, recipient, amount, memo = "" }: TransferDetails) {
8596
const recipientAccount = typeof recipient === "string" ? recipient : recipient.accountNumberHex;
86-
const transaction = await this.createTransaction(sender, [{ recipient: recipientAccount, amount }]);
97+
const transaction = await this.createTransaction(sender, [{ amount, memo, recipient: recipientAccount }]);
8798
await this.broadcastTransaction(transaction);
8899
}
89100

src/utils/transfer-details.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import type { Account } from "../account";
2+
import { throwError } from "./throw-error";
23

34
export class TransferDetails {
45
public sender: Account;
56
public recipient: Account | string;
67
public amount: number;
8+
public memo: string;
79

8-
constructor(sender: Account, recipient: Account | string, amount: number) {
10+
constructor(sender: Account, recipient: Account | string, amount: number, memo: string) {
911
this.sender = sender;
1012
this.recipient = recipient;
1113
this.amount = amount;
14+
this.memo = memo;
1215
}
1316
}

tests/bank.test.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,21 @@ describe("Bank", () => {
9797
it("getTransactions()", async () => {
9898
const transactions = await bank.getTransactions();
9999
expect(typeof transactions).toBe("object");
100-
expect(transactions.results[0]).toStrictEqual({
101-
id: "a486f2ca-2431-455a-bdb2-6dccbf7e1ace",
102-
block: {
103-
id: "30b5bafb-139c-43ad-a03f-8c04e77c35a2",
104-
created_date: "2021-03-30T03:38:45.827858Z",
105-
modified_date: "2021-03-30T03:38:45.827884Z",
106-
balance_key: "43731d388391f5a8b046066a4a3cf6164b16192a99fcd00bdeb627a98d31016e",
107-
sender: "7c2d3b7774b494a496c00d175bd68d04280acb3fd1bac5dc46cae2b67d7f5a4f",
108-
signature:
109-
"d8b6d6f56149519d26ebb901702c078a0b75b4f153a84655f1a1b161c52aff65086c3a0195e91ee8504790cbc699d7f8189c680aa89d609b5d12101d7190d407",
110-
},
111-
amount: 1,
112-
fee: "BANK",
113-
recipient: "9a275161478536d0a5b88ff05d429b9a9e63d0032a46e7a6a8f088da89c69da5",
114-
});
100+
expect(transactions.results[0]).toStrictEqual( {
101+
"id": "443aabd9-d06b-4c4b-af3b-5a21cbee523d",
102+
"block": {
103+
"id": "04f407d2-35fa-4416-99f4-1ea39612a014",
104+
"created_date": "2021-04-12T08:21:32.612926Z",
105+
"modified_date": "2021-04-12T08:21:32.612953Z",
106+
"balance_key": "d2af51bfc15be5af4c4120c488625b7b224f6acb84a4467a4dd8f1647a0ec8e8",
107+
"sender": "22d0f0047b572a6acb6615f7aae646b0b96ddc58bfd54ed2775f885baeba3d6a",
108+
"signature": "9e715ea8e5c173a87369215868c649fbe164444ea138d2fff4e4add80f4ccdb3a5ee6a529964b43a5b9fd611d504b58c52c380792ed359c036763942e003a002"
109+
},
110+
"amount": 1,
111+
"fee": "PRIMARY_VALIDATOR",
112+
"memo": "",
113+
"recipient": "4afb3eaad999e4c073be0fbde86b76f9370d53b398b9cab9d760825709a1d6b3"
114+
});
115115
});
116116

117117
it("getBanks()", async () => {
@@ -195,7 +195,8 @@ describe("Bank", () => {
195195
const res = await bank.addBlocks(
196196
"fakeBalanceLock",
197197
[{ amount: 1, recipient: "fakeAccountNumber" }],
198-
new tnb.Account()
198+
new tnb.Account(),
199+
'Memo'
199200
);
200201
expect(typeof res).toBe("object");
201202
expect(res).toStrictEqual({

0 commit comments

Comments
 (0)