Skip to content
Open
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
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 @@ -76,7 +76,7 @@
"stream-browserify": "^3.0.0",
"stream-http": "^3.2.0",
"tslib": "^2.3.0",
"ununifi-client": "^0.46.4",
"ununifi-client": "^0.46.5-dev2",
"zone.js": "~0.11.4"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
import {
TxConfirmDialogComponent,
TxConfirmDialogData,
} from '../../views/dialogs/txs/tx-confirm/tx-confirm-dialog.component';
import { TxCommonApplicationService } from '../cosmos/tx-common.application.service';
import { CopyTradingService } from './copy-trading.service';
import { Dialog } from '@angular/cdk/dialog';
import { Injectable } from '@angular/core';

@Injectable({
providedIn: 'root',
})
export class CopyTradingApplicationService {
constructor(
private readonly dialog: Dialog,
private readonly copyTradingService: CopyTradingService,
private readonly txCommonApplication: TxCommonApplicationService,
) {}

async createExemplaryTrader(name: string, description: string, profitCommissionRate: number) {
const prerequisiteData = await this.txCommonApplication.getPrerequisiteData();
if (!prerequisiteData) {
return;
}
const { address, publicKey, account, currentCosmosWallet, minimumGasPrice } = prerequisiteData;

const msg = this.copyTradingService.buildMsgCreateExemplaryTrader(
address,
name,
description,
profitCommissionRate,
);

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 registered as an Exemplary Trader.' },
})
.closed.toPromise();
location.reload();
}
}

async updateExemplaryTrader(name: string, description: string, profitCommissionRate: number) {
const prerequisiteData = await this.txCommonApplication.getPrerequisiteData();
if (!prerequisiteData) {
return;
}
const { address, publicKey, account, currentCosmosWallet, minimumGasPrice } = prerequisiteData;

const msg = this.copyTradingService.buildMsgUpdateExemplaryTrader(
address,
name,
description,
profitCommissionRate,
);

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 updated your Exemplary Trader.' },
})
.closed.toPromise();
location.reload();
}
}

async deleteExemplaryTrader() {
const prerequisiteData = await this.txCommonApplication.getPrerequisiteData();
if (!prerequisiteData) {
return;
}
const { address, publicKey, account, currentCosmosWallet, minimumGasPrice } = prerequisiteData;

const msg = this.copyTradingService.buildMsgDeleteExemplaryTrader(address);

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 deleted your Exemplary Trader.' },
})
.closed.toPromise();
location.reload();
}
}

async createTracing(
exemplaryTrader: string,
sizeCoef: number,
leverageCoef: number,
reverse: boolean,
) {
const prerequisiteData = await this.txCommonApplication.getPrerequisiteData();
if (!prerequisiteData) {
return;
}
const { address, publicKey, account, currentCosmosWallet, minimumGasPrice } = prerequisiteData;

const msg = this.copyTradingService.buildMsgCreateTracing(
address,
exemplaryTrader,
sizeCoef,
leverageCoef,
reverse,
);

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 create tracing the Exemplary Trader.' },
})
.closed.toPromise();
location.reload();
}
}

async deleteTracing() {
const prerequisiteData = await this.txCommonApplication.getPrerequisiteData();
if (!prerequisiteData) {
return;
}
const { address, publicKey, account, currentCosmosWallet, minimumGasPrice } = prerequisiteData;

const msg = this.copyTradingService.buildMsgDeleteTracing(address);

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 create tracing the Exemplary Trader.' },
})
.closed.toPromise();
location.reload();
}
}
}
18 changes: 18 additions & 0 deletions projects/portal/src/app/models/copy-trading/copy-trading.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export type CreateExemplaryTraderRequest = {
name: string;
description: string;
profitCommissionRate: number;
};

export type UpdateExemplaryTraderRequest = {
name: string;
description: string;
profitCommissionRate: number;
};

export type CreateTracingRequest = {
exemplaryTrader: string;
sizeCoef: number;
leverageCoef: number;
reverse: boolean;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { CosmosSDKService } from '../cosmos-sdk.service';
import { Injectable } from '@angular/core';
import { CosmosSDK } from '@cosmos-client/core/cjs/sdk';
import { Observable } from 'rxjs';
import { map, mergeMap, pluck } from 'rxjs/operators';
import ununificlient from 'ununifi-client';
import {
ExemplaryTraderAll200ResponseExemplaryTraderInner,
ExemplaryTraderTracing200ResponseTracingInner,
} from 'ununifi-client/esm/openapi';

@Injectable({ providedIn: 'root' })
export class CopyTradingQueryService {
private restSdk$: Observable<CosmosSDK>;

constructor(private cosmosSDK: CosmosSDKService) {
this.restSdk$ = this.cosmosSDK.sdk$.pipe(pluck('rest'));
}

listExemplaryTraders$(): Observable<ExemplaryTraderAll200ResponseExemplaryTraderInner[]> {
return this.restSdk$.pipe(
mergeMap((sdk) => ununificlient.rest.copyTrading.exemplaryTraderAll(sdk)),
map((res) => res.data.exemplaryTrader!),
);
}

getExemplaryTrader$(
address: string,
): Observable<ExemplaryTraderAll200ResponseExemplaryTraderInner> {
return this.restSdk$.pipe(
mergeMap((sdk) => ununificlient.rest.copyTrading.exemplaryTrader(sdk, address)),
map((res) => res.data.exemplaryTrader!),
);
}

listTracingsExemplaryTrader$(
address: string,
): Observable<ExemplaryTraderTracing200ResponseTracingInner[]> {
return this.restSdk$.pipe(
mergeMap((sdk) => ununificlient.rest.copyTrading.exemplaryTraderTracing(sdk, address)),
map((res) => res.data.tracing!),
);
}

listAllTracings$(): Observable<ExemplaryTraderTracing200ResponseTracingInner[]> {
return this.restSdk$.pipe(
mergeMap((sdk) => ununificlient.rest.copyTrading.tracingAll(sdk)),
map((res) => res.data.tracing!),
);
}

getTracing$(address: string): Observable<ExemplaryTraderTracing200ResponseTracingInner> {
return this.restSdk$.pipe(
mergeMap((sdk) => ununificlient.rest.copyTrading.tracing(sdk, address)),
map((res) => res.data.tracing!),
);
}
}
Loading