Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: create loan, deposit, borrowing and linking address examples #209

Merged
merged 7 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wild-olives-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@folks-finance/xchain-sdk": patch
---

Added more examples
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ const createAccount = async (sourceFolksChainId: FolksChainId) => {

Remember that any changes made to `FolksCore` (like changing the network or signer) will affect all subsequent module calls. This design allows for flexible and context-aware interactions with the Folks Finance protocol across different chains and environments.

More examples provided in [`./examples`](./examples) folder.

### React Usage

When using the SDK with React, there are a few additional considerations to ensure proper initialization and synchronization. Here's how to set up and use the SDK in a React environment:
Expand Down
100 changes: 100 additions & 0 deletions examples/borrow-stable-rate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { multiply, divide } from "dnum";
gidonkatten marked this conversation as resolved.
Show resolved Hide resolved
import { createClient, createWalletClient, http, parseUnits } from "viem";
import { mnemonicToAccount } from "viem/accounts";

import {
NetworkType,
FolksCore,
FolksLoan,
FolksPool,
FOLKS_CHAIN_ID,
getSupportedMessageAdapters,
Action,
MessageAdapterParamsType,
CHAIN_VIEM,
TESTNET_FOLKS_TOKEN_ID,
} from "../src/index.js";

import type { FolksCoreConfig, MessageAdapters, AccountId, LoanId } from "../src/index.js";

async function main() {
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,
gidonkatten marked this conversation as resolved.
Show resolved Hide resolved
provider: {
evm: {
[chain]: createClient({
chain: CHAIN_VIEM[chain],
transport: http(jsonRpcAddress),
}),
},
},
};

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

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

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

const { adapterIds, returnAdapterIds } = getSupportedMessageAdapters({
action: Action.Borrow,
messageAdapterParamType: MessageAdapterParamsType.ReceiveToken,
network: NetworkType.TESTNET,
sourceFolksChainId: chain,
destFolksChainId: chain,
folksTokenId: tokenId,
});

const adapters: MessageAdapters = {
adapterId: adapterIds[0],
returnAdapterId: returnAdapterIds[0],
};

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

const accountId = "0x7d6...b66" as AccountId; // Your xChainApp account id
const loanId = "0x166...c12" as LoanId; // Your loan id
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);
gidonkatten marked this conversation as resolved.
Show resolved Hide resolved

const prepareBorrowCall = await FolksLoan.prepare.borrow(
accountId,
loanId,
tokenId,
amountToBorrow,
maxStableRate,
chain,
adapters,
);
const createBorrowCallRes = await FolksLoan.write.borrow(
accountId,
loanId,
tokenId,
amountToBorrow,
maxStableRate,
chain,
prepareBorrowCall,
);
console.log(`Transaction ID: ${createBorrowCallRes}`);
}

main()
.then(() => {
console.log("done");
})
.catch((error: unknown) => {
console.error(error);
});
94 changes: 94 additions & 0 deletions examples/borrow-variable-rate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { createClient, createWalletClient, http, parseUnits } from "viem";
import { mnemonicToAccount } from "viem/accounts";

import {
NetworkType,
FolksCore,
FolksLoan,
FOLKS_CHAIN_ID,
getSupportedMessageAdapters,
Action,
MessageAdapterParamsType,
CHAIN_VIEM,
TESTNET_FOLKS_TOKEN_ID,
} from "../src/index.js";

import type { FolksCoreConfig, MessageAdapters, AccountId, LoanId } from "../src/index.js";

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

const folksConfig: FolksCoreConfig = {
network: NetworkType.TESTNET,
gidonkatten marked this conversation as resolved.
Show resolved Hide resolved
provider: {
evm: {
[chain]: createClient({
chain: CHAIN_VIEM[chain],
transport: http(jsonRpcAddress),
}),
},
},
};

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

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

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

const { adapterIds, returnAdapterIds } = getSupportedMessageAdapters({
action: Action.Borrow,
messageAdapterParamType: MessageAdapterParamsType.ReceiveToken,
network: NetworkType.TESTNET,
sourceFolksChainId: chain,
destFolksChainId: chain,
folksTokenId: tokenId,
});

const adapters: MessageAdapters = {
adapterId: adapterIds[0],
returnAdapterId: returnAdapterIds[0],
};

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

const accountId = "0x7d6...b66" as AccountId; // Your xChainApp account id
const loanId = "0x166...c12" as LoanId; // Your loan id
const amountToBorrow = parseUnits("1", 6); // 1 USDC (USDC has 6 decimals)

const prepareBorrowCall = await FolksLoan.prepare.borrow(
accountId,
loanId,
tokenId,
amountToBorrow,
BigInt(0),
chain,
adapters,
);
const createBorrowCallRes = await FolksLoan.write.borrow(
accountId,
loanId,
tokenId,
amountToBorrow,
BigInt(0),
chain,
prepareBorrowCall,
);
console.log(`Transaction ID: ${createBorrowCallRes}`);
}

main()
.then(() => {
console.log("done");
})
.catch((error: unknown) => {
console.error(error);
});
91 changes: 91 additions & 0 deletions examples/create-loan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { createClient, createWalletClient, http } from "viem";
import { mnemonicToAccount } from "viem/accounts";

import { convertStringToLoanName } from "../src/common/utils/lending.js";
import {
NetworkType,
FolksCore,
getRandomBytes,
FolksLoan,
FOLKS_CHAIN_ID,
BYTES4_LENGTH,
getSupportedMessageAdapters,
Action,
MessageAdapterParamsType,
LoanTypeId,
CHAIN_VIEM,
} from "../src/index.js";

import type { FolksCoreConfig, MessageAdapters, Nonce, AccountId } from "../src/index.js";

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

const folksConfig: FolksCoreConfig = {
network: NetworkType.TESTNET,
gidonkatten marked this conversation as resolved.
Show resolved Hide resolved
provider: {
evm: {
[chain]: createClient({
chain: CHAIN_VIEM[chain],
transport: http(jsonRpcAddress),
}),
},
},
};

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

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

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

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

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

const adapters: MessageAdapters = {
adapterId: adapterIds[0],
returnAdapterId: returnAdapterIds[0],
};

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

const accountId = "0x7d6...b66" as AccountId; //Your xChainApp account id
const loanName = convertStringToLoanName("Test Loan");

const prepareCreateLoanCall = await FolksLoan.prepare.createLoan(
accountId,
nonce,
LoanTypeId.GENERAL, // LoanTypeId.DEPOSIT for deposits
gidonkatten marked this conversation as resolved.
Show resolved Hide resolved
loanName,
adapters,
);
const createLoanCallRes = await FolksLoan.write.createLoan(
accountId,
nonce,
LoanTypeId.GENERAL, // LoanTypeId.DEPOSIT for deposits
loanName,
prepareCreateLoanCall,
);
console.log(`Transaction ID: ${createLoanCallRes}`);
}

main()
.then(() => {
console.log("done");
})
.catch((error: unknown) => {
console.error(error);
});
91 changes: 91 additions & 0 deletions examples/deposit-collateralise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { createClient, createWalletClient, http, parseUnits } from "viem";
gidonkatten marked this conversation as resolved.
Show resolved Hide resolved
import { mnemonicToAccount } from "viem/accounts";

import {
NetworkType,
FolksCore,
FolksLoan,
FOLKS_CHAIN_ID,
getSupportedMessageAdapters,
Action,
MessageAdapterParamsType,
LoanTypeId,
CHAIN_VIEM,
TESTNET_FOLKS_TOKEN_ID,
} from "../src/index.js";

import type { FolksCoreConfig, MessageAdapters, AccountId, LoanId } from "../src/index.js";

async function main() {
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,
gidonkatten marked this conversation as resolved.
Show resolved Hide resolved
provider: {
evm: {
[chain]: createClient({
chain: CHAIN_VIEM[chain],
transport: http(jsonRpcAddress),
}),
},
},
};

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

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

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

const { adapterIds, returnAdapterIds } = getSupportedMessageAdapters({
action: Action.Deposit,
messageAdapterParamType: MessageAdapterParamsType.SendToken,
network: NetworkType.TESTNET,
sourceFolksChainId: chain,
folksTokenId: tokenId,
});

const adapters: MessageAdapters = {
adapterId: adapterIds[0],
returnAdapterId: returnAdapterIds[0],
};

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

const accountId = "0x7d6...b66" as AccountId; // Your xChainApp account id
const loanId = "0x166...c12" as LoanId; // Your loan id
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
tokenId,
amountToDeposit,
adapters,
);
const createDepositCallRes = await FolksLoan.write.deposit(
accountId,
loanId,
amountToDeposit,
false,
gidonkatten marked this conversation as resolved.
Show resolved Hide resolved
prepareDepositCall,
);
console.log(`Transaction ID: ${createDepositCallRes}`);
}

main()
.then(() => {
console.log("done");
})
.catch((error: unknown) => {
console.error(error);
});
Loading
Loading