Skip to content
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
4 changes: 2 additions & 2 deletions packages/swapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
"test": "jest --passWithNoTests"
},
"dependencies": {
"@cetusprotocol/aggregator-sdk": "1.4.5",
"@cetusprotocol/aggregator-sdk": "1.4.6",
"@gemwallet/types": "workspace:*",
"@mayanfinance/swap-sdk": "13.2.0",
"@mysten/sui": "1.45.2",
"@mysten/sui": "2.9.1",
"@orca-so/whirlpools": "7.0.1",
"@orca-so/whirlpools-client": "6.2.0",
"@orca-so/whirlpools-core": "3.1.0",
Expand Down
23 changes: 12 additions & 11 deletions packages/swapper/src/cetus/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import {
BLUEFIN,
} from "@cetusprotocol/aggregator-sdk";
import { QuoteRequest, Quote, SwapQuoteData, AssetId, SwapQuoteDataType } from "@gemwallet/types";
import { SuiClient } from "@mysten/sui/client";
import { Transaction } from "@mysten/sui/transactions";
import { BN } from "bn.js";

// @ts-ignore — v2 ESM types unresolvable under moduleResolution "node"
import { SuiJsonRpcClient } from "@mysten/sui/jsonRpc";
// @ts-ignore — v2 ESM types unresolvable under moduleResolution "node"
import { Transaction } from "@mysten/sui/transactions";

import { SUI_COIN_TYPE } from "../chain/sui/constants";
import { calculateGasBudget, prefillTransaction, getGasPriceAndCoinRefs } from "../chain/sui/tx_builder";
import { Protocol } from "../protocol";
Expand All @@ -21,19 +24,19 @@ import { bnReplacer, bnReviver } from "./bn_replacer";

export class CetusAggregatorProvider implements Protocol {
private client: AggregatorClient;
private suiClient: SuiClient;
private suiRpcUrl: string;
private overlayFeeReceiver: string;
private readonly selectedProtocols: string[] = [CETUS, DEEPBOOKV2, DEEPBOOKV3, BLUEFIN];

constructor(suiRpcUrl: string) {
this.suiClient = new SuiClient({ url: suiRpcUrl });
this.suiRpcUrl = suiRpcUrl;
this.overlayFeeReceiver = getReferrerAddresses().sui;
this.client = this.createClient();
}

createClient(address?: string, overlayFeeRate?: number, overlayFeeReceiver?: string) {
return new AggregatorClient({
client: this.suiClient,
client: new SuiJsonRpcClient({ network: "mainnet", url: this.suiRpcUrl }),
env: Env.Mainnet,
overlayFeeRate,
overlayFeeReceiver,
Expand Down Expand Up @@ -118,18 +121,18 @@ export class CetusAggregatorProvider implements Protocol {
slippage: slippage_bps / 10000,
};

// create a new client with user's address as signer, overlay fee rate and overlay fee receiver
const client = this.createClient(
quote.quote.from_address,
quote.quote.referral_bps / 10000,
this.overlayFeeReceiver,
);

const gasPriceAndCoinRefsReq = getGasPriceAndCoinRefs(this.suiClient, quote.quote.from_address);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const suiClient = (client as any).client;
const gasPriceAndCoinRefsReq = getGasPriceAndCoinRefs(suiClient, quote.quote.from_address);
const fastRouterSwapReq = client.fastRouterSwap(swapParams);
const [{ gasPrice, coinRefs }] = await Promise.all([gasPriceAndCoinRefsReq, fastRouterSwapReq]);

// inspect transaction
const result = await client.devInspectTransactionBlock(txb);
if (result.error) {
throw new Error(`Swap simulation failed: ${result.error}`);
Expand All @@ -138,12 +141,10 @@ export class CetusAggregatorProvider implements Protocol {
throw new Error(`Swap simulation failed: ${result.effects.status.error}`);
}

// build transaction
const gasBudget = calculateGasBudget(result.effects);
prefillTransaction(txb, quote.quote.from_address, gasBudget, gasPrice, coinRefs);
const serializedTx = await txb.build({ client: this.suiClient });
const serializedTx = await txb.build({ client: suiClient });

// build quote data
const quoteData: SwapQuoteData = {
to: "",
value: "0",
Expand Down
42 changes: 33 additions & 9 deletions packages/swapper/src/chain/sui/tx_builder.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
import { SuiClient, TransactionEffects } from "@mysten/sui/client";
import { Transaction } from "@mysten/sui/transactions";

import { BigIntMath } from "../../bigint_math";
import { SUI_COIN_TYPE } from "./constants";

interface CoinRef {
objectId: string;
version: string;
digest: string;
}

export interface SuiRpcClient {
getReferenceGasPrice(): Promise<string | bigint>;
getCoins(input: { owner: string; coinType: string; limit: number }): Promise<{
data: { coinObjectId: string; version: string; digest: string }[];
}>;
}

export interface SuiTransactionEffects {
status: { status: string; error?: string };
gasUsed: { computationCost: string; storageCost: string; storageRebate: string };
}

export interface SuiTransactionPrerequisites {
gasPrice: bigint;
coinRefs: { objectId: string; version: string; digest: string }[];
coinRefs: CoinRef[];
}

export interface SuiTransactionBuilder {
setSender(address: string): void;
setGasPrice(price: bigint): void;
setGasBudget(budget: bigint): void;
setGasPayment(coins: CoinRef[]): void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
build(options: { client: any }): Promise<Uint8Array>;
}

export async function getGasPriceAndCoinRefs(
suiClient: SuiClient,
suiClient: SuiRpcClient,
ownerAddress: string,
): Promise<SuiTransactionPrerequisites> {
const [gasPrice, coins] = await Promise.all([
Expand All @@ -27,8 +51,8 @@ export async function getGasPriceAndCoinRefs(
return { gasPrice: BigInt(gasPrice), coinRefs };
}

export function calculateGasBudget(transactionEffects: TransactionEffects, increasePercentage: number = 20): bigint {
const gasUsed = transactionEffects.gasUsed;
export function calculateGasBudget(effects: SuiTransactionEffects, increasePercentage: number = 20): bigint {
const gasUsed = effects.gasUsed;
const computationBudget = BigInt(gasUsed.computationCost);
const storageBudget = BigInt(gasUsed.storageCost) - BigInt(gasUsed.storageRebate);

Expand All @@ -38,11 +62,11 @@ export function calculateGasBudget(transactionEffects: TransactionEffects, incre
}

export function prefillTransaction(
transaction: Transaction,
transaction: SuiTransactionBuilder,
senderAddress: string,
gasBudget: bigint,
gasPrice: bigint,
coinRefs: { objectId: string; version: string; digest: string }[],
coinRefs: CoinRef[],
) {
transaction.setSender(senderAddress);
transaction.setGasPrice(gasPrice);
Expand Down
11 changes: 7 additions & 4 deletions packages/swapper/src/mayan/sui.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { SwapQuoteData, QuoteRequest, SwapQuoteDataType } from "@gemwallet/types";
import { Quote as MayanQuote, ReferrerAddresses, createSwapFromSuiMoveCalls } from "@mayanfinance/swap-sdk";
import { SuiClient } from "@mysten/sui/client";

// @ts-ignore — v2 ESM types unresolvable under moduleResolution "node"
import { SuiJsonRpcClient } from "@mysten/sui/jsonRpc";

import { calculateGasBudget, prefillTransaction, getGasPriceAndCoinRefs } from "../chain/sui/tx_builder";
import { getReferrerAddresses } from "../referrer";
Expand All @@ -11,24 +13,25 @@ export async function buildSuiQuoteData(
suiRpc: string,
): Promise<SwapQuoteData> {
const referrerAddresses = getReferrerAddresses() as ReferrerAddresses;
const suiClient = new SuiClient({ url: suiRpc });
const suiClient = new SuiJsonRpcClient({ network: "mainnet", url: suiRpc });

try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const [suiTx, { gasPrice, coinRefs }] = await Promise.all([
createSwapFromSuiMoveCalls(
routeData,
request.from_address,
request.to_address,
referrerAddresses,
null,
suiClient,
suiClient as any,
),
getGasPriceAndCoinRefs(suiClient, request.from_address),
]);

const inspectResult = await suiClient.devInspectTransactionBlock({
transactionBlock: suiTx,
sender: request.from_address,
transactionBlock: suiTx,
});

if (inspectResult.error) {
Expand Down
Loading
Loading