Skip to content

Commit

Permalink
refactor: make last argument optional in semverSatisfies
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Apr 22, 2024
1 parent b3f867e commit 8b18b84
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
5 changes: 1 addition & 4 deletions src/tx/builder/field-types/gas-price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ export async function getCachedIncreasedGasPrice(node: Node): Promise<bigint> {

// TODO: remove after requiring [email protected]
const { nodeVersion } = await node._getCachedStatus();
// TODO: remove remove '6.12.0+' check after releasing 6.13.0
if (!nodeVersion.startsWith('6.12.0+') && !semverSatisfies(nodeVersion, '6.13.0', '8.0.0')) {
return 0n;
}
if (!semverSatisfies(nodeVersion, '6.13.0')) return 0n;

const { minGasPrice, utilization } = (await node.getRecentGasPrices())[0];
let gasPrice = utilization < 70 ? 0n : BigInt(
Expand Down
27 changes: 14 additions & 13 deletions src/utils/semver-satisfies.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
export default function semverSatisfies(
version: string,
geVersion: string,
ltVersion: string,
): boolean {
function verCmp(a: string, b: string): number {
const getComponents = (v: string): number[] => v
.split(/[-+]/)[0].split('.').map((i) => +i);

const versionComponents = getComponents(version);
const geComponents = getComponents(geVersion);
const ltComponents = getComponents(ltVersion);
const aComponents = getComponents(a);
const bComponents = getComponents(b);

const base = Math.max(...versionComponents, ...geComponents, ...ltComponents) + 1;
const base = Math.max(...aComponents, ...bComponents) + 1;
const componentsToNumber = (components: number[]): number => components.reverse()
.reduce((acc, n, idx) => acc + n * base ** idx, 0);

const vNumber = componentsToNumber(versionComponents);
const geNumber = componentsToNumber(geComponents);
const ltNumber = componentsToNumber(ltComponents);
return vNumber >= geNumber && vNumber < ltNumber;
return componentsToNumber(aComponents) - componentsToNumber(bComponents);
}

export default function semverSatisfies(
version: string,
geVersion: string,
ltVersion?: string,
): boolean {
return verCmp(version, geVersion) >= 0
&& (ltVersion == null || verCmp(version, ltVersion) < 0);
}
1 change: 1 addition & 0 deletions test/unit/semver-satisfies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ describe('semverSatisfies', () => {
expect(semverSatisfies('5.0.0', '3.0.0', '5.0.0')).to.equal(false);
expect(semverSatisfies('6.0.0-rc4', '6.0.0', '7.0.0')).to.equal(true);
expect(semverSatisfies('6.3.0+2.0f7ce80e', '6.0.0', '7.0.0')).to.equal(true);
expect(semverSatisfies('7.0.0', '6.13.0')).to.equal(true);
});
});

0 comments on commit 8b18b84

Please sign in to comment.