Skip to content

Commit 5ba6ade

Browse files
author
Cameron Manavian
authored
feat: add main arg support to Scripts (#745)
* add test files and refactor * update export * add helpers * add and update tests * Update docs * add cs * Code review tweaks * add tx params * add test for failure * refactor more like predicates * revise md * adjust default export * revert change * rm unused * rename * fix type * fix export * improve types * improve types * refactor * refactor * make more strict
1 parent f65ca86 commit 5ba6ade

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1012
-309
lines changed

.changeset/rotten-dogs-pump.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@fuel-ts/abi-coder": minor
3+
"@fuel-ts/contract": minor
4+
"fuels": minor
5+
"@fuel-ts/providers": minor
6+
"@fuel-ts/script": minor
7+
---
8+
9+
Add support for main args in scripts

docs/_guide/scripts/calling-a-script.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
# Calling a script
44

5-
We will use the `script` instance created in the [previous section](./instantiating-a-script.md) to call the script.
5+
Suppose your Sway script `main` function is written using the arguments passed to the `main` function like so:
66

7-
[@code:typescript](./packages/script/src/script.test.ts#typedoc:script-call)
7+
[@code:rust](./packages/fuel-gauge/test-projects/script-main-args/src/main.sw#typedoc:script-with-main-args)
8+
9+
You can still hand code out a solution wrapper using `callScript` utility to call your script with data. However, if you prefer to use the ABI generated from your script, you can use the `ScriptFactory` helper:
10+
11+
[@code:typescript](./packages/fuel-gauge/src/script-main-args.test.ts#typedoc:script-call-factory)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"@changesets/changelog-github": "^0.4.7",
4545
"@changesets/cli": "^2.25.0",
4646
"@ethersproject/bytes": "^5.7.0",
47-
"@jest/types": "28.1.0",
47+
"@jest/types": "29.4.3",
4848
"@types/jest": "^29.2.3",
4949
"@types/node": "^14.18.32",
5050
"@types/node-fetch": "^2.6.2",

packages/abi-coder/src/abi-coder.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ export default class AbiCoder {
169169
types: types.length,
170170
nonEmptyTypes: nonEmptyTypes.length,
171171
values: bytes.length,
172+
newOffset,
172173
},
173174
value: {
174175
types,

packages/abi-coder/src/interface.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ export default class Interface {
129129
encodeFunctionData(
130130
functionFragment: FunctionFragment | string,
131131
values: Array<InputValue>,
132-
offset = 0
132+
offset = 0,
133+
isMainArgs = false
133134
): Uint8Array {
134135
const fragment =
135136
typeof functionFragment === 'string' ? this.getFunction(functionFragment) : functionFragment;
@@ -145,8 +146,12 @@ export default class Interface {
145146
return selector;
146147
}
147148

148-
const isRef = inputs.length > 1 || isReferenceType(inputs[0].type);
149149
const args = this.abiCoder.encode(inputs, values, offset);
150+
if (isMainArgs) {
151+
return args;
152+
}
153+
154+
const isRef = inputs.length > 1 || isReferenceType(inputs[0].type);
150155
return concat([selector, new BooleanCoder().encode(isRef), args]);
151156
}
152157

packages/contract/package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,10 @@
3131
"@ethersproject/logger": "^5.7.0",
3232
"@ethersproject/sha2": "^5.7.0",
3333
"@fuel-ts/abi-coder": "workspace:*",
34-
"@fuel-ts/address": "workspace:*",
35-
"@fuel-ts/math": "workspace:*",
36-
"@fuel-ts/interfaces": "workspace:*",
3734
"@fuel-ts/keystore": "workspace:*",
3835
"@fuel-ts/merkle": "workspace:*",
36+
"@fuel-ts/program": "workspace:*",
3937
"@fuel-ts/providers": "workspace:*",
40-
"@fuel-ts/script": "workspace:*",
4138
"@fuel-ts/sparsemerkle": "workspace:*",
4239
"@fuel-ts/wallet": "workspace:*",
4340
"@fuel-ts/transactions": "workspace:*",

packages/contract/src/contracts/contract-factory.ts renamed to packages/contract/src/contract-factory.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ import { Logger } from '@ethersproject/logger';
44
import { Interface } from '@fuel-ts/abi-coder';
55
import type { JsonAbi } from '@fuel-ts/abi-coder';
66
import { randomBytes } from '@fuel-ts/keystore';
7+
import { Contract } from '@fuel-ts/program';
78
import type { CreateTransactionRequestLike, Provider } from '@fuel-ts/providers';
89
import { CreateTransactionRequest } from '@fuel-ts/providers';
910
import type { StorageSlot } from '@fuel-ts/transactions';
1011
import { MAX_GAS_PER_TX } from '@fuel-ts/transactions';
1112
import { versions } from '@fuel-ts/versions';
1213
import type { Account } from '@fuel-ts/wallet';
1314

14-
import { getContractId, getContractStorageRoot, includeHexPrefix } from '../util';
15-
16-
import Contract from './contract';
15+
import { getContractId, getContractStorageRoot, includeHexPrefix } from './util';
1716

1817
const logger = new Logger(versions.FUELS);
1918

packages/contract/src/index.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,2 @@
1-
export * from './types';
2-
export { default as ContractFactory } from './contracts/contract-factory';
3-
export { default as Contract } from './contracts/contract';
4-
export { FunctionInvocationScope } from './contracts/functions/invocation-scope';
5-
export { MultiCallInvocationScope } from './contracts/functions/multicall-scope';
6-
export {
7-
InvocationResult,
8-
FunctionInvocationResult,
9-
} from './contracts/functions/invocation-results';
1+
export { default as ContractFactory } from './contract-factory';
102
export * as ContractUtils from './util';

packages/contract/src/util.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,6 @@ export const getContractId = (
3737
return contractId;
3838
};
3939

40-
/**
41-
* Generic assert function to avoid undesirable errors
42-
*/
43-
export function assert(condition: unknown, message: string): asserts condition {
44-
if (!condition) {
45-
throw new Error(message);
46-
}
47-
}
48-
4940
export const includeHexPrefix = (value: string, options?: DataOptions) =>
5041
hexlify(value, {
5142
...options,

packages/fuel-gauge/src/contract-factory.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ describe('Contract Factory', () => {
8585
it('Creates a factory from inputs that can prepare call data', async () => {
8686
const factory = await createContractFactory();
8787

88-
const contact = await factory.deployContract();
88+
const contract = await factory.deployContract();
8989

90-
const prepared = contact.functions.increment_counter(1).getCallConfig();
90+
const prepared = contract.functions.increment_counter(1).getCallConfig();
9191
expect(prepared).toEqual({
92-
contract: expect.objectContaining({ id: contact.id }),
92+
program: expect.objectContaining({ id: contract.id }),
9393
func: expect.objectContaining({ name: 'increment_counter' }),
9494
args: [1],
9595
bytesOffset: 720,

0 commit comments

Comments
 (0)