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

add support of the uniswap v2 routes #75

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/auth-zkp-iden3/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/bridge/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/identity-gen-iden3/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/nft-checkout/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 1 addition & 3 deletions packages/nft-checkout/src/operations/evm/evm-operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const getEstimation = async ({

const {
path,
protocol,
impact,
amountIn: _amountIn,
amountOut: _amountOut,
Expand All @@ -72,6 +73,7 @@ export const getEstimation = async ({
from,
to,
path,
protocol,
impact,
gasPrice,
gasPriceInUSD,
Expand Down
7 changes: 6 additions & 1 deletion packages/nft-checkout/src/types/estimate.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -35,6 +39,7 @@ export type EstimateQueryParams = {

export type EstimateResponse = SwapEstimationInfo & {
path: string[]
protocol: EVMDexType
amountIn: ClassInstanceToPlainObject<Amount>
amountOut: ClassInstanceToPlainObject<Amount>
}
2 changes: 1 addition & 1 deletion packages/provider/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/providers-evm/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/providers-near/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/providers-solana/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-nft-checkout/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-provider/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/shared-zkp-iden3/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/enums/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export enum EVMDexType {
QuickSwap = 'QuickSwap',
PancakeSwap = 'PancakeSwap',
UniswapV3 = 'UniswapV3',
UniswapV2 = 'UniswapV2',
}

export enum ChainTypes {
Expand Down
2 changes: 1 addition & 1 deletion packages/swap/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BN } from '@distributedlab/tools'
import {
type BridgeChain,
ERC20_ABI,
EVMDexType,
type TransactionBundle,
} from '@rarimo/shared'
import { utils } from 'ethers'
Expand All @@ -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, [
Expand Down
18 changes: 12 additions & 6 deletions packages/swap/src/swappers/evm/execute-data/swap.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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,
Expand All @@ -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 = [
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions packages/swap/src/types/swapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
HexString,
TransactionBundle,
} from '@rarimo/shared'
import { EVMDexType } from '@rarimo/shared'

export type SwapperCreateFn = (p: IProvider) => Swapper

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/zkp-gen-iden3/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down