example-c-delivered-amount.ts

This file ships in @gibs/bridge-sdk, at node_modules/@gibs/bridge-sdk/docs/examples/example-c-delivered-amount.ts.

/**
 * Example C: what the recipient receives from a crossing.
 *
 * 1. Before the send, take the bridge fee off the amount, then the delivery
 *    fee that your fee terms ask for.
 * 2. After the send, read the indexer's `delivery.amount` for a completed
 *    transfer. It is the value that reached the delivery router. Take the
 *    fee director's cut off it with `directedDeliveryFee`.
 *
 * Typechecked only (`docs/examples/tsconfig.json` in `@gibs/bridge-sdk`). Never run against mainnet.
 */
import { FeeType } from '@gibs/bridge-sdk/fee-type';
import {
  amountAfterBridgeFee,
  bridgeCost,
  type DirectedDeliveryFee,
  deliveryFee,
  directedDeliveryFee,
  estimatedAmountOut,
} from '@gibs/bridge-sdk/settings';

/** The delivery fee terms you plan with. They match the fee director that you sign. */
export type PlannedDeliveryTerms = {
  readonly feeType: FeeType;
  /** The percent rate as a fraction of one ether. Read in percent mode only. */
  readonly percentFee: bigint;
  /** The fixed fee, or the gas cap, in base units of the delivered token. */
  readonly limit: bigint;
};

/**
 * The amount the recipient receives, before you send.
 *
 * @param options.amountToBridge - the amount that the crossing spends
 * @param options.bridgeFee - the on-chain bridge fee rate per 1e18, from `loadBridgeFees`
 * @param options.delivery - the fee terms when you buy delivery, or null for a crossing the user claims
 * @returns the amount, or null while a fee is unknown
 */
export const plannedRecipientAmount = ({
  amountToBridge,
  bridgeFee,
  delivery,
}: {
  readonly amountToBridge: bigint;
  readonly bridgeFee: bigint | null;
  readonly delivery: PlannedDeliveryTerms | null;
}): bigint | null => {
  const afterBridgeFee = amountAfterBridgeFee({
    amountToBridge,
    bridgeCost: bridgeCost({ amountToBridge, bridgeFee }),
  });
  if (afterBridgeFee === null) return null;
  if (delivery === null) return afterBridgeFee;
  const fee = deliveryFee({
    shouldDeliver: true,
    routeOffersDelivery: true,
    feeType: delivery.feeType,
    amountAfterBridgeFee: afterBridgeFee,
    percentFee: delivery.percentFee,
    fixedFee: delivery.limit,
    limit: delivery.limit,
  });
  if (fee.status !== 'requested') return afterBridgeFee;
  return estimatedAmountOut({ amountAfterBridgeFee: afterBridgeFee, fee: fee.amount });
};

/** The fee director that rode with the crossing. Read it from the message data. */
export type FeeDirector = {
  readonly settings: bigint;
  readonly limit: bigint;
  readonly multiplier: bigint;
};

/** What the recipient received, and whether the figure is exact. */
export type ReceivedAmount = {
  readonly amount: bigint;
  /** False in gas-tip mode: the router charged the gas, and `amount` is the lowest it can be. */
  readonly exact: boolean;
};

/**
 * The amount the recipient received, after the transfer completed.
 *
 * @param options.deliveryAmount - `delivery.amount` from the indexer, before the router's fee
 * @param options.director - the fee director of the crossing
 * @returns the amount, with whether it is exact
 */
export const receivedRecipientAmount = ({
  deliveryAmount,
  director,
}: {
  readonly deliveryAmount: bigint;
  readonly director: FeeDirector;
}): ReceivedAmount => {
  const fee: DirectedDeliveryFee = directedDeliveryFee({ ...director, value: deliveryAmount });
  return { amount: deliveryAmount - fee.fee, exact: fee.kind === 'exact' };
};