Skip to content

Commit

Permalink
fix(toBigInt): refactor function and improve tests (#197)
Browse files Browse the repository at this point in the history
* refactor(utils): toBigInt function use Math.pow instead of a for loop

* test(utils): improve toBigInt and fromBigInt tests and use toStrictEqual instead of toBe

* fix(utils): refactor toBigInt function to handle decimal precision correctly

* adding a property-based test to check fromBigInt & toBigInt

* Use BigInt for numbers

* Support BigInt uint8Array conversion functions

* Rename toBigInt/fromBigInt -> parseBigInt/formatBigInt

* fix typed encoding

* Fix transaction builder

* Fix rawJSON incompatability with Firefox using Integer instead of BigInt

---------

Co-authored-by: bchamagne <[email protected]>
Co-authored-by: Samuel Manzanera <[email protected]>
  • Loading branch information
3 people authored Jun 14, 2024
1 parent 09d31d1 commit e751517
Show file tree
Hide file tree
Showing 23 changed files with 940 additions and 521 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,14 +407,14 @@ This aims to prove the ownership or the delegatation of some secret to a given l
Add a UCO transfer to the `data.ledger.uco.transfers` section of the transaction

- `to` is hexadecimal encoding or Uint8Array representing the transaction address (recipient) to receive the funds
- `amount` is the number of uco to send (in Big Int ref function `toBigInt`)
- `amount` is the number of uco to send (in Big Int ref function `parseBigInt`)

#### addTokenTransfer(to, amount, tokenAddress, tokenId)

Add a token transfer to the `data.ledger.token.transfers` section of the transaction

- `to` is hexadecimal encoding or Uint8Array representing the transaction address (recipient) to receive the funds
- `amount` is the number of uco to send (in Big Int ref function `toBigInt`)
- `amount` is the number of uco to send (in Big Int ref function `parseBigInt`)
- `tokenAddress` is hexadecimal encoding or Uint8Array representing the token's address to spend
- `tokenId` is the ID of the token to send (default to: 0)

Expand Down Expand Up @@ -688,7 +688,7 @@ const tx = ...
const fee = await archethic.transaction.getTransactionFee(tx)
console.log(fee)
{
fee: 11779421, // Big Int format (ref function fromBigInt)
fee: 11779421, // Big Int format
rates: {
eur: 0.086326,
usd: 0.084913
Expand Down Expand Up @@ -746,7 +746,7 @@ console.log(token);
id: '9DC6196F274B979E5AB9E3D7A0B03FEE3E4C62C7299AD46C8ECF332A2C5B6574',
name: 'Mining UCO rewards',
properties: {},
supply: 3340000000000000, // Big Int format (ref function fromBigInt)
supply: 3340000000000000, // Big Int format
symbol: 'MUCO',
type: 'fungible'
}
Expand Down Expand Up @@ -1147,7 +1147,7 @@ await archethic.rpcWallet.setOrigin({
const tx = archethic.transaction
.new()
.setType("transfer")
.addTokenTransfer("0001ABCD...", Utils.toBigInt(12), "00001234...", 0)
.addTokenTransfer("0001ABCD...", Utils.parseBigInt('12'), "00001234...", 0)
.addRecipient("0001ABCD...", "swap");

archethic.rpcWallet
Expand Down Expand Up @@ -1324,33 +1324,33 @@ const cipher = Crypto.aesEncrypt(
<details>
<summary>Utils</summary>

### fromBigInt(number, decimals)
### formatBigInt(number, decimals)

Convert a big int number to a x decimals number (mainly use to display token amount)
Convert a big int number to a x decimals string (mainly use to display token amount)

- `number` Big Int number to convert to decimals number
- `decimals` number of decimals needed (default to 8)

```js
import { Utils } from "@archethicjs/sdk";
Utils.fromBigInt(1_253_450_000);
// 12.5345
Utils.fromBigInt(12_534_500, 6);
// 12.5345
Utils.formatBigInt(BigInt(1_253_450_000));
// '12.5345'
Utils.formatBigInt(BigInt(12_534_500), 6);
// '12.5345'
```

### toBigInt(number, decimals)
### parseBigInt(number, decimals)

Convert a decimals number to a BigInt number
Convert a string into a BigInt number

- `number` decimals number to convert to Big Int number
- `decimals` number of decimals (default to 8)

```js
import { Utils } from "@archethicjs/sdk";
Utils.toBigInt(12.5345);
Utils.parseBigInt('12.5345');
// 1_253_450_000
Utils.toBigInt(12.5345, 6);
Utils.parseBigInt('12.5345', 6);
// 12_534_500
```

Expand Down
7 changes: 2 additions & 5 deletions example/keychain/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Archethic, { Crypto, Utils } from "@archethicjs/sdk";
import Keychain from "../../src/keychain";

const { toBigInt } = Utils;
const { parseBigInt } = Utils;

let endpoint = document.querySelector("#endpoint").value;

Expand Down Expand Up @@ -83,10 +83,7 @@ window.sendTransaction = async () => {
const recipientAddress = document.querySelector("#tx_address").value;
const ucoAmount = document.querySelector("#tx_amount").value;

const tx = archethic.transaction
.new()
.setType("transfer")
.addUCOTransfer(recipientAddress, toBigInt(parseFloat(ucoAmount)));
const tx = archethic.transaction.new().setType("transfer").addUCOTransfer(recipientAddress, parseBigInt(ucoAmount));

const originPrivateKey = Utils.originPrivateKey;

Expand Down
9 changes: 0 additions & 9 deletions example/rpcDeriveKeypairAddress/app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import Archethic, { Utils } from "@archethicjs/sdk";
import { ArchethicRPCClient } from "../../dist/api/wallet_rpc";

const { toBigInt } = Utils;

let file_content = "";

let ucoTransfers = [];
let tokenTransfers = [];
let recipients = [];
let ownerships = [];

ArchethicRPCClient.instance.setOrigin({ name: "Wallet RPC example application" });

/** @type {Archethic | undefined} */
Expand Down
9 changes: 0 additions & 9 deletions example/rpcService/app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import Archethic, { Utils } from "@archethicjs/sdk";
import { ArchethicRPCClient } from "../../dist/api/wallet_rpc";

const { toBigInt } = Utils;

let file_content = "";

let ucoTransfers = [];
let tokenTransfers = [];
let recipients = [];
let ownerships = [];

ArchethicRPCClient.instance.setOrigin({ name: "Wallet RPC example application" });

/** @type {Archethic | undefined} */
Expand Down
Loading

0 comments on commit e751517

Please sign in to comment.