Skip to main content

Marketplace Contract Technical Reference

This is a technical reference for the Media Protocol Marketplace smart contract.

Structs

Provider

struct Provider {
string metadata;
string publicKey;
}
NameType
metadatastringMetadata for the provider.
publicKeystringThe encryption public key shared to the clients to encrypt their resources.

OfferTerms

struct OfferTerms {
uint pricePerSecond;
uint minDealDuration;
bool billFullPeriods;
bool singlePeriodOnly;
string metadata;
}
NameType
pricePerSeconduintPrice per second for the deals.
minDealDurationuintThe minimum duration that deal's must have, expressed in seconds.
billFullPeriodsboolIf enabled, once the minimum deal's duration has elapsed the next billing will cover a complete billing period up to the minimum duration.
singlePeriodOnlyboolIf enabled, only one period will be billed and then the deal will be considered finished. (Can't extend for more periods)
metadatastringUseful additional information about the offer.

Offer

struct Offer {
uint id;
address provider;
string publicKey;
uint maximumDeals;
bool autoAccept;
OfferTerms terms;
}
NameType
iduintThe id of the offer.
provideraddressThe address of the provider.
publicKeystringThe encryption public key shared to the clients to encrypt their resources.
maximumDealsuintThe maximum amount of deals that this offer can provide.
autoAcceptboolDefines whether the offer will auto-accept deals or not.
termsOfferTermsThe terms of the offer.

Deal

struct Deal {
uint id;
uint offerId;
address client;
address provider;
uint resourceId;
uint totalPayment;
uint blockedBalance;
OfferTerms terms;
Status status;
}
NameType
iduintThe id of the deal.
offerIduintThe id of the offer.
clientaddressThe address of the client.
provideraddressThe address of the provider.
resourceIduintThe id of the resource.
totalPaymentuintThe total payment for the deal.
blockedBalanceuintThe amount of tokens blocked for the deal.
termsOfferTermsThe terms of the deal.
statusStatusThe status of the deal.

Status

struct Status {
bool active;
uint createdAt;
uint acceptedAt;
uint billingStart;
bool cancelled;
uint cancelledAt;
}
NameType
activeboolWhether the deal is active or not.
createdAtuintThe timestamp when the deal was created.
acceptedAtuintThe timestamp when the deal was accepted.
billingStartuintThe timestamp when the billing started.
cancelledboolWhether the deal was cancelled or not.
cancelledAtuintThe timestamp when the deal was cancelled.

Public State Variables

General Variables

NameType
authorizedProxiesmapping(address => bool)Mapping of authorized addresses.
authorizedAddressesaddress[]Array of authorized addresses.
protocolFeeToaddressAddress that will receive the protocol fees.
protocolFeeRateuint256The rate of the protocol fees.
marketplacesCounteruintMarketplaces counter.
positionManagerIUniswapV3PositionManagerUniswap V3 position manager.

Marketplaces Variables

The following variables are mappings that store the data specific to each marketplaceId.

NameType
ownersmapping(uint => address)Mapping of marketplaces owners.
marketFeeTomapping(uint => address)Mapping of marketplaces fee recipients.
marketFeeRatemapping(uint => uint)Mapping of marketplaces fee rates.
REQUIRED_STAKEmapping(uint => uint)Mapping of required stakes for providers.
offerCountermapping(uint => uint)Mapping of offer counters.
offerAutoIncrementmapping(uint => uint)Mapping of offer auto-increments.
dealAutoIncrementmapping(uint => uint)Mapping of deal auto-increments.

Write Functions

initializeMarketplace

Initializes a new marketplace.

function initializeMarketplace(
uint requiredStake,
address _marketFeeTo,
uint256 _marketFeeRate
) external nonReentrant returns (uint marketplaceId);

Parameters:

NameType
requiredStakeuintThe required staked liquidity to be a provider.
_marketFeeToaddressThe address that will receive the marketplace fees.
_marketFeeRateuintThe marketplace fee rate. 100 to 100000 (0.01% to 10%).

Return values:

NameType
marketplaceIduintThe id of the created marketplace.

setRequiredStake

Sets the required stake for a marketplace.

function setRequiredStake(
uint marketplaceId,
uint requiredStake
) external nonReentrant returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace.
requiredStakeuintThe required liquidity to be a provider.

Return values:

Type
boolWhether the operation was successful or not.

transferMarketplaceOwnership

Transfers the ownership of a marketplace.

function transferMarketplaceOwnership(
uint marketplaceId,
address newOwner
) external nonReentrant returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace.
newOwneraddressThe address of the new owner.

Return values:

Type
boolWhether the operation was successful or not.

setMarketFeeRate

Allows marketplace owners to set the fee rate for their marketplace.

function setMarketFeeRate(
uint marketplaceId,
uint256 _feeRate
) external nonReentrant returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace you want to set the fee for.
_feeRateuintThe fee rate you want to set. 100 to 100000 (0.01% to 10%).

Return:

Type
boolWhether the setting was successful or not.

setMarketFeeTo

Allows marketplace owners to set the fee recipient for their marketplace.

function setMarketFeeTo(
uint marketplaceId,
address _feeTo
) external nonReentrant returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace you want to set the fee for.
_feeToaddressThe fee recipient you want to set.

Return:

Type
boolWhether the setting was successful or not.

registerProvider

Registers as provider in order to be able to create offers.

function registerProvider(
uint marketplaceId,
string calldata metadata,
string calldata publicKey,
uint nftId
) external nonReentrant onlyAuthorized returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace you are registering to.
metadatastringMetadata for the provider.
publicKeystringThe encryption public key shared to the clients to encrypt their resources.
nftIduintThe Uniswap V3 NFT ID with the required liquidity.

Return values:

Type
boolWhether the registration was successful or not.

unregisterProvider

Unregister as a provider and recover the staked lp tokens.

function unregisterProvider(
uint marketplaceId
) external nonReentrant onlyAuthorized returns(uint nftId);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.

Return values:

NameType
nftIduintThe unstaked Uniswap V3 NFT ID with the liquidity.

updateProvider

Updates the provider's information.

function updateProvider(
uint marketplaceId,
string calldata metadata,
string calldata publicKey
) external onlyAuthorized nonReentrant returns (bool updated);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
metadatastringThe new metadata for the provider.
publicKeystringThe new encryption public key

Return values:

NameType
updatedboolWhether the update was successful or not.

increaseProviderStake

Increases the provider's stake.

function increaseProviderStake(
uint marketplaceId,
uint liquidity,
uint amount0,
uint amount1
) external nonReentrant onlyAuthorized returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
liquidityuintThe amount of liquidity to increase.
amount0uintThe amount of token0 to increase.
amount1uintThe amount of token1 to increase.

Return values:

Type
boolWhether the increase was successful or not.

decreaseProviderStake

Decreases the provider's stake.

function decreaseProviderStake(
uint marketplaceId,
uint amount0,
uint amount1
) external nonReentrant onlyAuthorized returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
amount0uintThe amount of token0 to decrease.
amount1uintThe amount of token1 to decrease.

Return values:

Type
boolWhether the decrease was successful or not.

createOffer

Creates a new offer.

function createOffer(
uint marketplaceId,
uint maximumDeals,
bool autoAccept,
uint pricePerSecond,
uint minDealDuration,
bool billFullPeriods,
bool singlePeriodOnly,
string calldata metadata
) external nonReentrant returns (uint offerId);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
maximumDealsuintThe maximum amount of deals that this offer can provide.
autoAcceptboolDefines whether the offer will auto-accept deals or not.
pricePerSeconduintPrice per second for the deals.
minDealDurationuintThe minimum duration that deal's must have, expressed in seconds.
billFullPeriodsboolIf enabled, once the minimum deal's duration has elapsed the next billing will cover a complete billing period up to the minimum duration.
singlePeriodOnlyuintIf enabled, only one period will be billed and then the deal will be considered finished. (Can't extend for more periods)
metadatastringUseful additional information about the offer.

Return values:

NameType
offerIduintThe id of the created offer.

updateOffer

Updates an offer. It is important to clarify that once a Deal is taken, it cannot be modified. If you modify a offer with opened deals, only new Deals will follow the updated Offer parameters.

function updateOffer(
uint marketplaceId,
uint offerId,
uint maximumDeals,
bool autoAccept,
uint pricePerSecond,
uint minDealDuration,
bool billFullPeriods,
bool singlePeriodOnly,
string calldata metadata
) external nonReentrant (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
offerIduintThe id of the offer you want to update.
maximumDealsuintThe maximum amount of deals that this offer can provide.
autoAcceptboolDefines whether the offer will auto-accept deals or not.
pricePerSeconduintPrice per second for the deals.
minDealDurationuintThe minimum duration that deal's must have, expressed in seconds.
billFullPeriodsboolIf enabled, once the minimum deal's duration has elapsed the next billing will cover a complete billing period up to the minimum duration.
singlePeriodOnlyuintIf enabled, only one period will be billed and then the deal will be considered finished. (Can't extend for more periods)
metadatastringUseful additional information about the offer.

Return values:

Type
boolWhether the update was successful or not.

deleteOffer

Allows providers to delete their active offers.

function deleteOffer(
uint marketplaceId,
uint offerId
) external nonReentrant returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
offerIduintThe id of the offer you want to update.

Return values:

Type
boolWhether the deletion was successful or not.

acceptDeal

Allows providers to accept a deal.

function acceptDeal(uint marketplaceId, uint dealId) external nonReentrant returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
dealIduintThe id of the deal you want to accept.

Return:

Type
boolWhether the acceptance was successful or not.

rejectDeal

Allows providers to reject a deal.

function rejectDeal(uint marketplaceId, uint dealId) external nonReentrant returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
dealIduintThe id of the deal you want to reject.

Return:

Type
boolWhether the rejection was successful or not.

updateClient

Allows clients to update their metadata.

function updateClient(
uint marketplaceId,
string calldata metadata
) external nonReentrant returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
metadatastringThe new metadata for the client.

Return:

Type
boolWhether the update was successful or not.

createDeal

Allows a client to create a Deal from an Offer.

function createDeal(
uint marketplaceId,
uint resourceId,
uint offerId,
uint blockedBalance,
string calldata sharedKeyCopy
) external onlyAuthorized nonReentrant returns (
uint dealId,
uint actualDeposit
);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
resourceIduintThe id of the resource you want to create deals for.
offerIduintThe id of the offer you want to create deals for.
blockedBalanceuintThe amount of tokens you want to block for the deal.
sharedKeyCopystringThe encryption key for the deal.

Return values:

NameType
dealIduintThe id of the created deal.
actualDeposituintThe amount of tokens blocked for the deal.

createDeals

Allow clients to create deals from an array of offers for a single resource.

function createDeals(
uint marketplaceId,
uint resourceId,
uint[] calldata offerIds,
uint[] calldata blockedBalance,
string[] calldata _sharedKeyCopies
) external nonReentrant onlyAuthorized returns (uint[] memory dealsId, uint totalDeposit);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
resourceIduintThe id of the resource you want to create deals for.
offerIdsuint[]The ids of the offers you want to create deals for.
blockedBalanceuint[]The amount of tokens you want to block for each deal.
_sharedKeyCopiesstring[]The encryption keys for each deal.

Return values:

NameType
dealsIduint[]The ids of the created deals.
totalDeposituintThe total amount of tokens blocked for the deals.

cancelDeal

Allows clients and providers to cancel a deal.

function cancelDeal(uint marketplaceId, uint dealId) external nonReentrant returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
dealIduintThe id of the deal you want to cancel.

Return:

Type
boolWhether the cancellation was successful or not.

cancelAllDeals

Allows providers to cancel all their deals.

function cancelAllDeals(
uint marketplaceId,
uint resourceId
) external nonReentrant returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
resourceIduintThe id of the resource you want to cancel deals for.

Return:

Type
boolWhether the cancellation was successful or not.

addBalance

Allows clients to add balance to a deal.

function addBalance(
uint marketplaceId,
uint dealId,
uint blockedBalance
) external nonReentrant returns (uint actualDeposit);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
dealIduintThe id of the deal you want to add balance to.
blockedBalanceuintThe amount of tokens you want to add to the deal.

Return:

NameType
actualDeposituintThe amount of tokens that were actually deposited.

collectDeal

Allows clients and providers to collect a deal.

function collectDeal(
uint marketplaceId,
uint dealId
) external nonReentrant returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
dealIduintThe id of the deal you want to collect.

Return:

Type
boolWhether the collection was successful or not.

collectAllDeals

Allows providers to collect all their deals.

function collectAllDeals(uint marketplaceId) external nonReentrant returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.

Return:

Type
boolWhether the collection was successful or not.

extendBillingStart

Allows providers to extend the billing start of a deal.

function extendBillingStart(
uint marketplaceId,
uint dealId,
uint extension
) external nonReentrant returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace where you are registered.
dealIduintThe id of the deal you want to extend.
extensionuintThe amount of time you want to extend the deal.

Return:

Type
boolWhether the extension was successful or not.

authorizeProxy

Allows the contract owner to authorize a new address.

function authorizeProxy(address _addr) external onlyOwner returns (bool);

Parameters:

NameType
_addraddressThe address you want to authorize.

Return:

Type
boolWhether the authorization was successful or not.

deAuthorizeProxy

Allows the contract owner to de-authorize an address.

function deAuthorizeProxy(address _addr) external onlyOwner returns (bool);

Parameters:

NameType
_addraddressThe address you want to de-authorize.

Return:

Type
boolWhether the de-authorization was successful or not.

setProtocolFeeTo

Allows the contract owner to set the protocol fee recipient.

function setProtocolFeeTo(address feeTo) external onlyOwner returns (bool);

Parameters:

NameType
feeToaddressThe address of the protocol fee recipient.

Return:

Type
boolWhether the setting was successful or not.

setProtocolFeeRate

Allows the contract owner to set the protocol fee rate.

function setProtocolFeeRate(uint256 feeRate) external onlyOwner returns (bool);

Parameters:

NameType
feeRateuint256The rate of the protocol fee. 100 to 100000 (0.01% to 10%)

Return:

Type
boolWhether the setting was successful or not.

recoverTokens

Allows the contract owner to recover tokens sent unintentionally to the contract.

function recoverTokens(IERC20 _token) external onlyOwner returns (bool);

Parameters:

NameType
_tokenIERC20The token you want to recover.

Return:

Type
boolWhether the recovery was successful or not.

Read Functions

hasActiveDeals

Returns whether an offer has active deals or not.

function hasActiveDeals(uint marketplaceId, uint offerId) public view returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace.
offerIduintThe id of the offer.

Return:

Type
boolWhether the offer has active deals or not.

isRegisteredProvider

Returns whether a provider is registered or not.

function isRegisteredProvider(
uint marketplaceId,
address provider
) public view returns (bool);

Parameters:

NameType
marketplaceIduintThe id of the marketplace.
provideraddressThe address of the provider.

Return:

Type
boolWhether the provider is registered or not.

getProvider

Returns the provider details.

function getProvider(uint marketplaceId, address provider) external view returns (Provider memory);

Parameters:

NameType
marketplaceIduintThe id of the marketplace.
provideraddressThe address of the provider.

Return:

Type
Provider memoryThe provider details.

getClient

Returns the client details.

function getClient(
uint marketplaceId,
address client
) external view returns (string memory);

Parameters:

NameType
marketplaceIduintThe id of the marketplace.
clientaddressThe address of the client.

Return:

Type
string memoryThe client details.

getOffer

Returns the details of an offer.

function getOffer(uint marketplaceId, uint offerId) external view returns (Offer memory);

Parameters:

NameType
marketplaceIduintThe id of the marketplace.
offerIduintThe id of the offer.

Return:

Type
Offer memoryThe offer details.

getDeal

Returns the details of a deal.

function getDeal(uint marketplaceId, uint dealId) external view returns (Deal memory);

Parameters:

NameType
marketplaceIduintThe id of the marketplace.
dealIduintThe id of the deal.

Return:

Type
Deal memoryThe deal details.

getProviderOffers

Returns a list of offers a provider has created.

function getProviderOffers(uint marketplaceId, address provider) external view returns (uint[] memory);

Parameters:

NameType
marketplaceIduintThe id of the marketplace.
provideraddressThe address of the provider.

Return:

Type
uint[] memoryAn array of offer ids.

getOfferDealsIds

Returns a list of deal ids for an offer.

function getOfferDealsIds(uint marketplaceId, uint offerId) external view returns (uint[] memory);

Parameters:

NameType
marketplaceIduintThe id of the marketplace.
offerIduintThe id of the offer.

Return:

Type
uint[] memoryAn array of deal ids.

getClientDeals

Returns a list of deals a client has created.

function getClientDeals(uint marketplaceId, address client) external view returns (uint[] memory);

Parameters:

NameType
marketplaceIduintThe id of the marketplace.
clientaddressThe address of the client.

Return:

Type
uint[] memoryAn array of deal ids.

getProviderDeals

Returns a list of deals a provider has created.

function getProviderDeals(uint marketplaceId, address provider) external view returns (uint[] memory);

Parameters:

NameType
marketplaceIduintThe id of the marketplace.
provideraddressThe address of the provider.

Return:

Type
uint[] memoryAn array of deal ids.

getStakeAmount

Returns the amount of LP tokens a provider has staked.

function getStakeAmount(uint marketplaceId, address provider) public view returns (uint);

Parameters:

NameType
marketplaceIduintThe id of the marketplace.
provideraddressThe address of the provider.

Return:

Type
uintThe amount of LP tokens a provider has staked.

Events

Events are emitted for significant actions within the contract, such as registration of a provider, creation, acceptance, or cancellation of a deal. All event names and return values are self-descriptive.

  • AddressAuthorized(address _addr)
  • AddressDeauthorized(address _addr)
  • ProviderRegistered(uint _marketplaceId, address _provider)
  • ProviderUpdated(uint _marketplaceId, address _provider)
  • ProviderStakeDecreased(uint _marketplaceId, address _provider, uint _amount0, uint _amount1)
  • ProviderStakeIncreased(uint _marketplaceId, address _provider, uint _liquidity, uint _amount0, uint _amount1)
  • ProviderUnregistered(uint _marketplaceId, address _provider)
  • OfferCreated(uint _marketplaceId, uint _offerId)
  • OfferUpdated(uint _marketplaceId, uint _offerId)
  • OfferDeleted(uint _marketplaceId, uint _offerId)
  • ClientUpdated(uint _marketplaceId, address _client)
  • DealCreated(uint _marketplaceId, uint _dealId, uint _amount, bool _autoAccept)
  • DealAccepted(uint _marketplaceId, uint _dealId, uint _amount)
  • DealRejected(uint _marketplaceId, uint _dealId, uint _clientRefund)
  • DealCancelled(uint _marketplaceId, uint _dealId, uint _paymentToProvider, uint _totalPayment, uint _clientRefund)
  • DealCollected(uint _marketplaceId, uint _dealId, uint _paymentToProvider, uint _totalPayment)
  • AddedBalance(uint _marketplaceId, uint _blockedBalance, uint _dealId)
  • MarketplaceInitialized(uint _marketplaceId)
  • BillingStartExtended(uint marketplaceId, uint dealId, uint _extension)

Modifiers

Modifiers are used to conditionally change the behavior of functions. For instance, certain functions can only be executed by the contract owner or by authorized proxies.

onlyAuthorized

The onlyAuthorized modifier ensures that tx.origin is the same as msg.sender or that msg.sender is an authorized proxy. This is implemented to ensure that only the actual providers, clients, or authorized proxies such as the Marketplace Helper can execute specific functions. These functions include registerProvider, unregisterProvider, increaseProviderStake, decreaseProviderStake, createDeal, and createDeals.

onlyOwner

The onlyOwner modifiers ensures that msg.sender is the deplayer (owner). This is implemented to ensure that only the owner of the contract can execute functions like setToken, setProtocolFeeTo, setProtocolFeeRate, recoverTokens, authorizeProxy, and deAuthorizeProxy.

nonReentrant

This modifier makes sure that the function can't be executed again until the previous execution has finished.