Methods Overview

We’ll cover two main ways to take offers:

  1. Fixed Pricing – A set price defined by the maker.
  2. Dynamic Pricing – Rates pulled from on-chain price feeds, with a customizable slippage cap.

Depending on the offer’s takingOfferType, you may need to take the full amount (BlockOffer) or can take any partial amount (PartialOffer).


1. takeOfferFixed

function takeOfferFixed(
    uint256 offerId,
    uint256 withdrawalAmountPaid,
    address affiliate
) external;

What it does: You pay a fixed amount of the withdrawal asset to the maker, and receive the maker’s deposit asset from escrow.

Tip: If the offer is a BlockOffer, you must supply the full remaining amount. For PartialOffer, you can supply any amount up to what’s left.

Parameters:

NameTypeDescription
offerIduint256ID of the offer (from the API).
withdrawalAmountPaiduint256Amount you’ll pay (in token’s smallest units).
affiliateaddress(Optional) Affiliate address for fee sharing; use 0x000…000 if none.

Possible Errors:

ErrorWhen You Might See It
IncorrectOfferPricingType(type)You tried this on a non-fixed offer.
BlockOfferShouldBePaidFully(paid)BlockOffer but your payment was less than the full price.
DotcOfferHelper: OfferNotExistsOffer ID is invalid or already taken/cancelled.
OfferHelper: OfferExpiredOffer has expired.
AssetHelper: InsufficientAssetOwnerYour balance/allowance is too low.
SafeTransferLib: TransferFailedToken transfer failed (e.g. missing approval).

2. takeOfferDynamic

function takeOfferDynamic(
    uint256 offerId,
    uint256 withdrawalAmountPaid,
    uint256 maximumDepositToWithdrawalRate,
    address affiliate
) external;

What it does: Fetches live rates for your assets, caps slippage at maximumDepositToWithdrawalRate, then swaps your withdrawal asset for the maker’s deposit asset.

Tip: We recommend to copy the depositToWithdrawalRate from the API dotc_offers response. If you set maximumDepositToWithdrawalRate to 0, it will use the current rate without any extra cap.

Parameters:

NameTypeDescription
offerIduint256ID of the offer.
withdrawalAmountPaiduint256Amount you’ll pay (in token’s smallest units).
maximumDepositToWithdrawalRateuint256Max on-chain rate you’ll accept (in withdrawal token decimals).
affiliateaddress(Optional) Affiliate address; 0x000…000 if none.

Possible Errors:

ErrorWhen You Might See It
IncorrectOfferPricingType(type)You tried this on a non-dynamic offer.
DepositToWithdrawalRateOverflow()On-chain rate exceeded your max cap.
BlockOfferShouldBePaidFully(paid)BlockOffer but your payment was less than the required price.
InsufficientFundsForERC721(paid, price)NFT offer but your payment didn’t cover the single-item price.
DotcOfferHelper: OfferNotExistsOffer ID is invalid or already taken/cancelled.
OfferHelper: OfferExpiredOffer has expired.
AssetHelper: InsufficientAssetOwnerYour balance/allowance is too low.
SafeTransferLib: TransferFailedToken transfer failed.

Usage Tutorial

Examples use TypeScript & Ethers.js You can adapt these steps to any language or library.

1. Prerequisites

2. Full Example Script

Below is a complete script that fetches offers, approves your token, and takes an offer based on its pricing type. Copy, update your private key, API key, then run!

import { ethers } from "ethers";

async function main() {
  // 1. Fetch offers from the API
  const response = await fetch(
    "https://rpq.swarm.com/v1/client/dotc_offers?page=0&limit=1&network=polygon&withdrawalAssetSymbol=USDC",
    {
      headers: { "X-API-Key": "API_KEY" }
    }
  );
  const { offers } = await response.json();

  // 2. Setup wallet and contracts
  const PRIVATE_KEY = "a3f...";  // ← your wallet private key
  const RPC_URL      = "https://polygon-rpc.com/";  // ← any valid RPC endpoint
  const DOTC_ADDRESS = ethers.getAddress("0x22593b8749a4e4854c449c30054bb4d896374fa1"); // ← our DOTC contract address for your network (polygon in this example)

  const provider = new ethers.JsonRpcProvider(RPC_URL);
  const wallet   = new ethers.Wallet(PRIVATE_KEY, provider);
  const dotc     = new ethers.Contract(
    DOTC_ADDRESS,
    [
      "function takeOfferFixed(uint256,uint256,address)",
      "function takeOfferDynamic(uint256,uint256,uint256,address)"
    ],
    wallet
  );

  // 3. Approve the withdrawal token
  async function approveToken(tokenAddress: string, amount: bigint) {
    const erc20 = new ethers.Contract(
      tokenAddress,
      ["function approve(address,uint256) external"],
      wallet
    );
    const tx = await erc20.approve(DOTC_ADDRESS, amount);
    console.log("Approval tx hash:", tx.hash);
    await provider.waitForTransaction(tx.hash);
    console.log("✔️ Token approved!");
  }

  // 4a. Take a fixed-price offer
  async function takeFixed(
    offerId: bigint,
    amount: bigint,
    affiliate: string = ethers.ZeroAddress
  ) {
    const tx = await dotc.takeOfferFixed(offerId, amount, affiliate);
    console.log("takeOfferFixed tx hash:", tx.hash);
    const receipt = await provider.waitForTransaction(tx.hash);
    console.log("✔️ Fixed offer taken!", receipt);
  }

  // 4b. Take a dynamic-price offer
  async function takeDynamic(
    offerId: bigint,
    amount: bigint,
    maxRate: bigint,
    affiliate: string = ethers.ZeroAddress
  ) {
    const tx = await dotc.takeOfferDynamic(offerId, amount, maxRate, affiliate);
    console.log("takeOfferDynamic tx hash:", tx.hash);
    const receipt = await provider.waitForTransaction(tx.hash);
    console.log("✔️ Dynamic offer taken!", receipt);
  }

  // 5. Execute based on pricing type
  const offer = offers[0];
  const offerId = BigInt(offer.id); // convert hex id to decimals
  const amountIn = BigInt(offer.amountIn); // by passing amountIn we can take the whole offer

  // Approve
  await approveToken(offer.withdrawalAsset.address, amountIn);

  if (offer.offerPrice.pricingType === "FixedPricing") {
    await takeFixed(offerId, amountIn);
  } else if (offer.offerPrice.pricingType === "DynamicPricing") {
    const maxRate = BigInt(offer.depositToWithdrawalRate);
    await takeDynamic(offerId, amountIn, maxRate);
  }
}

main();

3. Run It

Use any online IDE (replit, codesandbox, etc) or save as trade.ts, then:

npm install ethers
npx ts-node trade.ts