From bc1788db21424614bce2149be9cdab96537ff787 Mon Sep 17 00:00:00 2001 From: Ryan Ghods Date: Tue, 2 Jan 2024 13:32:44 -0800 Subject: [PATCH] fix basis points rounding --- src/constants.ts | 6 ++++-- src/sdk.ts | 7 ++++--- src/utils/utils.ts | 13 ++++++++++--- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index ab6055113..afc8d9436 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,12 +1,14 @@ -import { ethers } from "ethers"; +import { FixedNumber, ZeroAddress } from "ethers"; +export const FIXED_NUMBER_100 = FixedNumber.fromValue(100); export const INVERSE_BASIS_POINT = 10_000; // 100 basis points per 1% +export const INVERSE_BASIS_POINT_BIGINT = BigInt(INVERSE_BASIS_POINT); export const MAX_EXPIRATION_MONTHS = 1; export const API_BASE_MAINNET = "https://api.opensea.io"; export const API_BASE_TESTNET = "https://testnets-api.opensea.io"; -export const DEFAULT_ZONE = ethers.ZeroAddress; +export const DEFAULT_ZONE = ZeroAddress; export const ENGLISH_AUCTION_ZONE_MAINNETS = "0x110b2b128a9ed1be5ef3232d8e4e41640df5c2cd"; export const ENGLISH_AUCTION_ZONE_TESTNETS = diff --git a/src/sdk.ts b/src/sdk.ts index e80d2bc4a..add6c7ed3 100644 --- a/src/sdk.ts +++ b/src/sdk.ts @@ -22,6 +22,7 @@ import { OpenSeaAPI } from "./api/api"; import { CollectionOffer, Listing, NFT, Order } from "./api/types"; import { INVERSE_BASIS_POINT, + INVERSE_BASIS_POINT_BIGINT, DEFAULT_ZONE, ENGLISH_AUCTION_ZONE_MAINNETS, ENGLISH_AUCTION_ZONE_TESTNETS, @@ -248,7 +249,7 @@ export class OpenSeaSDK { ): string => { return ( (amount * BigInt(basisPoints)) / - BigInt(INVERSE_BASIS_POINT) + INVERSE_BASIS_POINT_BIGINT ).toString(); }; @@ -1106,10 +1107,10 @@ export class OpenSeaSDK { // Validation if (startAmount == null || startAmountWei < 0) { - throw new Error(`Starting price must be a number >= 0`); + throw new Error("Starting price must be a number >= 0"); } if (isEther && orderSide === OrderSide.BID) { - throw new Error(`Offers must use wrapped ETH or an ERC-20 token.`); + throw new Error("Offers must use wrapped ETH or an ERC-20 token."); } if (priceDiffWei < 0) { throw new Error( diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 42a5daedf..9ac3b6640 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -2,11 +2,12 @@ import { CROSS_CHAIN_SEAPORT_V1_5_ADDRESS, ItemType, } from "@opensea/seaport-js/lib/constants"; -import { ethers } from "ethers"; +import { ethers, FixedNumber } from "ethers"; import { MAX_EXPIRATION_MONTHS, SHARED_STOREFRONT_LAZY_MINT_ADAPTER_CROSS_CHAIN_ADDRESS, SHARED_STOREFRONT_ADDRESSES, + FIXED_NUMBER_100, } from "../constants"; import { Chain, @@ -225,8 +226,14 @@ export const getAddressAfterRemappingSharedStorefrontAddressToLazyMintAdapterAdd * @returns sum of basis points */ export const feesToBasisPoints = (fees: Fee[]): number => { - const feeBasisPoints = fees.map((fee) => fee.fee * 100); - return feeBasisPoints.reduce((sum, basisPoints) => basisPoints + sum, 0); + const feeBasisPoints = fees.map((fee) => + Number(FixedNumber.fromValue(fee.fee).mul(FIXED_NUMBER_100).toString()), + ); + const totalBasisPoints = feeBasisPoints.reduce( + (sum, basisPoints) => basisPoints + sum, + 0, + ); + return totalBasisPoints; }; /**