Skip to content

Commit

Permalink
feat: token.Quantity docs & test, return this from parser functions
Browse files Browse the repository at this point in the history
  • Loading branch information
martonlederer committed Apr 13, 2024
1 parent f64e19b commit 2d4b5b1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ const denomination = aoCredToken.info.Denomination;
const logo = aoCredToken.info.Logo;
```

#### Token quantity

You can easily manage quantities as special, `bigint` based floating point numbers that keep precision, using the `token.Quantity` field. This field provides a [`Quantity`](#quantities) instance every time it's called, with a pre-configured denomination matching the token's denomination. Read more about quantities [here](#quantities).

```ts
// initialise a quantity from a token
const amountToSend = aoCredToken.Quantity.fromString("752.34");
```

#### Wallet balance

You can query the token balance for an address. This will return a `Quantity` instance.
Expand Down Expand Up @@ -86,7 +95,7 @@ The transfer functions allows you to send a message to the token process that in
```ts
// this will transfer 1000 CRED to the provided address
const id = await aoCredToken.transfer(
new Quantity(0n, aoCredToken.info.Denomination).fromString("1000"),
aoCredToken.Quantity.fromString("1000"),
"XjvCPN31XCLPFBo9FUeB7vAK0VC6TwY52MCS-6Iho8h"
);

Expand Down
9 changes: 7 additions & 2 deletions src/Quantity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default class Quantity {
fromString(value: string) {
if (!value) {
this.#qty = 0n;
return;
return this;
}

// replace formatters
Expand All @@ -112,7 +112,7 @@ export default class Quantity {
// empty value
if (value === "") {
this.#qty = 0n;
return;
return this;
}

// multiplier according to the denomination
Expand All @@ -138,6 +138,8 @@ export default class Quantity {

// set result
this.#qty = result;

return this;
}

/**
Expand Down Expand Up @@ -238,6 +240,7 @@ export default class Quantity {
*/
_one() {
this.#qty = 10n ** this.#D;
return this;
}

/**
Expand All @@ -260,6 +263,8 @@ export default class Quantity {

this.#D = newDenomination;
this.#qty = newInst.#qty;

return this;
}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ describe("Token testing", () => {
expect(Quantity.isQuantityOf(invalidQty, token)).toBeFalsy();
});

test("Quantity instance for token", () => {
const inst1 = token.Quantity.fromString("1000.245");

expect(Quantity.isQuantityOf(inst1, token)).toBeTruthy();
expect(inst1.toString()).toEqual("1000.245");

const inst2 = token.Quantity.fromNumber(2);

expect(Quantity.isQuantityOf(inst2, token)).toBeTruthy();
expect(inst2.toString()).toEqual("2");
expect(inst1.toString()).toEqual("1000.245");
});

test("Get balance returns the correct balance", async () => {
const existingBal = await token.getBalance(tokenID);

Expand Down

0 comments on commit 2d4b5b1

Please sign in to comment.