Skip to content

Commit

Permalink
fix basis points rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanio committed Jan 2, 2024
1 parent 27054e7 commit bc1788d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down
7 changes: 4 additions & 3 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -248,7 +249,7 @@ export class OpenSeaSDK {
): string => {
return (
(amount * BigInt(basisPoints)) /
BigInt(INVERSE_BASIS_POINT)
INVERSE_BASIS_POINT_BIGINT
).toString();
};

Expand Down Expand Up @@ -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(
Expand Down
13 changes: 10 additions & 3 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
};

/**
Expand Down

0 comments on commit bc1788d

Please sign in to comment.