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

refactor(amplifier): update to async style #351

Merged
merged 5 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
96 changes: 47 additions & 49 deletions cosmwasm/deploy-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const {
const { Command, Option } = require('commander');
const { addAmplifierOptions } = require('./cli-utils');

const upload = (client, wallet, chainName, config, options) => {
const upload = async (client, wallet, chainName, config, options) => {
const { reuseCodeId, contractName, fetchCodeId } = options;
const {
axelar: {
Expand All @@ -29,31 +29,28 @@ const upload = (client, wallet, chainName, config, options) => {
if (!fetchCodeId && (!reuseCodeId || isNil(contractConfig.codeId))) {
eguajardo marked this conversation as resolved.
Show resolved Hide resolved
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 { address, codeId } = await uploadContract(client, wallet, config, options);

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

if (!address) {
eguajardo marked this conversation as resolved.
Show resolved Hide resolved
return;
}

printInfo('Skipping upload. Reusing previously uploaded bytecode');
return Promise.resolve({ wallet, client });
if (chainConfig) {
contractConfig[chainConfig.axelarId] = {
...contractConfig[chainConfig.axelarId],
address,
};
} else {
contractConfig.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 +69,18 @@ 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);

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

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

const main = async (options) => {
Expand All @@ -92,19 +89,20 @@ 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)) {
return;
}

for (const chain of chains) {
await instantiate(client, wallet, chain.toLowerCase(), config, options);
}

saveConfig(config, env);
};

const programHandler = () => {
Expand Down
146 changes: 73 additions & 73 deletions cosmwasm/submit-proposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ const updateContractConfig = (contractConfig, chainConfig, key, value) => {
}
};

const predictAndUpdateAddress = (client, contractConfig, chainConfig, options, contractName, chainName) => {
return instantiate2AddressForProposal(client, contractConfig, options).then((contractAddress) => {
updateContractConfig(contractConfig, chainConfig, 'address', contractAddress);
const predictAndUpdateAddress = async (client, contractConfig, chainConfig, options) => {
const contractAddress = await instantiate2AddressForProposal(client, contractConfig, options);
updateContractConfig(contractConfig, chainConfig, 'address', contractAddress);

return contractAddress;
});
return contractAddress;
};

const printProposal = (proposal, proposalType) => {
Expand All @@ -59,7 +58,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 +71,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 +100,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 +126,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 +142,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 +169,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 +190,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());
}

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

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

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 = () => {
eguajardo marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Loading
Loading