Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add a section for numbers #1768

Merged
merged 14 commits into from
Feb 23, 2024
2 changes: 2 additions & 0 deletions .changeset/loud-wolves-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
45 changes: 45 additions & 0 deletions apps/docs-snippets/src/guide/types/numbers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { bn } from 'fuels';

import { DocSnippetProjectsEnum } from '../../../test/fixtures/forc-projects';
import { createAndDeployContractFromProject } from '../../utils';

/**
* @group node
*/
describe(__filename, () => {
test('should successfully create new Sway-compatible BigNumber from a JavaScript number', () => {
// #region numbers-docs-1
// #context import { bn } from 'fuels';

const originalNumber = 20;

const bigNumber = bn(originalNumber);

expect(bigNumber.toNumber()).toEqual(originalNumber);
// #endregion numbers-docs-1
});

test('should successfully create new Sway-compatible BigNumber from a string', () => {
// #region numbers-docs-2
// #context import { bn } from 'fuels';

const originalNumber = '9007199254740992';

const bigNumber = bn(originalNumber);

expect(bigNumber.toString()).toEqual(originalNumber);
// #endregion numbers-docs-2
});

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

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

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

expect(value.toNumber()).toEqual(originalNumber);
// #endregion numbers-docs-3
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ abi EchoValues {
fn echo_tuple(tuple: (u8, bool, u64)) -> (u8, bool, u64);

fn echo_b512(input: B512) -> B512;

fn echo_u64(value: u64) -> u64;
}

impl EchoValues for Contract {
Expand Down Expand Up @@ -44,5 +46,8 @@ impl EchoValues for Contract {
input
}
// #endregion bits512-3
fn echo_u64(value: u64) -> u64 {
value
}
}
// #endregion understanding-fuel-binary-file
4 changes: 4 additions & 0 deletions apps/docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ export default defineConfig({
text: 'Bytes32',
link: '/guide/types/bytes32',
},
{
text: 'Numbers',
link: '/guide/types/numbers',
},
{
text: 'String',
link: '/guide/types/string',
Expand Down
29 changes: 29 additions & 0 deletions apps/docs/src/guide/types/numbers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Numbers

In Sway, there are multiple types of numbers:

1. `u8` (8-bit unsigned integer)
2. `u16` (16-bit unsigned integer)
3. `u32` (32-bit unsigned integer)
4. `u64` (64-bit unsigned integer)
5. `u256` (256-bit unsigned integer)
Dhaiwat10 marked this conversation as resolved.
Show resolved Hide resolved
Dhaiwat10 marked this conversation as resolved.
Show resolved Hide resolved

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.

## 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:
Dhaiwat10 marked this conversation as resolved.
Show resolved Hide resolved

<<< @/../../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:

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

## Interacting with Numbers in Contract Methods

All numerical inputs and outputs to/from Sway program methods are `BigNumber` objects.

<<< @/../../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()`.
Loading