Skip to content

Instantly share code, notes, and snippets.

@abdelghafour
Last active June 6, 2024 15:09
Show Gist options
  • Save abdelghafour/f964f46ec2da1bd32bd61ca71ac0ad34 to your computer and use it in GitHub Desktop.
Save abdelghafour/f964f46ec2da1bd32bd61ca71ac0ad34 to your computer and use it in GitHub Desktop.
Inscription Lending

Offer

Build an offer

import axios from "axios";

const headers = { Authorization: MY_COMPANY_API_KEY };
const data = {
  fee: SUGGESTED_NETWORK_FEES,
  amount: OFFER_AMOUNT,
  lender: {
    cardinal: {
      value: LENDER_BTC_ADDRESS,
      publicKey: LENDER_BTC_PUBLIC_KEY,
    },
  },
  product: {
    id: PRODUCT_ID,
  },
  expiry: OFFER_EXPIRY_DATE //e.g: moment().add(1, "d").format(),
};

const { data: flow } = await axios.post(
  `${DEEP_LAKE_REST_API_URL}/flows/execute`,
  { data },
  { headers }
);

Broadcast an offer

import axios from "axios";
import qs from "qs";

const headers = { Authorization: MY_COMPANY_API_KEY };
const data = {
  state: "broadcast-offer",
  transactions: [{base64: 'SIGNED_BASE_64'}],
  product: {
    id: PRODUCT_ID,
  },
};
const where =  qs.stringify({ where: { id: FLOW_ID_FROM_OFFER_STEP } });


const { data: flow } = await axios.post(
  `${DEEP_LAKE_REST_API_URL}/flows/execute?${where}`,
  { data },
  { headers }
);

Cancel the offer

import axios from "axios";
import qs from "qs";

const headers = { Authorization: MY_COMPANY_API_KEY };
const data = {
  state: "cancel",
  fee: SUGGESTED_NETWORK_FEES,
  product: {
    id: PRODUCT_ID,
  },
};
const where =  qs.stringify({ where: { id: FLOW_ID_FROM_OFFER_STEP } });

const { data: flow } = await axios.post(
  `${DEEP_LAKE_REST_API_URL}/flows/execute?${where}`,
  { data },
  { headers }
);

Broadcast Cancel offer

import axios from "axios";
import qs from "qs";

const headers = { Authorization: MY_COMPANY_API_KEY };
const data = {
  state: "execute-cancel-signature",
  transactions: [{base64: 'SIGNED_BASE_64'}],
  product: {
    id: PRODUCT_ID,
  },
};

const { data: flow } = await axios.post(
  `${DEEP_LAKE_REST_API_URL}/flows/execute?${where}`,
  { data },
  { headers }
);

Loan using an offer

Spend path

import axios from "axios";
import qs from "qs";

const headers = { Authorization: MY_COMPANY_API_KEY };
const data = {
  state: "spend",
  fee: SUGGESTED_NETWORK_FEES,
  borrower: {
    utxo: {
      id: "ORDINAL_UTXO",
      sequence: ORDINAL_UTXO_INDEX,
    },
    ordinal: {
      value: BORROWER_ORDINAL_ADDRESS,
      publicKey: BORROWER_ORDINAL_PUBLIC_KEY,
    },
    cardinal: {
      value: BORROWER_BTC_ADDRESS,
      publicKey: BORROWER_BTC_PUBLIC_KEY,
    },
  },
  loan: {
    value: PARTIAL_AMOUNT_TO_UNLOCK,
    interest: LOAN_FULL_INTEREST_AMOUNT,
    liquidator: {
      ordinal: {
        value,
        publicKey,
      },
    },
  },
  protocol: {
    cardinal: {
      value: PROTOCOL_BTC_ADDRESS,
      publicKey: PROTOCOL_BTC_PUBLIC_KEY,
    },
  },
  cost: PROTOCOL_COST,
  expiry: OFFER_EXPIRY_DATE //e.g: moment().add(1, "d").format(),
  product: {
    id: PRODUCT_ID,
  },
};

const { data: flow } = await axios.post(
  `${DEEP_LAKE_REST_API_URL}/flows/execute?${where}`,
  { data },
  { headers }
);

Execute Signature for the Spend

import axios from "axios";
import qs from "qs";

const headers = { Authorization: MY_COMPANY_API_KEY };
const data = {
  state: "execute-spend-signature",
  transactions: [{base64: 'SIGNED_BASE_64'}],
  product: {
    id: PRODUCT_ID,
  },
};

const { data: flow } = await axios.post(
  `${DEEP_LAKE_REST_API_URL}/flows/execute?${where}`,
  { data },
  { headers }
);

Repayment

Build Repayment

import axios from "axios";
import qs from "qs";

const headers = { Authorization: MY_COMPANY_API_KEY };
const data = {
  state: "repay",
  amount: REPAYMENT_AMOUNT,
  fee: SUGGESTED_NETWORK_FEES,
  product: {
    id: PRODUCT_ID,
  },
};

const { data: flow } = await axios.post(
  `${DEEP_LAKE_REST_API_URL}/flows/execute?${where}`,
  { data },
  { headers }
);

Broadcast Repayment

import axios from "axios";
import qs from "qs";

const headers = { Authorization: MY_COMPANY_API_KEY };
const data = {
  state: "broadcast-repay",
  transactions: [{base64: 'SIGNED_BASE_64'}],
  product: {
    id: PRODUCT_ID,
  },
};

const { data: flow } = await axios.post(
  `${DEEP_LAKE_REST_API_URL}/flows/execute?${where}`,
  { data },
  { headers }
);

Claim collateral

Claim path

import axios from "axios";
import qs from "qs";

const headers = { Authorization: MY_COMPANY_API_KEY };
const data = {
  state: "claim",
  fee: SUGGESTED_NETWORK_FEES,
  product: {
    id: PRODUCT_ID,
  },
};

const { data: flow } = await axios.post(
  `${DEEP_LAKE_REST_API_URL}/flows/execute?${where}`,
  { data },
  { headers }
);

Execute Signature for the Claim

import axios from "axios";
import qs from "qs";

const headers = { Authorization: MY_COMPANY_API_KEY };
const data = {
  state: "execute-claim-signature",
  transactions: [{base64: 'SIGNED_BASE_64'}],
  product: {
    id: PRODUCT_ID,
  },
};

const { data: flow } = await axios.post(
  `${DEEP_LAKE_REST_API_URL}/flows/execute?${where}`,
  { data },
  { headers }
);

Liquidation collateral

Liquidation path

import axios from "axios";
import qs from "qs";

const headers = { Authorization: MY_COMPANY_API_KEY };
const data = {
  state: "liquidate",
  fee: SUGGESTED_NETWORK_FEES,
  product: {
    id: PRODUCT_ID,
  },
};

const { data: flow } = await axios.post(
  `${DEEP_LAKE_REST_API_URL}/flows/execute?${where}`,
  { data },
  { headers }
);

Execute Signature for the Liquidation

import axios from "axios";
import qs from "qs";

const headers = { Authorization: MY_COMPANY_API_KEY };
const data = {
  state: "execute-liquidate-signature",
  transactions: [{base64: 'SIGNED_BASE_64'}],
  product: {
    id: PRODUCT_ID,
  },
};

const { data: flow } = await axios.post(
  `${DEEP_LAKE_REST_API_URL}/flows/execute?${where}`,
  { data },
  { headers }
);

The flow API uses these APIs under the hood:

Escrow:

  • Create Escrow
  • Execute Escrow
  • Broadcast Escrow
  • Collateral API
  • Asset API
  • Action API

Outcomes:

  • Create Outcomes and integration with the oracle
  • Check one Outcome

Signatures:

  • Execute signature

Logs:

  • Audit trails
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment