From 18e210b6165a4571b9e02158b4e6dfbd0a85502f Mon Sep 17 00:00:00 2001 From: Edwin Guajardo Date: Fri, 10 Jan 2025 16:47:52 -0600 Subject: [PATCH] add migrate command --- cosmwasm/deploy-contract.js | 31 +++++++++++++++++++++++++++++++ cosmwasm/utils.js | 18 +++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/cosmwasm/deploy-contract.js b/cosmwasm/deploy-contract.js index 0d4de1a0..05f5251b 100644 --- a/cosmwasm/deploy-contract.js +++ b/cosmwasm/deploy-contract.js @@ -16,6 +16,7 @@ const { getCodeId, uploadContract, instantiateContract, + migrateContract, } = require('./utils'); const { Command } = require('commander'); @@ -69,6 +70,23 @@ const uploadInstantiate = async (client, wallet, config, options) => { await instantiate(client, wallet, config, options); }; +const migrate = async (client, wallet, config, options) => { + const { yes } = options; + const { contractConfig } = getAmplifierContractConfig(config, options); + + const codeId = await getCodeId(client, config, options); + printInfo('Using code id', codeId); + + if (prompt(`Proceed with contract migration on axelar?`, yes)) { + return; + } + + contractConfig.codeId = codeId; + + const { transactionHash } = await migrateContract(client, wallet, config, options); + printInfo('Migration completed. Transaction hash', transactionHash); +}; + const mainProcessor = async (processor, options) => { const { env } = options; const config = loadConfig(env); @@ -127,6 +145,19 @@ const programHandler = () => { instantiate2Options: true, }); + const migrateCmd = program + .command('migrate') + .description('Migrate contract') + .action((options) => { + mainProcessor(migrate, options); + }); + addAmplifierOptions(migrateCmd, { + contractOptions: true, + migrateOptions: true, + codeId: true, + fetchCodeId: true, + }); + program.parse(); }; diff --git a/cosmwasm/utils.js b/cosmwasm/utils.js index 353c1b98..f6f803b7 100644 --- a/cosmwasm/utils.js +++ b/cosmwasm/utils.js @@ -137,14 +137,12 @@ const uploadContract = async (client, wallet, config, options) => { const uploadFee = gasLimit === 'auto' ? 'auto' : calculateFee(gasLimit, GasPrice.fromString(gasPrice)); // uploading through stargate doesn't support defining instantiate permissions - return await client.upload(account.address, wasm, uploadFee); + return client.upload(account.address, wasm, uploadFee); }; const instantiateContract = async (client, wallet, initMsg, config, options) => { const { contractName, salt, instantiate2, chainName, admin } = options; - const [account] = await wallet.getAccounts(); - const { contractConfig } = getAmplifierContractConfig(config, options); const { @@ -171,6 +169,19 @@ const instantiateContract = async (client, wallet, initMsg, config, options) => return contractAddress; }; +const migrateContract = async (client, wallet, config, options) => { + const { msg } = options; + const [account] = await wallet.getAccounts(); + const { contractConfig } = getAmplifierContractConfig(config, options); + + const { + axelar: { gasPrice, gasLimit }, + } = config; + const migrateFee = gasLimit === 'auto' ? 'auto' : calculateFee(gasLimit, GasPrice.fromString(gasPrice)); + + return client.migrate(account.address, contractConfig.address, contractConfig.codeId, JSON.parse(msg), migrateFee); +}; + const validateAddress = (address) => { return isString(address) && isValidCosmosAddress(address); }; @@ -875,6 +886,7 @@ module.exports = { getCodeId, uploadContract, instantiateContract, + migrateContract, fetchCodeIdFromCodeHash, addDefaultInstantiateAddresses, getChainTruncationParams,