Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhaiwat10 committed Feb 21, 2024
1 parent 7795bbe commit 1926798
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 53 deletions.
7 changes: 4 additions & 3 deletions apps/docs-snippets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
"build:forc": "pnpm fuels-forc build -p test/fixtures/forc-projects --release"
},
"devDependencies": {
"fuels": "workspace:*",
"@fuel-ts/utils": "workspace:*",
"@fuel-ts/account": "workspace:*",
"@fuel-ts/errors": "workspace:*",
"@fuel-ts/account": "workspace:*"
"@fuel-ts/utils": "workspace:*",
"ethers": "^6.7.1",
"fuels": "workspace:*"
},
"keywords": [],
"author": "",
Expand Down
28 changes: 28 additions & 0 deletions apps/docs-snippets/src/guide/types/numbers.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { toBigInt } from 'ethers';
import { bn } from 'fuels';

import { DocSnippetProjectsEnum } from '../../../test/fixtures/forc-projects';
Expand Down Expand Up @@ -42,4 +43,31 @@ describe(__filename, () => {
expect(value.toNumber()).toEqual(originalNumber);
// #endregion numbers-docs-3
});

test('should succcesfully pass in and read a number to/from a contract - small numbers', async () => {
const contract = await createAndDeployContractFromProject(DocSnippetProjectsEnum.ECHO_VALUES);

// #region numbers-docs-4
const originalNumber = 20;

const { value } = await contract.functions.echo_u8(originalNumber).call();

expect(value).toEqual(originalNumber);
// #endregion numbers-docs-4
});

test('ethers -> fuels BigNum conversion', () => {
// #region numbers-docs-5
// #context import { toBigInt } from 'ethers';
// #context import { bn } from 'fuels';

const originalNumber = 20;

const ethersBigNum = toBigInt(originalNumber);

const fuelsBigNum = bn(ethersBigNum.toString());

expect(fuelsBigNum.toNumber()).toEqual(originalNumber);
// #endregion numbers-docs-5
});
});
24 changes: 19 additions & 5 deletions apps/docs/src/guide/types/numbers.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,36 @@ In Sway, there are multiple types of numbers:
4. `u64` (64-bit unsigned integer)
5. `u256` (256-bit unsigned integer)

Conveniently, all of these types are handled the same way in the TS SDK. This guide explains how to create and interact with Sway numbers while using the SDK.
This guide explains how to create and interact with Sway numbers while using the SDK.

## Creating Numbers

Just like Ethereum, when you pass in a number to a Sway program from JavaScript, you must first convert it to a `BigNum` object. Here's how you can do that:
### For `u64` and `u256`

When you pass in a `u64` or `u256` to a Sway program from JavaScript, you must first convert it to a `BigNum` object. Here's how you can do that:

<<< @/../../docs-snippets/src/guide/types/numbers.test.ts#numbers-docs-1{ts:line-numbers}

You can also create a number from a string. This is useful when you want to pass in a number that is too large to be represented as a JavaScript number. Here's how you can do that:
You can also create a `BigNum` from a string. This is useful when you want to pass in a number that is too large to be represented as a JavaScript number. Here's how you can do that:

<<< @/../../docs-snippets/src/guide/types/numbers.test.ts#numbers-docs-2{ts:line-numbers}

## Interacting with Numbers in Contract Methods
### For `u8`, `u16`, `u32` and `u64`

You don't need to do anything special to create these numbers. You can pass in a JavaScript number directly. See the examples below for more details.

All numerical inputs and outputs to/from Sway program methods are `BigNumber` objects.
## Examples: Interacting with Numbers in Contract Methods

### For `u64` and `u256`

<<< @/../../docs-snippets/src/guide/types/numbers.test.ts#numbers-docs-3{ts:line-numbers}

> Note: If a contract call returns a number that is too large to be represented as a JavaScript number, you can convert it to a string using the `.toString()` method instead of `.toNumber()`.
### For `u8`, `u16`, `u32` and `u64`

<<< @/../../docs-snippets/src/guide/types/numbers.test.ts#numbers-docs-4{ts:line-numbers}

### Using a `BigNum` from `ethers` with `fuels`

<<< @/../../docs-snippets/src/guide/types/numbers.test.ts#numbers-docs-5{ts:line-numbers}
Loading

0 comments on commit 1926798

Please sign in to comment.