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

[DEV-3452] Coingecko: TVL frankencoin / deuro #1894

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
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
5 changes: 0 additions & 5 deletions infrastructure/bicep/dfx-api.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ param moneroRpcCertificate string

param zchfGatewayUrl string
param zchfGraphUrl string
param zchfTvlUrl string
param zchfContractAddress string
param zchfEquityContractAddress string
param zchfStablecoinBridgeContractAddress string
Expand Down Expand Up @@ -895,10 +894,6 @@ resource apiAppService 'Microsoft.Web/sites@2018-11-01' = {
name: 'ZCHF_GRAPH_URL'
value: zchfGraphUrl
}
{
name: 'ZCHF_TVL_URL'
value: zchfTvlUrl
}
{
name: 'ZCHF_CONTRACT_ADDRESS'
value: zchfContractAddress
Expand Down
3 changes: 0 additions & 3 deletions infrastructure/bicep/parameters/dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,6 @@
"zchfGraphUrl": {
"value": "https://maindevponder.frankencoin.com"
},
"zchfTvlUrl": {
"value": "https://api.llama.fi/tvl/frankencoin"
},
"zchfContractAddress": {
"value": "0xB58E61C3098d85632Df34EecfB899A1Ed80921cB"
},
Expand Down
3 changes: 0 additions & 3 deletions infrastructure/bicep/parameters/loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,6 @@
"zchfGraphUrl": {
"value": "https://maindevponder.frankencoin.com"
},
"zchfTvlUrl": {
"value": "https://api.llama.fi/tvl/frankencoin"
},
"zchfContractAddress": {
"value": "0xB58E61C3098d85632Df34EecfB899A1Ed80921cB"
},
Expand Down
3 changes: 0 additions & 3 deletions infrastructure/bicep/parameters/prd.json
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,6 @@
"zchfGraphUrl": {
"value": "https://ponder.frankencoin.3dotshub.com"
},
"zchfTvlUrl": {
"value": "https://api.llama.fi/tvl/frankencoin"
},
"zchfContractAddress": {
"value": "0xB58E61C3098d85632Df34EecfB899A1Ed80921cB"
},
Expand Down
2 changes: 0 additions & 2 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,6 @@ export class Configuration {
zchfGatewayUrl: process.env.ZCHF_GATEWAY_URL,
zchfApiKey: process.env.ALCHEMY_API_KEY,
zchfGraphUrl: process.env.ZCHF_GRAPH_URL,
zchfTvlUrl: process.env.ZCHF_TVL_URL,
contractAddress: {
zchf: process.env.ZCHF_CONTRACT_ADDRESS,
equity: process.env.ZCHF_EQUITY_CONTRACT_ADDRESS,
Expand All @@ -455,7 +454,6 @@ export class Configuration {
deuroApiKey: process.env.ALCHEMY_API_KEY,
deuroChainId: +process.env.DEURO_CHAIN_ID,
deuroGraphUrl: process.env.DEURO_GRAPH_URL,
deuroTvlUrl: process.env.DEURO_TVL_URL,
},
ebel2x: {
contractAddress: process.env.EBEL2X_CONTRACT_ADDRESS,
Expand Down
6 changes: 2 additions & 4 deletions src/integration/blockchain/deuro/deuro-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ export class DEuroClient {
this.provider = new ethers.providers.JsonRpcProvider(providerUrl);
}

async getTvl(): Promise<number> {
return this.http.get<number>(`${Config.blockchain.deuro.deuroTvlUrl}`);
}

async getPositionV2s(): Promise<DEuroPositionGraphDto[]> {
const document = gql`
{
Expand All @@ -36,6 +32,8 @@ export class DEuroClient {
minted
reserveContribution
expiration
closed
denied
}
}
}
Expand Down
26 changes: 18 additions & 8 deletions src/integration/blockchain/deuro/deuro.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { LogSeverity } from 'src/subdomains/supporting/log/log.entity';
import { LogService } from 'src/subdomains/supporting/log/log.service';
import { PricingService } from 'src/subdomains/supporting/pricing/services/pricing.service';
import { EvmUtil } from '../shared/evm/evm.util';
import { FrankencoinBasedService } from '../shared/frankencoin/frankencoin-based.service';
import { DEuroClient } from './deuro-client';
import {
DEuroInfoDto,
Expand All @@ -23,16 +24,14 @@ import {
} from './dto/deuro.dto';

@Injectable()
export class DEuroService implements OnModuleInit {
export class DEuroService extends FrankencoinBasedService implements OnModuleInit {
private readonly logger = new DfxLogger(DEuroService);

private static readonly LOG_SYSTEM = 'EvmInformation';
private static readonly LOG_SUBSYSTEM = 'DEuroSmartContract';

private readonly client: DEuroClient;

private pricingService: PricingService;

private usd: Fiat;
private eur: Fiat;

Expand All @@ -44,14 +43,16 @@ export class DEuroService implements OnModuleInit {
private readonly logService: LogService,
private readonly fiatService: FiatService,
) {
super();

const { deuroGatewayUrl, deuroApiKey, deuroChainId } = GetConfig().blockchain.deuro;

this.client = new DEuroClient(http, deuroGatewayUrl, deuroApiKey);
this.chainId = deuroChainId;
}

async onModuleInit() {
this.pricingService = this.moduleRef.get(PricingService, { strict: false });
this.setup(this.moduleRef.get(PricingService, { strict: false }));

this.usd = await this.fiatService.getFiatByName('USD');
this.eur = await this.fiatService.getFiatByName('EUR');
Expand Down Expand Up @@ -163,9 +164,18 @@ export class DEuroService implements OnModuleInit {
}

async getTvl(): Promise<number> {
// TODO: Frankencoin TVL comes from "https://api.llama.fi/tvl/frankencoin"
// TODO: and DEuro TVL comes from "?????"
return 0; //this.client.getTvl();
const positionV2s = await this.client.getPositionV2s();

const collaterals = positionV2s.map((p) => {
return {
collateral: p.collateral,
collateralSymbol: p.collateralSymbol,
collateralBalance: p.collateralBalance,
collateralDecimals: p.collateralDecimals,
};
});

return this.getTvlByCollaterals(collaterals);
}

async getDEuroInfo(): Promise<DEuroInfoDto> {
Expand All @@ -185,7 +195,7 @@ export class DEuroService implements OnModuleInit {

const deuroLog = <DEuroLogDto>JSON.parse(maxDEuroLogEntity.message);

const priceUsdToEur = await this.pricingService.getPrice(this.usd, this.eur, true);
const priceUsdToEur = await this.getPrice(this.usd, this.eur);

return {
totalSupplyDeuro: deuroLog.totalSupply,
Expand Down
10 changes: 5 additions & 5 deletions src/integration/blockchain/deuro/dto/deuro.dto.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
export interface DEuroPositionGraphDto {
import { FrankencoinBasedCollateralDto } from '../../shared/frankencoin/frankencoin-based.dto';

export interface DEuroPositionGraphDto extends FrankencoinBasedCollateralDto {
id: string;
position: string;
owner: string;
deuro: string;
collateral: string;
price: string;
collateralSymbol: string;
collateralBalance: string;
collateralDecimals: number;
limitForClones: string;
availableForClones: string;
minted: string;
reserveContribution: number;
expiration: string;
closed: boolean;
denied: boolean;
}

export interface DEuroDepsGraphDto {
Expand Down
10 changes: 5 additions & 5 deletions src/integration/blockchain/frankencoin/dto/frankencoin.dto.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
export interface FrankencoinPositionGraphDto {
import { FrankencoinBasedCollateralDto } from '../../shared/frankencoin/frankencoin-based.dto';

export interface FrankencoinPositionGraphDto extends FrankencoinBasedCollateralDto {
id: string;
position: string;
owner: string;
zchf: string;
collateral: string;
price: string;
collateralSymbol: string;
collateralBalance: string;
collateralDecimals: number;
limitForClones: string;
availableForClones: string;
minted: string;
reserveContribution: number;
expiration: string;
closed: boolean;
denied: boolean;
}

export interface FrankencoinChallengeGraphDto {
Expand Down
10 changes: 5 additions & 5 deletions src/integration/blockchain/frankencoin/frankencoin-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@ import {
} from './dto/frankencoin.dto';

export class FrankencoinClient {
private provider: ethers.providers.JsonRpcProvider;
private readonly provider: ethers.providers.JsonRpcProvider;

constructor(private readonly http: HttpService, gatewayUrl: string, apiKey: string) {
const providerUrl = `${gatewayUrl}/${apiKey}`;
this.provider = new ethers.providers.JsonRpcProvider(providerUrl);
}

async getTvl(): Promise<number> {
return this.http.get<number>(`${Config.blockchain.frankencoin.zchfTvlUrl}`);
}

async getPositionV1s(): Promise<FrankencoinPositionGraphDto[]> {
const document = gql`
{
Expand All @@ -47,6 +43,8 @@ export class FrankencoinClient {
minted
reserveContribution
expiration
closed
denied
}
}
}
Expand Down Expand Up @@ -77,6 +75,8 @@ export class FrankencoinClient {
minted
reserveContribution
expiration
closed
denied
}
}
}
Expand Down
25 changes: 19 additions & 6 deletions src/integration/blockchain/frankencoin/frankencoin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { LogSeverity } from 'src/subdomains/supporting/log/log.entity';
import { LogService } from 'src/subdomains/supporting/log/log.service';
import { PricingService } from 'src/subdomains/supporting/pricing/services/pricing.service';
import { EvmUtil } from '../shared/evm/evm.util';
import { FrankencoinBasedService } from '../shared/frankencoin/frankencoin-based.service';
import {
FrankencoinChallengeGraphDto,
FrankencoinDelegationGraphDto,
Expand All @@ -28,16 +29,14 @@ import {
import { FrankencoinClient } from './frankencoin-client';

@Injectable()
export class FrankencoinService implements OnModuleInit {
export class FrankencoinService extends FrankencoinBasedService implements OnModuleInit {
private readonly logger = new DfxLogger(FrankencoinService);

private static readonly LOG_SYSTEM = 'EvmInformation';
private static readonly LOG_SUBSYSTEM = 'FrankencoinSmartContract';

private readonly client: FrankencoinClient;

private pricingService: PricingService;

private usd: Fiat;
private chf: Fiat;

Expand All @@ -47,13 +46,15 @@ export class FrankencoinService implements OnModuleInit {
private readonly logService: LogService,
private readonly fiatService: FiatService,
) {
super();

const { zchfGatewayUrl, zchfApiKey } = GetConfig().blockchain.frankencoin;

this.client = new FrankencoinClient(http, zchfGatewayUrl, zchfApiKey);
}

async onModuleInit() {
this.pricingService = this.moduleRef.get(PricingService, { strict: false });
this.setup(this.moduleRef.get(PricingService, { strict: false }));

this.usd = await this.fiatService.getFiatByName('USD');
this.chf = await this.fiatService.getFiatByName('CHF');
Expand Down Expand Up @@ -216,7 +217,19 @@ export class FrankencoinService implements OnModuleInit {
}

async getTvl(): Promise<number> {
return this.client.getTvl();
const positionV1s = await this.client.getPositionV1s();
const positionV2s = await this.client.getPositionV2s();

const collaterals = [...positionV1s, ...positionV2s].map((p) => {
return {
collateral: p.collateral,
collateralSymbol: p.collateralSymbol,
collateralBalance: p.collateralBalance,
collateralDecimals: p.collateralDecimals,
};
});

return this.getTvlByCollaterals(collaterals);
}

async getFrankencoinInfo(): Promise<FrankencoinInfoDto> {
Expand All @@ -236,7 +249,7 @@ export class FrankencoinService implements OnModuleInit {

const frankencoinLog = <FrankencoinLogDto>JSON.parse(maxFrankencoinLogEntity.message);

const priceUsdToChf = await this.pricingService.getPrice(this.usd, this.chf, true);
const priceUsdToChf = await this.getPrice(this.usd, this.chf);

return {
totalSupplyZchf: frankencoinLog.totalSupply,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface FrankencoinBasedCollateralDto {
collateral: string;
collateralSymbol: string;
collateralBalance: string;
collateralDecimals: number;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { groupBy, sumBy } from 'lodash';
import { Fiat } from 'src/shared/models/fiat/fiat.entity';
import { Price } from 'src/subdomains/supporting/pricing/domain/entities/price';
import { PriceSource } from 'src/subdomains/supporting/pricing/domain/entities/price-rule.entity';
import { PricingService } from 'src/subdomains/supporting/pricing/services/pricing.service';
import { EvmUtil } from '../evm/evm.util';
import { FrankencoinBasedCollateralDto } from './frankencoin-based.dto';

export abstract class FrankencoinBasedService {
private pricingService: PricingService;

setup(pricingService: PricingService) {
this.pricingService = pricingService;
}

async getPrice(from: Fiat, to: Fiat): Promise<Price> {
return this.pricingService.getPrice(from, to, true);
}

async getTvlByCollaterals(collaterals: FrankencoinBasedCollateralDto[]): Promise<number> {
const groupedCollaterals = groupBy(collaterals, (i) => i.collateral);

const collateralsWithTotalBalances = Object.keys(groupedCollaterals).map((key) => {
const first = groupedCollaterals[key][0];
return {
address: first.collateral,
symbol: first.collateralSymbol,
totalBalance: sumBy(groupedCollaterals[key], (i) =>
EvmUtil.fromWeiAmount(i.collateralBalance, i.collateralDecimals),
),
};
});

let tvl = 0;

for (const collateralWithTotalBalance of collateralsWithTotalBalances) {
const price = await this.pricingService.getPriceFrom(
PriceSource.COIN_GECKO,
collateralWithTotalBalance.address.toLowerCase(),
'usd',
'contract',
);

if (price) tvl += collateralWithTotalBalance.totalBalance / price.price;
}

return tvl;
}
}
Loading
Loading