Skip to content

Commit

Permalink
feat(amplifier): add support for fetchCodeId option to non-governance…
Browse files Browse the repository at this point in the history
… scripts (#316)

Co-authored-by: Talal Ashraf <talal@interoplabs.io>
  • Loading branch information
eguajardo and talalashraf authored Jul 29, 2024
1 parent 78b4174 commit c19b30e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
15 changes: 10 additions & 5 deletions cosmwasm/deploy-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
prepareWallet,
prepareClient,
getChains,
fetchCodeIdFromCodeHash,
uploadContract,
instantiateContract,
makeInstantiateMsg,
Expand All @@ -17,15 +18,15 @@ const {
const { Command, Option } = require('commander');

const upload = (client, wallet, chainName, config, options) => {
const { reuseCodeId, contractName } = options;
const { reuseCodeId, contractName, fetchCodeId } = options;
const {
axelar: {
contracts: { [contractName]: contractConfig },
},
chains: { [chainName]: chainConfig },
} = config;

if (!reuseCodeId || isNil(contractConfig.codeId)) {
if (!fetchCodeId && (!reuseCodeId || isNil(contractConfig.codeId))) {
printInfo('Uploading contract binary');

return uploadContract(client, wallet, config, options)
Expand Down Expand Up @@ -55,16 +56,18 @@ const upload = (client, wallet, chainName, config, options) => {
return Promise.resolve({ wallet, client });
};

const instantiate = (client, wallet, chainName, config, options) => {
const { contractName } = options;
const instantiate = async (client, wallet, chainName, config, options) => {
const { contractName, fetchCodeId } = options;
const {
axelar: {
contracts: { [contractName]: contractConfig },
},
chains: { [chainName]: chainConfig },
} = config;

if (!isNumber(contractConfig.codeId)) {
if (fetchCodeId) {
contractConfig.codeId = await fetchCodeIdFromCodeHash(client, contractConfig);
} else if (!isNumber(contractConfig.codeId)) {
throw new Error('Code Id is not defined');
}

Expand Down Expand Up @@ -137,6 +140,8 @@ const programHandler = () => {
program.addOption(new Option('--aarch64', 'aarch64').env('AARCH64').default(false));
program.addOption(new Option('-y, --yes', 'skip deployment prompt confirmation').env('YES'));

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
26 changes: 2 additions & 24 deletions cosmwasm/submit-proposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
prepareClient,
readWasmFile,
getChains,
fetchCodeIdFromCodeHash,
decodeProposalAttributes,
encodeStoreCodeProposal,
encodeStoreInstantiateProposal,
Expand Down Expand Up @@ -111,29 +112,6 @@ const storeInstantiate = (client, wallet, config, options, chainName) => {
});
};

const fetchAndUpdateCodeId = async (client, contractConfig) => {
if (!contractConfig.storeCodeProposalCodeHash) {
throw new Error('storeCodeProposalCodeHash not found in contract config');
}

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 {
Expand All @@ -148,7 +126,7 @@ const instantiate = async (client, wallet, config, options, chainName) => {
}

if (fetchCodeId) {
await fetchAndUpdateCodeId(client, contractConfig);
contractConfig.codeId = await fetchCodeIdFromCodeHash(client, contractConfig);
} else if (!isNumber(contractConfig.codeId)) {
throw new Error('Code Id is not defined');
}
Expand Down
24 changes: 24 additions & 0 deletions cosmwasm/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,29 @@ const makeInstantiateMsg = (contractName, chainName, config) => {
throw new Error(`${contractName} is not supported.`);
};

const fetchCodeIdFromCodeHash = async (client, contractConfig) => {
if (!contractConfig.storeCodeProposalCodeHash) {
throw new Error('storeCodeProposalCodeHash not found in contract config');
}

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');
}

return codeId;
};

const instantiate2AddressForProposal = (client, contractConfig, { contractName, salt, chainNames, runAs }) => {
return client
.getCodeDetails(contractConfig.codeId)
Expand Down Expand Up @@ -682,6 +705,7 @@ module.exports = {
uploadContract,
instantiateContract,
makeInstantiateMsg,
fetchCodeIdFromCodeHash,
instantiate2AddressForProposal,
decodeProposalAttributes,
encodeStoreCodeProposal,
Expand Down

0 comments on commit c19b30e

Please sign in to comment.