Skip to content

[GSW-2049] DrySwap, SwapRoute func(Exact_in, Exact_out) #600

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

Closed
wants to merge 2 commits into from
Closed
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
35 changes: 35 additions & 0 deletions packages/web/src/common/clients/gno-provider/methods/dry-swap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { GnoProvider } from "@gnolang/gno-js-client";

import { evaluateExpressionToNumber, makeABCIParams } from "@utils/rpc-utils";
import { DrySwapRequest } from "@repositories/swap/request/swap-route-request";
import { makeRawTokenAmount } from "@utils/token-utils";
import { makeRoutesQuery } from "@utils/swap-route-utils";
import { checkGnotPath } from "@utils/common";

export async function drySwap(gnoProvider: GnoProvider, packagePath: string, request: DrySwapRequest): Promise<number> {
const { inputToken, outputToken, tokenAmount, exactType, estimatedRoutes, tokenAmountLimit } = request;

const targetToken = exactType === "EXACT_IN" ? inputToken : outputToken;
const resultToken = exactType === "EXACT_IN" ? outputToken : inputToken;
const tokenAmountRaw = makeRawTokenAmount(targetToken, tokenAmount) || "0";
const tokenAmountLimitRaw = makeRawTokenAmount(resultToken, tokenAmountLimit) || "0";
const routesQuery = makeRoutesQuery(estimatedRoutes, checkGnotPath(inputToken.path));
const quotes = estimatedRoutes.map(route => route.quote).join(",");

const abciQueryParams = makeABCIParams("DrySwap", [
inputToken.path,
outputToken.path,
tokenAmountRaw,
exactType,
routesQuery,
quotes,
tokenAmountLimitRaw,
]);
try {
const abciResponse = await gnoProvider.evaluateExpression(packagePath, abciQueryParams);
return evaluateExpressionToNumber(abciResponse);
} catch (e) {
console.log(e);
}
return -1;
}
24 changes: 19 additions & 5 deletions packages/web/src/hooks/pool/data/use-reposition-handle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -427,14 +427,28 @@ export const useRepositionHandle = () => {
? BigNumber(estimatedRepositionAmounts?.amountB || 0).minus(BigNumber(currentAmounts?.amountB || 0))
: BigNumber(estimatedRepositionAmounts?.amountA || 0).minus(BigNumber(currentAmounts?.amountA || 0));

return swapRouterRepository
.sendSwapRoute({
...estimateSwapRequest,
const deadline = Math.floor(Date.now() / 1000) + 300; // 5분

// estimateSwapRequest.exactType에 따라 분기
if (estimateSwapRequest.exactType === "EXACT_IN") {
return swapRouterRepository.sendExactInSwapRoute({
inputToken: estimateSwapRequest.inputToken,
outputToken: estimateSwapRequest.outputToken,
estimatedRoutes: estimatedSwapResult.estimatedRoutes,
tokenAmount: inputAmount.toNumber(),
tokenAmountLimit: outputAmount.toNumber() * ((100 - DEFAULT_SLIPPAGE) / 100),
})
.catch(() => null);
deadline,
});
} else {
return swapRouterRepository.sendExactOutSwapRoute({
inputToken: estimateSwapRequest.inputToken,
outputToken: estimateSwapRequest.outputToken,
estimatedRoutes: estimatedSwapResult.estimatedRoutes,
tokenAmount: outputAmount.toNumber(),
tokenAmountLimit: inputAmount.toNumber() * ((100 + DEFAULT_SLIPPAGE) / 100),
deadline,
});
}
}, [
address,
currentAmounts,
Expand Down
33 changes: 23 additions & 10 deletions packages/web/src/hooks/swap/data/use-swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,29 @@ export const useSwap = ({ tokenA, tokenB, direction, slippage, swapFee = 15 }: U
return null;
}

const latestTokenAmountLimit = store.get(SwapState.swapConfirmModalState).tokenAmountLimit;

return swapRouterRepository.sendSwapRoute({
inputToken: tokenA,
outputToken: tokenB,
estimatedRoutes,
exactType: direction,
tokenAmount: direction === "EXACT_IN" ? Number(tokenAmount) : Number(tokenAmount) * exactOutPadding,
tokenAmountLimit: latestTokenAmountLimit || tokenAmountLimit,
});
// check this args
if (direction === "EXACT_IN") {
return swapRouterRepository.sendExactInSwapRoute({
inputToken: tokenA,
outputToken: tokenB,
tokenAmount: Number(tokenAmount),
estimatedRoutes: estimatedRoutes,
tokenAmountLimit: tokenAmountLimit,
deadline: Math.floor(Date.now() / 1000) + 60 * 20,
});
}

// check this args
if (direction === "EXACT_OUT") {
return swapRouterRepository.sendExactOutSwapRoute({
inputToken: tokenA,
outputToken: tokenB,
tokenAmount: Number(tokenAmount) * exactOutPadding,
estimatedRoutes: estimatedRoutes,
tokenAmountLimit: tokenAmountLimit,
deadline: Math.floor(Date.now() / 1000) + 60 * 20,
});
}
},
[
account,
Expand Down
16 changes: 15 additions & 1 deletion packages/web/src/repositories/swap/request/swap-route-request.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EstimatedRoute } from "@models/swap/swap-route-info";
import { TokenModel } from "@models/token/token-model";

export interface SwapRouteRequest {
export interface DrySwapRequest {
inputToken: TokenModel;

outputToken: TokenModel;
Expand All @@ -14,3 +14,17 @@ export interface SwapRouteRequest {

tokenAmountLimit: number;
}

export interface SwapRouteRequest {
inputToken: TokenModel;

outputToken: TokenModel;

tokenAmount: number;

estimatedRoutes: EstimatedRoute[];

tokenAmountLimit: number;

deadline: number;
}
57 changes: 53 additions & 4 deletions packages/web/src/repositories/swap/swap-router-repository-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ import { getGRC20Allowance } from "@common/clients/gno-provider";
import { DEFAULT_GAS_FEE } from "@common/values";
import { GnoProvider } from "@gnolang/gno-js-client";
import { GetRoutesRequest } from "./request/get-routes-request";
import { SwapRouteRequest } from "./request/swap-route-request";
import { SwapRouteRequest, DrySwapRequest } from "./request/swap-route-request";
import { UnwrapTokenRequest } from "./request/unwrap-token-request";
import { WrapTokenRequest } from "./request/wrap-token-request";
import { GetRoutesResponse } from "./response/get-routes-response";
import { SwapRouteFailedResponse, SwapRouteSuccessResponse } from "./response/swap-route-response";
import { SwapRouterRepository } from "./swap-router-repository";
import {
makeSwapRouteMessageWithApproves,
makeExactInSwapRouteMessageWithApproves,
makeExactOutSwapRouteMessageWithApproves,
makeUnwrapTokenMessages,
makeWrapTokenMessages,
} from "./swap-router.message";
import { drySwap } from "@common/clients/gno-provider/methods/dry-swap";

export class SwapRouterRepositoryImpl implements SwapRouterRepository {
private rpcProvider: GnoProvider | null;
Expand Down Expand Up @@ -81,7 +83,48 @@ export class SwapRouterRepositoryImpl implements SwapRouterRepository {
return response.data;
};

public sendSwapRoute = async (
//Todo: Implement this code
public getDrySwap = async (request: DrySwapRequest): Promise<number> => {
if (!this.rpcProvider) {
throw new CommonError("FAILED_INITIALIZE_GNO_PROVIDER");
}

// Discuss if needed
if (!PACKAGE_ROUTER_PATH) {
throw new CommonError("FAILED_INITIALIZE_ENVIRONMENT");
}

return await drySwap(this.rpcProvider, PACKAGE_ROUTER_PATH, request);
};

public sendExactInSwapRoute = async (
request: SwapRouteRequest,
): Promise<WalletResponse<SwapRouteSuccessResponse | SwapRouteFailedResponse>> => {
if (this.rpcProvider === null) {
throw new CommonError("FAILED_INITIALIZE_GNO_PROVIDER");
}

const address = await this.getAddress();

// Dry SWAP implement
// const drySwapResponse = await this.getDrySwap(request);
// if (drySwapResponse.status !== 200) {
// throw new SwapError("SWAP_FAILED");
// }

const messages = await makeExactInSwapRouteMessageWithApproves(
{ ...request, caller: address },
(packagePath, owner, spender) => getGRC20Allowance(this.rpcProvider!, packagePath, owner, spender),
);

return await this.walletClient!.sendTransaction({
messages,
gasFee: DEFAULT_GAS_FEE,
memo: "",
});
};

public sendExactOutSwapRoute = async (
request: SwapRouteRequest,
): Promise<WalletResponse<SwapRouteSuccessResponse | SwapRouteFailedResponse>> => {
if (this.rpcProvider === null) {
Expand All @@ -90,7 +133,13 @@ export class SwapRouterRepositoryImpl implements SwapRouterRepository {

const address = await this.getAddress();

const messages = await makeSwapRouteMessageWithApproves(
// Dry SWAP implement
// const drySwapResponse = await this.getDrySwap(request);
// if (drySwapResponse.status !== 200) {
// throw new SwapError("SWAP_FAILED");
// }

const messages = await makeExactOutSwapRouteMessageWithApproves(
{ ...request, caller: address },
(packagePath, owner, spender) => getGRC20Allowance(this.rpcProvider!, packagePath, owner, spender),
);
Expand Down
12 changes: 12 additions & 0 deletions packages/web/src/repositories/swap/swap-router-repository-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ export class SwapRouterRepositoryMock implements SwapRouterRepository {
throw new Error("Mock sendSwapRoute");
};

public getDrySwap = async () => {
throw new Error("Mcok drySwapRoute");
};

public sendExactInSwapRoute = async () => {
throw new Error("Mock sendExactInSwapRoute");
};

public sendExactOutSwapRoute = async () => {
throw new Error("Mock sendExactOutSwapRoute");
};

public sendWrapToken = async () => {
throw new Error("Mock sendWrapToken");
};
Expand Down
10 changes: 8 additions & 2 deletions packages/web/src/repositories/swap/swap-router-repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { WalletResponse } from "@common/clients/wallet-client/protocols";

import { GetRoutesRequest } from "./request/get-routes-request";
import { SwapRouteRequest } from "./request/swap-route-request";
import { DrySwapRequest, SwapRouteRequest } from "./request/swap-route-request";
import { UnwrapTokenRequest } from "./request/unwrap-token-request";
import { WrapTokenRequest } from "./request/wrap-token-request";
import { GetRoutesResponse } from "./response/get-routes-response";
Expand All @@ -10,7 +10,13 @@ import { SwapRouteFailedResponse, SwapRouteSuccessResponse } from "./response/sw
export interface SwapRouterRepository {
getRoutes: (request: GetRoutesRequest) => Promise<GetRoutesResponse>;

sendSwapRoute: (
getDrySwap: (request: DrySwapRequest) => Promise<number>;

sendExactInSwapRoute: (
request: SwapRouteRequest,
) => Promise<WalletResponse<SwapRouteSuccessResponse | SwapRouteFailedResponse>>;

sendExactOutSwapRoute: (
request: SwapRouteRequest,
) => Promise<WalletResponse<SwapRouteSuccessResponse | SwapRouteFailedResponse>>;

Expand Down
Loading
Loading