Skip to content

Commit

Permalink
feat!: refactor contract calls and multicall to JS + fuel-asm (#1164)
Browse files Browse the repository at this point in the history
* Adding WASM integration for `fuel-asm`

* Purging local asm packages and experiments

* Setting up temp env using local ASM package

* Removing test exclusivity

* Swapping local symlinks by published packages

* Re-generating changeset

* replay install

* replay

* replay

* adjust

* improve

* properly encode contract

* order

* add doc

* tweak

* export

* revert

* revise

* pass contract context

* cleanup

* handle pointer data

* refactor for vector data offset

* lint

* purge multicall Sway refs

* rm refs

* add large list multicall

* allow plain array or object for backwards compat

* filter non contract call receipts

* fix multicall issue

* lower gas cost

* fix typo

* adjust expected

* typo

* fix receipt filtering

* add back

* convert to be a number

* fix for normal script offset

* Update packages/program/src/contract-call-script.ts

Co-authored-by: Dhaiwat <[email protected]>

---------

Co-authored-by: anderson <[email protected]>
Co-authored-by: Dhaiwat <[email protected]>
  • Loading branch information
3 people committed Aug 12, 2023
1 parent abcc11c commit 1422e84
Show file tree
Hide file tree
Showing 29 changed files with 5,715 additions and 12,188 deletions.
8 changes: 8 additions & 0 deletions .changeset/big-onions-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"demo-nextjs": patch
"demo-react-cra": patch
"demo-react-vite": patch
"@fuel-ts/wallet": patch
---

Adding WASM integration for `@fuels/vm-asm`
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ apps/demo-react-cra
apps/demo-react-vite

packages/providers/src/__generated__
packages/program/src/multicall/static-out

out
CHANGELOG.md
Expand Down
2 changes: 0 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ apps/demo-react-vite
apps/demo-typegen/src/generated-types
apps/docs/.vitepress/cache/

/packages/program/src/multicall/static-out

__generated__
out
CHANGELOG.md
Expand Down
2 changes: 1 addition & 1 deletion apps/demo-react-cra/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from "react";
import logo from "./logo.svg";
import "./App.css";
import * as asm from "@fuels/vm-asm";
import { BaseAssetId, encrypt, decrypt } from "fuels";
import * as asm from "@fuels/vm-asm";

function App() {
const { log } = console;
Expand Down
6 changes: 3 additions & 3 deletions apps/docs-snippets/src/guide/contracts/multicalls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe(__filename, () => {
contextContract = await factory3.deployContract();
});

it('should successfully submit multiple calls from the same contract fuction', async () => {
it('should successfully submit multiple calls from the same contract function', async () => {
// #region multicall-1
const { value: results } = await counterContract
.multiCall([
Expand All @@ -56,7 +56,7 @@ describe(__filename, () => {
// #endregion multicall-1
});

it('should successfully submit multiple calls from different contracts fuctions', async () => {
it('should successfully submit multiple calls from different contracts functions', async () => {
// #region multicall-2
const chain = echoContract.multiCall([
echoContract.functions.echo_u8(17),
Expand All @@ -75,7 +75,7 @@ describe(__filename, () => {
// #endregion multicall-2
});

it('should successfully submit multiple calls from different contracts fuctions', async () => {
it('should successfully submit multiple calls from different contracts functions', async () => {
// #region multicall-3
const { value: results } = await contextContract
.multiCall([
Expand Down
3 changes: 1 addition & 2 deletions nodemon.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"**/Forc.lock",
"**/getSupportedVersions.ts",
"**/out/debug/**",
"apps/demo-typegen/src/generated-types/**",
"packages/program/src/multicall/static-out/**"
"apps/demo-typegen/src/generated-types/**"
]
}
2 changes: 1 addition & 1 deletion packages/fuel-gauge/src/contract-factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('Contract Factory', () => {
program: expect.objectContaining({ id: contract.id }),
func: expect.objectContaining({ name: 'increment_counter' }),
args: [1],
bytesOffset: 760,
bytesOffset: 0,
callParameters: undefined,
txParameters: undefined,
forward: undefined,
Expand Down
61 changes: 54 additions & 7 deletions packages/fuel-gauge/src/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,54 @@ describe('Contract', () => {
expect(JSON.stringify(results)).toEqual(JSON.stringify([bn(1337), bn(1337)]));
});

it('submits multiple calls, six calls', async () => {
const contract = await setupContract();

const { value: results } = await contract
.multiCall([
contract.functions.foo(1336),
contract.functions.foo(1336),
contract.functions.foo(1336),
contract.functions.foo(1336),

contract.functions.foo(1336),
contract.functions.foo(1336),
])
.call();
expect(JSON.stringify(results)).toEqual(
JSON.stringify([bn(1337), bn(1337), bn(1337), bn(1337), bn(1337), bn(1337)])
);
});

it('submits multiple calls, eight calls', async () => {
const contract = await setupContract();

const { value: results } = await contract
.multiCall([
contract.functions.foo(1336),
contract.functions.foo(1336),
contract.functions.foo(1336),
contract.functions.foo(1336),
contract.functions.foo(1336),
contract.functions.foo(1336),
contract.functions.foo(1336),
contract.functions.foo(1336),
])
.call();
expect(JSON.stringify(results)).toEqual(
JSON.stringify([
bn(1337),
bn(1337),
bn(1337),
bn(1337),
bn(1337),
bn(1337),
bn(1337),
bn(1337),
])
);
});

it('should fail to execute multiple calls if gasLimit is too low', async () => {
const contract = await setupContract();

Expand Down Expand Up @@ -421,13 +469,12 @@ describe('Contract', () => {
})
.call<[BN, BN]>();

// Allow values to be off by 2% since we don't have exact values
const allowedError = 0.02;
const minThreshold = 0.019;

expect(value[0].toNumber()).toBeGreaterThanOrEqual(500_000 * allowedError);
expect(value[0].toNumber()).toBeGreaterThanOrEqual(500_000 * minThreshold);
expect(value[0].toNumber()).toBeLessThanOrEqual(500_000);

expect(value[1].toNumber()).toBeGreaterThanOrEqual(1_000_000 * allowedError);
expect(value[1].toNumber()).toBeGreaterThanOrEqual(1_000_000 * minThreshold);
expect(value[1].toNumber()).toBeLessThanOrEqual(1_000_000);
});

Expand All @@ -446,7 +493,7 @@ describe('Contract', () => {

expect(toNumber(transactionCost.gasPrice)).toBe(0);
expect(toNumber(transactionCost.fee)).toBeGreaterThanOrEqual(0);
expect(toNumber(transactionCost.gasUsed)).toBeGreaterThan(1000);
expect(toNumber(transactionCost.gasUsed)).toBeGreaterThan(700);

const { value } = await invocationScope
.txParams({
Expand Down Expand Up @@ -478,7 +525,7 @@ describe('Contract', () => {

expect(toNumber(transactionCost.gasPrice)).toBe(1);
expect(toNumber(transactionCost.fee)).toBeGreaterThanOrEqual(1);
expect(toNumber(transactionCost.gasUsed)).toBeGreaterThan(1000);
expect(toNumber(transactionCost.gasUsed)).toBeGreaterThan(700);

// Test that gasUsed is correctly calculated
// and can be used as gasLimit
Expand Down Expand Up @@ -511,7 +558,7 @@ describe('Contract', () => {

expect(toNumber(transactionCost.gasPrice)).toBe(2);
expect(toNumber(transactionCost.fee)).toBeGreaterThanOrEqual(2);
expect(toNumber(transactionCost.gasUsed)).toBeGreaterThan(1000);
expect(toNumber(transactionCost.gasUsed)).toBeGreaterThan(700);

// Test that gasUsed is correctly calculated
// and can be used as gasLimit
Expand Down
4 changes: 2 additions & 2 deletions packages/program/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"dist"
],
"scripts": {
"prebuild": "sh ./scripts/build-multicall.sh",
"build": "tsup",
"postbuild": "tsx ../../scripts/postbuild.ts"
},
Expand All @@ -44,7 +43,8 @@
"@fuel-ts/providers": "workspace:*",
"@fuel-ts/transactions": "workspace:*",
"@fuel-ts/versions": "workspace:*",
"@fuel-ts/wallet": "workspace:*"
"@fuel-ts/wallet": "workspace:*",
"@fuels/vm-asm": "0.36.0"
},
"devDependencies": {
"@fuel-ts/forc": "workspace:*"
Expand Down
16 changes: 0 additions & 16 deletions packages/program/scripts/build-multicall.sh

This file was deleted.

11 changes: 0 additions & 11 deletions packages/program/scripts/process-multicall.ts

This file was deleted.

Loading

0 comments on commit 1422e84

Please sign in to comment.