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 further snippets with new infrastructure #3195

Closed
wants to merge 139 commits into from
Closed
Show file tree
Hide file tree
Changes from 135 commits
Commits
Show all changes
139 commits
Select commit Hold shift + click to select a range
28c0b1c
Starting new code-snippets app
arboleya Jul 2, 2024
48bbcba
Porting getting-started snippets
arboleya Jul 2, 2024
5b4f678
Ignoring new app in changesets
arboleya Jul 2, 2024
a45a02a
Adding changeset
arboleya Jul 2, 2024
b382460
Ignoring wrapped snippets files (generated)
arboleya Jul 2, 2024
a83e65c
Ignore test files trying to import files that haven’t been built yet
arboleya Jul 2, 2024
41c2943
Merge branch 'master' into aa/feat/infra-for-new-snippets
Torres-ssf Jul 3, 2024
596242c
Merge branch 'master' into aa/feat/infra-for-new-snippets
Torres-ssf Jul 3, 2024
89bd596
Using real code/file rather than code-string
arboleya Jul 4, 2024
89348f9
Adding example using `launchTestNode` and `LOCAL_NETWORK_URL` overrides
arboleya Jul 4, 2024
0b790b7
Skipping until forc workspace has at least one member
arboleya Jul 4, 2024
125aaad
Renaming variable
arboleya Jul 4, 2024
3406527
Fixing regex
arboleya Jul 4, 2024
01f8558
Ignoring lint issues individually
arboleya Jul 4, 2024
b63c163
Removing from browser group
arboleya Jul 4, 2024
36dbe16
Lintfix
arboleya Jul 4, 2024
663d9ce
Merge branch 'master' into aa/feat/infra-for-new-snippets
arboleya Jul 4, 2024
9134334
Restoring console after snippet
arboleya Jul 4, 2024
168f124
Keeping wrapper free from weird hacks
arboleya Jul 4, 2024
3dd1e3f
Adding mock utility
arboleya Jul 4, 2024
6ed91e2
Using mocking utility
arboleya Jul 4, 2024
939f9d6
Validating if `nertworkUrl` was properly mocked
arboleya Jul 4, 2024
571e85c
Merge branch 'master' into aa/feat/infra-for-new-snippets
arboleya Jul 4, 2024
f795f00
Mock provider instead?
arboleya Jul 4, 2024
5732d8c
Mocking only for localhost
arboleya Jul 4, 2024
020022f
Adjusting snippets
arboleya Jul 4, 2024
1388057
Wrapping snippet in a try/catch
arboleya Jul 5, 2024
48f1176
Merge branch 'master' into aa/feat/infra-for-new-snippets
arboleya Jul 15, 2024
98b02e1
Adding blank spaces
arboleya Jul 15, 2024
ea01d46
Removing mocking utility
arboleya Jul 15, 2024
2cacdb2
Adding pass-through options for `console.log` calls
arboleya Jul 15, 2024
86205c4
Adding new slot for injecting test node launcher into wrapped snippet
arboleya Jul 15, 2024
a6545af
Injecting node launcher for snippets using `LOCAL_NETWORK_URL`
arboleya Jul 15, 2024
fd9a886
Trim snippets
arboleya Jul 15, 2024
8295f4f
Cleanup snippets tests
arboleya Jul 15, 2024
70dde7c
Stop ignoring test for `docs-snippets2`
arboleya Jul 15, 2024
45b576b
Re-adding `docs-snippets2` tests to knip ignore list
arboleya Jul 15, 2024
802356a
Using internal builds
arboleya Jul 19, 2024
74f7cef
Fixing ignore rule
arboleya Jul 19, 2024
8c0be15
Adding basic counter Sway project
arboleya Jul 19, 2024
6af5a6a
Improving scripts
arboleya Jul 19, 2024
143d8be
Adding sample env file
arboleya Jul 19, 2024
5a47c48
Standardizing snippets and tests
arboleya Jul 19, 2024
c68ea14
Validate env constants existence
arboleya Jul 20, 2024
7a65623
Drafting new snippet
arboleya Jul 20, 2024
f2f4133
Merge branch 'master' into aa/feat/infra-for-new-snippets
arboleya Jul 20, 2024
c32a6e9
Adjusting script command
arboleya Jul 20, 2024
0d81092
Removing unnecessary wallet
arboleya Jul 20, 2024
65d6e80
Extacting launcher into another file
arboleya Jul 20, 2024
7ddc250
New API draft
arboleya Jul 20, 2024
28cce95
Syncing env and launcher template
arboleya Jul 20, 2024
47b19bd
Better docs
arboleya Jul 20, 2024
f0287c2
Adding new sway script
arboleya Jul 20, 2024
4ac46af
Removing API draft
arboleya Jul 20, 2024
e0c319e
Fixing file location
arboleya Jul 20, 2024
c647814
Add script to workspace
arboleya Jul 20, 2024
1ebc19d
Improve snippet
arboleya Jul 20, 2024
9c93682
Using new snippets
arboleya Jul 20, 2024
739d78e
Add new API draft
arboleya Jul 20, 2024
3808175
Matching extra blankspace
arboleya Jul 20, 2024
a91820f
Ignoring env file
arboleya Jul 20, 2024
7b8bbbb
Adding region marks
arboleya Jul 20, 2024
847c412
Avoid repetition
arboleya Jul 20, 2024
3f756e9
Trim env consts
arboleya Jul 20, 2024
16abcb4
Cleanup
arboleya Jul 20, 2024
094355b
Playing around new API
arboleya Jul 20, 2024
a5d3d08
Tidying up
arboleya Jul 20, 2024
501d913
Ignoring experimental file
arboleya Jul 20, 2024
71f0c01
Ignoring generated files in eslint
arboleya Jul 20, 2024
ae8fc47
Adjusting wallets count
arboleya Jul 20, 2024
4379cff
Moving chunk around
arboleya Jul 20, 2024
b4f2164
Adjusting ignore rule
arboleya Jul 20, 2024
8bf6293
Improving imports manipulation
arboleya Jul 20, 2024
9590341
Adding comments
arboleya Jul 20, 2024
4be8d63
Adjusting template markers
arboleya Jul 20, 2024
88a04b4
Tyop
arboleya Jul 20, 2024
57289fd
Update draft
arboleya Jul 20, 2024
71847b0
Update title
arboleya Jul 20, 2024
2f02691
Adjust deployment usage
arboleya Jul 20, 2024
a7fd7e7
Updating example
arboleya Jul 22, 2024
7f8c273
Merge branch 'master' into aa/feat/infra-for-new-snippets
arboleya Jul 22, 2024
247898c
Removing temp draft
arboleya Jul 22, 2024
dfe8eb9
Removing temp draft section
arboleya Jul 22, 2024
e73a076
Merge branch 'master' into aa/feat/infra-for-new-snippets
arboleya Aug 1, 2024
e03c3db
Merge branch 'master' into aa/feat/infra-for-new-snippets
arboleya Aug 11, 2024
aa09fbd
Updating broken test
arboleya Aug 11, 2024
0268ef3
Merge branch 'master' into aa/feat/infra-for-new-snippets
arboleya Aug 13, 2024
5cb05ae
Merge branch 'master' into aa/feat/infra-for-new-snippets
arboleya Aug 13, 2024
afa36c7
Merge branch 'master' into aa/feat/infra-for-new-snippets
arboleya Aug 30, 2024
6e05237
Merge branch 'master' into aa/feat/infra-for-new-snippets
arboleya Aug 31, 2024
739ccef
Merge branch 'master' into aa/feat/infra-for-new-snippets
arboleya Sep 5, 2024
dd4d3f8
Merge branch 'master' into aa/feat/infra-for-new-snippets
arboleya Sep 6, 2024
3c36528
Merge branch 'master' into aa/feat/infra-for-new-snippets
arboleya Sep 7, 2024
5d85686
Merge branch 'master' into aa/feat/infra-for-new-snippets
arboleya Sep 10, 2024
9647ccf
Using `NETWORK_URL` across the board
arboleya Sep 10, 2024
85c8cbd
Reword comment
arboleya Sep 10, 2024
1fcdf29
Adding scripts dir to tsconfig
arboleya Sep 10, 2024
3b463d0
Starting with more wallets
arboleya Sep 10, 2024
405b4fd
Merge branch 'master' into aa/feat/infra-for-new-snippets
arboleya Sep 13, 2024
9532319
Automatically generating tests for code snippets
arboleya Sep 13, 2024
00234d6
Forcing assertion failures to throw
arboleya Sep 13, 2024
8e17acb
Merge branch 'master' into aa/feat/infra-for-new-snippets
arboleya Sep 17, 2024
52d6c2d
Merge branch 'master' into aa/feat/infra-for-new-snippets
arboleya Sep 19, 2024
fb99481
Merge branch 'master' into mc/continue-infra-for-snippets
maschad Sep 20, 2024
eaca43f
docs: initial transpose of encoding + bytes docs
maschad Sep 22, 2024
2e53e9d
docs: add wallet management + utilities
maschad Sep 22, 2024
e657335
Merge branch 'master' into mc/continue-infra-for-snippets
maschad Sep 26, 2024
1ba314b
docs: add launch node testing snippets
maschad Sep 26, 2024
38c5ae6
Merge branch 'next' into mc/continue-infra-for-snippets
maschad Sep 27, 2024
63ebc63
docs: added configurable constant + custom transaction snippets
maschad Sep 27, 2024
dfee87d
lint: forc formatting
maschad Sep 27, 2024
60be4a5
docs: run test snippets in node
maschad Sep 27, 2024
546c116
docs: revert changes in test node original snippets
maschad Sep 28, 2024
607a872
chore: modify template to check test environment
maschad Sep 30, 2024
5a2aba8
Merge branch 'next' into mc/continue-infra-for-snippets
maschad Sep 30, 2024
7baad23
docs: fix doc links
maschad Sep 30, 2024
f6a5869
docs: update test comments
maschad Oct 1, 2024
9a58d2c
docs: remove unnecessary spacing
maschad Oct 1, 2024
f5893e5
docs: update variable naming
maschad Oct 1, 2024
a9aa0f2
docs: update vars
maschad Oct 1, 2024
f725f95
docs: update spacing
maschad Oct 1, 2024
ab529f2
docs: whitespace
maschad Oct 1, 2024
561b33c
docs: remove unnecessary comments
maschad Oct 1, 2024
84af072
docs: whitespace
maschad Oct 1, 2024
098b3db
docs: remove whitespace
maschad Oct 1, 2024
29f83c7
docs: remove whitespace
maschad Oct 1, 2024
2f094a1
docs: remove whitespace
maschad Oct 1, 2024
b2b4d13
docs: whitespace
maschad Oct 1, 2024
1a4dbe0
docs: add more spacing
maschad Oct 1, 2024
3c30cd2
ci: update workflows to include next branch (#3234)
maschad Oct 1, 2024
afa017e
build(deps): bump the deps-minor group with 9 updates (#3229)
dependabot[bot] Oct 1, 2024
a4b16b1
build(deps): bump chalk from 4.0.0 to 4.1.2 (#3231)
dependabot[bot] Oct 1, 2024
b43cf2b
chore: disable `no-continue` eslint rule (#3225)
nedsalk Oct 1, 2024
6f0289b
Merge branch 'next' into mc/continue-infra-for-snippets
maschad Oct 1, 2024
7f38a0c
chore: re-arrange console.assert statements
maschad Oct 1, 2024
ea05b14
Merge branch 'next' into mc/continue-infra-for-snippets
maschad Oct 3, 2024
9fe51f2
chore: remove console.assert
maschad Oct 3, 2024
e19b126
docs: add README for snippets
maschad Oct 4, 2024
4716eaa
lint: update README
maschad Oct 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ apps/demo-react-vite
apps/create-fuels-counter-guide
apps/docs-snippets2/src/typegend
apps/docs-snippets2/src/**/*.wrapped.ts
apps/docs-snippets2/src/**/*.test.ts
maschad marked this conversation as resolved.
Show resolved Hide resolved

packages/fuels/src/cli/commands/deploy/proxy
packages/fuels/test/fixtures/project
Expand Down
5 changes: 1 addition & 4 deletions apps/docs-snippets2/scripts/test-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ afterAll(() => {
console = { ...consoleBkp };
});

/**
* @group node
* @group browser
maschad marked this conversation as resolved.
Show resolved Hide resolved
*/
// %TEST_ENVIRONMENT%
arboleya marked this conversation as resolved.
Show resolved Hide resolved
test('%NAME%', async () => {
// TEST NODE LAUNCHER ———>>>
// %NODE_LAUNCHER%
Expand Down
15 changes: 15 additions & 0 deletions apps/docs-snippets2/scripts/wrap-snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ const wrapperFnContents = readFileSync(wrapperFnFilepath, 'utf-8');
export const wrapSnippet = (filepath: string) => {
const snippetContents = readFileSync(filepath, 'utf8');

/**
* Test environment
*/
let testEnvironments = '';

// Check if the filepath includes '.node' or '.browser'
if (filepath.includes('.node')) {
testEnvironments = '/**\n * @group node\n */';
} else if (filepath.includes('.browser')) {
testEnvironments = '/**\n * @group browser\n */';
} else {
testEnvironments = '/**\n * @group node\n * @group browser\n */';
}

maschad marked this conversation as resolved.
Show resolved Hide resolved
/*
Filter all imports from file.
*/
Expand Down Expand Up @@ -74,6 +88,7 @@ export const wrapSnippet = (filepath: string) => {
const wrappedSnippet =
// eslintDisableRule +
wrapperFnContents
.replace('// %TEST_ENVIRONMENT%', testEnvironments)
.replace('%IMPORTS%', imports)
.replace('%NAME%', basename(filepath))
.replace('// %SNIPPET%', indented)
Expand Down
52 changes: 52 additions & 0 deletions apps/docs-snippets2/src/create-fuels/decrement-counter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// #region full
import { Wallet, Provider } from 'fuels';

import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env';
import { CounterFactory } from '../typegend/contracts';

// Let's create our provider from the network URL.
const provider = await Provider.create(LOCAL_NETWORK_URL);
// Let's create our wallet from the private key.
const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider);

// Then we can deploy the contract.
const { waitForResult } = await CounterFactory.deploy(wallet);
const { contract } = await waitForResult();

// Lets setup some values to use in our example.
const initialCount = 0;
const incrementedValue = 5;
const decrementedValue = 2;

// We can now call the contract functions and test the results. Lets assert the initial value of the counter.
const { waitForResult: getCountWaitForResult } = await contract.functions.get_count().call();
const { value: initialGetCountValue } = await getCountWaitForResult();
console.assert(
maschad marked this conversation as resolved.
Show resolved Hide resolved
initialGetCountValue.toNumber() === initialCount,
'Initial value should be equal to the initial count.'
);

// Next we'll increment the counter, so that we can decrement it.
const { waitForResult: incWaitForResult } = await contract.functions.increment_count(5).call();
const { value: incValue } = await incWaitForResult();
console.assert(
incValue.toNumber() === incrementedValue,
'Incremented value should be equal to the incremented value.'
);

// Next, we'll decrement the counter by 3 and assert the new value.
const { waitForResult: decWaitForResult } = await contract.functions.decrement_count(3).call();
const { value: decValue } = await decWaitForResult();
console.assert(
decValue.toNumber() === decrementedValue,
'Decremented value should be equal to the decremented value.'
);

// Finally, we'll test the get count function again to ensure parity.
const { waitForResult: finalWaitForResult } = await contract.functions.get_count().call();
const { value: finalValue } = await finalWaitForResult();
console.assert(
finalValue.toNumber() === decrementedValue,
'Final value should be equal to the decremented value.'
);
// #endregion full
115 changes: 115 additions & 0 deletions apps/docs-snippets2/src/encoding/encode-and-decode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// #region full
import type { JsonAbi, TransactionResultReturnDataReceipt } from 'fuels';
import {
buildFunctionResult,
ReceiptType,
arrayify,
Script,
Interface,
Provider,
Wallet,
} from 'fuels';

// #region encode-and-decode-3
import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env';
import { ScriptSum } from '../typegend';

const provider = await Provider.create(LOCAL_NETWORK_URL);
const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider);

// First we need to build out the transaction via the script that we want to encode.
// For that we'll need the ABI and the bytecode of the script
const abi: JsonAbi = ScriptSum.abi;
const bytecode = ScriptSum.bytecode;

// Create the invocation scope for the script call, passing the initial
// value for the configurable constant
const script = new Script(bytecode, abi, wallet);
const initialValue = 10;
script.setConfigurableConstants({ AMOUNT: initialValue });
const invocationScope = script.functions.main(0);

// Create the transaction request, this can be picked off the invocation
// scope so the script bytecode is preset on the transaction
const request = await invocationScope.getTransactionRequest();
// #endregion encode-and-decode-3

// #region encode-and-decode-4

// Now we can encode the argument we want to pass to the function. The argument is required
// as a function parameter for all abi functions and we can extract it from the ABI itself
const argument = abi.functions
.find((f) => f.name === 'main')
?.inputs.find((i) => i.name === 'inputted_amount')?.concreteTypeId as string;

// The `Interface` class (imported from `fuels`) is the entry point for encoding and decoding all things abi-related.
// We will use its `encodeType` method and create the encoding required for
// a u32 which takes 4 bytes up of property space.

const abiInterface = new Interface(abi);
const argumentToAdd = 10;
const encodedArguments = abiInterface.encodeType(argument, [argumentToAdd]);
// Therefore the value of 10 will be encoded to:
// Uint8Array([0, 0, 0, 10]

// The encoded value can now be set on the transaction via the script data property
request.scriptData = encodedArguments;

// Now we can build out the rest of the transaction and then fund it
const txCost = await wallet.getTransactionCost(request);
request.maxFee = txCost.maxFee;
request.gasLimit = txCost.gasUsed;
await wallet.fund(request, txCost);

// Finally, submit the built transaction
const response = await wallet.sendTransaction(request);
await response.waitForResult();
// #endregion encode-and-decode-4

// #region encode-and-decode-5

// Get result of the transaction, including the contract call result. For this we'll need
// the previously created invocation scope, the transaction response and the script
const invocationResult = await buildFunctionResult({
funcScope: invocationScope,
isMultiCall: false,
program: script,
transactionResponse: response,
});

// The decoded value can be destructured from the `FunctionInvocationResult`
const { value } = invocationResult;

// Or we can decode the returned bytes ourselves, by retrieving the return data
// receipt that contains the returned bytes. We can get this by filtering on
// the returned receipt types
const returnDataReceipt = invocationResult.transactionResult.receipts.find(
(r) => r.type === ReceiptType.ReturnData
) as TransactionResultReturnDataReceipt;

// The data is in hex format so it makes sense to use arrayify so that the data
// is more human readable
const returnData = arrayify(returnDataReceipt.data);
// returnData = new Uint8Array([0, 0, 0, 20]

// And now we can decode the returned bytes in a similar fashion to how they were
// encoded, via the `Interface`
const [decodedReturnData] = abiInterface.decodeType(argument, returnData);
// 20

const totalValue = argumentToAdd + initialValue;
// #endregion encode-and-decode-5
// #endregion full
console.assert(value === totalValue, 'Value should be equal to the total value.');
console.assert(
decodedReturnData === totalValue,
'Decoded return data should be equal to the total value.'
);
console.assert(
encodedArguments.toString() === new Uint8Array([0, 0, 0, 10]).toString(),
'Encoded arguments should be equal to the total value.'
);
console.assert(
returnData.toString() === new Uint8Array([0, 0, 0, 20]).toString(),
'Return data should be equal to the total value.'
);
154 changes: 154 additions & 0 deletions apps/docs-snippets2/src/encoding/working-with-bytes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// #region full
import { randomBytes } from 'crypto';
import {
ArrayCoder,
B256Coder,
B512Coder,
BigNumberCoder,
BooleanCoder,
EnumCoder,
NumberCoder,
RawSliceCoder,
StdStringCoder,
StringCoder,
StructCoder,
TupleCoder,
VecCoder,
hexlify,
} from 'fuels';

// #region working-with-bytes-1
const u8Coder = new NumberCoder('u8');
const encodedU8 = u8Coder.encode(255);
console.assert(
encodedU8.toString() === new Uint8Array([255]).toString(),
'Encoded U8 should be equal to 255'
);

const u16Coder = new NumberCoder('u16');
const encodedU16 = u16Coder.encode(255);
console.assert(
encodedU16.toString() === new Uint8Array([0, 255]).toString(),
'Encoded U16 should be equal to 255'
);

const u32Coder = new NumberCoder('u32');
const encodedU32 = u32Coder.encode(255);

console.assert(
encodedU32.toString() === new Uint8Array([0, 0, 0, 255]).toString(),
'Encoded U32 should be equal to 255'
);

const u64Coder = new BigNumberCoder('u64');
const encodedU64 = u64Coder.encode(255);
console.assert(
encodedU64.toString() === new Uint8Array([0, 0, 0, 0, 0, 0, 0, 255]).toString(),
'Encoded U64 should be equal to 255'
);

const u256Coder = new BigNumberCoder('u256');
const encodedU256 = u256Coder.encode(255);
console.assert(
encodedU256.toString() ===
new Uint8Array([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
255,
]).toString(),
maschad marked this conversation as resolved.
Show resolved Hide resolved
'Encoded U256 should be equal to 255'
);
// #endregion working-with-bytes-1

// #region working-with-bytes-2
const booleanCoder = new BooleanCoder();
const encodedTrue = booleanCoder.encode(true);
console.assert(
encodedTrue.toString() === new Uint8Array([1]).toString(),
'Encoded True should be equal to 1'
);

const encodedFalse = booleanCoder.encode(false);
console.assert(
encodedFalse.toString() === new Uint8Array([0]).toString(),
'Encoded False should be equal to 0'
);

// #endregion working-with-bytes-2

// #region working-with-bytes-3
const stringCoder = new StringCoder(5);
const encoded = stringCoder.encode('hello');
// #endregion working-with-bytes-3

console.assert(
encoded.toString() === new Uint8Array([104, 101, 108, 108, 111]).toString(),
'Encoded should be equal to hello'
);

// #region working-with-bytes-4
const b256Coder = new B256Coder();
const encodedB256 = b256Coder.encode(hexlify(randomBytes(32)));
console.assert(encodedB256.length === 32, 'Encoded B256 should be equal to 32');

const b512Coder = new B512Coder();
const encodedB512 = b512Coder.encode(hexlify(randomBytes(64)));
console.assert(encodedB512.length === 64, 'Encoded B512 should be equal to 64');
// #endregion working-with-bytes-4

// #region working-with-bytes-5
const tupleCoder = new TupleCoder([new NumberCoder('u8'), new NumberCoder('u16')]);
const encodedTuple = tupleCoder.encode([255, 255]);
console.assert(
encodedTuple.toString() === new Uint8Array([255, 0, 255]).toString(),
'Encoded Tuple should be equal to [255, 0, 255]'
);

const structCoder = new StructCoder('struct', {
a: new NumberCoder('u8'),
b: new NumberCoder('u16'),
});
const encodedStruct = structCoder.encode({ a: 255, b: 255 });
console.assert(
encodedStruct.toString() === new Uint8Array([255, 0, 255]).toString(),
'Encoded Struct should be equal to [255, 0, 255]'
);

const arrayCoder = new ArrayCoder(new NumberCoder('u8'), 4);
const encodedArray = arrayCoder.encode([255, 0, 255, 0]);
console.assert(
encodedArray.toString() === new Uint8Array([255, 0, 255, 0]).toString(),
'Encoded Array should be equal to [255, 0, 255, 0]'
);

const enumCoder = new EnumCoder('enum', { a: new NumberCoder('u32') });
const encodedEnum = enumCoder.encode({ a: 255 });
console.assert(
encodedEnum.toString() === new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255]).toString(),
'Encoded Enum should be equal to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255]'
);
// #endregion working-with-bytes-5

// #region working-with-bytes-6
const vecCoder = new VecCoder(new NumberCoder('u8'));
const encodedVec = vecCoder.encode([255, 0, 255]);
console.assert(
encodedVec.toString() === new Uint8Array([0, 0, 0, 0, 0, 0, 0, 3, 255, 0, 255]).toString(),
'Encoded Vec should be equal to [0, 0, 0, 0, 0, 0, 0, 3, 255, 0, 255]'
);

const stdStringCoder = new StdStringCoder();
const encodedStdString = stdStringCoder.encode('hello');
console.assert(
encodedStdString.toString() ===
new Uint8Array([0, 0, 0, 0, 0, 0, 0, 5, 104, 101, 108, 108, 111]).toString(),
'Encoded StdString should be equal to [0, 0, 0, 0, 0, 0, 0, 5, 104, 101, 108, 108, 111]'
);

const rawSliceCoder = new RawSliceCoder();
const encodedRawSlice = rawSliceCoder.encode([1, 2, 3, 4]);
console.assert(
encodedRawSlice.toString() === new Uint8Array([0, 0, 0, 0, 0, 0, 0, 4, 1, 2, 3, 4]).toString(),
'Encoded RawSlice should be equal to [0, 0, 0, 0, 0, 0, 0, 4, 1, 2, 3, 4]'
);
// #endregion working-with-bytes-6
// #endregion full
Loading