Skip to content

Instantly share code, notes, and snippets.

@abdelghafour
Last active March 6, 2024 20:27
Show Gist options
  • Save abdelghafour/71a698034ce35054c2ba7f22860a52f8 to your computer and use it in GitHub Desktop.
Save abdelghafour/71a698034ce35054c2ba7f22860a52f8 to your computer and use it in GitHub Desktop.
BTC Collateralization Through Flow API

Create the Stake:

import axios from "axios";


const headers = { Authorization: MY_COMPANY_API_KEY };
const data = {
  fee: SUGGESTED_NETWORK_FEES,
  staker: {
    cardinal: {
      amount: STAKING_AMOUNT,
      value: STAKER_BTC_ADDRESS,
      publicKey: STAKER_BTC_PUBLIC_KEY,
    },
  },
  expiry: moment().add(1, "d").format(),
};

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

Sign and broadcast the Stake:

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

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


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

Unlock the Stake:

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

const headers = { Authorization: MY_COMPANY_API_KEY };
const data = {
  state: "unstake",
  fee: SUGGESTED_NETWORK_FEES,
  amount: UNSTAKE_AMOUNT,
  index: INDEX_OF_THE_PATH_YOU_WOULD_LIKE_TO_UNSTAKE,
};
const where =  qs.stringify({ where: { id: FLOW_ID_FROM_STAKE_STEP } });

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

Sign and broadcast the UnStake:

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

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

const { data: escrow } = 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