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(sui): support all contract deployment in deploy-contract script #323

Merged
merged 32 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
fd13b23
chore: add test contract deployment into deploy-contract script
npty Jul 26, 2024
49d9057
chore: update post deployment for test contract
npty Jul 26, 2024
f1874dc
chore: remove unnecessary address record
npty Jul 26, 2024
473521d
chore: refactor deploy-contract
npty Jul 29, 2024
8aaa5cd
fix: choices is overrided by the handler
npty Jul 29, 2024
ab5d5c3
chore: update test contract deployment doc
npty Jul 29, 2024
c44a132
Merge branch 'main' into chore/merge-deploy-test-into-deploy-contract
npty Jul 30, 2024
74fe620
chore: refactor commands in deploy script
npty Jul 30, 2024
c29a4fd
chore: use common utils
npty Jul 30, 2024
5feec84
chore: move getDomainSeparator to common
npty Jul 30, 2024
2481958
fix: all contracts deployment
npty Jul 30, 2024
8f0eac1
chore: lint
npty Jul 30, 2024
4dc660f
chore: move getSigners and getChannelId to utils
npty Jul 31, 2024
9d5efa6
chore: refactor upgrade
npty Jul 31, 2024
292945c
chore: fix missing package address
npty Jul 31, 2024
845c015
chore: refactor deploy script
npty Jul 31, 2024
bf9a68a
Merge branch 'main' into chore/merge-deploy-test-into-deploy-contract
npty Jul 31, 2024
98e6f64
chore: update README.md
npty Jul 31, 2024
07f46c2
chore: prettier
npty Jul 31, 2024
d44cef7
chore: remove program and deploy function in deploy-utils.js
npty Jul 31, 2024
4f41195
chore: move getDeplooyGatewayOptions to deploy-utils
npty Jul 31, 2024
fe2be1a
chore: use constant for 0x2
npty Jul 31, 2024
bdbbce9
chore: prettier
npty Jul 31, 2024
29b8bd1
feat: deploy Operators contract
npty Jul 31, 2024
6f2010e
chore: move the command options into deploy-contract file
npty Aug 1, 2024
d0002f1
chore: create a mapping for command options and post deployment scripts
npty Aug 1, 2024
5eed902
chore: add toml parser
npty Aug 1, 2024
6b3d1c4
chore: fix default value for config.sui
npty Aug 1, 2024
4964688
chore: use loadConfig instead of loadSuiConfig
npty Aug 1, 2024
1a78c79
chore: reword
npty Aug 1, 2024
e56c054
chore: prettier
npty Aug 1, 2024
fd2421e
chore: fix prettier
npty Aug 1, 2024
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
14 changes: 13 additions & 1 deletion common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@ const {
const { normalizeBech32 } = require('@cosmjs/encoding');

function loadConfig(env) {
return require(`${__dirname}/../axelar-chains-config/info/${env}.json`);
const config = require(`${__dirname}/../axelar-chains-config/info/${env}.json`);

if (!config.sui) {
npty marked this conversation as resolved.
Show resolved Hide resolved
config.sui = {
networkType: env === 'local' ? 'localnet' : env,
name: 'Sui',
contracts: {
AxelarGateway: {},
},
};
}

return config;
}

function saveConfig(config, env) {
Expand Down
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"@mysten/sui": "^1.3.0",
"@stellar/stellar-sdk": "^12.0.0-rc3",
"axios": "^1.6.2",
"path": "^0.12.7"
"path": "^0.12.7",
"toml": "^3.0.0"
},
"devDependencies": {
"@ledgerhq/hw-transport-node-hid": "^6.27.21",
Expand Down
108 changes: 81 additions & 27 deletions sui/deploy-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ const { Transaction } = require('@mysten/sui/transactions');
const {
utils: { arrayify },
} = ethers;
const { saveConfig, printInfo, validateParameters, writeJSON, getDomainSeparator } = require('../common');
const { saveConfig, printInfo, validateParameters, writeJSON, getDomainSeparator, loadConfig } = require('../common');
const { addBaseOptions, addOptionsToCommands } = require('./cli-utils');
const { getWallet, printWalletInfo, broadcast } = require('./sign-utils');
const { bytes32Struct, signersStruct } = require('./types-utils');
const { upgradePackage, addDeployOptions, UPGRADE_POLICIES } = require('./deploy-utils');
const { upgradePackage, UPGRADE_POLICIES } = require('./deploy-utils');
const {
loadSuiConfig,
getSigners,
deployPackage,
getObjectIdsByObjectTypes,
Expand All @@ -38,6 +37,30 @@ const {
*/
const PACKAGE_DIRS = ['gas_service', 'test', 'axelar_gateway', 'operators'];

/**
* Post-Deployment Functions Mapping
*
* This object maps each package name to a post-deployment function.
*/
const POST_DEPLOY_FUNCTIONS = {
GasService: postDeployGasService,
Test: postDeployTest,
Operators: postDeployOperators,
AxelarGateway: postDeployAxelarGateway,
};

/**
* Command Options Mapping
*
* This object maps each package name to a function that returns an array of command options.
*/
const CMD_OPTIONS = {
npty marked this conversation as resolved.
Show resolved Hide resolved
AxelarGateway: () => [...DEPLOY_CMD_OPTIONS, ...GATEWAY_CMD_OPTIONS],
GasService: () => DEPLOY_CMD_OPTIONS,
Test: () => DEPLOY_CMD_OPTIONS,
Operators: () => DEPLOY_CMD_OPTIONS,
};

/**
* Supported Move Packages
*
Expand All @@ -62,7 +85,8 @@ const supportedPackages = PACKAGE_DIRS.map((dir) => ({
* Define post-deployment functions for each supported package below.
*/

async function postDeployGasService(published, chain) {
async function postDeployGasService(published, args) {
const { chain } = args;
const [gasCollectorCapObjectId, gasServiceObjectId] = getObjectIdsByObjectTypes(published.publishTxn, [
`${published.packageId}::gas_service::GasCollectorCap`,
`${published.packageId}::gas_service::GasService`,
Expand All @@ -73,7 +97,8 @@ async function postDeployGasService(published, chain) {
};
}

async function postDeployTest(published, config, chain, options) {
async function postDeployTest(published, args) {
const { chain, config, options } = args;
const [keypair, client] = getWallet(chain, options);
const relayerDiscovery = config.sui.contracts.AxelarGateway?.objects?.RelayerDiscovery;

Expand All @@ -92,18 +117,20 @@ async function postDeployTest(published, config, chain, options) {
printInfo('Register transaction', registerTx.digest);
}

async function postDeployOperators(published, chain) {
async function postDeployOperators(published, args) {
const { chain } = args;
const [operatorsObjectId, ownerCapObjectId] = getObjectIdsByObjectTypes(published.publishTxn, [
`${published.packageId}::operators::Operators`,
`${published.packageId}::operators::OwnerCap`,
]);
chain.contracts.Operators.objects = {
Operators: operatorsObjectId,
OwnerCap: ownerCapObjectId,
Operators: operatorsObjectId,
OwnerCap: ownerCapObjectId,
};
}

async function postDeployAxelarGateway(published, keypair, client, config, chain, options) {
async function postDeployAxelarGateway(published, args) {
const { keypair, client, config, chain, options } = args;
npty marked this conversation as resolved.
Show resolved Hide resolved
const { packageId, publishTxn } = published;
const { minimumRotationDelay, policy, previousSigners } = options;
const operator = options.operator || keypair.toSuiAddress();
Expand Down Expand Up @@ -178,30 +205,19 @@ async function postDeployAxelarGateway(published, keypair, client, config, chain
async function deploy(keypair, client, supportedContract, config, chain, options) {
const { packageDir, packageName } = supportedContract;

// Deploy package
const published = await deployPackage(packageDir, client, keypair, options);

printInfo(`Deployed ${packageName}`, published.publishTxn.digest);

// Update chain configuration with deployed contract address
chain.contracts[packageName] = {
address: published.packageId,
};

switch (packageName) {
case 'GasService':
await postDeployGasService(published, chain);
break;
case 'AxelarGateway':
await postDeployAxelarGateway(published, keypair, client, config, chain, options);
break;
case 'Test':
await postDeployTest(published, config, chain, options);
break;
case 'Operators':
await postDeployOperators(published, chain);
break;
default:
throw new Error(`${packageName} is not supported.`);
}
// Execute post-deployment function
const executePostDeploymentFn = POST_DEPLOY_FUNCTIONS[packageName];
executePostDeploymentFn(published, { keypair, client, config, chain, options });
npty marked this conversation as resolved.
Show resolved Hide resolved

printInfo(`${packageName} Configuration Updated`, JSON.stringify(chain.contracts[packageName], null, 2));
}
Expand Down Expand Up @@ -232,7 +248,7 @@ async function upgrade(keypair, client, supportedPackage, policy, config, chain,
}

async function mainProcessor(args, options, processor) {
const config = loadSuiConfig(options.env);
const config = loadConfig(options.env);
const [keypair, client] = getWallet(config.sui, options);
await printWalletInfo(keypair, client, config.sui, options);
await processor(keypair, client, ...args, config, config.sui, options);
Expand All @@ -249,6 +265,44 @@ async function mainProcessor(args, options, processor) {
}
}

/**
* Command Options
*
* This section defines options for the command that are specific to each package.
*/

// Common deploy command options for all packages
const DEPLOY_CMD_OPTIONS = [
new Option('--policy <policy>', 'upgrade policy for upgrade cap: For example, use "any_upgrade" to allow all types of upgrades')
.choices(['any_upgrade', 'code_upgrade', 'dep_upgrade'])
.default('any_upgrade'),
];

// Gateway deploy command options
const GATEWAY_CMD_OPTIONS = [
new Option('--signers <signers>', 'JSON with the initial signer set').env('SIGNERS'),
new Option('--operator <operator>', 'operator for the gateway (defaults to the deployer address)').env('OPERATOR'),
new Option('--minimumRotationDelay <minimumRotationDelay>', 'minium delay for signer rotations (in second)')
.argParser((val) => parseInt(val) * 1000)
.default(24 * 60 * 60),
new Option('--domainSeparator <domainSeparator>', 'domain separator'),
new Option('--nonce <nonce>', 'nonce for the signer (defaults to HashZero)'),
new Option('--previousSigners <previousSigners>', 'number of previous signers to retain').default('15'),
];

const addDeployOptions = (program) => {
// Get the package name from the program name
const packageName = program.name();

// Find the corresponding options for the package
const options = CMD_OPTIONS[packageName]();

// Add the options to the program
options.forEach((option) => program.addOption(option));

return program;
};

if (require.main === module) {
// 1st level command
const program = new Command('deploy-contract').description('Deploy/Upgrade packages');
Expand All @@ -274,7 +328,7 @@ if (require.main === module) {
const upgradeContractCmds = supportedPackages.map((supportedPackage) => {
const { packageName } = supportedPackage;
return new Command(packageName)
.description(`Deploy ${packageName} contract`)
.description(`Upgrade ${packageName} contract`)
.command(`${packageName} <policy>`)
npty marked this conversation as resolved.
Show resolved Hide resolved
.addOption(new Option('--sender <sender>', 'transaction sender'))
.addOption(new Option('--digest <digest>', 'digest hash for upgrade'))
Expand Down
6 changes: 3 additions & 3 deletions sui/deploy-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { saveConfig, prompt, printInfo } = require('../common/utils');
const { loadConfig, saveConfig, prompt, printInfo } = require('../common/utils');
const { Command } = require('commander');
const { loadSuiConfig, deployPackage, getBcsBytesByObjectId } = require('./utils');
const { deployPackage, getBcsBytesByObjectId } = require('./utils');
const { singletonStruct } = require('./types-utils');
const { Transaction } = require('@mysten/sui/transactions');
const { addBaseOptions } = require('./cli-utils');
Expand Down Expand Up @@ -54,7 +54,7 @@ async function processCommand(config, chain, options) {
}

async function mainProcessor(options, processor) {
const config = loadSuiConfig(options.env);
const config = loadConfig(options.env);

await processor(config, config.sui, options);
saveConfig(config, options.env);
Expand Down
34 changes: 0 additions & 34 deletions sui/deploy-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const { bcs } = require('@mysten/bcs');
const { fromB64 } = require('@mysten/bcs');
const { printInfo, validateParameters } = require('../common/utils');
const { getObjectIdsByObjectTypes, suiPackageAddress } = require('./utils');
const { Option } = require('commander');
const UPGRADE_POLICIES = {
code_upgrade: 'only_additive_upgrades',
dependency_upgrade: 'only_dep_upgrades',
Expand Down Expand Up @@ -79,40 +78,7 @@ async function upgradePackage(client, keypair, packageToUpgrade, contractConfig,
}
}

const getDeployGatewayOptions = () => {
return [
new Option('--signers <signers>', 'JSON with the initial signer set').env('SIGNERS'),
new Option('--operator <operator>', 'operator for the gateway (defaults to the deployer address)').env('OPERATOR'),
new Option('--minimumRotationDelay <minimumRotationDelay>', 'minium delay for signer rotations (in second)')
.argParser((val) => parseInt(val) * 1000)
.default(24 * 60 * 60),
new Option('--domainSeparator <domainSeparator>', 'domain separator'),
new Option('--nonce <nonce>', 'nonce for the signer (defaults to HashZero)'),
new Option('--previousSigners <previousSigners>', 'number of previous signers to retain').default('15'),
new Option('--policy <policy>', 'upgrade policy for upgrade cap: For example, use "any_upgrade" to allow all types of upgrades')
.choices(['any_upgrade', 'code_upgrade', 'dep_upgrade'])
.default('any_upgrade'),
];
};

const addDeployOptions = (program) => {
switch (program.name()) {
case 'AxelarGateway':
getDeployGatewayOptions().forEach((option) => program.addOption(option));
break;
case 'GasService':
case 'Operators':
case 'Test':
break;
default:
throw new Error(`Unsupported package: ${program.name()}. `);
}

return program;
};

module.exports = {
UPGRADE_POLICIES,
upgradePackage,
addDeployOptions,
};
5 changes: 3 additions & 2 deletions sui/gas-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ const { saveConfig, printInfo, printError } = require('../common/utils');
const { Command } = require('commander');
const { Transaction } = require('@mysten/sui/transactions');
const { bcs } = require('@mysten/sui/bcs');
const { loadConfig } = require('../common/utils');
const { gasServiceStruct } = require('./types-utils');
const { loadSuiConfig, getBcsBytesByObjectId } = require('./utils');
const { getBcsBytesByObjectId } = require('./utils');
const { ethers } = require('hardhat');
const { getFormattedAmount } = require('./amount-utils');
const {
Expand Down Expand Up @@ -160,7 +161,7 @@ async function processCommand(command, chain, args, options) {
}

async function mainProcessor(options, args, processor, command) {
const config = loadSuiConfig(options.env);
const config = loadConfig(options.env);
await processor(command, config.sui, args, options);
saveConfig(config, options.env);
}
Expand Down
4 changes: 2 additions & 2 deletions sui/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const {
constants: { HashZero },
} = ethers;

const { loadConfig } = require('../common/utils');
const { addBaseOptions } = require('./cli-utils');
const { getWallet, printWalletInfo, getRawPrivateKey, broadcast } = require('./sign-utils');
const { bytes32Struct, signersStruct, messageToSignStruct, messageStruct, proofStruct } = require('./types-utils');
const { loadSuiConfig } = require('./utils');
const { getSigners } = require('./deploy-gateway');
const secp256k1 = require('secp256k1');

Expand Down Expand Up @@ -218,7 +218,7 @@ async function rotateSigners(keypair, client, config, chain, args, options) {
}

async function mainProcessor(processor, args, options) {
const config = loadSuiConfig(options.env);
const config = loadConfig(options.env);

const [keypair, client] = getWallet(config.sui, options);
await printWalletInfo(keypair, client, config.sui, options);
Expand Down
5 changes: 3 additions & 2 deletions sui/gmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const { saveConfig, printInfo } = require('../common/utils');
const { Command } = require('commander');
const { Transaction } = require('@mysten/sui/transactions');
const { bcs } = require('@mysten/sui/bcs');
const { loadSuiConfig, getBcsBytesByObjectId } = require('./utils');
const { getBcsBytesByObjectId } = require('./utils');
const { loadConfig } = require('../common/utils');
const { ethers } = require('hardhat');
const {
utils: { arrayify },
Expand Down Expand Up @@ -165,7 +166,7 @@ async function processCommand(command, chain, args, options) {
}

async function mainProcessor(command, options, args, processor) {
const config = loadSuiConfig(options.env);
const config = loadConfig(options.env);
await processor(command, config.sui, args, options);
saveConfig(config, options.env);
}
Expand Down
4 changes: 2 additions & 2 deletions sui/multisig.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { fromB64 } = require('@mysten/bcs');
const { addBaseOptions } = require('./cli-utils');
const { getWallet, getMultisig, signTransactionBlockBytes, broadcastSignature } = require('./sign-utils');
const { getSignedTx, storeSignedTx } = require('../evm/sign-utils');
const { loadSuiConfig } = require('./utils');
const { loadConfig } = require('../common/utils');
const { printInfo, validateParameters } = require('../common/utils');

async function signTx(keypair, client, options) {
Expand Down Expand Up @@ -143,7 +143,7 @@ async function processCommand(chain, options) {
}

async function mainProcessor(options, processor) {
const config = loadSuiConfig(options.env);
const config = loadConfig(options.env);
await processor(config.sui, options);
}

Expand Down
4 changes: 2 additions & 2 deletions sui/transfer-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { Command, Option } = require('commander');
const { printInfo, validateParameters } = require('../common/utils');
const { addExtendedOptions } = require('./cli-utils');
const { getWallet, printWalletInfo } = require('./sign-utils');
const { loadSuiConfig } = require('./utils');
const { loadConfig } = require('../common/utils');

async function processCommand(chain, options) {
const [keypair, client] = getWallet(chain, options);
Expand Down Expand Up @@ -54,7 +54,7 @@ async function processCommand(chain, options) {
}

async function mainProcessor(options, processor) {
const config = loadSuiConfig(options.env);
const config = loadConfig(options.env);
await processor(config.sui, options);
}

Expand Down
Loading
Loading