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

make dispatcher reinitializable + add initilization to upgrade specs #134

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 6 additions & 1 deletion contracts/core/Dispatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ contract Dispatcher is OwnableUpgradeable, UUPSUpgradeable, ReentrancyGuard, IDi
* @dev This method should be called only once during contract deployment.
* @dev For contract upgarades, which need to reinitialize the contract, use the reinitializer modifier.
*/
function initialize(string memory initPortPrefix, IFeeVault _feeVault) public virtual initializer nonReentrant {
function initialize(string memory initPortPrefix, IFeeVault _feeVault)
external
virtual
reinitializer(2)
nonReentrant
{
if (bytes(initPortPrefix).length == 0) {
revert IBCErrors.invalidPortPrefix();
}
Expand Down
12 changes: 1 addition & 11 deletions src/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,8 @@ const getDeployData = (
);
}

// var encodedInitData = "";
let initData = "";

if (init) {
const initArgs = init.args.map((arg: any) => {
return typeof arg === "string" ? renderString(arg, env) : arg;
});
const iFace = new ethers.Interface([`function ${init.signature}`]);
initData = iFace.encodeFunctionData(init.signature, initArgs);
}
return {
args: renderArgs(deployArgs, initData, env),
args: renderArgs(deployArgs, init, env),
libraries: libs,
factory,
contractFactoryConstructor,
Expand Down
2 changes: 1 addition & 1 deletion src/evm/contracts/factories/Dispatcher__factory.ts

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/evm/schemas/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ const TxItemSchema = z.object({
address: z.string().nullish(),
factoryName: z.optional(z.string()),
args: z.optional(z.array(z.any())),
init: z.optional(
z.object({
signature: z.string().min(1),
args: z.array(z.string().min(1)),
})
),
});

type TxItem = z.infer<typeof TxItemSchema>;
Expand Down
5 changes: 3 additions & 2 deletions src/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,15 @@ export async function sendTxToChain(
const deployer = accountRegistry.mustGet(
tx.deployer ? tx.deployer : DEFAULT_DEPLOYER
);
const deployedContractAddress = renderArgs([tx.address], "", env)[0];

const deployedContractAddress = renderArgs([tx.address], tx.init, env)[0];

const ethersContract = new ethers.Contract(
deployedContractAddress,
deployedContractAbi,
deployer
);
const args = renderArgs(tx.args, "", env);
const args = renderArgs(tx.args, tx.init, env);
RnkSngh marked this conversation as resolved.
Show resolved Hide resolved
logger.info(
`calling ${tx.signature} on ${tx.name} @:${deployedContractAddress} with args: \n [${args}]`
);
Expand Down
27 changes: 24 additions & 3 deletions src/utils/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import yargs from "yargs/yargs";
import { hideBin } from "yargs/helpers";
import { AccountRegistry } from "../evm/account";
import { ethers } from "ethers";

export interface StringToStringMap {
[key: string]: string | null | undefined;
Expand Down Expand Up @@ -257,21 +258,41 @@ export async function readFromDeploymentFile(
}
}

const compileInitArgs = (
init: { args: any[]; signature: string } | undefined,
env: StringToStringMap
) => {
if (!init) {
return "";
}
const initArgs = init.args.map((arg: any) => {
return typeof arg === "string" ? renderString(arg, env) : arg;
});
const iFace = new ethers.Interface([`function ${init.signature}`]);
return iFace.encodeFunctionData(init.signature, initArgs);
};
RnkSngh marked this conversation as resolved.
Show resolved Hide resolved

/**
*
* @param args Render the args for the contract deployment through looking them up in environment
* @param init replace initArgs with the init string
* @param env to look up the args in
* @returns
*/
export const renderArgs = (args: any[] | undefined, init: string, env: any) => {
export const renderArgs = (
args: any[] | undefined,
init: { args: any[]; signature: string } | undefined,
env: any
) => {
const initData = compileInitArgs(init, env);

return args
? args.map((arg: any) => {
if (typeof arg !== "string") return arg;
if (arg === "$INITARGS") {
if (init === "")
if (initData === "")
throw new Error(`Found $INITARGS but no args to replace it with.`);
return init;
return initData;
RnkSngh marked this conversation as resolved.
Show resolved Hide resolved
}
return renderString(arg, env);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {IBCErrors} from "../../../contracts/libs/IbcErrors.sol";
* which can be relayed to a rollup module on the Polymerase chain
*/
contract DispatcherV2Initializable is DispatcherV2 {
function initialize(string memory initPortPrefix, IFeeVault _feeVault) public override reinitializer(2) {
function initialize(string memory initPortPrefix, IFeeVault _feeVault) public override reinitializer(3) {
__Ownable_init();
portPrefix = initPortPrefix;
portPrefixLen = uint32(bytes(initPortPrefix).length);
Expand Down
Loading