Skip to content

Commit

Permalink
refactor(amplifier): update to async style (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
eguajardo authored Aug 30, 2024
1 parent 31b2f67 commit e80a522
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 209 deletions.
88 changes: 38 additions & 50 deletions cosmwasm/deploy-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
require('dotenv').config();
const { isNil } = require('lodash');

const { instantiate2Address } = require('@cosmjs/cosmwasm-stargate');

const { isNumber, printInfo, loadConfig, saveConfig, prompt, getChainConfig } = require('../common');
const {
prepareWallet,
prepareClient,
fromHex,
getSalt,
getChains,
updateContractConfig,
fetchCodeIdFromCodeHash,
uploadContract,
instantiateContract,
Expand All @@ -17,8 +22,8 @@ const {
const { Command, Option } = require('commander');
const { addAmplifierOptions } = require('./cli-utils');

const upload = (client, wallet, chainName, config, options) => {
const { reuseCodeId, contractName, fetchCodeId } = options;
const upload = async (client, wallet, chainName, config, options) => {
const { reuseCodeId, contractName, fetchCodeId, instantiate2, salt, chainNames } = options;
const {
axelar: {
contracts: { [contractName]: contractConfig },
Expand All @@ -29,31 +34,22 @@ const upload = (client, wallet, chainName, config, options) => {
if (!fetchCodeId && (!reuseCodeId || isNil(contractConfig.codeId))) {
printInfo('Uploading contract binary');

return uploadContract(client, wallet, config, options)
.then(({ address, codeId }) => {
printInfo('Uploaded contract binary');
contractConfig.codeId = codeId;

if (!address) {
return;
}

if (chainConfig) {
contractConfig[chainConfig.axelarId] = {
...contractConfig[chainConfig.axelarId],
address,
};
} else {
contractConfig.address = address;
}

printInfo('Expected contract address', address);
})
.then(() => ({ wallet, client }));
}
const { checksum, codeId } = await uploadContract(client, wallet, config, options);

printInfo('Uploaded contract binary');
contractConfig.codeId = codeId;

if (instantiate2) {
const [account] = await wallet.getAccounts();
const address = instantiate2Address(fromHex(checksum), account.address, getSalt(salt, contractName, chainNames), 'axelar');

printInfo('Skipping upload. Reusing previously uploaded bytecode');
return Promise.resolve({ wallet, client });
updateContractConfig(contractConfig, chainConfig, 'address', address);

printInfo('Expected contract address', address);
}
} else {
printInfo('Skipping upload. Reusing previously uploaded bytecode');
}
};

const instantiate = async (client, wallet, chainName, config, options) => {
Expand All @@ -72,18 +68,11 @@ const instantiate = async (client, wallet, chainName, config, options) => {
}

const initMsg = makeInstantiateMsg(contractName, chainName, config);
return instantiateContract(client, wallet, initMsg, config, options).then((contractAddress) => {
if (chainConfig) {
contractConfig[chainConfig.axelarId] = {
...contractConfig[chainConfig.axelarId],
address: contractAddress,
};
} else {
contractConfig.address = contractAddress;
}
const contractAddress = await instantiateContract(client, wallet, initMsg, config, options);

printInfo(`Instantiated ${chainName === 'none' ? '' : chainName.concat(' ')}${contractName}. Address`, contractAddress);
});
updateContractConfig(contractConfig, chainConfig, 'address', contractAddress);

printInfo(`Instantiated ${chainName === 'none' ? '' : chainName.concat(' ')}${contractName}. Address`, contractAddress);
};

const main = async (options) => {
Expand All @@ -92,19 +81,18 @@ const main = async (options) => {

const chains = getChains(config, options);

await prepareWallet(options)
.then((wallet) => prepareClient(config, wallet))
.then(({ wallet, client }) => upload(client, wallet, chains[0], config, options))
.then(({ wallet, client }) => {
if (uploadOnly || prompt(`Proceed with deployment on axelar?`, yes)) {
return;
}

return chains.reduce((promise, chain) => {
return promise.then(() => instantiate(client, wallet, chain.toLowerCase(), config, options));
}, Promise.resolve());
})
.then(() => saveConfig(config, env));
const wallet = await prepareWallet(options);
const client = await prepareClient(config, wallet);

await upload(client, wallet, chains[0], config, options);

if (!(uploadOnly || prompt(`Proceed with deployment on axelar?`, yes))) {
for (const chain of chains) {
await instantiate(client, wallet, chain.toLowerCase(), config, options);
}
}

saveConfig(config, env);
};

const programHandler = () => {
Expand Down
165 changes: 81 additions & 84 deletions cosmwasm/submit-proposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ require('dotenv').config();

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

const { instantiate2Address } = require('@cosmjs/cosmwasm-stargate');

const {
prepareWallet,
prepareClient,
fromHex,
getSalt,
readWasmFile,
getChains,
updateContractConfig,
fetchCodeIdFromCodeHash,
decodeProposalAttributes,
encodeStoreCodeProposal,
Expand All @@ -18,7 +23,6 @@ const {
encodeExecuteContractProposal,
submitProposal,
makeInstantiateMsg,
instantiate2AddressForProposal,
governanceAddress,
} = require('./utils');
const { isNumber, saveConfig, loadConfig, printInfo, prompt } = require('../common');
Expand All @@ -33,23 +37,15 @@ const {
const { Command, Option } = require('commander');
const { addAmplifierOptions } = require('./cli-utils');

const updateContractConfig = (contractConfig, chainConfig, key, value) => {
if (chainConfig) {
contractConfig[chainConfig.axelarId] = {
...contractConfig[chainConfig.axelarId],
[key]: value,
};
} else {
contractConfig[key] = value;
}
};
const predictAndUpdateAddress = async (client, contractConfig, chainConfig, options) => {
const { contractName, salt, chainNames, runAs } = options;

const predictAndUpdateAddress = (client, contractConfig, chainConfig, options, contractName, chainName) => {
return instantiate2AddressForProposal(client, contractConfig, options).then((contractAddress) => {
updateContractConfig(contractConfig, chainConfig, 'address', contractAddress);
const { checksum } = await client.getCodeDetails(contractConfig.codeId);
const contractAddress = instantiate2Address(fromHex(checksum), runAs, getSalt(salt, contractName, chainNames), 'axelar');

return contractAddress;
});
updateContractConfig(contractConfig, chainConfig, 'address', contractAddress);

return contractAddress;
};

const printProposal = (proposal, proposalType) => {
Expand All @@ -59,7 +55,7 @@ const printProposal = (proposal, proposalType) => {
);
};

const storeCode = (client, wallet, config, options) => {
const storeCode = async (client, wallet, config, options) => {
const { contractName } = options;
const {
axelar: {
Expand All @@ -72,18 +68,17 @@ const storeCode = (client, wallet, config, options) => {
printProposal(proposal, StoreCodeProposal);

if (prompt(`Proceed with proposal submission?`, options.yes)) {
return Promise.resolve();
return;
}

return submitProposal(client, wallet, config, options, proposal).then((proposalId) => {
printInfo('Proposal submitted', proposalId);
const proposalId = await submitProposal(client, wallet, config, options, proposal);
printInfo('Proposal submitted', proposalId);

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

const storeInstantiate = (client, wallet, config, options, chainName) => {
const storeInstantiate = async (client, wallet, config, options, chainName) => {
const { contractName, instantiate2 } = options;
const {
axelar: {
Expand All @@ -102,15 +97,14 @@ const storeInstantiate = (client, wallet, config, options, chainName) => {
printProposal(proposal, StoreAndInstantiateContractProposal);

if (prompt(`Proceed with proposal submission?`, options.yes)) {
return Promise.resolve();
return;
}

return submitProposal(client, wallet, config, options, proposal).then((proposalId) => {
printInfo('Proposal submitted', proposalId);
const proposalId = await submitProposal(client, wallet, config, options, proposal);
printInfo('Proposal submitted', proposalId);

updateContractConfig(contractConfig, chainConfig, 'storeInstantiateProposalId', proposalId);
contractConfig.storeCodeProposalCodeHash = createHash('sha256').update(readWasmFile(options)).digest().toString('hex');
});
updateContractConfig(contractConfig, chainConfig, 'storeInstantiateProposalId', proposalId);
contractConfig.storeCodeProposalCodeHash = createHash('sha256').update(readWasmFile(options)).digest().toString('hex');
};

const instantiate = async (client, wallet, config, options, chainName) => {
Expand All @@ -129,7 +123,7 @@ const instantiate = async (client, wallet, config, options, chainName) => {
}

if (predictOnly) {
return predictAndUpdateAddress(client, contractConfig, chainConfig, options, contractName, chainName);
return predictAndUpdateAddress(client, contractConfig, chainConfig, options);
}

const initMsg = makeInstantiateMsg(contractName, chainName, config);
Expand All @@ -145,21 +139,20 @@ const instantiate = async (client, wallet, config, options, chainName) => {
}

if (prompt(`Proceed with proposal submission?`, options.yes)) {
return Promise.resolve();
return;
}

return submitProposal(client, wallet, config, options, proposal).then((proposalId) => {
printInfo('Proposal submitted', proposalId);
const proposalId = await submitProposal(client, wallet, config, options, proposal);
printInfo('Proposal submitted', proposalId);

updateContractConfig(contractConfig, chainConfig, 'instantiateProposalId', proposalId);
updateContractConfig(contractConfig, chainConfig, 'instantiateProposalId', proposalId);

if (instantiate2) {
return predictAndUpdateAddress(client, contractConfig, chainConfig, options, contractName, chainName);
}
});
if (instantiate2) {
return predictAndUpdateAddress(client, contractConfig, chainConfig, options);
}
};

const execute = (client, wallet, config, options, chainName) => {
const execute = async (client, wallet, config, options, chainName) => {
const { contractName } = options;
const {
axelar: {
Expand All @@ -173,14 +166,13 @@ const execute = (client, wallet, config, options, chainName) => {
printProposal(proposal, ExecuteContractProposal);

if (prompt(`Proceed with proposal submission?`, options.yes)) {
return Promise.resolve();
return;
}

return submitProposal(client, wallet, config, options, proposal).then((proposalId) => {
printInfo('Proposal submitted', proposalId);
const proposalId = await submitProposal(client, wallet, config, options, proposal);
printInfo('Proposal submitted', proposalId);

updateContractConfig(contractConfig, chainConfig, 'executeProposalId', proposalId);
});
updateContractConfig(contractConfig, chainConfig, 'executeProposalId', proposalId);
};

const main = async (options) => {
Expand All @@ -195,53 +187,58 @@ const main = async (options) => {
config.axelar.contracts[contractName] = {};
}

await prepareWallet(options)
.then((wallet) => prepareClient(config, wallet))
.then(({ wallet, client }) => {
switch (proposalType) {
case 'store':
return storeCode(client, wallet, config, options);
const wallet = await prepareWallet(options);
const client = await prepareClient(config, wallet);

case 'storeInstantiate': {
const chains = getChains(config, options);
switch (proposalType) {
case 'store':
await storeCode(client, wallet, config, options);
break;

return chains.reduce((promise, chain) => {
return promise.then(() => storeInstantiate(client, wallet, config, options, chain.toLowerCase()));
}, Promise.resolve());
}
case 'storeInstantiate': {
const chains = getChains(config, options);

case 'instantiate': {
const chains = getChains(config, options);

return chains.reduce((promise, chain) => {
return promise.then(() =>
instantiate(client, wallet, config, options, chain.toLowerCase()).then((contractAddress) => {
if (contractAddress) {
printInfo(
`Predicted address for ${
chain.toLowerCase() === 'none' ? '' : chain.toLowerCase().concat(' ')
}${contractName}. Address`,
contractAddress,
);
}
}),
);
}, Promise.resolve());
}
for (const chain of chains) {
await storeInstantiate(client, wallet, config, options, chain.toLowerCase());
}

break;
}

case 'execute': {
const chains = getChains(config, options);
case 'instantiate': {
const chains = getChains(config, options);

return chains.reduce((promise, chain) => {
return promise.then(() => execute(client, wallet, config, options, chain.toLowerCase()));
}, Promise.resolve());
for (const chain of chains) {
const contractAddress = await instantiate(client, wallet, config, options, chain.toLowerCase());

if (contractAddress) {
printInfo(
`Predicted address for ${
chain.toLowerCase() === 'none' ? '' : chain.toLowerCase().concat(' ')
}${contractName}. Address`,
contractAddress,
);
}
}

break;
}

case 'execute': {
const chains = getChains(config, options);

default:
throw new Error('Invalid proposal type');
for (const chain of chains) {
await execute(client, wallet, config, options, chain.toLowerCase());
}
})
.then(() => saveConfig(config, env));

break;
}

default:
throw new Error('Invalid proposal type');
}

saveConfig(config, env);
};

const programHandler = () => {
Expand Down
Loading

0 comments on commit e80a522

Please sign in to comment.