Skip to content

Commit

Permalink
v1.1.0-alpha.3
Browse files Browse the repository at this point in the history
  • Loading branch information
zinoadidi authored Apr 12, 2021
2 parents 9c69b7c + afae3c1 commit 6f6d35b
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 340 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

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.

## [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)


### Features

* Added support for memo ([#140](https://github.com/thenewboston-developers/thenewboston-js/issues/140)) ([ad5ed54](https://github.com/thenewboston-developers/thenewboston-js/commit/ad5ed54efc1f09b0ec2525fc0255ab39ab1df61d))

## [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)


Expand Down
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "thenewboston",
"version": "1.1.0-alpha.2",
"version": "1.1.0-alpha.3",
"description": "JavaScript library for thenewboston.",
"author": {
"name": "thenewboston-developers",
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 6f6d35b

Please sign in to comment.