Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getDefaultConduit,
getListingPaymentToken,
getOfferPaymentToken,
getOfferPaymentTokenDecimals,
getSeaportAddress,
} from "./utils/utils"

Expand Down Expand Up @@ -62,7 +63,8 @@ export class OpenSeaSDK extends BaseOpenSeaSDK {
})

const cachedPaymentTokenDecimals: { [address: string]: number } = {}
cachedPaymentTokenDecimals[getOfferPaymentToken(chain).toLowerCase()] = 18
cachedPaymentTokenDecimals[getOfferPaymentToken(chain).toLowerCase()] =
getOfferPaymentTokenDecimals(chain)
cachedPaymentTokenDecimals[getListingPaymentToken(chain).toLowerCase()] = 18

super({
Expand Down
11 changes: 11 additions & 0 deletions src/utils/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,17 @@ export const getOfferPaymentToken = (chain: Chain) => {
}
}

/** Returns the decimals for the chain's default offer currency. */
export const getOfferPaymentTokenDecimals = (chain: Chain): number => {
switch (chain) {
case Chain.Arc:
case Chain.StableChain:
return 6
default:
return 18
}
}

/**
* Returns the default currency for listings on the given chain.
* @param chain The chain to get the listing payment token for
Expand Down
4 changes: 3 additions & 1 deletion src/viem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
getDefaultConduit,
getListingPaymentToken,
getOfferPaymentToken,
getOfferPaymentTokenDecimals,
getSeaportAddress,
} from "./utils/utils"

Expand Down Expand Up @@ -101,7 +102,8 @@ export class OpenSeaSDK extends BaseOpenSeaSDK {
})

const cachedPaymentTokenDecimals: { [address: string]: number } = {}
cachedPaymentTokenDecimals[getOfferPaymentToken(chain).toLowerCase()] = 18
cachedPaymentTokenDecimals[getOfferPaymentToken(chain).toLowerCase()] =
getOfferPaymentTokenDecimals(chain)
cachedPaymentTokenDecimals[getListingPaymentToken(chain).toLowerCase()] = 18

super({
Expand Down
61 changes: 60 additions & 1 deletion test/sdk/getPriceParameters.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { ethers } from "ethers"
import { createPublicClient, defineChain, http } from "viem"
import { describe, expect, test, vi } from "vitest"
import { OpenSeaSDK } from "../../src"
import { ZERO_ADDRESS } from "../../src/constants"
import { OrderSide } from "../../src/types"
import { Chain, OrderSide } from "../../src/types"
import { getChainId, getOfferPaymentToken } from "../../src/utils"
import { OpenSeaSDK as ViemSDK } from "../../src/viem"
import { sdk } from "../utils/sdk"

describe("SDK: _getPriceParameters", () => {
Expand All @@ -27,4 +32,58 @@ describe("SDK: _getPriceParameters", () => {
),
).rejects.toThrow("Too many decimal places")
})

describe.each([
["Arc", Chain.Arc],
["Stable Chain", Chain.StableChain],
])("%s default offer currency", (_name, chain) => {
const chainId = Number(getChainId(chain))
const rpcUrl = "http://127.0.0.1:1"

test.each([
[
"ethers",
() =>
new OpenSeaSDK(
new ethers.JsonRpcProvider(rpcUrl, chainId, {
staticNetwork: true,
}),
{ chain },
),
],
[
"viem",
() => {
const viemChain = defineChain({
id: chainId,
name: chain,
nativeCurrency: { name: "Native", symbol: "NATIVE", decimals: 18 },
rpcUrls: { default: { http: [rpcUrl] } },
})
return new ViemSDK(
{
publicClient: createPublicClient({
chain: viemChain,
transport: http(rpcUrl),
}),
rpcUrl,
},
{ chain },
)
},
],
])("uses six decimals with the %s entrypoint", async (_entrypoint, createSDK) => {
const chainSDK = createSDK()
const getPaymentToken = vi.spyOn(chainSDK.api, "getPaymentToken")

await expect(
(chainSDK as any)._getPriceParameters(
OrderSide.OFFER,
getOfferPaymentToken(chain),
"1.5",
),
).resolves.toEqual({ basePrice: 1500000n })
expect(getPaymentToken).not.toHaveBeenCalled()
})
})
})
13 changes: 13 additions & 0 deletions test/utils/chain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
getListingPaymentToken,
getNativeWrapTokenAddress,
getOfferPaymentToken,
getOfferPaymentTokenDecimals,
getSeaportAddress,
getSignedZone,
usesAlternateProtocol,
Expand Down Expand Up @@ -269,6 +270,18 @@ describe("Utils: chain", () => {
})
})

describe("getOfferPaymentTokenDecimals", () => {
test("returns six for the Arc and Stable Chain offer mirrors", () => {
expect(getOfferPaymentTokenDecimals(Chain.Arc)).toBe(6)
expect(getOfferPaymentTokenDecimals(Chain.StableChain)).toBe(6)
})

test("returns 18 for existing wrapped-native offer currencies", () => {
expect(getOfferPaymentTokenDecimals(Chain.Mainnet)).toBe(18)
expect(getOfferPaymentTokenDecimals(Chain.Base)).toBe(18)
})
})

describe("getListingPaymentToken", () => {
test("returns ETH (0x0) for Mainnet", () => {
expect(getListingPaymentToken(Chain.Mainnet)).toBe(ZERO_ADDRESS)
Expand Down