Skip to content

Commit

Permalink
feat(cosmwasm): add clarification to address prediction using governa…
Browse files Browse the repository at this point in the history
…nce proposal (#451)
  • Loading branch information
eguajardo authored Nov 26, 2024
1 parent b41ffe0 commit 4fc3d8c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
4 changes: 3 additions & 1 deletion cosmwasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ Use the option `--fetchCodeId` to retrieve and update the code id from the netwo

Note: The rules for chain name specification and the use of `--instantiate2` as described in the "Deploy the contracts" and "Constant Address Deployment" sections above also apply when instantiating through governance. Refer to those sections for details on omitting chain names for certain contracts and using `--instantiate2` for address prediction.

Since the instantiation is not executed until the porposal passes, the contract address cannot be known in advance and therefore it cannot be saved in the config, unless the address is predicted using the `--instantiate2` flag.

Order of execution to satisfy dependencies:

1. `node cosmwasm/submit-proposal.js instantiate -c Router -t "Router roposal title" -d "Router proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --instantiate2 --predictOnly`
Expand All @@ -194,7 +196,7 @@ Order of execution to satisfy dependencies:

The command `storeInstantiate` from the `submit-proposal` script, allows uploading and instantiating in one step. However, there are a couple of caveats to be aware of:

1. There is no support for `instantiate2` in this proposal type. This means that the contract address will not be known until the proposal is executed.
1. There is no support for `instantiate2` using this proposal type. This means that the contract address will not be known until the proposal is executed and therefore it cannot be saved in the config.

2. Since governance proposals are executed asynchronously, both the codeId and contract address are not immediately available. Querying the network for the correct values could be tricky if multiple proposals are executed together.

Expand Down
22 changes: 14 additions & 8 deletions cosmwasm/submit-proposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ const { ParameterChangeProposal } = require('cosmjs-types/cosmos/params/v1beta1/
const { Command } = require('commander');
const { addAmplifierOptions } = require('./cli-utils');

const predictAndUpdateAddress = async (client, contractConfig, options) => {
const predictAddress = async (client, contractConfig, options) => {
const { contractName, salt, chainName, runAs } = options;

const { checksum } = await client.getCodeDetails(contractConfig.codeId);
const contractAddress = instantiate2Address(fromHex(checksum), runAs, getSalt(salt, contractName, chainName), 'axelar');

contractConfig.address = contractAddress;

printInfo(`Predicted address for ${chainName ? chainName.concat(' ') : ''}${contractName}. Address`, contractAddress);

return contractAddress;
};

const printProposal = (proposal, proposalType) => {
Expand Down Expand Up @@ -119,8 +119,13 @@ const instantiate = async (client, wallet, config, options) => {

await updateCodeId(client, config, options);

let contractAddress;

if (predictOnly) {
return predictAndUpdateAddress(client, contractConfig, options);
contractAddress = await predictAddress(client, contractConfig, options);
contractConfig.address = contractAddress;

return;
}

const initMsg = makeInstantiateMsg(contractName, chainName, config);
Expand All @@ -131,9 +136,13 @@ const instantiate = async (client, wallet, config, options) => {
if (instantiate2) {
proposal = encodeInstantiate2Proposal(config, options, initMsg);
proposalType = InstantiateContract2Proposal;

contractAddress = await predictAddress(client, contractConfig, options);
} else {
proposal = encodeInstantiateProposal(config, options, initMsg);
proposalType = InstantiateContractProposal;

printInfo('Contract address cannot be predicted without using `--instantiate2` flag, address will not be saved in the config');
}

if (!confirmProposalSubmission(options, proposal, proposalType)) {
Expand All @@ -143,10 +152,7 @@ const instantiate = async (client, wallet, config, options) => {
const proposalId = await callSubmitProposal(client, wallet, config, options, proposal);

contractConfig.instantiateProposalId = proposalId;

if (instantiate2) {
return predictAndUpdateAddress(client, contractConfig, options);
}
if (instantiate2) contractConfig.address = contractAddress;
};

const execute = async (client, wallet, config, options) => {
Expand Down

0 comments on commit 4fc3d8c

Please sign in to comment.