Skip to content

Commit

Permalink
feat: Added support for memo (#140)
Browse files Browse the repository at this point in the history
* Add memo

* Memo validation

* Fix memo

* update docs and tests

* Add memo tests

* Remove console.log
  • Loading branch information
AbhayAysola authored Apr 12, 2021
1 parent ec91924 commit ad5ed54
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 338 deletions.
5 changes: 4 additions & 1 deletion docs/account-payment-handler.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ await paymentHandler.init();
const recipientAccount = new Account("fakeSigningKey");
const amount = 1000;

await sendCoins(recipientAccount, 1000);
// You can use this method to send memos as well
await sendCoins(recipientAccount, amount, "memo");
```

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

const txs = [
Expand All @@ -67,6 +69,7 @@ const txs = [
},
{
amount: 100,
memo: "hi",
recipient: "fakeAccountNumber2",
},
{
Expand Down
1 change: 1 addition & 0 deletions docs/primary-validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const transactions = [
},
{
amount: 1,
memo: "hi",
recipient: "fakeAccountNumber",
},
{
Expand Down
4 changes: 2 additions & 2 deletions src/account-payment-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export class AccountPaymentHandler {
await this.client.init();
}

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

async sendBulkTransactions(transactions: Transaction[]) {
Expand Down
3 changes: 2 additions & 1 deletion src/models/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** The model for thenewboston transactions. */
export interface Transaction {
amount: number;
recipient: string;
fee?: string;
memo?: string;
recipient: string;
}
15 changes: 13 additions & 2 deletions src/payment-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ export class PaymentHandler {
*
*/
async createTransaction(sender: Account, txs: Transaction[]) {
txs = txs.map((tx) => {
if (tx.memo) {
tx.memo?.trim();
if (!/^[a-zA-Z0-9_ ]*$/.test(tx.memo))
throwError("Invalid memo", "Memo can only contain alphanumeric characters, spaces, and underscores");
if (tx.memo.length > 64) throwError("Invalid memo", "Memo cannot exceed 64 characters");
}
if (tx.memo === "") delete tx.memo;
return tx;
});

const { balance_lock: balanceLock } = await this.primaryValidator!.getAccountBalanceLock(
sender.accountNumberHex
).catch((err) =>
Expand Down Expand Up @@ -81,9 +92,9 @@ export class PaymentHandler {
* Sends a specific amount of coins to a given account from the sender.
* @param transferDetails The object with transfer details like sender, recipient and amount
*/
async sendCoins({ sender, recipient, amount }: TransferDetails) {
async sendCoins({ sender, recipient, amount, memo = "" }: TransferDetails) {
const recipientAccount = typeof recipient === "string" ? recipient : recipient.accountNumberHex;
const transaction = await this.createTransaction(sender, [{ recipient: recipientAccount, amount }]);
const transaction = await this.createTransaction(sender, [{ amount, memo, recipient: recipientAccount }]);
await this.broadcastTransaction(transaction);
}

Expand Down
5 changes: 4 additions & 1 deletion src/utils/transfer-details.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import type { Account } from "../account";
import { throwError } from "./throw-error";

export class TransferDetails {
public sender: Account;
public recipient: Account | string;
public amount: number;
public memo: string;

constructor(sender: Account, recipient: Account | string, amount: number) {
constructor(sender: Account, recipient: Account | string, amount: number, memo: string) {
this.sender = sender;
this.recipient = recipient;
this.amount = amount;
this.memo = memo;
}
}
33 changes: 17 additions & 16 deletions tests/bank.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,21 @@ describe("Bank", () => {
it("getTransactions()", async () => {
const transactions = await bank.getTransactions();
expect(typeof transactions).toBe("object");
expect(transactions.results[0]).toStrictEqual({
id: "a486f2ca-2431-455a-bdb2-6dccbf7e1ace",
block: {
id: "30b5bafb-139c-43ad-a03f-8c04e77c35a2",
created_date: "2021-03-30T03:38:45.827858Z",
modified_date: "2021-03-30T03:38:45.827884Z",
balance_key: "43731d388391f5a8b046066a4a3cf6164b16192a99fcd00bdeb627a98d31016e",
sender: "7c2d3b7774b494a496c00d175bd68d04280acb3fd1bac5dc46cae2b67d7f5a4f",
signature:
"d8b6d6f56149519d26ebb901702c078a0b75b4f153a84655f1a1b161c52aff65086c3a0195e91ee8504790cbc699d7f8189c680aa89d609b5d12101d7190d407",
},
amount: 1,
fee: "BANK",
recipient: "9a275161478536d0a5b88ff05d429b9a9e63d0032a46e7a6a8f088da89c69da5",
});
expect(transactions.results[0]).toStrictEqual( {
"id": "443aabd9-d06b-4c4b-af3b-5a21cbee523d",
"block": {
"id": "04f407d2-35fa-4416-99f4-1ea39612a014",
"created_date": "2021-04-12T08:21:32.612926Z",
"modified_date": "2021-04-12T08:21:32.612953Z",
"balance_key": "d2af51bfc15be5af4c4120c488625b7b224f6acb84a4467a4dd8f1647a0ec8e8",
"sender": "22d0f0047b572a6acb6615f7aae646b0b96ddc58bfd54ed2775f885baeba3d6a",
"signature": "9e715ea8e5c173a87369215868c649fbe164444ea138d2fff4e4add80f4ccdb3a5ee6a529964b43a5b9fd611d504b58c52c380792ed359c036763942e003a002"
},
"amount": 1,
"fee": "PRIMARY_VALIDATOR",
"memo": "",
"recipient": "4afb3eaad999e4c073be0fbde86b76f9370d53b398b9cab9d760825709a1d6b3"
});
});

it("getBanks()", async () => {
Expand Down Expand Up @@ -195,7 +195,8 @@ describe("Bank", () => {
const res = await bank.addBlocks(
"fakeBalanceLock",
[{ amount: 1, recipient: "fakeAccountNumber" }],
new tnb.Account()
new tnb.Account(),
'Memo'
);
expect(typeof res).toBe("object");
expect(res).toStrictEqual({
Expand Down
Loading

0 comments on commit ad5ed54

Please sign in to comment.