Skip to content
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

915: Revert swap contract to v4 #916

Merged
3 commits merged into from
Jun 12, 2024
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
19 changes: 5 additions & 14 deletions src/entities/OrderERC20/OrderERC20Service.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import { Server, SwapERC20 } from "@airswap/libraries";
import {
createOrderERC20,
OrderERC20,
Pricing,
toAtomicString,
TokenInfo,
UnsignedOrderERC20,
} from "@airswap/utils";
import { Server } from "@airswap/libraries";
import { OrderERC20, toAtomicString, UnsignedOrderERC20 } from "@airswap/utils";
import { Web3Provider } from "@ethersproject/providers";

import { BigNumber } from "bignumber.js";

import { LAST_LOOK_ORDER_EXPIRY_SEC } from "../../constants/configParams";
import { AppError, isAppError } from "../../errors/appError";
import { isAppError } from "../../errors/appError";
import { createOrderERC20Signature } from "../../helpers/createSwapSignature";
import { isPromiseFulfilledResult } from "../../helpers/promise";
import { ExtendedPricing } from "../ExtendedPricing/ExtendedPricing";
import { getPricingQuoteAmount } from "../ExtendedPricing/ExtendedPricingHelpers";
import { getSwapErc20Address } from "../../helpers/swapErc20";
import { isOrderERC20 } from "./OrderERC20Helpers";
import { transformUnsignedOrderERC20ToOrderERC20 } from "./OrderERC20Transformers";

Expand Down Expand Up @@ -74,7 +65,7 @@ export const signOrderERC20AndSendForConsideration = async (
const signature = await createOrderERC20Signature(
unsignedOrder,
library.getSigner(),
SwapERC20.getAddress(chainId)!,
getSwapErc20Address(chainId)!,
chainId
);

Expand Down
10 changes: 5 additions & 5 deletions src/features/balances/balancesApi.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { SwapERC20, Wrapper, BatchCall } from "@airswap/libraries";
import { Wrapper, BatchCall } from "@airswap/libraries";

import erc20Abi from "erc-20-abi";
import { BigNumber, ethers, EventFilter, Event } from "ethers";
import { hexZeroPad, id } from "ethers/lib/utils";
import { BigNumber, ethers } from "ethers";

import { getSwapErc20Address } from "../../helpers/swapErc20";

interface WalletParams {
chainId: number;
Expand Down Expand Up @@ -30,7 +30,7 @@ const fetchBalancesOrAllowances: (
? [walletAddress, tokenAddresses]
: spenderAddressType === "Swap"
? // sender, spender, tokens.
[walletAddress, SwapERC20.getAddress(chainId), tokenAddresses]
[walletAddress, getSwapErc20Address(chainId), tokenAddresses]
: [walletAddress, Wrapper.getAddress(chainId), tokenAddresses];
const amounts: BigNumber[] = await contract[method].apply(null, args);
return amounts.map((amount) => amount.toString());
Expand Down
6 changes: 3 additions & 3 deletions src/features/makeOtc/makeOtcActions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { SwapERC20 } from "@airswap/libraries";
import {
createOrderERC20,
toAtomicString,
Expand All @@ -17,6 +16,7 @@ import {
} from "../../components/Toasts/ToastController";
import { AppErrorType, isAppError } from "../../errors/appError";
import { createOrderERC20Signature } from "../../helpers/createSwapSignature";
import { getSwapErc20Address } from "../../helpers/swapErc20";
import { sendOrderToIndexers } from "../indexer/indexerHelpers";
import { setError, setStatus, setUserOrder } from "./makeOtcSlice";

Expand Down Expand Up @@ -76,7 +76,7 @@ export const createOtcOrder = createAsyncThunk(
const signature = await createOrderERC20Signature(
unsignedOrder,
params.library.getSigner(),
SwapERC20.getAddress(params.chainId) || "",
getSwapErc20Address(params.chainId) || "",
params.chainId
);

Expand All @@ -95,7 +95,7 @@ export const createOtcOrder = createAsyncThunk(
...unsignedOrder,
...signature,
chainId: params.chainId,
swapContract: SwapERC20.getAddress(params.chainId) || "",
swapContract: getSwapErc20Address(params.chainId) || "",
};

dispatch(setUserOrder(fullOrder));
Expand Down
9 changes: 2 additions & 7 deletions src/features/metadata/metadataApi.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { SwapERC20 } from "@airswap/libraries";
import { TokenInfo, getTokenInfo } from "@airswap/utils";
import { Web3Provider } from "@ethersproject/providers";

import * as ethers from "ethers";
import uniqBy from "lodash.uniqby";

import { getSwapErc20Contract } from "../../helpers/swapErc20";
import { MetadataTokens } from "./metadataSlice";

const tokensCache: {
[chainId: number]: TokenInfo[];
} = {};

export const getActiveTokensLocalStorageKey: (
account: string,
chainId: number
Expand Down Expand Up @@ -94,6 +89,6 @@ export const getProtocolFee = async (
provider: Web3Provider
): Promise<number> => {
return (
await SwapERC20.getContract(provider, chainId).protocolFee()
await getSwapErc20Contract(provider, chainId).protocolFee()
).toNumber();
};
5 changes: 3 additions & 2 deletions src/features/orders/ordersActions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Registry, SwapERC20 } from "@airswap/libraries";
import { Registry } from "@airswap/libraries";
import {
FullOrderERC20,
OrderERC20,
Expand Down Expand Up @@ -40,6 +40,7 @@ import { AppErrorType, isAppError } from "../../errors/appError";
import transformUnknownErrorToAppError from "../../errors/transformUnknownErrorToAppError";
import { createOrderERC20Signature } from "../../helpers/createSwapSignature";
import getWethAddress from "../../helpers/getWethAddress";
import { getSwapErc20Address } from "../../helpers/swapErc20";
import toRoundedAtomicString from "../../helpers/toRoundedAtomicString";
import i18n from "../../i18n/i18n";
import { TransactionStatusType } from "../../types/transactionTypes";
Expand Down Expand Up @@ -397,7 +398,7 @@ export const takeLastLookOrder =
const signature = await createOrderERC20Signature(
unsignedOrder,
library.getSigner(),
SwapERC20.getAddress(chainId)!,
getSwapErc20Address(chainId)!,
chainId
);

Expand Down
16 changes: 10 additions & 6 deletions src/features/orders/ordersHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Server, SwapERC20, Wrapper, WETH } from "@airswap/libraries";
import { Server, Wrapper, WETH } from "@airswap/libraries";
import {
toAtomicString,
parseCheckResult,
Expand All @@ -12,12 +12,16 @@ import erc20Abi from "erc-20-abi";
import { BigNumber, ethers, Transaction } from "ethers";

import { RFQ_EXPIRY_BUFFER_MS } from "../../constants/configParams";
import { AppError, AppErrorType } from "../../errors/appError";
import { AppError } from "../../errors/appError";
import {
SwapError,
transformSwapErrorToAppError,
} from "../../errors/swapError";
import transformUnknownErrorToAppError from "../../errors/transformUnknownErrorToAppError";
import {
getSwapErc20Address,
getSwapErc20Contract,
} from "../../helpers/swapErc20";

const REQUEST_ORDER_TIMEOUT_MS = 5000;

Expand All @@ -28,7 +32,7 @@ async function swap(
provider: ethers.providers.Web3Provider,
order: OrderERC20 | FullOrderERC20
) {
let contract = await SwapERC20.getContract(provider.getSigner(), chainId);
let contract = await getSwapErc20Contract(provider.getSigner(), chainId);
if ("senderWallet" in order && order.senderWallet === ADDRESS_ZERO) {
return contract.swapAnySender(
await (await provider.getSigner()).getAddress(),
Expand Down Expand Up @@ -97,7 +101,7 @@ export async function approveToken(
) {
const spender =
contractType === "Swap"
? SwapERC20.getAddress(provider.network.chainId)
? getSwapErc20Address(provider.network.chainId)
: Wrapper.getAddress(provider.network.chainId);
const erc20Contract = new ethers.Contract(
baseToken,
Expand Down Expand Up @@ -200,7 +204,7 @@ export async function check(
isSwapWithWrap?: boolean
): Promise<AppError[]> {
const strings = await (
await SwapERC20.getContract(provider, chainId)
await getSwapErc20Contract(provider, chainId)
).check(senderWallet, ...orderERC20ToParams(order));

const errors = parseCheckResult(strings) as SwapError[];
Expand All @@ -225,7 +229,7 @@ export async function getNonceUsed(
order: FullOrderERC20,
provider: ethers.providers.BaseProvider
): Promise<boolean> {
return (await SwapERC20.getContract(provider, order.chainId)).nonceUsed(
return (await getSwapErc20Contract(provider, order.chainId)).nonceUsed(
order.signerWallet,
order.nonce
);
Expand Down
5 changes: 2 additions & 3 deletions src/features/takeOtc/takeOtcActions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { SwapERC20 } from "@airswap/libraries";
import {
decompressFullOrderERC20,
FullOrderERC20,
Expand All @@ -9,11 +8,11 @@ import { createAsyncThunk } from "@reduxjs/toolkit";
import { providers } from "ethers";

import {
notifyConfirmation,
notifyError,
notifyRejectedByUserError,
} from "../../components/Toasts/ToastController";
import { SubmittedCancellation } from "../../entities/SubmittedTransaction/SubmittedTransaction";
import { getSwapErc20Contract } from "../../helpers/swapErc20";
import i18n from "../../i18n/i18n";
import {
TransactionStatusType,
Expand Down Expand Up @@ -73,7 +72,7 @@ export const cancelOrder = createAsyncThunk(

dispatch(setStatus("signing"));

const tx = await SwapERC20.getContract(
const tx = await getSwapErc20Contract(
params.library.getSigner(),
params.chainId
)
Expand Down
6 changes: 2 additions & 4 deletions src/features/transactions/hooks/useLatestApproveFromEvents.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useEffect, useState } from "react";

import { SwapERC20 } from "@airswap/libraries";
import { useWeb3React } from "@web3-react/core";

import erc20Abi from "erc-20-abi";
Expand All @@ -13,6 +12,7 @@ import { transformToApproveEvent } from "../../../entities/ApproveEvent/ApproveE
import { isApprovalTransaction } from "../../../entities/SubmittedTransaction/SubmittedTransactionHelpers";
import { getTransactionReceiptMined } from "../../../helpers/ethers";
import { compareAddresses } from "../../../helpers/string";
import { getSwapErc20Address } from "../../../helpers/swapErc20";
import { selectActiveTokens } from "../../metadata/metadataSlice";
import useLatestPendingTransaction from "./useLatestPendingTransaction";

Expand Down Expand Up @@ -69,9 +69,7 @@ const useLatestApproveFromEvents = (
)
return;

if (
!compareAddresses(spenderAddress, SwapERC20.getAddress(chainId) || "")
)
if (!compareAddresses(spenderAddress, getSwapErc20Address(chainId) || ""))
return;

setLatestApprove(
Expand Down
4 changes: 2 additions & 2 deletions src/features/transactions/hooks/useLatestCancelFromEvents.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useEffect, useState } from "react";

import { SwapERC20 } from "@airswap/libraries";
import { useWeb3React } from "@web3-react/core";

import { BigNumber, Event } from "ethers";

import { CancelEvent } from "../../../entities/CancelEvent/CancelEvent";
import { transformToCancelEvent } from "../../../entities/CancelEvent/CancelEventTransformers";
import { compareAddresses } from "../../../helpers/string";
import { getSwapErc20Contract } from "../../../helpers/swapErc20";
import useNetworkSupported from "../../../hooks/useNetworkSupported";

const useLatestCancelFromEvents = (
Expand All @@ -26,7 +26,7 @@ const useLatestCancelFromEvents = (

if (account === accountState && chainId === chainIdState) return;

const swapContract = SwapERC20.getContract(provider, chainId);
const swapContract = getSwapErc20Contract(provider, chainId);
const cancelEvent = "Cancel";

swapContract.protocolFeeWallet().then(() => {
Expand Down
3 changes: 2 additions & 1 deletion src/features/transactions/hooks/useLatestSwapFromEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { BigNumber, Event } from "ethers";
import { FullSwapERC20Event } from "../../../entities/FullSwapERC20Event/FullSwapERC20Event";
import { transformToFullSwapERC20Event } from "../../../entities/FullSwapERC20Event/FullSwapERC20EventTransformers";
import { compareAddresses } from "../../../helpers/string";
import { getSwapErc20Contract } from "../../../helpers/swapErc20";
import useNetworkSupported from "../../../hooks/useNetworkSupported";

const useLatestSwapFromEvents = (
Expand All @@ -27,7 +28,7 @@ const useLatestSwapFromEvents = (

if (account === accountState && chainId === chainIdState) return;

const swapContract = SwapERC20.getContract(provider, chainId);
const swapContract = getSwapErc20Contract(provider, chainId);
const swapEvent = "SwapERC20";

swapContract.protocolFeeWallet().then((feeReceiver: string) => {
Expand Down
3 changes: 2 additions & 1 deletion src/features/transactions/hooks/useSwapLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useWeb3React } from "@web3-react/core";
import { Event } from "ethers";

import getContractEvents from "../../../helpers/getContractEvents";
import { getSwapErc20Contract } from "../../../helpers/swapErc20";
import useNetworkSupported from "../../../hooks/useNetworkSupported";

interface SwapLogs {
Expand Down Expand Up @@ -79,7 +80,7 @@ const useSwapLogs = (

if (account === accountState && chainId === chainIdState) return;

const swapContract = SwapERC20.getContract(provider, chainId);
const swapContract = getSwapErc20Contract(provider, chainId);
const wrapperContract = Wrapper.getContract(provider, chainId);
actions.execute(swapContract, wrapperContract, account);

Expand Down
Loading
Loading