Skip to content

Commit

Permalink
remaining of swaps sdk migrations, fix wrap/unwrap (#1746)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Sinclair <[email protected]>
  • Loading branch information
BrodyHughes and DanielSinclair authored Oct 26, 2024
1 parent 30ef631 commit 5d27d77
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 100 deletions.
11 changes: 5 additions & 6 deletions src/core/raps/actions/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ETH_ADDRESS as ETH_ADDRESS_AGGREGATORS,
Quote,
ChainId as SwapChainId,
SwapType,
WRAPPED_ASSET,
fillQuote,
getQuoteExecutionDetails,
Expand All @@ -22,7 +23,6 @@ import { ChainId } from '~/core/types/chains';
import { NewTransaction, TxHash } from '~/core/types/transactions';
import { add } from '~/core/utils/numbers';
import { isLowerCaseMatch } from '~/core/utils/strings';
import { isUnwrapEth, isWrapEth } from '~/core/utils/swaps';
import { addNewTransaction } from '~/core/utils/transactions';
import { getProvider } from '~/core/wagmi/clientToProvider';
import { TransactionSimulationResponse } from '~/entries/popup/pages/messages/useSimulateTransaction';
Expand Down Expand Up @@ -232,23 +232,22 @@ export const executeSwap = async ({
}): Promise<Transaction | null> => {
if (!wallet || !quote) return null;

const { sellTokenAddress, buyTokenAddress } = quote;
const transactionParams = {
gasLimit: toHex(gasLimit) || undefined,
nonce: nonce ? toHex(`${nonce}`) : undefined,
...gasParams,
};

// Wrap Eth
if (isWrapEth({ buyTokenAddress, sellTokenAddress, chainId })) {
// Wrap native
if (quote.swapType === SwapType.wrap) {
return wrapNativeAsset(
quote.buyAmount,
wallet,
chainId as unknown as SwapChainId,
transactionParams,
);
// Unwrap Weth
} else if (isUnwrapEth({ buyTokenAddress, sellTokenAddress, chainId })) {
// Unwrap native
} else if (quote.swapType === SwapType.unwrap) {
return unwrapNativeAsset(
quote.sellAmount,
wallet,
Expand Down
54 changes: 10 additions & 44 deletions src/core/raps/unlockAndCrosschainSwap.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import {
ALLOWS_PERMIT,
ChainId,
ETH_ADDRESS as ETH_ADDRESS_AGGREGATOR,
PermitSupportedTokenList,
WRAPPED_ASSET,
} from '@rainbow-me/swaps';
import { Address } from 'viem';

import { ETH_ADDRESS } from '../references';
import { isNativeAsset } from '../utils/chains';
import { add } from '../utils/numbers';
import { isLowerCaseMatch } from '../utils/strings';

import { assetNeedsUnlocking, estimateApprove } from './actions';
import { estimateCrosschainSwapGasLimit } from './actions/crosschainSwap';
Expand All @@ -29,29 +19,20 @@ export const estimateUnlockAndCrosschainSwap = async (
const {
from: accountAddress,
sellTokenAddress,
buyTokenAddress,
allowanceTarget,
allowanceNeeded,
} = quote as {
from: Address;
sellTokenAddress: Address;
buyTokenAddress: Address;
allowanceTarget: Address;
allowanceNeeded: boolean;
};

const isNativeAssetUnwrapping =
(isLowerCaseMatch(sellTokenAddress, WRAPPED_ASSET?.[chainId]) &&
isLowerCaseMatch(buyTokenAddress, ETH_ADDRESS)) ||
isLowerCaseMatch(buyTokenAddress, ETH_ADDRESS_AGGREGATOR);

let gasLimits: (string | number)[] = [];
let swapAssetNeedsUnlocking = false;

// Aggregators represent native asset as 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE
const nativeAsset =
isLowerCaseMatch(ETH_ADDRESS_AGGREGATOR, sellTokenAddress) ||
isNativeAsset(assetToSell.address, chainId);

if (!isNativeAssetUnwrapping && !nativeAsset) {
if (allowanceNeeded) {
swapAssetNeedsUnlocking = await assetNeedsUnlocking({
owner: accountAddress,
amount: sellAmount,
Expand Down Expand Up @@ -95,28 +76,19 @@ export const createUnlockAndCrosschainSwapRap = async (

const {
from: accountAddress,
sellTokenAddress,
buyTokenAddress,
allowanceTarget,
allowanceNeeded,
} = quote as {
from: Address;
sellTokenAddress: Address;
buyTokenAddress: Address;
allowanceTarget: Address;
allowanceNeeded: boolean;
};

const isNativeAssetUnwrapping =
isLowerCaseMatch(sellTokenAddress, WRAPPED_ASSET[`${chainId}`]) &&
isLowerCaseMatch(buyTokenAddress, ETH_ADDRESS);

// Aggregators represent native asset as 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE
const nativeAsset =
isLowerCaseMatch(ETH_ADDRESS_AGGREGATOR, sellTokenAddress) ||
assetToSell?.isNativeAsset;

let swapAssetNeedsUnlocking = false;

if (!isNativeAssetUnwrapping && !nativeAsset) {
if (allowanceNeeded) {
swapAssetNeedsUnlocking = await assetNeedsUnlocking({
owner: accountAddress,
amount: sellAmount,
Expand All @@ -125,14 +97,8 @@ export const createUnlockAndCrosschainSwapRap = async (
chainId,
});
}
const allowsPermit =
!nativeAsset &&
chainId === ChainId.mainnet &&
ALLOWS_PERMIT[
assetToSell.address?.toLowerCase() as keyof PermitSupportedTokenList
];

if (swapAssetNeedsUnlocking && !allowsPermit) {

if (swapAssetNeedsUnlocking) {
const unlock = createNewAction('unlock', {
fromAddress: accountAddress,
amount: sellAmount,
Expand All @@ -146,8 +112,8 @@ export const createUnlockAndCrosschainSwapRap = async (
// create a swap rap
const swap = createNewAction('crosschainSwap', {
chainId,
permit: swapAssetNeedsUnlocking && allowsPermit,
requiresApprove: swapAssetNeedsUnlocking && !allowsPermit,
permit: swapAssetNeedsUnlocking,
requiresApprove: swapAssetNeedsUnlocking,
quote,
meta: swapParameters.meta,
assetToSell,
Expand Down
60 changes: 10 additions & 50 deletions src/core/raps/unlockAndSwap.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
import {
ALLOWS_PERMIT,
ETH_ADDRESS as ETH_ADDRESS_AGGREGATOR,
PermitSupportedTokenList,
WRAPPED_ASSET,
getRainbowRouterContractAddress,
} from '@rainbow-me/swaps';
import { getRainbowRouterContractAddress } from '@rainbow-me/swaps';
import { Address } from 'viem';

import { ETH_ADDRESS } from '../references';
import { ChainId } from '../types/chains';
import { isNativeAsset } from '../utils/chains';
import { add } from '../utils/numbers';
import { isLowerCaseMatch } from '../utils/strings';
import { isUnwrapEth } from '../utils/swaps';

import {
assetNeedsUnlocking,
Expand All @@ -35,26 +24,18 @@ export const estimateUnlockAndSwap = async (
const {
from: accountAddress,
sellTokenAddress,
buyTokenAddress,
allowanceNeeded,
} = quote as {
from: Address;
sellTokenAddress: Address;
buyTokenAddress: Address;
allowanceNeeded: boolean;
};

const isNativeAssetUnwrapping =
isLowerCaseMatch(sellTokenAddress, WRAPPED_ASSET?.[chainId]) &&
(isLowerCaseMatch(buyTokenAddress, ETH_ADDRESS?.[chainId]) ||
isLowerCaseMatch(buyTokenAddress, ETH_ADDRESS_AGGREGATOR?.[chainId]));

let gasLimits: (string | number)[] = [];
let swapAssetNeedsUnlocking = false;

const nativeAsset =
isLowerCaseMatch(ETH_ADDRESS_AGGREGATOR, sellTokenAddress) ||
isNativeAsset(sellTokenAddress, chainId);

if (!isNativeAssetUnwrapping && !nativeAsset) {
if (allowanceNeeded) {
swapAssetNeedsUnlocking = await assetNeedsUnlocking({
owner: accountAddress,
amount: sellAmount,
Expand Down Expand Up @@ -110,30 +91,16 @@ export const createUnlockAndSwapRap = async (
const { sellAmount, quote, chainId, assetToSell, assetToBuy } =
swapParameters;

const {
from: accountAddress,
sellTokenAddress,
buyTokenAddress,
} = quote as {
const { from: accountAddress, allowanceNeeded } = quote as {
from: Address;
sellTokenAddress: Address;
buyTokenAddress: Address;
allowanceNeeded: boolean;
};

const isNativeAssetUnwrapping = isUnwrapEth({
buyTokenAddress,
chainId,
sellTokenAddress,
});

// Aggregators represent native asset as 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE
const nativeAsset =
isLowerCaseMatch(ETH_ADDRESS_AGGREGATOR, sellTokenAddress) ||
isNativeAsset(sellTokenAddress, chainId);

let swapAssetNeedsUnlocking = false;

if (!isNativeAssetUnwrapping && !nativeAsset) {
if (allowanceNeeded) {
swapAssetNeedsUnlocking = await assetNeedsUnlocking({
owner: accountAddress,
amount: sellAmount as string,
Expand All @@ -143,14 +110,7 @@ export const createUnlockAndSwapRap = async (
});
}

const allowsPermit =
!nativeAsset &&
chainId === ChainId.mainnet &&
ALLOWS_PERMIT[
assetToSell.address?.toLowerCase() as keyof PermitSupportedTokenList
];

if (swapAssetNeedsUnlocking && !allowsPermit) {
if (swapAssetNeedsUnlocking) {
const unlock = createNewAction('unlock', {
fromAddress: accountAddress,
amount: sellAmount,
Expand All @@ -165,8 +125,8 @@ export const createUnlockAndSwapRap = async (
const swap = createNewAction('swap', {
chainId,
sellAmount,
permit: swapAssetNeedsUnlocking && allowsPermit,
requiresApprove: swapAssetNeedsUnlocking && !allowsPermit,
permit: swapAssetNeedsUnlocking,
requiresApprove: swapAssetNeedsUnlocking,
quote,
meta: swapParameters.meta,
assetToSell,
Expand Down

0 comments on commit 5d27d77

Please sign in to comment.