Skip to content

Commit

Permalink
[fe-sdk] Get protocol tvl (#615)
Browse files Browse the repository at this point in the history
* Get Protocol TVL

* logging

* logging

* change

* PR feedback
  • Loading branch information
panieldark authored Nov 23, 2023
1 parent 10ae72f commit fe573a4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-horses-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nocturne-xyz/frontend-sdk": patch
---

Adds Protocol TVL
42 changes: 27 additions & 15 deletions actors/deposit-screener/src/screening/checks/RuleSet.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { CachedFetchOptions } from "@nocturne-xyz/offchain-utils";
import IORedis from "ioredis";
import { Logger } from "winston";
import { ScreeningDepositRequest } from "..";
import {
API_CALL_MAP,
ApiCallNames,
ApiCallToReturnType,
ApiCallReturnData,
ApiCallToReturnType,
} from "./apiCalls";
import IORedis from "ioredis";
import { Logger } from "winston";

export interface Rejection {
type: "Rejection";
Expand Down Expand Up @@ -224,9 +224,11 @@ export class RuleSet {
});
if (result.type === "Rejection") {
this.logger.info(
`Screener execution for deposit:`,
deposit,
rulesLogList
`Screener execution on deposit for addr ${deposit.spender}`,
{
deposit: { ...deposit },
results: { ...toLoggable(rulesLogList) },
}
);
return result;
} else if (result.type === "Delay") {
Expand All @@ -237,15 +239,25 @@ export class RuleSet {
}
currRule = currRule.next;
}

const ruleResults = rulesLogList.reduce((acc, { ruleName, result }) => {
acc[ruleName] = result;
return acc;
}, {} as Record<string, Awaited<ReturnType<RuleLike["check"]>>>);
this.logger.info(`Screener execution for deposit:`, {
...deposit,
...ruleResults,
});
this.logger.info(
`Screener execution for deposit for addr ${deposit.spender}`,
{
deposit: { ...deposit },
results: { ...toLoggable(rulesLogList) },
}
);
return { type: "Delay", timeSeconds: delaySeconds };
}
}

const toLoggable = (
rulesLogList: {
ruleName: string;
result: Awaited<ReturnType<RuleLike["check"]>>;
}[]
): Record<string, Rejection | DelayAction | typeof ACTION_NOT_TRIGGERED> => {
return rulesLogList.reduce((acc, { ruleName, result }) => {
acc[ruleName] = result;
return acc;
}, {} as Record<string, Awaited<ReturnType<RuleLike["check"]>>>);
};
16 changes: 16 additions & 0 deletions packages/frontend-sdk/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import { E_ALREADY_LOCKED, Mutex, tryAcquire } from "async-mutex";
import retry from "async-retry";
import * as JSON from "bigint-json-serialization";
import { BigNumber, ContractTransaction, ethers } from "ethers";
import ERC20_ABI from "./abis/ERC20.json";
import { DepositAdapter, SubgraphDepositAdapter } from "./depositFetching";
import { SnapStateSdk, getSigner } from "./metamask";
import { GetSnapOptions } from "./metamask/types";
Expand Down Expand Up @@ -288,6 +289,21 @@ export class NocturneSdk {
return newOpRequestBuilder(this.provider, this.chainId);
}

async getProtocolTvl(): Promise<Map<string, bigint>> {
const tellerAddress = this.sdkConfig.config.tellerAddress;
const tvlByAsset = new Map<string, bigint>();
for (const [assetName, { address }] of this.sdkConfig.config.erc20s) {
const erc20Contract = new ethers.Contract(
address,
ERC20_ABI,
this.provider,
);
const balance = await erc20Contract.balanceOf(tellerAddress);
tvlByAsset.set(assetName, balance.toBigInt());
}
return tvlByAsset;
}

/**
* Call `depositManager.instantiateErc20MultiDeposit` given the provided
* `erc20Address`, `valuse`, and `gasCompPerDeposit`.
Expand Down

0 comments on commit fe573a4

Please sign in to comment.