Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
RampPhase
} from "@vortexfi/shared";
import Big from "big.js";
import { encodeFunctionData, PublicClient } from "viem";
import { encodeFunctionData } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import logger from "../../../../config/logger";
import { MOONBEAM_EXECUTOR_PRIVATE_KEY } from "../../../../constants/constants";
Expand All @@ -20,15 +20,6 @@ import { BasePhaseHandler } from "../base-phase-handler";
* Handler for the monerium self-transfer phase
*/
export class MoneriumOnrampSelfTransferHandler extends BasePhaseHandler {
private polygonClient: PublicClient;
private evmClientManager: EvmClientManager;

constructor() {
super();
this.evmClientManager = EvmClientManager.getInstance();
this.polygonClient = this.evmClientManager.getClient(Networks.Polygon);
}

/**
* Get the phase name
*/
Expand Down Expand Up @@ -112,7 +103,8 @@ export class MoneriumOnrampSelfTransferHandler extends BasePhaseHandler {
],
functionName: "permit"
});
permitHash = await this.evmClientManager.sendTransactionWithBlindRetry(Networks.Polygon, account, {
const evmClientManager = EvmClientManager.getInstance();
permitHash = await evmClientManager.sendTransactionWithBlindRetry(Networks.Polygon, account, {
data: permitData,
to: ERC20_EURE_POLYGON_V2
});
Expand Down Expand Up @@ -174,8 +166,10 @@ export class MoneriumOnrampSelfTransferHandler extends BasePhaseHandler {
* @param chainId The chain ID
*/
private async waitForTransactionConfirmation(txHash: string): Promise<void> {
const evmClientManager = EvmClientManager.getInstance();
try {
const receipt = await this.polygonClient.waitForTransactionReceipt({
const polygonClient = evmClientManager.getClient(Networks.Polygon);
const receipt = await polygonClient.waitForTransactionReceipt({
hash: txHash as `0x${string}`
});
if (!receipt || receipt.status !== "success") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
SquidRouterPayResponse
} from "@vortexfi/shared";
import Big from "big.js";
import { createWalletClient, encodeFunctionData, Hash, PublicClient } from "viem";
import { encodeFunctionData, Hash } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { moonbeam, polygon } from "viem/chains";
import logger from "../../../../config/logger";
Expand All @@ -32,22 +32,6 @@ const DEFAULT_SQUIDROUTER_GAS_ESTIMATE = "1600000"; // Estimate used to calculat
* Handler for the squidRouter pay phase. Checks the status of the Axelar bridge and pays on native GLMR fee.
*/
export class SquidRouterPayPhaseHandler extends BasePhaseHandler {
private moonbeamPublicClient: PublicClient;
private polygonPublicClient: PublicClient;
private moonbeamWalletClient: ReturnType<typeof createWalletClient>;
private polygonWalletClient: ReturnType<typeof createWalletClient>;

constructor() {
super();
const evmClientManager = EvmClientManager.getInstance();
this.moonbeamPublicClient = evmClientManager.getClient(Networks.Moonbeam);
this.polygonPublicClient = evmClientManager.getClient(Networks.Polygon);

const moonbeamExecutorAccount = privateKeyToAccount(MOONBEAM_FUNDING_PRIVATE_KEY as `0x${string}`);
this.moonbeamWalletClient = evmClientManager.getWalletClient(Networks.Moonbeam, moonbeamExecutorAccount);
this.polygonWalletClient = evmClientManager.getWalletClient(Networks.Polygon, moonbeamExecutorAccount);
}

/**
* Get the phase name
*/
Expand Down Expand Up @@ -141,12 +125,16 @@ export class SquidRouterPayPhaseHandler extends BasePhaseHandler {

payTxHash = await this.executeFundTransaction(nativeToFundRaw, swapHash as `0x${string}`, logIndex, state, quote);

const evmClientManager = EvmClientManager.getInstance();
const moonbeamExecutorAccount = privateKeyToAccount(MOONBEAM_FUNDING_PRIVATE_KEY as `0x${string}`);

const isPolygon = quote.inputCurrency !== FiatToken.BRL;
const subsidyToken = isPolygon ? SubsidyToken.MATIC : SubsidyToken.GLMR;
const subsidyAmount = nativeToDecimal(nativeToFundRaw, 18).toNumber(); // Both MATIC and GLMR have 18 decimals
const payerAccount = isPolygon
? this.polygonWalletClient.account?.address
: this.moonbeamWalletClient.account?.address;
const payerAccount = evmClientManager.getWalletClient(
isPolygon ? Networks.Polygon : Networks.Moonbeam,
moonbeamExecutorAccount
).account?.address;

if (payerAccount) {
await this.createSubsidy(state, subsidyAmount, subsidyToken, payerAccount, payTxHash);
Expand Down Expand Up @@ -205,8 +193,13 @@ export class SquidRouterPayPhaseHandler extends BasePhaseHandler {
swapHash: `0x${string}`,
logIndex: number
): Promise<Hash> {
const evmClientManager = EvmClientManager.getInstance();
try {
const walletClientAccount = this.moonbeamWalletClient.account;
const moonbeamExecutorAccount = privateKeyToAccount(MOONBEAM_FUNDING_PRIVATE_KEY as `0x${string}`);
const moonbeamPublicClient = evmClientManager.getClient(Networks.Moonbeam);
const moonbeamWalletClient = evmClientManager.getWalletClient(Networks.Moonbeam, moonbeamExecutorAccount);

const walletClientAccount = moonbeamWalletClient.account;

if (!walletClientAccount) {
throw new Error("SquidRouterPayPhaseHandler: Moonbeam wallet client account not found.");
Expand All @@ -218,9 +211,9 @@ export class SquidRouterPayPhaseHandler extends BasePhaseHandler {
functionName: "addNativeGas"
});

const { maxFeePerGas, maxPriorityFeePerGas } = await this.moonbeamPublicClient.estimateFeesPerGas();
const { maxFeePerGas, maxPriorityFeePerGas } = await moonbeamPublicClient.estimateFeesPerGas();

const gasPaymentHash = await this.moonbeamWalletClient.sendTransaction({
const gasPaymentHash = await moonbeamWalletClient.sendTransaction({
account: walletClientAccount,
chain: moonbeam,
data: transactionData,
Expand Down Expand Up @@ -250,8 +243,13 @@ export class SquidRouterPayPhaseHandler extends BasePhaseHandler {
swapHash: `0x${string}`,
logIndex: number
): Promise<Hash> {
const evmClientManager = EvmClientManager.getInstance();
try {
const walletClientAccount = this.polygonWalletClient.account;
const polygonPublicClient = evmClientManager.getClient(Networks.Polygon);
const moonbeamExecutorAccount = privateKeyToAccount(MOONBEAM_FUNDING_PRIVATE_KEY as `0x${string}`);
const polygonWalletClient = evmClientManager.getWalletClient(Networks.Polygon, moonbeamExecutorAccount);

const walletClientAccount = polygonWalletClient.account;

if (!walletClientAccount) {
throw new Error("SquidRouterPayPhaseHandler: Polygon wallet client account not found.");
Expand All @@ -264,9 +262,9 @@ export class SquidRouterPayPhaseHandler extends BasePhaseHandler {
functionName: "addNativeGas"
});

const { maxFeePerGas, maxPriorityFeePerGas } = await this.polygonPublicClient.estimateFeesPerGas();
const { maxFeePerGas, maxPriorityFeePerGas } = await polygonPublicClient.estimateFeesPerGas();

const gasPaymentHash = await this.polygonWalletClient.sendTransaction({
const gasPaymentHash = await polygonWalletClient.sendTransaction({
account: walletClientAccount,
chain: polygon,
data: transactionData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@ import { BasePhaseHandler } from "../base-phase-handler";
* Handler for the squidRouter phase
*/
export class SquidRouterPhaseHandler extends BasePhaseHandler {
private moonbeamClient: PublicClient;
private polygonClient: PublicClient;

constructor() {
super();
const evmClientManager = EvmClientManager.getInstance();
this.moonbeamClient = evmClientManager.getClient(Networks.Moonbeam);
this.polygonClient = evmClientManager.getClient(Networks.Polygon);
}

/**
* Get the phase name
*/
Expand Down Expand Up @@ -120,25 +110,27 @@ export class SquidRouterPhaseHandler extends BasePhaseHandler {
* @returns The appropriate public client
*/
private async getPublicClient(state: RampState): Promise<PublicClient> {
const evmClientManager = EvmClientManager.getInstance();

try {
const quote = await QuoteTicket.findByPk(state.quoteId);
if (!quote) {
throw new Error(`Quote not found for ramp ${state.id}`);
}

if (quote.inputCurrency === FiatToken.EURC) {
return this.polygonClient;
return evmClientManager.getClient(Networks.Polygon);
} else if (quote.inputCurrency === FiatToken.BRL) {
return this.moonbeamClient;
return evmClientManager.getClient(Networks.Moonbeam);
} else {
logger.info(
`SquidRouterPhaseHandler: Using Moonbeam client as default for input currency: ${quote.inputCurrency}. This is a bug.`
);
return this.moonbeamClient;
return evmClientManager.getClient(Networks.Moonbeam);
}
} catch (error) {
logger.error("SquidRouterPhaseHandler: Error determining public client, defaulting to moonbeam", error);
return this.moonbeamClient;
return evmClientManager.getClient(Networks.Moonbeam);
}
}

Expand Down