Skip to main content

Fetching Offers/Deals

The MarketplaceViewer contract is a read-only contract that enables users to view the state of the Marketplace contract. All functions available in the MarketplaceViewer contract are also accessible through the SDK. Click here to view the technical reference for the MarketplaceViewer contract and see all the available functions.

Additionally, there are extra functions added to the SDK to simplify interaction with the MarketplaceViewer contract. Below is a detailed explanation of each of these functions:

getPaginatedOffers

Gets an offers page from a specific marketplace. Using some offer id as a starting point and a number of offers to fetch.

Parameters:

  • marketplaceId: The id of the marketplace to get offers from.
  • start: The index to start fetching offers from. Default is 0.
  • steps: The number of offers to fetch per request. Default is 20.

Return:

  • Return an array of Offer objects

Example:

  import { Sdk, MarketplaceViewer } from 'media-sdk';

// initialize the sdk
const sdk = new Sdk();

const marketplaceViewer = new MarketplaceViewer(sdk);

// get 20 offers per request until there are no more offers
let offers = await marketplaceViewer.getAllOffersPaginating({
marketplaceId: 1,
start: 0,
steps: 20
})

getAllOffersPaginating

Gets all offers from a specific marketplace by paginating through the offers.

Parameters

  • marketplaceId: The id of the marketplace to get offers from.
  • start: The index to start fetching offers from. Default is 0.
  • steps: The number of offers to fetch per request. Default is 20.

Return:

  • Return an array of Offer objects

Example:

  import { Sdk, MarketplaceViewer } from 'media-sdk';

// initialize the sdk
const sdk = new Sdk();

const marketplaceViewer = new MarketplaceViewer(sdk);

// get all offers from a marketplace
let offers = await marketplaceViewer.getAllOffersPaginating({
marketplaceId: 1,
start: 0,
steps: 20
})

getPaginatedDeals

Gets a deals page from a specific marketplace. Using some deal id as a starting point and a number of deals to fetch.

Parameters:

  • marketplaceId: The id of the marketplace to get deals from.
  • start: The index to start fetching deals from. Default is 0.
  • steps: The number of deals to fetch per request. Default is 20.

Return:

  • Return an array of Deal objects

Example:

  import { Sdk, MarketplaceViewer } from 'media-sdk';

// initialize the sdk
const sdk = new Sdk();

const marketplaceViewer = new MarketplaceViewer(sdk);

// get 20 deals per request until there are no more deals
let deals = await marketplaceViewer.getPaginatedDeals({
marketplaceId: 1,
start: 0,
steps: 20
})

getAllDealsPaginating

Gets all deals from a specific marketplace by paginating through the deals.

Parameters:

  • marketplaceId: The id of the marketplace to get deals from.
  • start: The index to start fetching deals from. Default is 0.
  • steps: The number of deals to fetch per request. Default is 20.

Return:

  • Return an array of Deal objects

Example:

  import { Sdk, MarketplaceViewer } from 'media-sdk';

// initialize the sdk
const sdk = new Sdk();

const marketplaceViewer = new MarketplaceViewer(sdk);

// get all deals from a marketplace
let deals = await marketplaceViewer.getAllDealsPaginating({
marketplaceId: 1,
start: 0,
steps: 20
})

getOfferById

Gets an offer by its id.

Parameters:

  • marketplaceId: The id of the marketplace to get the offer from.
  • offerId: The id of the offer to get.

Return

Example:

  import { Sdk, MarketplaceViewer } from 'media-sdk';

// initialize the sdk
const sdk = new Sdk();

const marketplaceViewer = new MarketplaceViewer(sdk);

// get an offer by its id
let offer = await marketplaceViewer.getOfferById({
marketplaceId: 1,
offerId: 1
})

getDealById

Gets a deal by its id.

Parameters:

  • marketplaceId: The id of the marketplace to get the deal from.
  • dealId: The id of the deal to get.

Return

  • Return a Deal object

Example:

  import { Sdk, MarketplaceViewer } from 'media-sdk';

// initialize the sdk
const sdk = new Sdk();

const marketplaceViewer = new MarketplaceViewer(sdk);

// get a deal by its id
let deal = await marketplaceViewer.getDealById({
marketplaceId: 1,
dealId: 1
})

Glossary

Offer

interface Offer {
id: bigint,
provider: Address,
publicKey: string,
maximumDeals: bigint,
autoAccept: boolean,
terms: {
pricePerSecond: bigint,
minDealDuration: bigint,
billFullPeriods: boolean,
singlePeriodOnly: boolean,
metadata: string
}
}

Deal

interface Deal {
id: bigint,
offerId: bigint,
client: Address,
provider: Address,
resourceId: bigint,
totalPayment: bigint,
blockedBalance: bigint,
terms: {
pricePerSecond: bigint,
minDealDuration: bigint,
billFullPeriods: boolean,
singlePeriodOnly: boolean,
metadata: string
},
status: {
active: true,
createdAt: bigint,
acceptedAt: bigint,
billingStart: bigint,
cancelled: boolean,
cancelledAt: bigint
}
}


Address type is part of the viem's types. Check Viem Address for more details.