> For the complete documentation index, see [llms.txt](https://instadapp-3.gitbook.io/instadapp-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://instadapp-3.gitbook.io/instadapp-docs/dsa-sdk/usecases/debt-swap.md).

# Debt Swap

**Step 1**

[Installation](https://docs.instadapp.io/installation) or just use your browser console.

**Step 2**

[Setup Account](https://docs.instadapp.io/setup)

**Step 3**

Trigger the following uniquely designed spells to fulfill this use case. Check [this section](https://docs.instadapp.io/cast) for generic details around casting spells.

The DSA will cast the spells across the [Compound](https://docs.instadapp.io/connectors/compound/), [OasisDEX](https://docs.instadapp.io/connectors/oasis/) and [Instapool](https://docs.instadapp.io/connectors/instapool/) connectors in the specified sequence.

#### USDC Debt -> DAI Debt <a href="#usdc-debt---dai-debt" id="usdc-debt---dai-debt"></a>

**Benefits**

* When DAI > $1. Arbitrage benefit due to price difference of DAI & USDC.
* Help make DAI stable.

**Recipe**

1. **Instapool**: borrow DAI
2. **OasisDEX**: Swap DAI for USDC
3. **Compound**: Payback USDC
4. **Compound**: Borrow DAI
5. **Instapool**: return DAI

**Requirements**

* User must have a some debt in USDC.

```
let borrowAmount = 20; // 20 DAI
let borrowAmtInWei = dsa.tokens.fromDecimal(borrowAmount, "dai"); // borrow flash loan and swap via OasisDEX

let slippage = 0.1; // 0.1% slippage.
let dai_address = dsa.tokens.info.dai.address
let usdc_address = dsa.tokens.info.usdc.address

let buyAmount = await dsa.oasis.getBuyAmount("USDC", "DAI", borrowAmount, slippage);

let spells = dsa.Spell();

spells.add({
  connector: "instapool",
  method: "flashBorrow",
  args: [dai_address, borrowAmtInWei, 0, 0]
});

spells.add({
  connector: "oasis",
  method: "sell",
  args: [usdc_address, dai_address, borrowAmtInWei, buyAmount.unitAmt, 0, "423"] // setting USDC amount with id 423
});

spells.add({
  connector: "compound",
  method: "payback",
  args: [usdc_address, 0, "423", 0] // get Payback amount with id 423
});

spells.add({
  connector: "compound",
  method: "borrow",
  args: [dai_address, borrowAmtInWei, 0, 0]
});

spells.add({
  connector: "instapool",
  method: "flashPayback",
  args: [dai_address, 0, 0]
});

dsa.cast(spells).then(console.log)
```

#### DAI Debt -> USDC Debt <a href="#dai-debt---usdc-debt" id="dai-debt---usdc-debt"></a>

**Benefits**

* When DAI < $1. Arbitrage benefit due to price difference of DAI & USDC.
* Help make DAI stable.

**Recipe**

1. **Instapool**: borrow USDC
2. **OasisDEX**: Swap USDC for DAI
3. **Compound**: Payback DAI
4. **Compound**: Borrow USDC
5. **Instapool**: return USDC

**Requirements**

* User must have a some debt in DAI.

```
let borrowAmount = 20; // 20 USDC
let borrowAmtInWei = dsa.tokens.fromDecimal(borrowAmount, "usdc"); // borrow flash loan and swap via OasisDEX

let slippage = 0.2; // 0.2% slippage.
let dai_address = dsa.tokens.info.dai.address
let usdc_address = dsa.tokens.info.usdc.address

let buyAmount = await dsa.oasis.getBuyAmount("DAI", "USDC", borrowAmount, slippage);

let spells = dsa.Spell();

spells.add({
  connector: "instapool",
  method: "flashBorrow",
  args: [usdc_address, borrowAmtInWei, 0, 0]
});

spells.add({
  connector: "oasis",
  method: "sell",
  args: [dai_address, usdc_address, borrowAmtInWei, buyAmount.unitAmt, 0, "924"] // setting DAI amount with id 924
});

spells.add({
  connector: "compound",
  method: "payback",
  args: [dai_address, 0, "924", 0] // get Payback amount with id 924
});

spells.add({
  connector: "compound",
  method: "borrow",
  args: [usdc_address, borrowAmtInWei, 0, 0]
});

spells.add({
  connector: "instapool",
  method: "flashPayback",
  args: [usdc_address, 0, 0]
});

dsa.cast(spells).then(console.log)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://instadapp-3.gitbook.io/instadapp-docs/dsa-sdk/usecases/debt-swap.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
