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): add support for fetchCodeId option to non-governance scripts #316

Merged
merged 2 commits into from
Jul 29, 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
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
Loading