Skip to content
Merged
Changes from all 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
64 changes: 35 additions & 29 deletions projects/8lends/index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
const { sumTokens2 } = require("../helper/unwrapLPs")
const sdk = require('@defillama/sdk')

const FUNDRAISING_CONTRACT = '0xf435A133D6cDCb81061F18a4763560f9931DB57D';
const dataAbi = "function projects(uint256) view returns (uint256 hardCap, uint256 softCap, uint256 totalInvested, uint256 startAt, uint256 preFundDuration, uint256 investorInterestRate, uint256 openStageEndAt, (uint256 platformInterestRate, uint256 totalRepaid, address borrower, uint256 fundedTime, address loanToken, uint8 stage) innerStruct)"
const COMING_SOON_STAGE = 0;
const OPENED_STAGE = 1;
const CANCELED_STAGE = 2;
const PREFUNDED_STAGE = 3;
const FUNDED_STAGE = 4;
const REPAID_STAGE = 5;
const { sumTokens2 } = require("../helper/unwrapLPs");
const sdk = require("@defillama/sdk");

const FUNDRAISING_CONTRACT = "0xf435A133D6cDCb81061F18a4763560f9931DB57D";
const dataAbi = "function projects(uint256) view returns (uint256 hardCap, uint256 softCap, uint256 totalInvested, uint256 startAt, uint256 preFundDuration, uint256 investorInterestRate, uint256 openStageEndAt, (uint256 platformInterestRate, uint256 totalRepaid, address borrower, uint256 fundedTime, address loanToken, uint8 stage) innerStruct)";

const STAGES = {
COMING_SOON: 0,
OPENED: 1,
CANCELED: 2,
PREFUNDED: 3,
FUNDED: 4,
REPAID: 5,
};

const ACTIVE_STAGES = new Set([STAGES.OPENED, STAGES.PREFUNDED, STAGES.FUNDED]);

async function getTokens (api) {
const projects = await api.fetchList({ target: FUNDRAISING_CONTRACT, lengthAbi: "projectCount", itemAbi: dataAbi });
return { projects, tokens: projects.map(project => project.innerStruct.loanToken) }
}

async function tvl(api) {
const projectsData = await api.fetchList({ lengthAbi: 'projectCount', itemAbi: dataAbi, target: FUNDRAISING_CONTRACT })
const tokens = projectsData.map(project => project.innerStruct.loanToken)
return sumTokens2({ api, owner: FUNDRAISING_CONTRACT, tokens, })
const { tokens } = await getTokens(api)
return sumTokens2({ api, owner: FUNDRAISING_CONTRACT, tokens });
}

async function borrowed(api) {
const projectsData = await api.fetchList({ lengthAbi: 'projectCount', itemAbi: dataAbi, target: FUNDRAISING_CONTRACT })
const tokens = projectsData.map(project => project.innerStruct.loanToken)
const tvlApi = new sdk.ChainApi({ chain: api.chain, block: api.block, })
// Filter only projects that are in OPENED, PREFUNDED, or FUNDED stages
// These are actively fundraising or have been funded
const filtered = projectsData.filter(project => {
const stage = Number(project.innerStruct.stage);
return stage === OPENED_STAGE || stage === PREFUNDED_STAGE || stage === FUNDED_STAGE;
})
filtered.forEach(project => api.add(project.innerStruct.loanToken, project.totalInvested))
await sumTokens2({ api: tvlApi, owner: FUNDRAISING_CONTRACT, tokens, })
api.getBalancesV2().subtract(tvlApi.getBalancesV2())
const { projects, tokens } = await getTokens(api)
projects.forEach((project) => {
const stage = Number(project?.innerStruct?.stage ?? -1);
if (ACTIVE_STAGES.has(stage)) api.add(project.innerStruct.loanToken, project.totalInvested)
})

const tvlApi = new sdk.ChainApi({ chain: api.chain, block: api.block });
await sumTokens2({ api: tvlApi, owner: FUNDRAISING_CONTRACT, tokens });
api.getBalancesV2().subtract(tvlApi.getBalancesV2());
}

module.exports = {
methodology: 'Funds in the contract not yet withdrawn by the borrower are counted as TVL, rest of the investment is considered borrowed.',
base: { tvl, borrowed, }
};
methodology: "Funds still held in the fundraising contract are counted as TVL; the remainder of invested funds is counted as borrowed.",
base: { tvl, borrowed },
};
Loading