From e33efd85430fb4b1af6c2805bfe5afeadbb88673 Mon Sep 17 00:00:00 2001 From: Semen Loktionov Date: Tue, 5 Sep 2023 17:28:43 +0300 Subject: [PATCH] add support of the uniswap v2 routes --- CHANGELOG.md | 9 +++++++-- packages/auth-zkp-iden3/package.json | 2 +- packages/bridge/package.json | 2 +- packages/client/package.json | 2 +- packages/identity-gen-iden3/package.json | 2 +- packages/nft-checkout/package.json | 2 +- .../src/operations/evm/evm-operation.ts | 4 +--- .../operations/evm/helpers/get-estimation.ts | 2 ++ packages/nft-checkout/src/types/estimate.ts | 7 ++++++- packages/provider/package.json | 2 +- packages/providers-evm/package.json | 2 +- packages/providers-near/package.json | 2 +- packages/providers-solana/package.json | 2 +- packages/react-nft-checkout/package.json | 2 +- packages/react-provider/package.json | 2 +- packages/shared-zkp-iden3/package.json | 2 +- packages/shared/package.json | 2 +- packages/shared/src/enums/chain.ts | 1 + packages/swap/package.json | 2 +- .../evm/execute-data/intermediate-bundle.ts | 6 ++++-- .../swap/src/swappers/evm/execute-data/swap.ts | 18 ++++++++++++------ packages/swap/src/types/swapper.ts | 2 ++ packages/zkp-gen-iden3/package.json | 2 +- 23 files changed, 50 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3adecdff..605f3176a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,11 +5,15 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.0.0-rc.23] - 2023-09-05 +### Added +- `@rarimo/swap` - Support for the Goerli and Ethereum Uniswap V2 swap routes + ## [2.0.0-rc.22] - 2023-09-04 ### Changed - `@rarimo/react-provider` - Updated vite react plugin version - `@rarimo/react-nft-checkout` - Updated vite react plugin version -- `@rarimo/client` - Harcoded fee amount in the broadcaster for transaction to estimating gas +- `@rarimo/client` - Hardcoded fee amount in the broadcaster for transaction to estimating gas ### Removed - `@rarimo/client` - Hardcoded fee amount from config @@ -458,7 +462,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Under the hood changes - Initiated repo with `@rarimo/provider` and `@rarimo/nft-checkout` packages -[Unreleased]: https://github.com/rarimo/js-sdk/compare/2.0.0-rc.22...HEAD +[Unreleased]: https://github.com/rarimo/js-sdk/compare/2.0.0-rc.23...HEAD +[2.0.0-rc.23]: https://github.com/rarimo/js-sdk/compare/2.0.0-rc.22...2.0.0-rc.23 [2.0.0-rc.22]: https://github.com/rarimo/js-sdk/compare/2.0.0-rc.21...2.0.0-rc.22 [2.0.0-rc.21]: https://github.com/rarimo/js-sdk/compare/2.0.0-rc.20...2.0.0-rc.21 [2.0.0-rc.20]: https://github.com/rarimo/js-sdk/compare/2.0.0-rc.19...2.0.0-rc.20 diff --git a/packages/auth-zkp-iden3/package.json b/packages/auth-zkp-iden3/package.json index e95502ec5..61ca90da7 100644 --- a/packages/auth-zkp-iden3/package.json +++ b/packages/auth-zkp-iden3/package.json @@ -1,6 +1,6 @@ { "name": "@rarimo/auth-zkp-iden3", - "version": "2.0.0-rc.22", + "version": "2.0.0-rc.23", "description": "Tools that authenticate a zero-knowledge proof and provide verifiable credentials from it.", "repository": { "type": "git", diff --git a/packages/bridge/package.json b/packages/bridge/package.json index 3af6314e0..e39e02b27 100644 --- a/packages/bridge/package.json +++ b/packages/bridge/package.json @@ -1,6 +1,6 @@ { "name": "@rarimo/bridge", - "version": "2.0.0-rc.22", + "version": "2.0.0-rc.23", "description": "Internal tools that other Rarimo packages use to bridge tokens.", "repository": { "type": "git", diff --git a/packages/client/package.json b/packages/client/package.json index 243522d25..0c29a4a45 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -1,6 +1,6 @@ { "name": "@rarimo/client", - "version": "2.0.0-rc.22", + "version": "2.0.0-rc.23", "description": "The Rarimo chain client", "repository": { "type": "git", diff --git a/packages/identity-gen-iden3/package.json b/packages/identity-gen-iden3/package.json index 54f750739..50a66a60f 100644 --- a/packages/identity-gen-iden3/package.json +++ b/packages/identity-gen-iden3/package.json @@ -1,6 +1,6 @@ { "name": "@rarimo/identity-gen-iden3", - "version": "2.0.0-rc.22", + "version": "2.0.0-rc.23", "description": "Tools that generate distributed identities for the Iden3 protocol.", "repository": { "type": "git", diff --git a/packages/nft-checkout/package.json b/packages/nft-checkout/package.json index 020cf19ac..e6d321f4e 100644 --- a/packages/nft-checkout/package.json +++ b/packages/nft-checkout/package.json @@ -1,6 +1,6 @@ { "name": "@rarimo/nft-checkout", - "version": "2.0.0-rc.22", + "version": "2.0.0-rc.23", "description": "Features of the Rarimo SDK that create cross-chain transactions based on the Rarimo protocol.", "repository": { "type": "git", diff --git a/packages/nft-checkout/src/operations/evm/evm-operation.ts b/packages/nft-checkout/src/operations/evm/evm-operation.ts index c148986b2..952e3ee8d 100644 --- a/packages/nft-checkout/src/operations/evm/evm-operation.ts +++ b/packages/nft-checkout/src/operations/evm/evm-operation.ts @@ -167,9 +167,7 @@ export const EVMOperation = (provider: IProvider): CheckoutOperation => { ? estimations.some(i => !i.from.chain.contractAddress) : !estimations[0].from.chain.contractAddress - if (isChainNotSupported) { - throw new errors.OperationChainNotSupportedError() - } + if (isChainNotSupported) throw new errors.OperationChainNotSupportedError() _setStatus(CheckoutOperationStatus.CheckoutStarted) diff --git a/packages/nft-checkout/src/operations/evm/helpers/get-estimation.ts b/packages/nft-checkout/src/operations/evm/helpers/get-estimation.ts index ac40e26a1..d9c62ca65 100644 --- a/packages/nft-checkout/src/operations/evm/helpers/get-estimation.ts +++ b/packages/nft-checkout/src/operations/evm/helpers/get-estimation.ts @@ -61,6 +61,7 @@ export const getEstimation = async ({ const { path, + protocol, impact, amountIn: _amountIn, amountOut: _amountOut, @@ -72,6 +73,7 @@ export const getEstimation = async ({ from, to, path, + protocol, impact, gasPrice, gasPriceInUSD, diff --git a/packages/nft-checkout/src/types/estimate.ts b/packages/nft-checkout/src/types/estimate.ts index 0fc67fbb1..bf5ce71d3 100644 --- a/packages/nft-checkout/src/types/estimate.ts +++ b/packages/nft-checkout/src/types/estimate.ts @@ -1,5 +1,9 @@ import type { Token } from '@rarimo/bridge' -import type { Amount, ClassInstanceToPlainObject } from '@rarimo/shared' +import type { + Amount, + ClassInstanceToPlainObject, + EVMDexType, +} from '@rarimo/shared' import type { SwapOpts } from '@rarimo/swap' export type SwapEstimation = SwapOpts & SwapEstimationInfo @@ -35,6 +39,7 @@ export type EstimateQueryParams = { export type EstimateResponse = SwapEstimationInfo & { path: string[] + protocol: EVMDexType amountIn: ClassInstanceToPlainObject amountOut: ClassInstanceToPlainObject } diff --git a/packages/provider/package.json b/packages/provider/package.json index 86c2174eb..5cb299067 100644 --- a/packages/provider/package.json +++ b/packages/provider/package.json @@ -1,6 +1,6 @@ { "name": "@rarimo/provider", - "version": "2.0.0-rc.22", + "version": "2.0.0-rc.23", "description": "A common interface for access to wallets (EVM and non-EVM) in the Rarimo SDK, used by packages that provide access to wallets on specific chains such as @rarimo/providers-evm, @rarimo/providers-solana, and @rarimo/providers-near.", "repository": { "type": "git", diff --git a/packages/providers-evm/package.json b/packages/providers-evm/package.json index 43d8c918f..d94e70c3e 100644 --- a/packages/providers-evm/package.json +++ b/packages/providers-evm/package.json @@ -1,6 +1,6 @@ { "name": "@rarimo/providers-evm", - "version": "2.0.0-rc.22", + "version": "2.0.0-rc.23", "description": "Features of the Rarimo SDK that provide access to wallets and the ability to interact with them on EVM-compatible blockchains.", "repository": { "type": "git", diff --git a/packages/providers-near/package.json b/packages/providers-near/package.json index da895932f..b765eec59 100644 --- a/packages/providers-near/package.json +++ b/packages/providers-near/package.json @@ -1,6 +1,6 @@ { "name": "@rarimo/providers-near", - "version": "2.0.0-rc.22", + "version": "2.0.0-rc.23", "description": "Features of the Rarimo SDK that provide access to wallets and the ability to interact with them on the NEAR blockchain.", "repository": { "type": "git", diff --git a/packages/providers-solana/package.json b/packages/providers-solana/package.json index 4cf7c8de5..eca8733c8 100644 --- a/packages/providers-solana/package.json +++ b/packages/providers-solana/package.json @@ -1,6 +1,6 @@ { "name": "@rarimo/providers-solana", - "version": "2.0.0-rc.22", + "version": "2.0.0-rc.23", "description": "Features of the Rarimo SDK that provide access to wallets and the ability to interact with them on the Solana blockchain.", "repository": { "type": "git", diff --git a/packages/react-nft-checkout/package.json b/packages/react-nft-checkout/package.json index fcd4ece21..3b84078cc 100644 --- a/packages/react-nft-checkout/package.json +++ b/packages/react-nft-checkout/package.json @@ -1,6 +1,6 @@ { "name": "@rarimo/react-nft-checkout", - "version": "2.0.0-rc.22", + "version": "2.0.0-rc.23", "description": "React components that you can use in your UI to create cross-chain transactions with the Rarimo protocol.", "license": "MIT", "repository": { diff --git a/packages/react-provider/package.json b/packages/react-provider/package.json index cc77ecd84..ec3bd7675 100644 --- a/packages/react-provider/package.json +++ b/packages/react-provider/package.json @@ -1,6 +1,6 @@ { "name": "@rarimo/react-provider", - "version": "2.0.0-rc.22", + "version": "2.0.0-rc.23", "description": "Tools to connect to wallets in React applications through the Rarimo SDK.", "repository": { "type": "git", diff --git a/packages/shared-zkp-iden3/package.json b/packages/shared-zkp-iden3/package.json index 93bea4aaf..d91564f50 100644 --- a/packages/shared-zkp-iden3/package.json +++ b/packages/shared-zkp-iden3/package.json @@ -1,6 +1,6 @@ { "name": "@rarimo/shared-zkp-iden3", - "version": "2.0.0-rc.22", + "version": "2.0.0-rc.23", "description": "Internal tools that other Rarimo packages use for identity functionality.", "repository": { "type": "git", diff --git a/packages/shared/package.json b/packages/shared/package.json index cd7d62d85..16ed0cc5a 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -1,6 +1,6 @@ { "name": "@rarimo/shared", - "version": "2.0.0-rc.22", + "version": "2.0.0-rc.23", "description": "Utility functions, types and constants shared across Rarimo packages.", "repository": { "type": "git", diff --git a/packages/shared/src/enums/chain.ts b/packages/shared/src/enums/chain.ts index 916f8fcd9..0a0aea84c 100644 --- a/packages/shared/src/enums/chain.ts +++ b/packages/shared/src/enums/chain.ts @@ -20,6 +20,7 @@ export enum EVMDexType { QuickSwap = 'QuickSwap', PancakeSwap = 'PancakeSwap', UniswapV3 = 'UniswapV3', + UniswapV2 = 'UniswapV2', } export enum ChainTypes { diff --git a/packages/swap/package.json b/packages/swap/package.json index e6a492a3a..7f10565d0 100644 --- a/packages/swap/package.json +++ b/packages/swap/package.json @@ -1,6 +1,6 @@ { "name": "@rarimo/swap", - "version": "2.0.0-rc.22", + "version": "2.0.0-rc.23", "description": "Internal tools that other Rarimo packages use to swap tokens.", "repository": { "type": "git", diff --git a/packages/swap/src/swappers/evm/execute-data/intermediate-bundle.ts b/packages/swap/src/swappers/evm/execute-data/intermediate-bundle.ts index 3e05be5f9..2b8b40e2b 100644 --- a/packages/swap/src/swappers/evm/execute-data/intermediate-bundle.ts +++ b/packages/swap/src/swappers/evm/execute-data/intermediate-bundle.ts @@ -2,6 +2,7 @@ import { BN } from '@distributedlab/tools' import { type BridgeChain, ERC20_ABI, + EVMDexType, type TransactionBundle, } from '@rarimo/shared' import { utils } from 'ethers' @@ -22,17 +23,18 @@ export const buildIntermediateBundleData = ( receiver: string, bundle?: TransactionBundle, ): string => { - const { from, to, amountIn, amountOut, path } = opts + const { from, to, amountIn, amountOut, path, protocol } = opts const swapDiamondContractAddress = chainTo.contractAddress const intermediateTokenAddress = from.address + const isV2Route = protocol === EVMDexType.UniswapV2 const swapDiamondFunctionData = encodeCommandPayload([ buildPayload(SwapCommands.TransferFromErc20, [ intermediateTokenAddress, amountIn.value, ]), - ...buildSwapData(from, to, amountIn, amountOut, path!), + ...buildSwapData(from, to, amountIn, amountOut, path!, isV2Route), ...(to.isNative && to.isUniswapV3 ? [ buildPayload(SwapCommands.UnwrapNative, [ diff --git a/packages/swap/src/swappers/evm/execute-data/swap.ts b/packages/swap/src/swappers/evm/execute-data/swap.ts index d4b34cb53..34adf6b64 100644 --- a/packages/swap/src/swappers/evm/execute-data/swap.ts +++ b/packages/swap/src/swappers/evm/execute-data/swap.ts @@ -1,5 +1,5 @@ import type { Token } from '@rarimo/bridge' -import { Amount, type TransactionBundle } from '@rarimo/shared' +import { Amount, EVMDexType, type TransactionBundle } from '@rarimo/shared' import { CALLER_ADDRESS, CONTRACT_BALANCE, THIS_ADDRESS } from '@/const' import { SwapCommands } from '@/enums' @@ -59,10 +59,14 @@ export const getSwapData = ( throw new TypeError('path, amountOut args are required for swap') } - data.push(...buildSwapData(from, to, amountIn, amountOut, args.path)) + const isV2Route = args.protocol === EVMDexType.UniswapV2 + + data.push( + ...buildSwapData(from, to, amountIn, amountOut, args.path, isV2Route), + ) // If to.isNative swap output token, we need to unwrap it for UniswapV3 - if (to.isNative && to.isUniswapV3) { + if (to.isNative && to.isUniswapV3 && !isV2Route) { data.push( buildPayload(SwapCommands.UnwrapNative, [ THIS_ADDRESS, @@ -81,7 +85,9 @@ export const buildSwapData = ( amountIn: Amount, amountOut: Amount, path: string[], + isV2Route: boolean, ): CommandPayload[] => { + const isV2 = from.isUniswapV2 || isV2Route const data = [] const swapValues = [ @@ -91,10 +97,10 @@ export const buildSwapData = ( // path type is different for UniswapV2 and UniswapV3 routers, // for the V3 router it is `address[]`, for the V2 router it is `bytes` // (concatenated addresses) - from.isUniswapV2 ? path : path[0], + isV2 ? path : path[0], ] - if (from.isUniswapV2) { + if (isV2) { let command = from.isTraderJoe ? SwapCommands.SwapTokensForExactTokensTj : SwapCommands.SwapTokensForExactTokensV2 @@ -113,7 +119,7 @@ export const buildSwapData = ( data.push(buildPayload(command, swapValues)) } - if (from.isUniswapV3) { + if (from.isUniswapV3 && !isV2Route) { const command = SwapCommands.ExactOutput // from.isNative determines for the UniswapV3 router, does input token be // native or not diff --git a/packages/swap/src/types/swapper.ts b/packages/swap/src/types/swapper.ts index b07fd44ca..89ac32ba9 100644 --- a/packages/swap/src/types/swapper.ts +++ b/packages/swap/src/types/swapper.ts @@ -10,6 +10,7 @@ import type { HexString, TransactionBundle, } from '@rarimo/shared' +import { EVMDexType } from '@rarimo/shared' export type SwapperCreateFn = (p: IProvider) => Swapper @@ -97,6 +98,7 @@ export type SwapOpts = { amountOut: Amount // DEX swap a token path, required if swap is required path?: string[] + protocol: EVMDexType } export type IntermediateTokenOpts = SwapOpts diff --git a/packages/zkp-gen-iden3/package.json b/packages/zkp-gen-iden3/package.json index f0babbddd..286802a52 100644 --- a/packages/zkp-gen-iden3/package.json +++ b/packages/zkp-gen-iden3/package.json @@ -1,6 +1,6 @@ { "name": "@rarimo/zkp-gen-iden3", - "version": "2.0.0-rc.22", + "version": "2.0.0-rc.23", "description": "Tools that generate zero-knowledge proofs for the Iden3 protocol.", "repository": { "type": "git",