Skip to content

Commit

Permalink
chore: wrap hot potato pattern into a helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
npty committed Aug 15, 2024
1 parent 44a1ebf commit b4e1965
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 92 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"homepage": "https://github.com/axelarnetwork/axelar-contract-deployments#readme",
"dependencies": {
"@axelar-network/axelar-cgp-solidity": "6.3.1",
"@axelar-network/axelar-cgp-sui": "^0.4.0",
"@axelar-network/axelar-cgp-sui": "^0.0.0-snapshot.218635e",
"@axelar-network/axelar-gmp-sdk-solidity": "5.10.0",
"@axelar-network/interchain-token-service": "1.2.4",
"@cosmjs/cosmwasm-stargate": "^0.32.1",
Expand Down
122 changes: 35 additions & 87 deletions sui/operators.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,58 @@
const { Command, Option } = require('commander');
const { Transaction } = require('@mysten/sui/transactions');
const { bcs } = require('@mysten/sui/bcs');
const { printInfo, printError, loadConfig } = require('../common/utils');
const { operatorsStruct } = require('./types-utils');
const { addBaseOptions, addOptionsToCommands, parseSuiUnitAmount } = require('./cli-utils');
const { getWallet, printWalletInfo, broadcast } = require('./sign-utils');
const { getBcsBytesByObjectId, findOwnedObjectId } = require('./utils');
const { getBcsBytesByObjectId, findOwnedObjectId, getBagContentId } = require('./utils');

async function getGasCollectorCapId(client, gasServiceConfig, contractConfig) {

Check failure on line 9 in sui/operators.js

View workflow job for this annotation

GitHub Actions / lint

'getGasCollectorCapId' is defined but never used
const operatorId = contractConfig.objects.Operators;

// Get and parse operator data
const operatorBytes = await getBcsBytesByObjectId(client, operatorId);
const operatorBytes = await getBcsBytesByObjectId(client, contractConfig);
const parsedOperator = operatorsStruct.parse(operatorBytes);

// Get the capabilities bag ID
const bagId = parsedOperator.caps.id;

// Find the GasCollectorCap bag ID
const bagResult = await client.getDynamicFields({
parentId: bagId,
name: 'caps',
});
const gasCollectorBagId = bagResult.data.find(
(cap) => cap.objectType === `${gasServiceConfig.address}::gas_service::GasCollectorCap`,
)?.objectId;

if (!gasCollectorBagId) {
throw new Error('GasCollectorCap not found in the operator capabilities bag');
}

// Get the actual cap ID from the bag ID
const gasCollectorCapObject = await client.getObject({
id: gasCollectorBagId,
options: {
showContent: true,
},
});

// Extract and return the gas collector cap ID
const gasCollectorCapId = gasCollectorCapObject.data.content.fields.value.fields.id.id;
return gasCollectorCapId;
return getBagContentId(client, `${gasServiceConfig.address}::gas_service::GasCollectorCap`, bagId, 'caps');
}

async function collectGas(keypair, client, gasServiceConfig, contractConfig, args, options) {
const [amount] = args;
const receiver = options.receiver || keypair.toSuiAddress();

function operatorMoveCall(contractConfig, gasServiceConfig, operatorCapId, moveCall) {
const operatorId = contractConfig.objects.Operators;
const gasCollectorCapId = await getGasCollectorCapId(client, gasServiceConfig, contractConfig);
const operatorCapId = await findOwnedObjectId(client, keypair.toSuiAddress(), `${contractConfig.address}::operators::OperatorCap`);
const gasCollectorCapId = gasServiceConfig.objects.GasCollectorCap;

const tx = new Transaction();

const [cap, loanedCap] = tx.moveCall({
target: `${contractConfig.address}::operators::loan_cap`,
arguments: [tx.object(operatorId), tx.object(operatorCapId), tx.pure(bcs.Address.serialize(gasCollectorCapId).toBytes())],
arguments: [tx.object(operatorId), tx.object(operatorCapId), tx.object(gasCollectorCapId)],
typeArguments: [`${gasServiceConfig.address}::gas_service::GasCollectorCap`],
});

tx.moveCall({
target: `${gasServiceConfig.address}::gas_service::collect_gas`,
arguments: [tx.object(gasServiceConfig.objects.GasService), cap, tx.pure.address(receiver), tx.pure.u64(amount)],
});
moveCall(tx, cap);

tx.moveCall({
target: `${contractConfig.address}::operators::restore_cap`,
arguments: [
tx.object(operatorId),
tx.object(operatorCapId),
tx.pure(bcs.Address.serialize(gasCollectorCapId).toBytes()),
cap,
loanedCap,
],
arguments: [tx.object(operatorId), tx.object(operatorCapId), tx.object(gasCollectorCapId), cap, loanedCap],
typeArguments: [`${gasServiceConfig.address}::gas_service::GasCollectorCap`],
});

return tx;
}

async function collectGas(keypair, client, gasServiceConfig, contractConfig, args, options) {
const [amount] = args;
const receiver = options.receiver || keypair.toSuiAddress();

const operatorCapId = await findOwnedObjectId(client, keypair.toSuiAddress(), `${contractConfig.address}::operators::OperatorCap`);

const tx = operatorMoveCall(contractConfig, gasServiceConfig, operatorCapId, (tx, cap) => {
tx.moveCall({
target: `${gasServiceConfig.address}::gas_service::collect_gas`,
arguments: [tx.object(gasServiceConfig.objects.GasService), cap, tx.pure.address(receiver), tx.pure.u64(amount)],
});
});

const receipt = await broadcast(client, keypair, tx);

printInfo('Gas collected', receipt.digest);
Expand All @@ -85,48 +62,19 @@ async function refund(keypair, client, gasServiceConfig, contractConfig, args, o
const [messageId] = args;
const amount = options.amount;
const receiver = options.receiver || keypair.toSuiAddress();

if (!gasServiceConfig) {
throw new Error('Gas service package not found.');
}

if (!contractConfig) {
throw new Error('Operators package not found.');
}

const operatorId = contractConfig.objects.Operators;
const gasCollectorCapId = await getGasCollectorCapId(client, gasServiceConfig, contractConfig);
const operatorCapId = await findOwnedObjectId(client, keypair.toSuiAddress(), `${contractConfig.address}::operators::OperatorCap`);

const tx = new Transaction();

const [cap, loanedCap] = tx.moveCall({
target: `${contractConfig.address}::operators::loan_cap`,
arguments: [tx.object(operatorId), tx.object(operatorCapId), tx.pure(bcs.Address.serialize(gasCollectorCapId).toBytes())],
typeArguments: [`${gasServiceConfig.address}::gas_service::GasCollectorCap`],
});

tx.moveCall({
target: `${gasServiceConfig.address}::gas_service::refund`,
arguments: [
tx.object(gasServiceConfig.objects.GasService),
cap,
tx.pure.string(messageId),
tx.pure.address(receiver),
tx.pure.u64(amount),
],
});

tx.moveCall({
target: `${contractConfig.address}::operators::restore_cap`,
arguments: [
tx.object(operatorId),
tx.object(operatorCapId),
tx.pure(bcs.Address.serialize(gasCollectorCapId).toBytes()),
cap,
loanedCap,
],
typeArguments: [`${gasServiceConfig.address}::gas_service::GasCollectorCap`],
const tx = operatorMoveCall(contractConfig, gasServiceConfig, operatorCapId, (tx, cap) => {
tx.moveCall({
target: `${gasServiceConfig.address}::gas_service::refund`,
arguments: [
tx.object(gasServiceConfig.objects.GasService),
cap,
tx.pure.string(messageId),
tx.pure.address(receiver),
tx.pure.u64(amount),
],
});
});

const receipt = await broadcast(client, keypair, tx);
Expand Down
23 changes: 23 additions & 0 deletions sui/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,28 @@ const findOwnedObjectId = async (client, ownerAddress, objectType) => {
return targetObject.data.content.fields.id.id;
};

const getBagContentId = async (client, objectType, bagId, bagName) => {
const result = await client.getDynamicFields({
parentId: bagId,
name: bagName,
});

const objectId = result.data.find((cap) => cap.objectType === objectType)?.objectId;

if (!objectId) {
throw new Error(`${objectType} not found in the capabilities bag`);
}

const objectDetails = await client.getObject({
id: objectId,
options: {
showContent: true,
},
});

return objectDetails.data.content.fields.value.fields.id.id;
};

module.exports = {
suiPackageAddress,
suiClockAddress,
Expand All @@ -176,4 +198,5 @@ module.exports = {
getItsChannelId,
getSquidChannelId,
getSigners,
getBagContentId,
};

0 comments on commit b4e1965

Please sign in to comment.