Skip to content

Commit

Permalink
feat(sui): support all contract deployment in deploy-contract script (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
npty authored Aug 1, 2024
1 parent 35df60a commit cd3c913
Show file tree
Hide file tree
Showing 14 changed files with 496 additions and 384 deletions.
81 changes: 80 additions & 1 deletion common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,27 @@ const chalk = require('chalk');
const https = require('https');
const http = require('http');
const readlineSync = require('readline-sync');
const { CosmWasmClient } = require('@cosmjs/cosmwasm-stargate');
const { ethers } = require('hardhat');
const {
utils: { keccak256, hexlify },
} = ethers;
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) {
config.sui = {
networkType: env === 'local' ? 'localnet' : env,
name: 'Sui',
contracts: {
AxelarGateway: {},
},
};
}

return config;
}

function saveConfig(config, env) {
Expand Down Expand Up @@ -331,6 +349,66 @@ function toBigNumberString(number) {
return Math.ceil(number).toLocaleString('en', { useGrouping: false });
}

const isValidCosmosAddress = (str) => {
try {
normalizeBech32(str);

return true;
} catch (error) {
return false;
}
};

async function getDomainSeparator(config, chain, options) {
// Allow any domain separator for local deployments or `0x` if not provided
if (options.env === 'local') {
return options.domainSeparator || ethers.constants.HashZero;
}

if (isKeccak256Hash(options.domainSeparator)) {
// return the domainSeparator for debug deployments
return options.domainSeparator;
}

const {
axelar: { contracts, chainId },
} = config;
const {
Router: { address: routerAddress },
} = contracts;

if (!isString(chain.axelarId)) {
throw new Error(`missing or invalid axelar ID for chain ${chain.name}`);
}

if (!isString(routerAddress) || !isValidCosmosAddress(routerAddress)) {
throw new Error(`missing or invalid router address`);
}

if (!isString(chainId)) {
throw new Error(`missing or invalid chain ID`);
}

printInfo(`Retrieving domain separator for ${chain.name} from Axelar network`);
const domainSeparator = hexlify((await getContractConfig(config, chain.axelarId)).domain_separator);
const expectedDomainSeparator = calculateDomainSeparator(chain.axelarId, routerAddress, chainId);

if (domainSeparator !== expectedDomainSeparator) {
throw new Error(`unexpected domain separator (want ${expectedDomainSeparator}, got ${domainSeparator})`);
}

return domainSeparator;
}

const getContractConfig = async (config, chain) => {
const key = Buffer.from('config');
const client = await CosmWasmClient.connect(config.axelar.rpc);
const value = await client.queryContractRaw(config.axelar.contracts.MultisigProver[chain].address, key);
return JSON.parse(Buffer.from(value).toString('ascii'));
};

const calculateDomainSeparator = (chain, router, network) => keccak256(Buffer.from(`${chain}${router}${network}`));

module.exports = {
loadConfig,
saveConfig,
Expand Down Expand Up @@ -362,4 +440,5 @@ module.exports = {
toBigNumberString,
timeout,
validateParameters,
getDomainSeparator,
};
44 changes: 2 additions & 42 deletions evm/deploy-amplifier-gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {
ContractFactory,
Contract,
Wallet,
utils: { defaultAbiCoder, keccak256, hexlify },
utils: { defaultAbiCoder, keccak256 },
getDefaultProvider,
} = ethers;

Expand All @@ -22,59 +22,19 @@ const {
mainProcessor,
deployContract,
getGasOptions,
isKeccak256Hash,
getContractConfig,
isString,
getWeightedSigners,
getContractJSON,
getDeployedAddress,
getDeployOptions,
getDomainSeparator,
} = require('./utils');
const { calculateDomainSeparator, isValidCosmosAddress } = require('../cosmwasm/utils');
const { addExtendedOptions } = require('./cli-utils');
const { storeSignedTx, signTransaction, getWallet } = require('./sign-utils.js');

const { WEIGHTED_SIGNERS_TYPE, encodeWeightedSigners } = require('@axelar-network/axelar-gmp-sdk-solidity/scripts/utils');
const AxelarAmplifierGatewayProxy = require('@axelar-network/axelar-gmp-sdk-solidity/artifacts/contracts/gateway/AxelarAmplifierGatewayProxy.sol/AxelarAmplifierGatewayProxy.json');
const AxelarAmplifierGateway = require('@axelar-network/axelar-gmp-sdk-solidity/artifacts/contracts/gateway/AxelarAmplifierGateway.sol/AxelarAmplifierGateway.json');

async function getDomainSeparator(config, chain, options) {
printInfo(`Retrieving domain separator for ${chain.name} from Axelar network`);

if (isKeccak256Hash(options.domainSeparator)) {
// return the domainSeparator for debug deployments
return options.domainSeparator;
}

const {
axelar: { contracts, chainId },
} = config;
const {
Router: { address: routerAddress },
} = contracts;

if (!isString(chain.axelarId)) {
throw new Error(`missing or invalid axelar ID for chain ${chain.name}`);
}

if (!isString(routerAddress) || !isValidCosmosAddress(routerAddress)) {
throw new Error(`missing or invalid router address`);
}

if (!isString(chainId)) {
throw new Error(`missing or invalid chain ID`);
}

const domainSeparator = hexlify((await getContractConfig(config, chain.axelarId)).domain_separator);
const expectedDomainSeparator = calculateDomainSeparator(chain.axelarId, routerAddress, chainId);

if (domainSeparator !== expectedDomainSeparator) {
throw new Error(`unexpected domain separator (want ${expectedDomainSeparator}, got ${domainSeparator})`);
}

return domainSeparator;
}

async function getSetupParams(config, chain, operator, options) {
const { signers: signerSets, verifierSetId } = await getWeightedSigners(config, chain, options);
printInfo('Setup params', JSON.stringify([operator, signerSets], null, 2));
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
18 changes: 12 additions & 6 deletions sui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Deploy the gateway package:
- By querying the signer set from the Amplifier contract (this only works if Amplifier contracts have been setup):

```bash
node sui/deploy-contract.js deploy axelar_gateway
node sui/deploy-contract.js deploy AxelarGateway
```

Note: the `minimumRotationDelay` is in `seconds` unit. The default value is `24 * 60 * 60` (1 day).
Expand All @@ -64,21 +64,21 @@ Use `--help` flag to see other setup params that can be overridden.
- For testing convenience, you can use the secp256k1 wallet as the signer set for the gateway.

```bash
node sui/deploy-contract.js deploy axelar_gateway --signers wallet --nonce test
node sui/deploy-contract.js deploy AxelarGateway --signers wallet --nonce test
```

- You can also provide a JSON object with a full signer set:

```bash
node sui/deploy-contract.js deploy axelar_gateway -e testnet --signers '{"signers": [{"pub_key": "0x020194ead85b350d90472117e6122cf1764d93bf17d6de4b51b03d19afc4d6302b", "weight": 1}], "threshold": 1, "nonce": "0x0000000000000000000000000000000000000000000000000000000000000000"}'
node sui/deploy-contract.js deploy AxelarGateway -e testnet --signers '{"signers": [{"pub_key": "0x020194ead85b350d90472117e6122cf1764d93bf17d6de4b51b03d19afc4d6302b", "weight": 1}], "threshold": 1, "nonce": "0x0000000000000000000000000000000000000000000000000000000000000000"}'
```

Upgrading Gateway:

To update the gateway run the following command:

```bash
node sui/deploy-contract.js upgrade axelar_gateway <policy>
node sui/deploy-contract.js upgrade AxelarGateway <policy>
```

policy should be one of the following:
Expand All @@ -92,13 +92,19 @@ Provide `--txFilePath` with `--offline` to generate tx data file for offline sig
Deploy the Gas Service package:

```bash
node sui/deploy-contract.js deploy gas_service
node sui/deploy-contract.js deploy GasService
```

Deploy the test GMP package:

```bash
node sui/deploy-test.js
node sui/deploy-contract.js deploy Test
```

Deploy the Operators package:

```bash
node sui/deploy-contract.js deploy Operators
```

Call Contract:
Expand Down
Loading

0 comments on commit cd3c913

Please sign in to comment.