From 24020e144e52cdf382fd79cd3e1467c7e7f7efb6 Mon Sep 17 00:00:00 2001 From: npty Date: Mon, 5 Aug 2024 14:35:12 +0700 Subject: [PATCH] chore: extract id finding to utils --- sui/operators.js | 63 ++++++++++++++++++++++++++++++------------------ sui/utils.js | 18 ++++++++++++++ 2 files changed, 58 insertions(+), 23 deletions(-) diff --git a/sui/operators.js b/sui/operators.js index b6f4e6f8..28482bed 100644 --- a/sui/operators.js +++ b/sui/operators.js @@ -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', @@ -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; + 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.'); } - 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({ 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`], }); diff --git a/sui/utils.js b/sui/utils.js index 1ef47c8c..c68e2778 100644 --- a/sui/utils.js +++ b/sui/utils.js @@ -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,