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(amplifier): allow to automatically fetch the code id from the network when submitting an instantiation proposal #308

Merged
merged 3 commits into from
Jul 23, 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
22 changes: 12 additions & 10 deletions cosmwasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,22 @@ Prerequisites: Submit a proposal to upload the bytecode as described in the prev
Example usage:

```
node cosmwasm/submit-proposal.js --proposalType instantiate -c ServiceRegistry -t "Proposal title" -d "Proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000
node cosmwasm/submit-proposal.js --proposalType instantiate -c ServiceRegistry -t "Proposal title" -d "Proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --fetchCodeId
```

Use the option `--fetchCodeId` to retrieve and update the code id from the network by comparing the code hash of the uploaded bytecode with the code hash submitted through the store code proposal mentioned in the previous section.

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.

Order of execution to satisfy dependencies:
1. `node cosmwasm/submit-proposal.js --proposalType instantiate -c Router -t "Router roposal title" -d "Router proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --instantiate2 --predictOnly`
2. `node cosmwasm/submit-proposal.js --proposalType instantiate -c NexusGateway -t "NexusGateway roposal title" -d "NexusGateway proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --instantiate2 --predictOnly`
3. `node cosmwasm/submit-proposal.js --proposalType instantiate -c NexusGateway -t "NexusGateway roposal title" -d "NexusGateway proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --instantiate2 -y`
4. `node cosmwasm/submit-proposal.js --proposalType instantiate -c Router -t "Router roposal title" -d "Router proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --instantiate2 -y`
5. `node cosmwasm/submit-proposal.js --proposalType instantiate -c ServiceRegistry -t "ServiceRegistry roposal title" -d "ServiceRegistry proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --instantiate2 -y`
6. `node cosmwasm/submit-proposal.js --proposalType instantiate -c Rewards -t "Rewards roposal title" -d "Rewards proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --instantiate2 -y`
7. `node cosmwasm/submit-proposal.js --proposalType instantiate -c Coordinator -t "Coordinator roposal title" -d "Coordinator proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --instantiate2 -y`
8. `node cosmwasm/submit-proposal.js --proposalType instantiate -c Multisig -t "Multisig roposal title" -d "Multisig proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --instantiate2 -y`
9. `node cosmwasm/submit-proposal.js --proposalType instantiate -c VotingVerifier -t "VotingVerifier roposal title" -d "VotingVerifier proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --instantiate2 -y -n "avalanche"`
10. `node cosmwasm/submit-proposal.js --proposalType instantiate -c Gateway -t "Gateway roposal title" -d "Gateway proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --instantiate2 -y -n "avalanche"`
11. `node cosmwasm/submit-proposal.js --proposalType instantiate -c MultisigProver -t "MultisigProver roposal title" -d "MultisigProver proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --instantiate2 -y -n "avalanche"`
3. `node cosmwasm/submit-proposal.js --proposalType instantiate -c NexusGateway -t "NexusGateway roposal title" -d "NexusGateway proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --instantiate2 --fetchCodeId -y`
4. `node cosmwasm/submit-proposal.js --proposalType instantiate -c Router -t "Router roposal title" -d "Router proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --instantiate2 --fetchCodeId -y`
5. `node cosmwasm/submit-proposal.js --proposalType instantiate -c ServiceRegistry -t "ServiceRegistry roposal title" -d "ServiceRegistry proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --instantiate2 --fetchCodeId -y`
6. `node cosmwasm/submit-proposal.js --proposalType instantiate -c Rewards -t "Rewards roposal title" -d "Rewards proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --instantiate2 --fetchCodeId -y`
7. `node cosmwasm/submit-proposal.js --proposalType instantiate -c Coordinator -t "Coordinator roposal title" -d "Coordinator proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --instantiate2 --fetchCodeId -y`
8. `node cosmwasm/submit-proposal.js --proposalType instantiate -c Multisig -t "Multisig roposal title" -d "Multisig proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --instantiate2 --fetchCodeId -y`
9. `node cosmwasm/submit-proposal.js --proposalType instantiate -c VotingVerifier -t "VotingVerifier roposal title" -d "VotingVerifier proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --instantiate2 --fetchCodeId -y -n "avalanche"`
10. `node cosmwasm/submit-proposal.js --proposalType instantiate -c Gateway -t "Gateway roposal title" -d "Gateway proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --instantiate2 --fetchCodeId -y -n "avalanche"`
11. `node cosmwasm/submit-proposal.js --proposalType instantiate -c MultisigProver -t "MultisigProver roposal title" -d "MultisigProver proposal description" -r $RUN_AS_ACCOUNT --deposit 100000000 --instantiate2 --fetchCodeId -y -n "avalanche"`
33 changes: 31 additions & 2 deletions cosmwasm/submit-proposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

require('dotenv').config();

const { createHash } = require('crypto');

const {
prepareWallet,
prepareClient,
readWasmFile,
getChains,
decodeProposalAttributes,
encodeStoreCodeProposal,
Expand Down Expand Up @@ -66,11 +69,31 @@ const storeCode = (client, wallet, config, options) => {
printInfo('Proposal submitted', proposalId);

contractConfig.storeCodeProposalId = proposalId;
contractConfig.storeCodeProposalCodeHash = createHash('sha256').update(readWasmFile(options)).digest().toString('hex');
});
};

const instantiate = (client, wallet, config, options, chainName) => {
const { contractName, instantiate2, predictOnly } = options;
const fetchAndUpdateCodeId = async (client, contractConfig) => {
const codes = await client.getCodes(); // TODO: create custom function to retrieve codes more efficiently and with pagination
let codeId;

// most likely to be near the end, so we iterate backwards. We also get the latest if there are multiple
for (let i = codes.length - 1; i >= 0; i--) {
if (codes[i].checksum.toUpperCase() === contractConfig.storeCodeProposalCodeHash.toUpperCase()) {
codeId = codes[i].id;
break;
}
}

if (!codeId) {
throw new Error('codeId not found on network for the given codeHash');
}

contractConfig.codeId = codeId;
};

const instantiate = async (client, wallet, config, options, chainName) => {
const { contractName, instantiate2, predictOnly, fetchCodeId } = options;
const {
axelar: {
contracts: { [contractName]: contractConfig },
Expand All @@ -82,6 +105,10 @@ const instantiate = (client, wallet, config, options, chainName) => {
return predictAndUpdateAddress(client, contractConfig, chainConfig, options, contractName, chainName);
}

if (fetchCodeId) {
await fetchAndUpdateCodeId(client, contractConfig);
}

const initMsg = makeInstantiateMsg(contractName, chainName, config);

let proposal;
Expand Down Expand Up @@ -190,6 +217,8 @@ const programHandler = () => {
new Option('-i, --instantiateAddresses <instantiateAddresses>', 'comma separated list of addresses allowed to instantiate'),
);

program.addOption(new Option('--fetchCodeId', 'fetch code id from the chain by comparing to the uploaded code hash'));

program.action((options) => {
main(options);
});
Expand Down
14 changes: 9 additions & 5 deletions cosmwasm/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ const calculateDomainSeparator = (chain, router, network) => keccak256(Buffer.fr

const getSalt = (salt, contractName, chainNames) => fromHex(getSaltFromKey(salt || contractName.concat(chainNames)));

const readWasmFile = ({ artifactPath, contractName, aarch64 }) =>
readFileSync(`${artifactPath}/${pascalToSnake(contractName)}${aarch64 ? '-aarch64' : ''}.wasm`);

const getChains = (config, { chainNames, instantiate2 }) => {
let chains = chainNames.split(',').map((str) => str.trim());

Expand All @@ -65,11 +68,11 @@ const getChains = (config, { chainNames, instantiate2 }) => {
};

const uploadContract = async (client, wallet, config, options) => {
const { artifactPath, contractName, instantiate2, salt, aarch64, chainNames } = options;
const { contractName, instantiate2, salt, chainNames } = options;
return wallet
.getAccounts()
.then(([account]) => {
const wasm = readFileSync(`${artifactPath}/${pascalToSnake(contractName)}${aarch64 ? '-aarch64' : ''}.wasm`);
const wasm = readWasmFile(options);
const {
axelar: { gasPrice, gasLimit },
} = config;
Expand Down Expand Up @@ -507,9 +510,9 @@ const getSubmitProposalParams = (options) => {
};

const getStoreCodeParams = (options) => {
const { artifactPath, contractName, aarch64, source, builder, instantiateAddresses } = options;
const { source, builder, instantiateAddresses } = options;

const wasm = readFileSync(`${artifactPath}/${pascalToSnake(contractName)}${aarch64 ? '-aarch64' : ''}.wasm`);
const wasm = readWasmFile(options);

let codeHash;

Expand Down Expand Up @@ -540,7 +543,7 @@ const getInstantiateContractParams = (config, options, msg) => {
return {
...getSubmitProposalParams(options),
admin,
codeId: contractConfig.codeId, // TODO: get codeId from previous proposal
codeId: contractConfig.codeId,
label: contractName,
msg: Buffer.from(JSON.stringify(msg)),
};
Expand Down Expand Up @@ -629,6 +632,7 @@ module.exports = {
prepareWallet,
prepareClient,
calculateDomainSeparator,
readWasmFile,
getChains,
uploadContract,
instantiateContract,
Expand Down
Loading