Skip to content

Commit

Permalink
Merge pull request #103 from defactor-com/feat/update-sdk-counter-par…
Browse files Browse the repository at this point in the history
…ty-pools-documentation

Feat/update sdk counter party pools documentation
  • Loading branch information
xavier506 committed Jun 4, 2024
2 parents f09c652 + d782765 commit dc94d57
Show file tree
Hide file tree
Showing 4 changed files with 288 additions and 47 deletions.
63 changes: 21 additions & 42 deletions docs/introduction/software-development-kit.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ You can find the source code for this repository in [github.com/defactor-com/sdk

- Support for cjs, es, and esm modules.
- Developed using TypeScript for enhanced type safety and clarity.
- Seamless integration with `ERC20CollateralPool` and `Pools`.
- Seamless integration with [`ERC20CollateralPool`](/docs/pools/back-end-api/sdk/collateral-pool) and [`Pools`](/docs/pools/back-end-api/sdk/counterparty-pool).
- Well-defined interfaces that accurately model the Defactor contracts for easy interaction and integration.

This library is built on top of `ether@6.x.x` to provide a simple and easy to use interface to interact with the Defactor contracts.
This library is built on top of [`ether@6.x.x`](https://docs.ethers.org/v6/api/contract/) to provide a simple and easy to use interface to interact with the Defactor contracts.

## Installation

Expand Down Expand Up @@ -44,6 +44,7 @@ This library is built to provide a simple and easy to use interface to interact
### Instantiating the SDK

```typescript
// Collateral Pool Contract
const provider = new SelfProvider(
DefactorSDK.ERC20CollateralPool, // contract constructor
ERC20_COLLATERAL_POOL_ETH_ADDRESS, // contract address
Expand Down Expand Up @@ -75,60 +76,38 @@ const providerInstance = new SelfProvider.SelfProvider<ERC20CollateralPool>(
const liquidationProtocolFee =
providerInstance.contract.LIQUIDATION_PROTOCOL_FEE;

console.log(liquidationProtocolFee);
console.log(liquidationProtocolFee); // 5n
```

## Methods Available
### Integrations with the contracts

```typescript
async USDC_FEES_COLLECTED(): Promise<bigint> // Returns the total USDC fees collected.
async getUsdc(): Promise<string> // Returns the USDC contract address.
async getTotalPools(): Promise<bigint> // Returns the total number of pools.
async getPool(poolId: bigint): Promise<Pool> // Returns the pool with the given ID.
async getPools(offset: bigint, limit: bigint): Promise<Array<Pool>> // Returns a list of pools within the given range.
async getTotalLending(poolId: bigint, address: string): Promise<bigint> // Returns the total amount of lending for a given pool and address.
async getLoan(poolId: bigint, address: string, lendingId: bigint): Promise<Lend> // Returns a specific loan.
async addPool(pool: PoolInput): Promise<ethers.ContractTransaction | ethers.TransactionResponse> // Adds a new pool.
async lend(poolId: bigint, amount: bigint): Promise<ethers.ContractTransaction | ethers.TransactionResponse> // Lends a certain amount to a pool.
async borrow(poolId: bigint, amount: bigint): Promise<ethers.ContractTransaction | ethers.TransactionResponse> // Borrows a certain amount from a pool.
async getBorrow(poolId: bigint, borrowerAddress: string, borrowId: bigint): Promise<Borrow> // Returns a specific borrow.
async calculateCollateralTokenAmount(poolId: bigint, amount: bigint): Promise<bigint> // Calculates the amount of collateral tokens for a given amount.
async repay(poolId: bigint, borrowerAddress: string, borrowId: bigint): Promise<ethers.ContractTransaction | ethers.TransactionResponse> // Repays a borrow.
async getLiquidationInfo(pool: Pool): Promise<PoolLiquidationInfo> // Returns information about the liquidation of a pool.
async liquidatePool(poolId: bigint): Promise<ethers.ContractTransaction | ethers.TransactionResponse> // Liquidates a pool.
```
- Pools
- [`Counterparty Pool`](/docs/pools/back-end-api/sdk/counterparty-pool).
- [`Collateral Pool`](/docs/pools/back-end-api/sdk/collateral-pool)

## Examples

Import the library and instantiate the `SelfProvider` class with the `ERC20CollateralPool` contract.
### Initialize the ERC20

```typescript
import { SelfProvider } from "@defactor/defactor-sdk";
import { ERC20 } from "@defactor/defactor-sdk";

const providerInstance = new SelfProvider.SelfProvider<ERC20CollateralPool>(
ERC20CollateralPool,
contractConfig.contractAddress!, // loaded from config file
contractConfig.providerUrl!, // loaded from config file
contractConfig.privateKey! // loaded from config file
const usdcTokenContract = new Erc20(
contractConfig.usdcTokenAddress, // loaded from config file
contractConfig.providerUrl, // loaded from config file
contractConfig.privateKey // loaded from config file
);
```

### Lending to a Pool
### Approve an amount of ERC20 tokens

```typescript
const poolId = 1;
const amount = BigInt(100_000000); // 100 USDC

const lendTx = await providerInstance.lend(poolId, amount);
console.log(lendTx);
```

### Borrowing from a Pool
Give to the contract the allowance to spend the indicated amount of USDC by the user.

```typescript
const poolId = 1;
const amount = BigInt(10_000000); // 10 USDC
const amount = BigInt(200_000000); // 200 USDC

const borrowTx = await providerInstance.borrow(poolId, amount);
console.log(borrowTx);
await usdcTokenContract.approve(
providerInstance.contract.address, // the address of the contract
amount
);
```
89 changes: 89 additions & 0 deletions docs/pools/back-end-api/sdk/collateral-pool.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
title: "Collateral Pool Contract"
sidebar_position: 2
---
The [Defactor SDK](/docs/introduction/software-development-kit) provides utilities and classes to seamless interaction with the Defactor contracts. In this specific section, the focus is in the [Collateral Pool Contract](/docs/pools/smart-contracts/erc20-collateral-pool-contract/smart-contract-erc20-collateral-pool) and `ERC20CollateralPool` class, which implements the fundamental methods to interact with the contract in question.

## Methods Available

```typescript
// Returns the total USDC fees collected.
async USDC_FEES_COLLECTED(): Promise<bigint>

// Returns the USDC contract address.
async getUsdc(): Promise<string>

// Returns the total number of pools.
async getTotalPools(): Promise<bigint>

// Returns the pool with the given ID.
async getPool(poolId: bigint): Promise<Pool>

// Returns a list of pools within the given range.
async getPools(offset: bigint, limit: bigint): Promise<Array<Pool>>

// Returns the total amount of lending for a given pool and address.
async getTotalLending(poolId: bigint, address: string): Promise<bigint>

// Returns a specific loan.
async getLoan(poolId: bigint, address: string, lendingId: bigint): Promise<Lend>

// Adds a new pool.
async addPool(pool: PoolInput): Promise<ethers.ContractTransaction | ethers.TransactionResponse>

// Lends a certain amount to a pool.
async lend(poolId: bigint, amount: bigint): Promise<ethers.ContractTransaction | ethers.TransactionResponse>

// Borrows a certain amount from a pool.
async borrow(poolId: bigint, amount: bigint): Promise<ethers.ContractTransaction | ethers.TransactionResponse>

// Returns a specific borrow.
async getBorrow(poolId: bigint, borrowerAddress: string, borrowId: bigint): Promise<Borrow>

// Calculates the amount of collateral tokens for a given amount.
async calculateCollateralTokenAmount(poolId: bigint, amount: bigint): Promise<bigint>

// Repays a borrow.
async repay(poolId: bigint, borrowerAddress: string, borrowId: bigint): Promise<ethers.ContractTransaction | ethers.TransactionResponse>

// Returns information about the liquidation of a pool.
async getLiquidationInfo(pool: Pool): Promise<PoolLiquidationInfo>

// Liquidates a pool.
async liquidatePool(poolId: bigint): Promise<ethers.ContractTransaction | ethers.TransactionResponse>
```

## Examples

Import the library and instantiate the `SelfProvider` class with the `ERC20CollateralPool` contract.

```typescript
import { ERC20CollateralPool, SelfProvider } from "@defactor/defactor-sdk";

const providerInstance = new SelfProvider.SelfProvider<ERC20CollateralPool>(
ERC20CollateralPool,
contractConfig.contractAddress!, // loaded from config file
contractConfig.providerUrl!, // loaded from config file
contractConfig.privateKey! // loaded from config file
);
```

### Lending to a Pool

```typescript
const poolId = 1;
const amount = BigInt(100_000000); // 100 USDC

const lendTx = await providerInstance.lend(poolId, amount);
console.log(lendTx);
```

### Borrowing from a Pool

```typescript
const poolId = 1;
const amount = BigInt(10_000000); // 10 USDC

const borrowTx = await providerInstance.borrow(poolId, amount);
console.log(borrowTx);
```
178 changes: 178 additions & 0 deletions docs/pools/back-end-api/sdk/counterparty-pool.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
---
title: "Counterparty Pool Contract"
sidebar_position: 1
---

The [Defactor SDK](/docs/introduction/software-development-kit) provides utilities and classes to seamless interaction with the Defactor contracts. In this specific section, the focus is in the [CounterParty Pool Contract](/docs/pools/smart-contracts/pools-contract/smart-contract-erc20-collateral-pool) and `Pools` class, which implements the fundamental methods to interact with the contract in question.

## Constants

```typescript
INTEREST_DECIMAL_PLACES // Use to adjust the decimals in the interest rate calculation.
POOL_FEE // The required amount of USDC to create a pool.
COLLECT_POOL_MAX_SECS // Maximum unix epoch time for collecting funds from a pool.
COLLECT_POOL_MAX_DAYS // Maximum days for collecting funds from a pool.
MIN_POOL_CLOSED_SECS // Minimum unix epoch time before a pool can be archived
MIN_POOL_CLOSED_DAYS // Minimum days before a pool can be archived
```

## Methods Available

```typescript
// Returns the USDC address configured as the base token.
async USD_ADDRESS(): Promise<string>

// Returns if the contract is paused.
async isPaused(): Promise<boolean>

// Returns the pool with the given poolId.
async getPool(poolId: bigint): Promise<Pool>

// Returns a list of pools within the given range.
async getPools(offset: bigint, limit: bigint): Promise<Pagination<Pool>>

// Pause the contract
async pause(): Promise<ethers.ContractTransaction | ethers.TransactionResponse>

// Unpause the contract
async unpause(): Promise<ethers.ContractTransaction | ethers.TransactionResponse>

// Create a new pool
async createPool(pool: PoolInput): Promise<ethers.ContractTransaction | ethers.TransactionResponse>

// Allows the owner of the pool collects the committed amount and active the pool
async collectPool(poolId: bigint): Promise<ethers.ContractTransaction | ethers.TransactionResponse>

// Allows the owner of the pool deposits the rewards
async depositRewards(
poolId: bigint,
amount: bigint
): Promise<ethers.ContractTransaction | ethers.TransactionResponse>

// Allows to close the pool
async closePool(poolId: bigint): Promise<ethers.ContractTransaction | ethers.TransactionResponse>

// Allows to archive the pool
async archivePool(poolId: bigint): Promise<ethers.ContractTransaction | ethers.TransactionResponse>

// Allows the lender (investor) to commit the specific amount to the pool
async commitToPool(
poolId: bigint,
amount: bigint
): Promise<ethers.ContractTransaction | ethers.TransactionResponse>

// Transfer the committed amount to the pool by the lender (investor)
async uncommitFromPool(poolId: bigint): Promise<ethers.ContractTransaction | ethers.TransactionResponse>

// Allows to the lender (investor) to claim their rewards
async claim(poolId: bigint): Promise<ethers.ContractTransaction | ethers.TransactionResponse>
```

## Examples

### Initialize the SelfProvider

Import the library and instantiate the `SelfProvider` class with the `Pools` contract.

```typescript
import { Pools, SelfProvider } from "@defactor/defactor-sdk";

const provider = new SelfProvider.SelfProvider<Pools>(
Pools,
contractConfig.contractAddress, // loaded from config file
contractConfig.providerUrl, // loaded from config file
contractConfig.AlicePrivateKey // loaded from config file
)
```

### Access to constants

```typescript
const poolCreationFee: bigint = provider.contract.POOL_FEE
```

### Create a pool

In the following example Alice will create a pool with a soft cap of 100 USDC, a hard cap of 600 USDC, a minimum APR of 50 USDC and as collateral 15 of GOLD.

:::info

To successfully create the pool, the contract must have the allowance to transfer the fee in USDC and the collateral tokens.

:::

```typescript
await provider.contract.createPool({
softCap: BigInt(100_000000), // 100 USDC
hardCap: BigInt(600_000000), // 600 USDC
deadline: BigInt(1734652800), // 2024-12-20T00:00:00.000Z
minimumAPR: BigInt(50_000000), // 50 USDC
collateralTokens: [
{
contractAddress: contractConfig.goldTokenAddress, // loaded from config file
amount: BigInt(15_000000), // 15 GOLD
id: null
}
]
})
```

### Commit to a pool

In the following example Bob commits 200 USDC to Alice's pool.

:::info

To successfully commit to the pool, the contract must have the allowance to transfer the amount of USDC.

:::

```typescript
const poolId = BigInt(0)
const amount = BigInt(200_000000) // 200 USDC

const provider = new SelfProvider.SelfProvider<Pools>(
Pools,
contractConfig.contractAddress, // loaded from config file
contractConfig.providerUrl, // loaded from config file
contractConfig.BobPrivateKey // loaded from config file
);

await provider.contract.commitToPool(poolId, amount)
```

### Get a pool

Get a pool using the sequential id.

```typescript
const poolId = BigInt(0)
const pool = await provider.contract.getPool(poolId)

console.log(pool)
```

Example output:

```typescript
{
softCap: 100000000n,
hardCap: 600000000n,
totalCommitted: 0n,
totalRewards: 0n,
rewardsPaidOut: 0n,
createdAt: 1717372800n,
deadline: 1734652800n,
minimumAPR: 50000000n,
closedTime: 0n,
poolOwner: '0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC',
poolStatus: 'CREATED',
collateralTokens: [
{
contractAddress: '0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC',
amount: 15000000n
id: 0n
}
]
}
```
5 changes: 0 additions & 5 deletions docs/pools/back-end-api/sdk/index.md

This file was deleted.

0 comments on commit dc94d57

Please sign in to comment.