Skip to content

sell1155OnSecondary

The sell1155OnSecondary function on the collectorClient allows you to sell Zora ERC1155 tokens for ETH on the secondary market. This function prepares a transaction with slippage protection for selling a specified quantity of tokens for ETH.

Important Limitations

Before using this function, ensure that the following conditions are met:

  1. The ERC1155 token must be configured to use the ZoraTimedSaleStrategy.
  2. The primary mint period for the token must have ended.
  3. The secondary market for the token must have been launched.
  4. There must be sufficient liquidity in the pool for the swap.
  5. The seller must own the tokens they are attempting to sell.

If any of these conditions are not met, the function will return an error and the transaction cannot be executed. Always check the error field in the returned object before proceeding with the transaction.

Usage

example.ts
import {
  useAccount,
  useChainId,
  usePublicClient,
  useWriteContract,
} from "wagmi";
import { createCollectorClient } from "@zoralabs/protocol-sdk";
 
const chainId = useChainId();
const publicClient = usePublicClient()!;
const { address } = useAccount();
 
const collectorClient = createCollectorClient({ chainId, publicClient });
 
// Prepare the sell transaction
const { parameters, price, error } = await collectorClient.sell1155OnSecondary({
  // 1155 contract address
  contract: "0xCD860870DE521cDb0C5ae89E80bBa057Cd30Bf8C",
  // 1155 token id to sell
  tokenId: 1n,
  // quantity of tokens to sell
  quantity: 3n,
  // account that will execute the sell transaction
  account: address!,
  // (optional) Slippage tolerance, ensuring that a minimum amount of ETH is received
  // for the given quantity of 1155 tokens to sell.  Defaults to 0.005 (0.5%)
  slippage: 0.005,
});
 
if (error) {
  throw new Error(error);
}
 
console.log("Price per token (wei):", price!.wei.perToken);
console.log("Total price (wei):", price!.wei.total);
 
const { writeContract } = useWriteContract();
 
// Write the sell transaction to the network
writeContract(parameters);

Returns

{ error?: string, parameters?: SimulateContractParameters, price: QuotePrice }

error

  • Type: string | undefined

An error message if the operation cannot be completed. This could be due to reasons such as insufficient token balance, market not being active, or other contract-specific issues.

parameters

  • Type: SimulateContractParameters | undefined

Prepared parameters for simulating/writing a transaction using viem/wagmi. This will be undefined if there's an error.

price

  • Type: QuotePrice

Detailed information about the price breakdown for the sale. This includes:

  • wei: Price breakdown in wei
    • perToken: Price per individual token in wei
    • total: Total price for all tokens in wei
  • sparks: Price breakdown in sparks
    • perToken: Price per individual token in sparks
    • total: Total price for all tokens in sparks
  • usdc: Async function that returns the price breakdown in USDC
    • perToken: Price per individual token in USDC
    • total: Total price in USDC

Parameters

contract

  • Type: Address

The address of the ERC1155 contract to sell an 1155 of.

tokenId

  • Type: bigint

The ID of the ERC1155 token to sell.

quantity

  • Type: bigint

The quantity of 1155 tokens to sell.

account

  • Type: Address | Account

The account to use for the transaction. This can be either an Ethereum address or a full Account object. This account must have the necessary token balance to complete the sale.

slippage (optional)

  • Type: number
  • Default: 0.005 (0.5%)

The maximum acceptable slippage percentage for the transaction. This helps protect against price movements between the time the transaction is submitted and when it's executed. Slippage protection when selling ensures that a minimum amount of ETH is received for the sale.

recipient (optional)

  • Type: Address

The address that will receive the ETH from the sale. If not specified, the ETH will be sent to the account address.