Skip to content

Commit

Permalink
update futureId and misc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zoeyTM committed Jun 17, 2024
1 parent 9241bae commit 6f5ec1c
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,14 @@ export function resolveFutureData(
return data;
}

// this type coercion is safe because we know the type of data is EncodeFunctionCallFuture
return findResultForFutureById(deploymentState, data.id) as string;
const result = findResultForFutureById(deploymentState, data.id);

assertIgnitionInvariant(
typeof result === "string",
"Expected future data to be a string"
);

return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export interface EncodeFunctionCallExecutionState
artifactId: string;
functionName: string;
args: SolidityParameterType[];
result?: string;
result: string;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/internal/module-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import { assertIgnitionInvariant } from "./utils/assertions";
import {
toCallFutureId,
toContractFutureId,
toEncodeFunctionCallFutureId,
toReadEventArgumentFutureId,
toSendDataFutureId,
} from "./utils/future-id-builders";
Expand Down Expand Up @@ -637,7 +638,7 @@ class IgnitionModuleBuilderImplementation<
);
}

const futureId = toCallFutureId(
const futureId = toEncodeFunctionCallFutureId(
this._module.id,
options.id,
contractFuture.module.id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SendDataFuture } from "../../../types/module";
import { SendDataExecutionState } from "../../execution/types/execution-state";
import { assertIgnitionInvariant } from "../../utils/assertions";
import { findResultForFutureById } from "../../views/find-result-for-future-by-id";
import {
ReconciliationContext,
Expand All @@ -17,10 +18,15 @@ export function reconcileData(
return compare(future, "Data", exState.data, future.data ?? "0x");
}

return compare(
future,
"Data",
exState.data,
findResultForFutureById(context.deploymentState, future.data.id) as string
const newData = findResultForFutureById(
context.deploymentState,
future.data.id
);

assertIgnitionInvariant(
typeof newData === "string",
"Expected data to be a string"
);

return compare(future, "Data", exState.data, newData);
}
36 changes: 35 additions & 1 deletion packages/core/src/internal/utils/future-id-builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function toContractFutureId(
}

/**
* Construct the future id for a call, static call, or encoded function call, namespaced by the moduleId.
* Construct the future id for a call or static call, namespaced by the moduleId.
*
* @param moduleId - the id of the module the future is part of
* @param userProvidedId - the overriding id provided by the user (it will still
Expand Down Expand Up @@ -87,6 +87,40 @@ export function toCallFutureId(
return `${moduleId}${MODULE_SEPERATOR}${submoduleContractId}${SUBKEY_SEPERATOR}${functionName}`;
}

/**
* Construct the future id for an encoded function call, namespaced by the moduleId.
*
* @param moduleId - the id of the module the future is part of
* @param userProvidedId - the overriding id provided by the user (it will still
* be namespaced)
* @param contractName - the contract or library name that forms part of the
* fallback
* @param functionName - the function name that forms part of the fallback
* @returns the future id
*/
export function toEncodeFunctionCallFutureId(
moduleId: string,
userProvidedId: string | undefined,
contractModuleId: string,
contractId: string,
functionName: string
) {
if (userProvidedId !== undefined) {
return `${moduleId}${MODULE_SEPERATOR}${userProvidedId}`;
}

if (moduleId === contractModuleId) {
return `${moduleId}${MODULE_SEPERATOR}encodeFunctionCall(${contractId}${SUBKEY_SEPERATOR}${functionName})`;
}

// We replace the MODULE_SEPARATOR for SUBMODULE_SEPARATOR
const submoduleContractId = `${contractModuleId}${SUBMODULE_SEPARATOR}${contractId.substring(
contractModuleId.length + MODULE_SEPERATOR.length
)}`;

return `${moduleId}${MODULE_SEPERATOR}encodeFunctionCall(${submoduleContractId}${SUBKEY_SEPERATOR}${functionName})`;
}

/**
* Construct the future id for a read event argument future, namespaced by
* the moduleId.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import { DeploymentParameters } from "../../../types/deploy";
import { EncodeFunctionCallFuture } from "../../../types/module";
import { ERRORS } from "../../errors-list";
import { validateArtifactFunction } from "../../execution/abi";
import { retrieveNestedRuntimeValues } from "../utils";
import {
filterToAccountRuntimeValues,
retrieveNestedRuntimeValues,
validateAccountRuntimeValue,
} from "../utils";

export async function validateNamedEncodeFunctionCall(
future: EncodeFunctionCallFuture<string, string>,
artifactLoader: ArtifactResolver,
deploymentParameters: DeploymentParameters,
_accounts: string[]
accounts: string[]
): Promise<string[]> {
const errors: IgnitionError[] = [];

Expand Down Expand Up @@ -47,6 +51,13 @@ export async function validateNamedEncodeFunctionCall(

const runtimeValues = retrieveNestedRuntimeValues(future.args);
const moduleParams = runtimeValues.filter(isModuleParameterRuntimeValue);
const accountParams = [...filterToAccountRuntimeValues(runtimeValues)];

errors.push(
...accountParams.flatMap((arv) =>
validateAccountRuntimeValue(arv, accounts)
)
);

const missingParams = moduleParams.filter(
(param) =>
Expand Down
9 changes: 6 additions & 3 deletions packages/core/src/types/module-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,12 @@ export interface IgnitionModuleBuilder {
): StaticCallFuture<ContractNameT, FunctionNameT>;

/**
* Encode a function call.
* ABI encode a function call, including both the function's name and
* the parameters it is being called with. This is useful when
* sending a raw transaction to invoke a smart contract or
* when invoking a smart contract proxied through an intermediary function.
*
* @param contractFuture - The contract ABI to encode with
* @param contractFuture - The contract that the ABI for encoding will be taken from
* @param functionName - The name of the function
* @param args - The arguments to pass to the function
* @param options - The options for the call
Expand All @@ -398,7 +401,7 @@ export interface IgnitionModuleBuilder {
* ```
* const myContract = m.contract("MyContract");
* const data = m.encodeFunctionCall(myContract, "updateCounter", [100]);
* m.send("callFunctionOnContract", myContract, 0n, data);
* m.send("callUpdateCounter", myContract, 0n, data);
* ```
*/
encodeFunctionCall<
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ m.call(..., { id: "MyUniqueId"})`
);
});

it("should validate a module parameter with a default value that is an AccountRuntimeValue for a negative index", async () => {
it("should not validate a module parameter with a default value that is an AccountRuntimeValue for a negative index", async () => {
const fakerArtifact: Artifact = {
...fakeArtifact,
abi: [
Expand Down
30 changes: 15 additions & 15 deletions packages/core/test/encodeFunctionCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
NamedEncodeFunctionCallFutureImplementation,
} from "../src/internal/module";
import { getFuturesFromModule } from "../src/internal/utils/get-futures-from-module";
import { validateNamedContractCall } from "../src/internal/validation/futures/validateNamedContractCall";
import { validateNamedEncodeFunctionCall } from "../src/internal/validation/futures/validateNamedEncodeFunctionCall";
import { FutureType } from "../src/types/module";

import {
Expand Down Expand Up @@ -74,7 +74,7 @@ describe("encodeFunctionCall", () => {
);

const callFuture = [...moduleWithDependentContracts.futures].find(
({ id }) => id === "Module1#Example.test"
({ id }) => id === "Module1#encodeFunctionCall(Module1#Example.test)"
);

if (!(callFuture instanceof NamedEncodeFunctionCallFutureImplementation)) {
Expand Down Expand Up @@ -107,7 +107,7 @@ describe("encodeFunctionCall", () => {
);

const callFuture = [...moduleWithDependentContracts.futures].find(
({ id }) => id === "Module1#Example.test"
({ id }) => id === "Module1#encodeFunctionCall(Module1#Example.test)"
);

if (!(callFuture instanceof NamedEncodeFunctionCallFutureImplementation)) {
Expand Down Expand Up @@ -318,7 +318,7 @@ describe("encodeFunctionCall", () => {

return { sameContract1 };
}),
`The autogenerated future id ("Module1#SameContract.test") is already used. Please provide a unique id, as shown below:
`The autogenerated future id ("Module1#encodeFunctionCall(Module1#SameContract.test)") is already used. Please provide a unique id, as shown below:
m.encodeFunctionCall(..., { id: "MyUniqueId"})`
);
Expand Down Expand Up @@ -383,7 +383,7 @@ m.encodeFunctionCall(..., { id: "MyUniqueId"})`
);

assertValidationError(
await validateNamedContractCall(
await validateNamedEncodeFunctionCall(
future as any,
setupMockArtifactResolver({ Another: {} as any }),
{},
Expand Down Expand Up @@ -411,7 +411,7 @@ m.encodeFunctionCall(..., { id: "MyUniqueId"})`
);

assertValidationError(
await validateNamedContractCall(
await validateNamedEncodeFunctionCall(
future as any,
setupMockArtifactResolver(),
{},
Expand Down Expand Up @@ -453,7 +453,7 @@ m.encodeFunctionCall(..., { id: "MyUniqueId"})`
);

assertValidationError(
await validateNamedContractCall(
await validateNamedEncodeFunctionCall(
future as any,
setupMockArtifactResolver(),
{},
Expand Down Expand Up @@ -514,7 +514,7 @@ m.encodeFunctionCall(..., { id: "MyUniqueId"})`
);

assertValidationError(
await validateNamedContractCall(
await validateNamedEncodeFunctionCall(
future as any,
setupMockArtifactResolver(),
{},
Expand All @@ -539,7 +539,7 @@ m.encodeFunctionCall(..., { id: "MyUniqueId"})`
);

assertValidationError(
await validateNamedContractCall(
await validateNamedEncodeFunctionCall(
future as any,
setupMockArtifactResolver({ Another: fakeArtifact }),
{},
Expand Down Expand Up @@ -583,7 +583,7 @@ m.encodeFunctionCall(..., { id: "MyUniqueId"})`
);

await assert.isFulfilled(
validateNamedContractCall(
validateNamedEncodeFunctionCall(
future as any,
setupMockArtifactResolver({ Another: fakeArtifact }),
{},
Expand All @@ -609,7 +609,7 @@ m.encodeFunctionCall(..., { id: "MyUniqueId"})`
);

assertValidationError(
await validateNamedContractCall(
await validateNamedEncodeFunctionCall(
future as any,
setupMockArtifactResolver({ Another: fakeArtifact }),
{},
Expand Down Expand Up @@ -655,7 +655,7 @@ m.encodeFunctionCall(..., { id: "MyUniqueId"})`
);

await assert.isFulfilled(
validateNamedContractCall(
validateNamedEncodeFunctionCall(
future as any,
setupMockArtifactResolver({ Another: fakeArtifact }),
{},
Expand Down Expand Up @@ -698,7 +698,7 @@ m.encodeFunctionCall(..., { id: "MyUniqueId"})`
);

await assert.isFulfilled(
validateNamedContractCall(
validateNamedEncodeFunctionCall(
future as any,
setupMockArtifactResolver({ Another: fakeArtifact }),
{},
Expand All @@ -707,7 +707,7 @@ m.encodeFunctionCall(..., { id: "MyUniqueId"})`
);
});

it("should validate a module parameter with a default value that is an AccountRuntimeValue for a negative index", async () => {
it("should not validate a module parameter with a default value that is an AccountRuntimeValue for a negative index", async () => {
const fakerArtifact: Artifact = {
...fakeArtifact,
abi: [
Expand Down Expand Up @@ -741,7 +741,7 @@ m.encodeFunctionCall(..., { id: "MyUniqueId"})`
);

assertValidationError(
await validateNamedContractCall(
await validateNamedEncodeFunctionCall(
future as any,
setupMockArtifactResolver({ Test: fakerArtifact }),
{},
Expand Down
Loading

0 comments on commit 6f5ec1c

Please sign in to comment.