Skip to content

Commit

Permalink
feat(cosmwasm): add subcommand for migration through admin account (#495
Browse files Browse the repository at this point in the history
)
  • Loading branch information
eguajardo authored Jan 14, 2025
1 parent f752faf commit 2e2c713
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
7 changes: 4 additions & 3 deletions cosmwasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ This folder contains deployment scripts for cosmwasm contracts needed for amplif

Deploy each contract. Chain name should match the key of an object in the `chains` section of the config. Chain name should be omitted for contracts that are not chain specific.

`node deploy-contract.js [upload|instantiate|upload-instantiate] -m [mnemonic] -a [path to contract artifacts] -c [contract name] -e [environment] -n <chain name>`
`node deploy-contract.js [upload|instantiate|upload-instantiate|migrate] -m [mnemonic] -a [path to contract artifacts] -c [contract name] -e [environment] -n <chain name>`

Available subcommands:

Expand All @@ -126,6 +126,8 @@ Available subcommands:

- `upload-instantiate`: Both uploads and then instantiates a contract using the code Id that was just created. It doesn't accept `--codeId` nor `--fetchCodeId` options

- `migrate`: Migrates a contract using a new codeId, which is retrieved the same way as `instantiate` subcommand. The migrate message must be provided using the `--msg` option.

Some of the contracts depend on each other and need to be deployed in a specific order. Note the connection router and axelarnet gateway each need to know the other's address, so you need to pass `--instantiate2`, and upload both contract before instatiating them.

Example deployments:
Expand All @@ -146,9 +148,8 @@ Example deployments:
### Constant Address Deployment

To deploy with a constant address using instantiate2, pass the `--instantiate2` flag.
To upload the contract and compute the expected address without instantiating, pass `--instantiate2` and `-u`. This will write the contract address and the code id to the config file.
To upload the contract and compute the expected address without instantiating, pass `--instantiate2` while using the `upload` subcommand. This will write the contract address and the code id to the config file.
A salt can be passed with `-s`. If no salt is passed but a salt is needed for constant address deployment, the contract name will be used as a salt.
Pass `-r` to skip the upload step, and reuse the previous code id (specified in the config).

### Deploying through governance proposals

Expand Down
31 changes: 31 additions & 0 deletions cosmwasm/deploy-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const {
getCodeId,
uploadContract,
instantiateContract,
migrateContract,
} = require('./utils');

const { Command } = require('commander');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
};

Expand Down
18 changes: 15 additions & 3 deletions cosmwasm/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
};
Expand Down Expand Up @@ -887,6 +898,7 @@ module.exports = {
getCodeId,
uploadContract,
instantiateContract,
migrateContract,
fetchCodeIdFromCodeHash,
fetchCodeIdFromContract,
addDefaultInstantiateAddresses,
Expand Down

0 comments on commit 2e2c713

Please sign in to comment.