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

feat: add support for dynamic configurables #3702

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion internal/forc/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.66.6
git:xunilrj/dynamic-types-configurables
23 changes: 10 additions & 13 deletions packages/abi-coder/src/Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { arrayify } from '@fuel-ts/utils';

import { AbiCoder } from './AbiCoder';
import { FunctionFragment } from './FunctionFragment';
import type { DecodedValue, InputValue } from './encoding/coders/AbstractCoder';
import type { Coder, DecodedValue, InputValue } from './encoding/coders/AbstractCoder';
import type { JsonAbiArgument, JsonAbiOld } from './types/JsonAbi';
import type { Configurable, JsonAbi } from './types/JsonAbiNew';
import { type EncodingVersion } from './utils/constants';
Expand Down Expand Up @@ -88,26 +88,23 @@ export class Interface {
});
}

encodeType(concreteTypeId: string, value: InputValue): Uint8Array {
getCoder(concreteTypeId: string): Coder {
const typeArg = parseConcreteType(
this.jsonAbi,
this.jsonAbiOld.types,
concreteTypeId,
''
) as JsonAbiArgument;
return AbiCoder.encode(this.jsonAbiOld, typeArg, value, {
encoding: this.encoding,
});
return AbiCoder.getCoder(this.jsonAbiOld, typeArg, { encoding: this.encoding });
}

decodeType(concreteTypeId: string, data: Uint8Array): [DecodedValue | undefined, number] {
const typeArg = parseConcreteType(
this.jsonAbi,
this.jsonAbiOld.types,
concreteTypeId,
''
) as JsonAbiArgument;
encodeType(concreteTypeId: string, value: InputValue): Uint8Array {
const coder = this.getCoder(concreteTypeId);
return coder.encode(value);
}

return AbiCoder.decode(this.jsonAbiOld, typeArg, data, 0, { encoding: this.encoding });
decodeType(concreteTypeId: string, data: Uint8Array): [DecodedValue | undefined, number] {
const coder = this.getCoder(concreteTypeId);
return coder.decode(data, 0) as [DecodedValue | undefined, number];
}
}
1 change: 1 addition & 0 deletions packages/abi-coder/src/types/JsonAbi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ export interface JsonAbiConfigurable {
name: string;
configurableType: JsonAbiArgument;
offset: number;
indirect?: boolean;
}
1 change: 1 addition & 0 deletions packages/abi-coder/src/types/JsonAbiNew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,5 @@ export interface Configurable {
readonly name: string;
readonly concreteTypeId: string;
readonly offset: number;
readonly indirect?: boolean;
}
1 change: 1 addition & 0 deletions packages/abi-coder/src/utils/transpile-abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export function transpileAbi(abi) {
name: conf.name,
configurableType: parseConcreteType(abi, types, conf.concreteTypeId),
offset: conf.offset,
indirect: conf.indirect ?? false,
}));

// 5. loggedTypes
Expand Down
1 change: 1 addition & 0 deletions packages/abi-typegen/src/types/interfaces/JsonAbi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ export interface JsonAbiConfigurable {
name: string;
configurableType: JsonAbiArgument;
offset: number;
indirect?: boolean;
}
1 change: 1 addition & 0 deletions packages/abi-typegen/src/types/interfaces/JsonAbiNew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,5 @@ export interface Configurable {
readonly name: string;
readonly concreteTypeId: string;
readonly offset: number;
readonly indirect?: boolean;
}
1 change: 1 addition & 0 deletions packages/abi-typegen/src/utils/transpile-abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export function transpileAbi(abi) {
name: conf.name,
configurableType: parseConcreteType(abi, types, conf.concreteTypeId),
offset: conf.offset,
indirect: conf.indirect ?? false,
}));

// 5. loggedTypes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,20 @@ const abi = {
{
"name": "SHOULD_RETURN",
"concreteTypeId": "b760f44fa5965c2474a3b471467a22c43185152129295af588b022ae50b50903",
"offset": 2584
"offset": 2584,
"indirect": false
},
{
"name": "AN_OPTION",
"concreteTypeId": "2da102c46c7263beeed95818cd7bee801716ba8303dddafdcd0f6c9efda4a0f1",
"offset": 2560
"offset": 2560,
"indirect": false
},
{
"name": "A_GENERIC_STRUCT",
"concreteTypeId": "71df88006611ffff852cf617defb70f77adaf507305088cedd41d276c783aab0",
"offset": 2576
"offset": 2576,
"indirect": false
}
]
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ const abi = {
{
"name": "FEE",
"concreteTypeId": "c89951a24c6ca28c13fd1cfdc646b2b656d69e61a92b91023be7eb58eb914b6b",
"offset": 888
"offset": 888,
"indirect": false
},
{
"name": "ADDRESS",
"concreteTypeId": "7c5ee1cecf5f8eacd1284feb5f0bf2bdea533a51e2f0c9aabe9236d335989f3b",
"offset": 856
"offset": 856,
"indirect": false
}
]
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ const abi = {
{
"name": "SHOULD_RETURN",
"concreteTypeId": "b760f44fa5965c2474a3b471467a22c43185152129295af588b022ae50b50903",
"offset": 696
"offset": 696,
"indirect": false
}
]
};
Expand Down
1 change: 1 addition & 0 deletions packages/abi-typegen/test/utils/getNewAbiTypegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export function getNewAbiTypegen(
typeArguments: null,
},
offset: 120,
indirect: false,
},
];

Expand Down
1 change: 1 addition & 0 deletions packages/account/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from './wallet-manager';
export * from './predicate';
export * from './providers';
export * from './connectors';
export { createConfigurables } from './utils/createConfigurables';
export { deployScriptOrPredicate } from './utils/deployScriptOrPredicate';
export {
getBytecodeId,
Expand Down
59 changes: 7 additions & 52 deletions packages/account/src/predicate/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
TransactionRequestLike,
TransactionResponse,
} from '../providers';
import { createConfigurables } from '../utils/createConfigurables';
import { deployScriptOrPredicate } from '../utils/deployScriptOrPredicate';

import { getPredicateRoot } from './utils';
Expand Down Expand Up @@ -191,11 +192,11 @@ export class Predicate<
}

if (configurableConstants && Object.keys(configurableConstants).length) {
predicateBytes = Predicate.setConfigurableConstants(
predicateBytes,
configurableConstants,
abiInterface
);
const configurables = createConfigurables({
bytecode: predicateBytes,
abi: abiInterface,
});
predicateBytes = configurables.set(configurableConstants);
}

return {
Expand Down Expand Up @@ -241,53 +242,6 @@ export class Predicate<
}));
}

/**
* Sets the configurable constants for the predicate.
*
* @param bytes - The bytes of the predicate.
* @param configurableConstants - Configurable constants to be set.
* @param abiInterface - The ABI interface of the predicate.
* @returns The mutated bytes with the configurable constants set.
*/
private static setConfigurableConstants(
bytes: Uint8Array,
configurableConstants: { [name: string]: unknown },
abiInterface: Interface
) {
const mutatedBytes = bytes;

try {
if (Object.keys(abiInterface.configurables).length === 0) {
throw new FuelError(
ErrorCode.INVALID_CONFIGURABLE_CONSTANTS,
'Predicate has no configurable constants to be set'
);
}

Object.entries(configurableConstants).forEach(([key, value]) => {
if (!abiInterface?.configurables[key]) {
throw new FuelError(
ErrorCode.CONFIGURABLE_NOT_FOUND,
`No configurable constant named '${key}' found in the Predicate`
);
}

const { offset } = abiInterface.configurables[key];

const encoded = abiInterface.encodeConfigurable(key, value as InputValue);

mutatedBytes.set(encoded, offset);
});
} catch (err) {
throw new FuelError(
ErrorCode.INVALID_CONFIGURABLE_CONSTANTS,
`Error setting configurable constants: ${(<Error>err).message}.`
);
}

return mutatedBytes;
}

/**
* Returns the index of the witness placeholder that was added to this predicate.
* If no witness placeholder was added, it returns -1.
Expand Down Expand Up @@ -347,6 +301,7 @@ export class Predicate<
abi: newAbi,
provider: this.provider,
data: this.predicateData,
configurableConstants: this.configurableConstants,
}) as T,
});
}
Expand Down
Loading