Skip to content

Commit

Permalink
chore: extract id finding to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
npty committed Aug 5, 2024
1 parent a7e8b7d commit 24020e1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 23 deletions.
63 changes: 40 additions & 23 deletions sui/operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,19 @@ const { printInfo, 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 } = require('./utils');
const { getBcsBytesByObjectId, findOwnedObjectId } = require('./utils');

async function collectGas(keypair, client, config, chain, args, options) {
const [amount] = args;
const receiver = options.receiver || keypair.toSuiAddress();
const gasServiceConfig = config.sui.contracts.GasService;
const operatorsConfig = config.sui.contracts.Operators;

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

if (!operatorsConfig) {
throw new Error('Operators package not found.');
}
async function getGasCollectorCapId(client, gasServiceConfig, operatorsConfig) {
const operatorId = operatorsConfig.objects.Operators;

const operatorCapId = operatorsConfig.objects.Operators;
const operatorBytes = await getBcsBytesByObjectId(client, operatorsConfig.objects.Operators);
// Get and parse operator data
const operatorBytes = await getBcsBytesByObjectId(client, operatorId);
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',
Expand All @@ -38,28 +31,52 @@ async function collectGas(keypair, client, config, chain, args, options) {
(cap) => cap.objectType === `${gasServiceConfig.address}::gas_service::GasCollectorCap`,
)?.objectId;

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

console.log('GasCollectorBagId', gasCollectorBagId);

// 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;
}

if (!gasCollectorCapId) {
throw new Error('GasCollectorCap not found in the operator capabilities bag');
async function collectGas(keypair, client, config, chain, args, options) {
const [amount] = args;

Check failure on line 54 in sui/operators.js

View workflow job for this annotation

GitHub Actions / lint

'amount' is assigned a value but never used
const receiver = options.receiver || keypair.toSuiAddress();

Check failure on line 55 in sui/operators.js

View workflow job for this annotation

GitHub Actions / lint

'receiver' is assigned a value but never used
const gasServiceConfig = config.sui.contracts.GasService;
const operatorsConfig = config.sui.contracts.Operators;

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

console.log('GasCollectorBag:', gasCollectorBagId);
console.log('GasCollectorCap:', gasCollectorCapId);
console.log('OperatorCap:', operatorCapId);
console.log('Operators:', operatorsConfig.objects.Operators);
if (!operatorsConfig) {
throw new Error('Operators package not found.');
}

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

printInfo('GasCollectorCap', gasCollectorCapId);
printInfo('OperatorCap', operatorCapId);
printInfo('Operators', operatorId);

const tx = new Transaction();

const borrowedCap = tx.moveCall({

Check failure on line 77 in sui/operators.js

View workflow job for this annotation

GitHub Actions / lint

'borrowedCap' is assigned a value but never used
target: `${operatorsConfig.address}::operators::borrow_cap`,
arguments: [tx.object(operatorsConfig.objects.Operators), tx.object(operatorCapId), tx.object(gasCollectorCapId)],
arguments: [tx.object(operatorId), tx.object(operatorCapId), tx.pure(bcs.Address.serialize(gasCollectorCapId).toBytes())],
typeArguments: [`${gasServiceConfig.address}::gas_service::GasCollectorCap`],
});

Expand Down
18 changes: 18 additions & 0 deletions sui/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,27 @@ const getSigners = async (keypair, config, chain, options) => {
return getAmplifierSigners(config, chain);
};

const findOwnedObjectId = async (client, ownerAddress, objectType) => {
const ownedObjects = await client.getOwnedObjects({
owner: ownerAddress,
options: {
showContent: true,
},
});

const targetObject = ownedObjects.data.find(({ data }) => data.content.type === objectType);

if (!targetObject) {
throw new Error(`No object found for type: ${objectType}`);
}

return targetObject.data.content.fields.id.id;
};

module.exports = {
suiPackageAddress,
suiClockAddress,
findOwnedObjectId,
getAmplifierSigners,
getBcsBytesByObjectId,
deployPackage,
Expand Down

0 comments on commit 24020e1

Please sign in to comment.