Skip to content

Commit

Permalink
Merge pull request #693 from UnUniFi/develop
Browse files Browse the repository at this point in the history
v4.0.0
  • Loading branch information
Senna46 authored Nov 23, 2023
2 parents fe68ee2 + b3b93ec commit 3e58eb7
Show file tree
Hide file tree
Showing 26 changed files with 397 additions and 270 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"stream-browserify": "^3.0.0",
"stream-http": "^3.2.0",
"tslib": "^2.3.0",
"ununifi-client": "^4.0.0-rc6",
"ununifi-client": "^4.0.0-rc8",
"zone.js": "~0.11.4"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,8 @@ export class AddressComponent implements OnInit {
),
);

const denomMetadataMap$ = this.bankQuery.getDenomMetadataMap$();
this.tvl$ = combineLatest([balances$, denomMetadataMap$]).pipe(
mergeMap(async ([balances, denomMetadataMap]) => {
this.tvl$ = balances$.pipe(
mergeMap(async (balances) => {
const vaultBalances = balances
?.filter((balance) => balance.denom?.includes('yieldaggregator/vaults/'))
.sort(
Expand All @@ -166,9 +165,8 @@ export class AddressComponent implements OnInit {
const values = await Promise.all(
amounts.map(async (redeemAmount) => {
return this.bandProtocolService.convertToUSDAmount(
redeemAmount.total_amount?.denom || '',
redeemAmount.total_amount?.amount || '',
denomMetadataMap,
redeemAmount.symbol || '',
redeemAmount.total_amount || '',
);
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ export class YieldaggregatorComponent implements OnInit {
),
),
);
this.addressTVLs$ = combineLatest([this.addressBalances$, denomMetadataMap$]).pipe(
mergeMap(([addressBalances, denomMetadataMap]) =>
this.addressTVLs$ = this.addressBalances$.pipe(
mergeMap((addressBalances) =>
Promise.all(
addressBalances.map(async (addressBalance) => {
const vaultBalances = addressBalance.balances?.filter((balance) =>
Expand All @@ -216,9 +216,8 @@ export class YieldaggregatorComponent implements OnInit {
const values = await Promise.all(
amounts.map(async (redeemAmount) => {
return this.bandProtocolService.convertToUSDAmount(
redeemAmount.total_amount?.denom || '',
redeemAmount.total_amount?.amount || '',
denomMetadataMap,
redeemAmount.symbol || '',
redeemAmount.total_amount || '',
);
}),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getDenomExponent } from '../cosmos/bank.model';
import { getSymbolExponent } from '../cosmos/bank.model';
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import cosmosclient from '@cosmos-client/core';

export const rest = 'https://laozi1.bandchain.org/api';
export type TokenAmountUSD = {
Expand Down Expand Up @@ -34,20 +33,8 @@ export class BandProtocolService {
}
}

async convertToUSDAmount(
denom: string,
amount: string,
denomMetadataMap: { [denom: string]: cosmosclient.proto.cosmos.bank.v1beta1.IMetadata },
): Promise<number> {
const symbol = denomMetadataMap[denom].symbol;
const display = denomMetadataMap[denom].display;
if (!symbol) {
throw new Error(`Symbol not found for denom ${denom}`);
}
if (!display) {
throw new Error(`Display not found for denom ${denom}`);
}
const exponent = getDenomExponent(denom);
async convertToUSDAmount(symbol: string, amount: string): Promise<number> {
const exponent = getSymbolExponent(symbol);
const symbolAmount = Number(amount) / 10 ** (exponent || 0);
const price = await this.getPrice(symbol);
if (!price) {
Expand All @@ -58,21 +45,16 @@ export class BandProtocolService {
return usdAmount;
}

calcDepositUSDAmount(
denom: string,
calcUSDAmount(
symbol: string,
amount: number,
symbolPriceMap: { [symbol: string]: number },
denomMetadataMap: { [denom: string]: cosmosclient.proto.cosmos.bank.v1beta1.IMetadata },
): number {
const symbol = denomMetadataMap[denom].symbol;
if (!symbol) {
throw new Error(`Symbol not found for denom ${denom}`);
}
const price = symbolPriceMap[symbol];
if (!price) {
throw new Error(`Price not found for symbol ${symbol}`);
}
const exponent = getDenomExponent(denom);
const exponent = getSymbolExponent(symbol);
const symbolAmount = amount / 10 ** (exponent || 0);
const usdAmount = symbolAmount * price;
return usdAmount;
Expand Down
13 changes: 11 additions & 2 deletions projects/portal/src/app/models/cosmos/bank.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,27 @@ export const denomExponentMap: { [denom: string]: number } = {
uusd: 6,
uusdc: 6,
udlp: 6,
'': 0,
'': 6,
};

export const symbolExponent: { [symbol: string]: number } = {};

const ibcPattern = /^ibc\//;
const iyaPattern = /^yieldaggregator\/vaults\//;

export function getDenomExponent(denom?: string): number {
if (!denom) {
return 0;
return 6;
}
if (ibcPattern.test(denom) || iyaPattern.test(denom)) {
return 6;
}
return denomExponentMap[denom];
}

export function getSymbolExponent(symbol?: string): number {
if (!symbol) {
return 6;
}
return symbolExponent[symbol] || 6;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,14 @@ export class YieldAggregatorApplicationService {
private readonly dialog: Dialog,
) {}

async depositToVault(vaultId: string, symbol: string, amount: number) {
async depositToVault(vaultId: string, denom: string, amount: number) {
const prerequisiteData = await this.txCommonApplication.getPrerequisiteData();
if (!prerequisiteData) {
return;
}
const { address, publicKey, account, currentCosmosWallet, minimumGasPrice } = prerequisiteData;

const msg = this.yieldAggregatorService.buildMsgDepositToVault(
address,
vaultId,
symbol,
amount,
);
const msg = this.yieldAggregatorService.buildMsgDepositToVault(address, vaultId, denom, amount);

const simulationResult = await this.txCommonApplication.simulate(
msg,
Expand Down Expand Up @@ -72,23 +67,23 @@ export class YieldAggregatorApplicationService {

async withdrawFromVault(
vaultId: string,
denom: string,
lp_denom: string,
readableAmount: number,
redeemAmount: number,
feeAmount: number,
symbol: string,
) {
// open confirm dialog if feeAmount > redeemAmount
if (feeAmount > redeemAmount) {
const txFeeConfirmedResult = await this.dialog
.open<boolean>(WithdrawFeeConfirmDialogComponent, {
data: { redeemAmount, feeAmount, denom },
data: { redeemAmount, feeAmount, symbol },
})
.closed.toPromise();
if (!txFeeConfirmedResult) {
return;
}
}

const prerequisiteData = await this.txCommonApplication.getPrerequisiteData();
if (!prerequisiteData) {
return;
Expand All @@ -98,7 +93,7 @@ export class YieldAggregatorApplicationService {
const msg = this.yieldAggregatorService.buildMsgWithdrawFromVault(
address,
vaultId,
denom,
lp_denom,
readableAmount,
);

Expand Down Expand Up @@ -139,11 +134,65 @@ export class YieldAggregatorApplicationService {
}
}

async withdrawFromVaultWithUnbonding(vaultId: string, lp_denom: string, amount: number) {
const prerequisiteData = await this.txCommonApplication.getPrerequisiteData();
if (!prerequisiteData) {
return;
}
const { address, publicKey, account, currentCosmosWallet, minimumGasPrice } = prerequisiteData;

const msg = this.yieldAggregatorService.buildMsgWithdrawFromVaultWithUnbondingTime(
address,
vaultId,
lp_denom,
amount,
);

const simulationResult = await this.txCommonApplication.simulate(
msg,
publicKey,
account,
minimumGasPrice,
);
if (!simulationResult) {
return;
}
const { gas, fee } = simulationResult;

if (!(await this.txCommonApplication.confirmFeeIfUnUniFiWallet(currentCosmosWallet, fee))) {
return;
}

const txHash = await this.txCommonApplication.broadcast(
msg,
currentCosmosWallet,
publicKey,
account,
gas,
fee,
);
if (!txHash) {
return;
}

if (txHash) {
await this.dialog
.open<TxConfirmDialogData>(TxConfirmDialogComponent, {
data: {
txHash: txHash,
msg: 'Successfully requested withdrawing from the vault. The payment will be made after the unbonding time.',
},
})
.closed.toPromise();
location.reload();
}
}

async createVault(
name: string,
denom: string,
symbol: string,
description: string,
strategies: { id: string; weight: number }[],
strategies: { denom: string; id: string; weight: number }[],
commissionRate: number,
reserveRate: number,
fee: cosmosclient.proto.cosmos.base.v1beta1.ICoin,
Expand All @@ -158,7 +207,7 @@ export class YieldAggregatorApplicationService {

const msg = this.yieldAggregatorService.buildMsgCreateVault(
address,
denom,
symbol,
name,
description,
strategies,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@ export type DepositToVaultRequest = {

export type WithdrawFromVaultRequest = {
vaultId: string;
denom: string;
lp_denom: string;
readableAmount: number;
redeemAmount: number;
feeAmount: number;
symbol: string;
};

export type WithdrawFromVaultWithUnbondingRequest = {
vaultId: string;
lp_denom: string;
readableAmount: number;
};

export type CreateVaultRequest = {
name: string;
denom: string;
symbol: string;
description: string;
strategies: { id: string; weight: number }[];
strategies: { denom: string; id: string; weight: number }[];
commissionRate: number;
reserveRate: number;
fee: { denom: string; amount: string };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { Observable } from 'rxjs';
import { map, mergeMap, pluck } from 'rxjs/operators';
import ununifi from 'ununifi-client';
import {
DenomInfos200ResponseInfoInner,
EstimateMintAmount200Response,
EstimateRedeemAmount200Response,
StrategyAll200ResponseStrategiesInner,
SymbolInfos200ResponseInfoInner,
Vault200Response,
VaultAll200ResponseVaultsInner,
YieldAggregatorParams200ResponseParams,
Expand Down Expand Up @@ -80,4 +82,18 @@ export class YieldAggregatorQueryService {
const res = await ununifi.rest.yieldAggregator.estimateRedeemAmount(sdk, id, amount);
return res.data!;
}

listDenomInfos$(): Observable<DenomInfos200ResponseInfoInner[]> {
return this.restSdk$.pipe(
mergeMap((sdk) => ununifi.rest.yieldAggregator.denomInfos(sdk)),
map((res) => res.data.info!),
);
}

listSymbolInfos$(): Observable<SymbolInfos200ResponseInfoInner[]> {
return this.restSdk$.pipe(
mergeMap((sdk) => ununifi.rest.yieldAggregator.symbolInfos(sdk)),
map((res) => res.data.info!),
);
}
}
Loading

0 comments on commit 3e58eb7

Please sign in to comment.