Skip to content

Commit

Permalink
docs(examples): liquidation status check, PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
moshenskyDV committed Jan 6, 2025
1 parent f9de360 commit e879b52
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 217 deletions.
94 changes: 0 additions & 94 deletions examples/borrow-variable-rate.ts

This file was deleted.

31 changes: 10 additions & 21 deletions examples/borrow-stable-rate.ts → examples/borrow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { multiply, divide } from "dnum";
import { createClient, createWalletClient, http, parseUnits } from "viem";
import * as dn from "dnum";
import { createWalletClient, http, parseUnits } from "viem";
import { mnemonicToAccount } from "viem/accounts";

import {
Expand All @@ -18,38 +18,28 @@ import {
import type { FolksCoreConfig, MessageAdapters, AccountId, LoanId } from "../src/index.js";

async function main() {
const network = NetworkType.TESTNET;
const chain = FOLKS_CHAIN_ID.BSC_TESTNET;
const tokenId = TESTNET_FOLKS_TOKEN_ID.BNB;
const jsonRpcAddress = "https://my-rpc.avax-testnet.network/<API_KEY>";

const folksConfig: FolksCoreConfig = {
network: NetworkType.TESTNET,
provider: {
evm: {
[chain]: createClient({
chain: CHAIN_VIEM[chain],
transport: http(jsonRpcAddress),
}),
},
},
};
const folksConfig: FolksCoreConfig = { network, provider: { evm: {} } };

FolksCore.init(folksConfig);
FolksCore.setNetwork(NetworkType.TESTNET);
FolksCore.setNetwork(network);

const MNEMONIC = "your mnemonic here";
const account = mnemonicToAccount(MNEMONIC);

const signer = createWalletClient({
account,
chain: CHAIN_VIEM[chain],
transport: http(jsonRpcAddress),
transport: http(),
});

const { adapterIds, returnAdapterIds } = getSupportedMessageAdapters({
action: Action.Borrow,
messageAdapterParamType: MessageAdapterParamsType.ReceiveToken,
network: NetworkType.TESTNET,
network,
sourceFolksChainId: chain,
destFolksChainId: chain,
folksTokenId: tokenId,
Expand All @@ -67,15 +57,14 @@ async function main() {
const amountToBorrow = parseUnits("0.0005", 18); // 0.0005 BNB (BNB has 18 decimals)
const poolInfo = await FolksPool.read.poolInfo(tokenId);
const interestRate = poolInfo.stableBorrowData.interestRate[0];
const stableRateSlippagePercent = 5; // 5% max deviation from current rate
const [maxStableRate] = divide(multiply(interestRate, 100 + stableRateSlippagePercent), 100);
const [maxStableRate] = dn.mul(interestRate, 1.05); // 5% max deviation from current rate

const prepareBorrowCall = await FolksLoan.prepare.borrow(
accountId,
loanId,
tokenId,
amountToBorrow,
maxStableRate,
maxStableRate, // Use BigInt(0) for variable rate
chain,
adapters,
);
Expand All @@ -84,7 +73,7 @@ async function main() {
loanId,
tokenId,
amountToBorrow,
maxStableRate,
maxStableRate, // Use BigInt(0) for variable rate
chain,
prepareBorrowCall,
);
Expand Down
35 changes: 10 additions & 25 deletions examples/create-loan.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createClient, createWalletClient, http } from "viem";
import { createWalletClient, http } from "viem";
import { mnemonicToAccount } from "viem/accounts";

import { convertStringToLoanName } from "../src/common/utils/lending.js";
Expand All @@ -19,23 +19,13 @@ import {
import type { FolksCoreConfig, MessageAdapters, Nonce, AccountId } from "../src/index.js";

async function main() {
const network = NetworkType.TESTNET;
const chain = FOLKS_CHAIN_ID.AVALANCHE_FUJI;
const jsonRpcAddress = "https://my-rpc.avax-testnet.network/<API_KEY>";

const folksConfig: FolksCoreConfig = {
network: NetworkType.TESTNET,
provider: {
evm: {
[chain]: createClient({
chain: CHAIN_VIEM[chain],
transport: http(jsonRpcAddress),
}),
},
},
};
const folksConfig: FolksCoreConfig = { network, provider: { evm: {} } };

FolksCore.init(folksConfig);
FolksCore.setNetwork(NetworkType.TESTNET);
FolksCore.setNetwork(network);

const nonce: Nonce = getRandomBytes(BYTES4_LENGTH) as Nonce;

Expand All @@ -45,13 +35,13 @@ async function main() {
const signer = createWalletClient({
account,
chain: CHAIN_VIEM[chain],
transport: http(jsonRpcAddress),
transport: http(),
});

const { adapterIds, returnAdapterIds } = getSupportedMessageAdapters({
action: Action.CreateLoan,
messageAdapterParamType: MessageAdapterParamsType.Data,
network: NetworkType.TESTNET,
network,
sourceFolksChainId: chain,
});

Expand All @@ -62,20 +52,15 @@ async function main() {

FolksCore.setFolksSigner({ signer, folksChainId: chain });

const accountId = "0x7d6...b66" as AccountId; //Your xChainApp account id
const accountId = "0x7d6...b66" as AccountId; // Your xChainApp account id
const loanType = LoanTypeId.GENERAL; // LoanTypeId.DEPOSIT for deposits
const loanName = convertStringToLoanName("Test Loan");

const prepareCreateLoanCall = await FolksLoan.prepare.createLoan(
accountId,
nonce,
LoanTypeId.GENERAL, // LoanTypeId.DEPOSIT for deposits
loanName,
adapters,
);
const prepareCreateLoanCall = await FolksLoan.prepare.createLoan(accountId, nonce, loanType, loanName, adapters);
const createLoanCallRes = await FolksLoan.write.createLoan(
accountId,
nonce,
LoanTypeId.GENERAL, // LoanTypeId.DEPOSIT for deposits
loanType,
loanName,
prepareCreateLoanCall,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createClient, createWalletClient, http, parseUnits } from "viem";
import { createWalletClient, http, parseUnits } from "viem";
import { mnemonicToAccount } from "viem/accounts";

import {
Expand All @@ -17,38 +17,28 @@ import {
import type { FolksCoreConfig, MessageAdapters, AccountId, LoanId } from "../src/index.js";

async function main() {
const network = NetworkType.TESTNET;
const chain = FOLKS_CHAIN_ID.AVALANCHE_FUJI;
const tokenId = TESTNET_FOLKS_TOKEN_ID.AVAX;
const jsonRpcAddress = "https://my-rpc.avax-testnet.network/<API_KEY>";

const folksConfig: FolksCoreConfig = {
network: NetworkType.TESTNET,
provider: {
evm: {
[chain]: createClient({
chain: CHAIN_VIEM[chain],
transport: http(jsonRpcAddress),
}),
},
},
};
const folksConfig: FolksCoreConfig = { network, provider: { evm: {} } };

FolksCore.init(folksConfig);
FolksCore.setNetwork(NetworkType.TESTNET);
FolksCore.setNetwork(network);

const MNEMONIC = "your mnemonic here";
const account = mnemonicToAccount(MNEMONIC);

const signer = createWalletClient({
account,
chain: CHAIN_VIEM[chain],
transport: http(jsonRpcAddress),
transport: http(),
});

const { adapterIds, returnAdapterIds } = getSupportedMessageAdapters({
action: Action.Deposit,
messageAdapterParamType: MessageAdapterParamsType.SendToken,
network: NetworkType.TESTNET,
network,
sourceFolksChainId: chain,
folksTokenId: tokenId,
});
Expand All @@ -62,12 +52,13 @@ async function main() {

const accountId = "0x7d6...b66" as AccountId; // Your xChainApp account id
const loanId = "0x166...c12" as LoanId; // Your loan id
const loanType = LoanTypeId.GENERAL; // LoanTypeId.DEPOSIT for deposits
const amountToDeposit = parseUnits("0.1", 18); // 0.1 AVAX (AVAX has 18 decimals)

const prepareDepositCall = await FolksLoan.prepare.deposit(
accountId,
loanId,
LoanTypeId.GENERAL, // LoanTypeId.DEPOSIT for deposits
loanType,
tokenId,
amountToDeposit,
adapters,
Expand All @@ -76,7 +67,7 @@ async function main() {
accountId,
loanId,
amountToDeposit,
false,
true,
prepareDepositCall,
);
console.log(`Transaction ID: ${createDepositCallRes}`);
Expand Down
Loading

0 comments on commit e879b52

Please sign in to comment.