Skip to content

Commit

Permalink
feature: added results for bridge operations
Browse files Browse the repository at this point in the history
  • Loading branch information
H34D committed Apr 10, 2024
1 parent 0ee6ec7 commit 4f6d0f0
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 61 deletions.
23 changes: 17 additions & 6 deletions src/modules/token/deposit.ts
Expand Up @@ -5,7 +5,7 @@ import {
import { BigNumber, utils } from "ethers";

import { Messages } from "../../collections";
import { MasaInterface } from "../../interface";
import { BaseResult, MasaInterface } from "../../interface";

/**
*
Expand All @@ -15,7 +15,11 @@ import { MasaInterface } from "../../interface";
export const deposit = async (
masa: MasaInterface,
amount: string,
): Promise<void> => {
): Promise<BaseResult> => {
const result: BaseResult = {
success: false,
};

const tokenAmount = BigNumber.from(utils.parseEther(amount));

console.log(`Depositing ${parseFloat(amount).toLocaleString()} MASA!`);
Expand All @@ -25,8 +29,10 @@ export const deposit = async (
(masa.config.networkName !== "masa" &&
masa.config.networkName !== "masatest")
) {
console.log(`Unable to deposit on ${masa.config.networkName}!`);
return;
result.message = `Unable to deposit on ${masa.config.networkName}!`;
console.error(result.message);

return result;
}

// origin
Expand All @@ -38,7 +44,7 @@ export const deposit = async (
try {
const { deposit } = oft;

console.log("Depositting ...");
console.log("Depositing ...");

const { wait, hash } = await deposit({
value: tokenAmount,
Expand All @@ -54,9 +60,14 @@ export const deposit = async (
await wait();

console.log("Deposit done!");

result.success = true;
} catch (error: unknown) {
if (error instanceof Error) {
console.error(error.message);
result.message = `Deposit failed! ${error.message}`;
console.error(result.message);
}
}

return result;
};
147 changes: 97 additions & 50 deletions src/modules/token/swap.ts
Expand Up @@ -14,8 +14,15 @@ import { SendParamStruct as SendParamStructMasaTokenOFT } from "@masa-finance/ma
import { BigNumber, constants, utils } from "ethers";

import { Messages, SupportedNetworks } from "../../collections";
import { MasaInterface, NetworkName } from "../../interface";
import { BaseResult, MasaInterface, NetworkName } from "../../interface";

const lzUrl = (txHash: string, testnet: boolean = false) =>
`https://${testnet ? "testnet." : ""}layerzeroscan.com/tx/${txHash}`;

/**
*
* @param masa
*/
export const loadOFTContract = (
masa: MasaInterface,
): MasaToken | MasaTokenOFT | MasaTokenNativeOFT | undefined => {
Expand Down Expand Up @@ -50,26 +57,35 @@ export const loadOFTContract = (
return oft;
};

export interface QuoteResult extends BaseResult {
nativeFee: BigNumber;
lzTokenFee: BigNumber;
gasLimit: BigNumber;
transactionCost: BigNumber;
}

/**
*
* @param masa
* @param sendParameters
*/
export const getSwapQuote = async (
masa: MasaInterface,
sendParameters:
| SendParamStructMasaToken
| SendParamStructMasaTokenNativeOFT
| SendParamStructMasaTokenOFT,
): Promise<
| {
nativeFee: BigNumber;
lzTokenFee: BigNumber;
gasLimit: BigNumber;
transactionCost: BigNumber;
}
| undefined
> => {
): Promise<BaseResult | QuoteResult> => {
let result: BaseResult | QuoteResult = {
success: false,
};

const oft = loadOFTContract(masa);

if (!oft) {
console.error("Unable to load OFT!");
return;
result.message = "Unable to load OFT!";
console.error(result);
return result;
}

try {
Expand Down Expand Up @@ -112,19 +128,30 @@ export const getSwapQuote = async (

const transactionCost = gasLimit.mul(gasPrice);

return {
result = {
success: true,
nativeFee,
lzTokenFee,
gasLimit,
transactionCost,
};
} catch (error: unknown) {
if (error instanceof Error) {
console.error(`Failed to quote send! ${error.message}`);
result.message = `Failed to quote send! ${error.message}`;
console.error(result.message);
}
}

return result;
};

/**
*
* @param eid
* @param receiverAddress
* @param tokenAmount
* @param slippage
*/
export const getSwapParameters = (
eid: EndpointId,
receiverAddress: string,
Expand Down Expand Up @@ -166,6 +193,10 @@ export const getSwapParameters = (
};
};

export interface SwapResult extends BaseResult {
layerZeroScanUrl?: string;
}

/**
*
* @param masa
Expand All @@ -178,7 +209,11 @@ export const swap = async (
to: NetworkName,
amount: string,
slippage?: number,
): Promise<void> => {
): Promise<SwapResult> => {
const result: SwapResult = {
success: false,
};

console.log(
`Swapping ${parseFloat(amount).toLocaleString()} MASA from '${masa.config.networkName}' to '${to}'!`,
);
Expand All @@ -188,8 +223,10 @@ export const swap = async (
const { lzEndpointId: toEID } = SupportedNetworks[to] ?? {};

if (!masa.config.network?.addresses.tokens?.MASA || !toEID) {
console.log(`Unable to swap from ${masa.config.networkName} to ${to}!`);
return;
result.message = `Unable to swap from ${masa.config.networkName} to ${to}!`;
console.error(result.message);

return result;
}

const tokenAmount = BigNumber.from(utils.parseEther(amount));
Expand All @@ -208,8 +245,10 @@ export const swap = async (
const oft = loadOFTContract(masa);

if (!oft) {
console.error("Unable to load OFT!");
return;
result.message = "Unable to load OFT!";
console.error(result.message);

return result;
}

try {
Expand All @@ -218,48 +257,56 @@ export const swap = async (
const peer = await oft.peers(toEID);

if (peer === utils.hexZeroPad(constants.AddressZero, 32)) {
console.error(
`'${oft.address}' has no registered peer for network ${toEID}!`,
);
return;
result.message = `'${oft.address}' has no registered peer for network ${toEID}!`;
console.error(result.message);
return result;
}

const fees = await getSwapQuote(masa, sendParameters);

if (!fees) {
console.error("Unable to load fees!");
return;
if (!fees.success) {
result.message = `Unable to load fees! ${fees.message}`;
console.error(result.message);
return result;
}

console.log(
`Fees NativeFee: ${utils.formatEther(fees.nativeFee)} LZTokenFee: ${utils.formatEther(fees.lzTokenFee)}`,
);
if ("nativeFee" in fees) {
console.log(
`Fees NativeFee: ${utils.formatEther(fees.nativeFee)} LZTokenFee: ${utils.formatEther(fees.lzTokenFee)}`,
);

console.log("Swapping ...");

const { wait, hash } = await send(sendParameters, fees, address, {
value:
masa.config.networkName === "masa" ||
masa.config.networkName === "masatest"
? fees.nativeFee.add(sendParameters.amountLD)
: fees.nativeFee,
});

console.log(
Messages.WaitingToFinalize(
hash,
masa.config.network.blockExplorerUrls?.[0],
),
);
console.log("Swapping ...");

const { wait, hash } = await send(sendParameters, fees, address, {
value:
masa.config.networkName === "masa" ||
masa.config.networkName === "masatest"
? fees.nativeFee.add(sendParameters.amountLD)
: fees.nativeFee,
});

console.log(
Messages.WaitingToFinalize(
hash,
masa.config.network.blockExplorerUrls?.[0],
),
);

await wait();
await wait();

console.log(
"Swap done! Please note: it can take some times (20-30 mins) for the token to show up on the target network!",
);
result.success = true;
result.layerZeroScanUrl = lzUrl(hash, masa.config.network.isTestnet);

console.log(
`Swap done! Please note: it can take some times (20-30 mins) for the token to show up on the target network! You can check the status on Layerzero scan: ${result.layerZeroScanUrl}`,
);
}
} catch (error: unknown) {
if (error instanceof Error) {
console.error(error.message);
result.message = `Swap failed! ${error.message}`;
console.error(result.message);
}
}

return result;
};
19 changes: 14 additions & 5 deletions src/modules/token/withdraw.ts
Expand Up @@ -5,7 +5,7 @@ import {
import { BigNumber, utils } from "ethers";

import { Messages } from "../../collections";
import { MasaInterface } from "../../interface";
import { BaseResult, MasaInterface } from "../../interface";

/**
*
Expand All @@ -15,7 +15,11 @@ import { MasaInterface } from "../../interface";
export const withdraw = async (
masa: MasaInterface,
amount: string,
): Promise<void> => {
): Promise<BaseResult> => {
const result: BaseResult = {
success: false,
};

const tokenAmount = BigNumber.from(utils.parseEther(amount));

console.log(`Withdrawing ${parseFloat(amount).toLocaleString()} MASA!`);
Expand All @@ -25,8 +29,9 @@ export const withdraw = async (
(masa.config.networkName !== "masa" &&
masa.config.networkName !== "masatest")
) {
console.log(`Unable to withdraw on ${masa.config.networkName}!`);
return;
result.message = `Unable to withdraw on ${masa.config.networkName}!`;
console.log(result.message);
return result;
}

// origin
Expand All @@ -52,9 +57,13 @@ export const withdraw = async (
await wait();

console.log("Withdraw done!");
result.success = true;
} catch (error: unknown) {
if (error instanceof Error) {
console.error(error.message);
result.message = `Withdraw failed! ${error.message}`;
console.error(result.message);
}
}

return result;
};

0 comments on commit 4f6d0f0

Please sign in to comment.