From 6d2b62351bb8b93a68dd688ff76f3738c0a8dfce Mon Sep 17 00:00:00 2001 From: Juan Enrique Alcaraz Date: Thu, 30 Mar 2023 16:44:07 +0200 Subject: [PATCH] Add pathfinder max transfer steps as core option --- src/index.js | 9 +++++++++ src/token.js | 34 +++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/index.js b/src/index.js index 9c9f67e7..7f0094fe 100644 --- a/src/index.js +++ b/src/index.js @@ -82,6 +82,15 @@ export default class CirclesCore { type: 'string', default: 'server', }, + pathfinderMaxTransferSteps: { + type: 'number', + /* Due to block gas limit of 12.500.000 a transitive transaction can have a + * limited number of steps. The limit below gives a 50% buffer between the + * gas estimate and the block gas limit. + * For more information, see the Circles handbook. + */ + default: 30, + }, }); // Expose error classes and constants diff --git a/src/token.js b/src/token.js index 0750f001..67de65de 100644 --- a/src/token.js +++ b/src/token.js @@ -6,13 +6,6 @@ import checkOptions from '~/common/checkOptions'; import { getTokenContract } from '~/common/getContracts'; import { getVersion } from '~/safe'; -/* Due to block gas limit of 12.500.000 a transitive transaction can have a - * limited number of steps. The limit below gives a 50% buffer between the - * gas estimate and the block gas limit. - * For more information, see the Circles handbook. - */ -const MAX_TRANSFER_STEPS = 30; - /** * Find maximumFlow and transfer steps through a trust graph from someone to * someone else to transitively send an amount of Circles using the binary @@ -23,6 +16,8 @@ const MAX_TRANSFER_STEPS = 30; * @param {Web3} web3 - Web3 instance * @param {Object} utils - core utils * @param {Object} userOptions - search arguments + * @param {string} pathfinderType - pathfinder execution type + * @param {number} pathfinderMaxTransferSteps - default pathfinder server max transfer steps * * @return {Object[]} - transaction steps */ @@ -31,6 +26,7 @@ export async function findTransitiveTransfer( utils, userOptions, pathfinderType, + pathfinderMaxTransferSteps, ) { let result; if (pathfinderType == 'cli') { @@ -38,7 +34,12 @@ export async function findTransitiveTransfer( result = await findTransitiveTransferCli(web3, utils, userOptions); } else if (pathfinderType == 'server') { // call server - result = await findTransitiveTransferServer(web3, utils, userOptions); + result = await findTransitiveTransferServer( + web3, + utils, + userOptions, + pathfinderMaxTransferSteps, + ); } return result; } @@ -111,7 +112,12 @@ async function findTransitiveTransferCli(web3, utils, userOptions) { * * @return {Object[]} - transaction steps */ -async function findTransitiveTransferServer(web3, utils, userOptions) { +async function findTransitiveTransferServer( + web3, + utils, + userOptions, + pathfinderMaxTransferSteps, +) { const options = checkOptions(userOptions, { from: { type: web3.utils.checkAddressChecksum, @@ -124,7 +130,7 @@ async function findTransitiveTransferServer(web3, utils, userOptions) { }, maxTransfers: { type: 'number', - default: MAX_TRANSFER_STEPS, + default: pathfinderMaxTransferSteps, }, }); @@ -219,7 +225,7 @@ export default function createTokenModule( globalOptions, ) { const { hub } = contracts; - const { pathfinderType } = globalOptions; + const { pathfinderType, pathfinderMaxTransferSteps } = globalOptions; return { /** * Returns true if there are enough balance on this Safe address to deploy @@ -450,6 +456,7 @@ export default function createTokenModule( utils, userOptions, pathfinderType, + pathfinderMaxTransferSteps, ); }, @@ -531,7 +538,7 @@ export default function createTokenModule( }, maxTransfers: { type: 'number', - default: MAX_TRANSFER_STEPS, + default: pathfinderMaxTransferSteps, }, }; } @@ -578,6 +585,7 @@ export default function createTokenModule( utils, options, pathfinderType, + pathfinderMaxTransferSteps, ); if (web3.utils.toBN(response.maxFlowValue).lt(options.value)) { throw new TransferError( @@ -589,7 +597,7 @@ export default function createTokenModule( }, ); } - if (response.transferSteps.length > MAX_TRANSFER_STEPS) { + if (response.transferSteps.length > pathfinderMaxTransferSteps) { throw new TransferError( 'Too many transfer steps', ErrorCodes.TOO_COMPLEX_TRANSFER,