Skip to content

Commit

Permalink
Merge pull request #46 from rarimo/fix/internal-tokens-fetch-testnets
Browse files Browse the repository at this point in the history
fix pulling internal tokens mapping for the testnets
  • Loading branch information
napalmpapalam authored Jun 1, 2023
2 parents ed7435c + 85d3fc2 commit 5a9c212
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 45 deletions.
11 changes: 8 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ 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).

## [1.5.0] - 2023-06-01
### Fixed
- `@rarimo/nft-checkout` - Pulling internal tokens mapping for testnets

## [1.5.0-rc.25] - 2023-05-25
### Added
- `@rarimo/provider` - Fix `RawProvider` type
### Fixed
- `@rarimo/provider` - `RawProvider` type

## [1.5.0-rc.24] - 2023-05-25
### Added
Expand Down Expand Up @@ -261,7 +265,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/1.5.0-rc.25...HEAD
[Unreleased]: https://github.com/rarimo/js-sdk/compare/1.5.0...HEAD
[1.5.0]: https://github.com/rarimo/js-sdk/compare/1.5.0-rc.25...1.5.0
[1.5.0-rc.25]: https://github.com/rarimo/js-sdk/compare/1.5.0-rc.24...1.5.0-rc.25
[1.5.0-rc.24]: https://github.com/rarimo/js-sdk/compare/1.5.0-rc.23...1.5.0-rc.24
[1.5.0-rc.23]: https://github.com/rarimo/js-sdk/compare/1.5.0-rc.22...1.5.0-rc.23
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": "1.5.0-rc.25",
"version": "1.5.0",
"description": "Tools to create cross-train transactions with the Rarimo Protocol",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/nft-checkout/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Config } from './types'

export const DEFAULT_CONFIG: Config = {
UNISWAP_V3_TOKEN_LIST_URL:
'https://ipfs.tokend.io/ipfs/ipns/tokens.uniswap.org',
'https://ipfs.testnet.rarimo.com/ipfs/ipns/tokens.uniswap.org',
TRADER_JOE_TOKEN_LIST_URL:
'https://raw.githubusercontent.com/traderjoe-xyz/joe-tokenlists/main/mc.tokenlist.json',
PANCAKE_SWAP_TOKEN_LIST_URL:
Expand Down
9 changes: 9 additions & 0 deletions packages/nft-checkout/src/const/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const EVM_CHAINS: BridgeChain[] = [
icon: 'https://raw.githubusercontent.com/rarimo/js-sdk/main/assets/logos/eth-logo.png',
contractAddress: '0x85718348D854CE2768e96D87a2ed6d12d619b67B',
contactVersion: SwapContractVersion.UniswapV3,
isTestnet: false,
},
{
id: EVM_CHAIN_IDS[ChainNames.Goerli],
Expand All @@ -49,6 +50,7 @@ export const EVM_CHAINS: BridgeChain[] = [
icon: 'https://raw.githubusercontent.com/rarimo/js-sdk/main/assets/logos/eth-logo.png',
contractAddress: '0xe3C6b16AFAB73D836f12252f376613ceF967B5e1',
contactVersion: SwapContractVersion.UniswapV3,
isTestnet: true,
},
{
id: EVM_CHAIN_IDS[ChainNames.Sepolia],
Expand All @@ -64,6 +66,7 @@ export const EVM_CHAINS: BridgeChain[] = [
icon: 'https://raw.githubusercontent.com/rarimo/js-sdk/main/assets/logos/eth-logo.png',
contractAddress: '',
contactVersion: SwapContractVersion.UniswapV3,
isTestnet: true,
},
{
id: EVM_CHAIN_IDS[ChainNames.Polygon],
Expand All @@ -79,6 +82,7 @@ export const EVM_CHAINS: BridgeChain[] = [
icon: 'https://raw.githubusercontent.com/rarimo/js-sdk/main/assets/logos/matic-logo.png',
contractAddress: '',
contactVersion: SwapContractVersion.QuickSwap,
isTestnet: false,
},
{
id: EVM_CHAIN_IDS[ChainNames.Mumbai],
Expand All @@ -94,6 +98,7 @@ export const EVM_CHAINS: BridgeChain[] = [
contractAddress: '0x24Ae0B9DC81d2E4e9383e283163913BF200a579F',
icon: 'https://raw.githubusercontent.com/rarimo/js-sdk/main/assets/logos/matic-logo.png',
contactVersion: SwapContractVersion.QuickSwap,
isTestnet: true,
},
{
id: EVM_CHAIN_IDS[ChainNames.Avalanche],
Expand All @@ -109,6 +114,7 @@ export const EVM_CHAINS: BridgeChain[] = [
contractAddress: '0xaebaBd54CdD1418b0eb88A5e877EdD6cbC5804f4',
icon: 'https://raw.githubusercontent.com/rarimo/js-sdk/main/assets/logos/avax-logo.png',
contactVersion: SwapContractVersion.TraderJoe,
isTestnet: false,
},
{
id: EVM_CHAIN_IDS[ChainNames.Fuji],
Expand All @@ -124,6 +130,7 @@ export const EVM_CHAINS: BridgeChain[] = [
icon: 'https://raw.githubusercontent.com/rarimo/js-sdk/main/assets/logos/avax-logo.png',
contractAddress: '0x13734D554d25EA67DFD45653659e447996a1C9B4',
contactVersion: SwapContractVersion.TraderJoe,
isTestnet: true,
},
{
id: EVM_CHAIN_IDS[ChainNames.BinanceSmartChain],
Expand All @@ -139,6 +146,7 @@ export const EVM_CHAINS: BridgeChain[] = [
icon: 'https://raw.githubusercontent.com/rarimo/js-sdk/main/assets/logos/bnb-logo.png',
contractAddress: '0x85718348D854CE2768e96D87a2ed6d12d619b67B',
contactVersion: SwapContractVersion.PancakeSwap,
isTestnet: false,
},
{
id: EVM_CHAIN_IDS[ChainNames.Chapel],
Expand All @@ -154,6 +162,7 @@ export const EVM_CHAINS: BridgeChain[] = [
icon: 'https://raw.githubusercontent.com/rarimo/js-sdk/main/assets/logos/bnb-logo.png',
contractAddress: '0x08a87595f4423AaF591155aa2cEF31Fb904BcdE8',
contactVersion: SwapContractVersion.PancakeSwap,
isTestnet: true,
},
]

Expand Down
9 changes: 0 additions & 9 deletions packages/nft-checkout/src/errors/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,3 @@ export class OperatorWrappedTokenNotFound extends RuntimeError {
super(message)
}
}

export class OperationSwapIntoNativeNotSupported extends RuntimeError {
public name = 'OperationSwapIntoNativeNotSupported'
constructor(
message = 'NFT Checkout now not supports swapping into native tokens',
) {
super(message)
}
}
73 changes: 48 additions & 25 deletions packages/nft-checkout/src/operations/evm/evm-operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import {
NATIVE_TOKEN_WRAP_SLIPPAGE_MULTIPLIER,
SWAP_CONTRACT_ABIS,
} from '@/const'
import type { PaymentToken, Price, Token } from '@/entities'
import { OperationEventBusEvents } from '@/enums'
import { PaymentToken, Price, Token } from '@/entities'
import { ChainNames, OperationEventBusEvents } from '@/enums'
import { errors } from '@/errors'
import { toLow } from '@/helpers'
import type {
Expand All @@ -34,6 +34,7 @@ import type {
INFTCheckoutOperation,
OperationCreateParams,
Target,
TokenSymbol,
TxBundle,
} from '@/types'
import { CheckoutOperationStatus, DestinationTransactionStatus } from '@/types'
Expand All @@ -51,7 +52,7 @@ export { TARGET_TOKEN_SYMBOLS } from './helpers/chain'
// We always use liquidity pool and not control those token contracts
// In case when `isWrapped: false`, bridge contract won't try to burn tokens
const IS_TOKEN_WRAPPED = false

const NATIVE_TOKEN_ADDRESS = '0x0000000000000000000000000000000000000000'
const DESTINATION_TX_PULL_INTERVAL = 2000

type InternalChain = JsonApiRecordBase<'chain'> & {
Expand Down Expand Up @@ -144,10 +145,6 @@ export class EVMOperation
throw new errors.OperationInvalidChainPairError()
}

if (toLow(target.swapTargetTokenSymbol) === toLow(from.token.symbol)) {
throw new errors.OperationSwapIntoNativeNotSupported()
}

this.#chainFrom = from
this.#target = target

Expand Down Expand Up @@ -218,34 +215,22 @@ export class EVMOperation
async #getPaymentTokensWithPairs(
result: PaymentToken[],
): Promise<PaymentToken[]> {
const internalToken = await this.getInternalTokenMapping(
this.#target?.swapTargetTokenSymbol ?? '',
)
this.#targetToken = await this.#getTargetToken()

if (!internalToken) return []
if (!this.#targetToken) return []

const chain = internalToken?.chains.find(
i => toLow(i.id) === toLow(this.#chainFrom?.name),
const tokens = result.filter(
i => toLow(i.symbol) !== toLow(this.#targetToken?.symbol),
)

if (!chain) return []

const targetToken = this.#tokens.find(
i => toLow(i.address) === toLow(chain.token_address),
)

if (!targetToken) return []

this.#targetToken = targetToken

const estimatedPrices = await Promise.allSettled(
result.map(i =>
tokens.map(i =>
new Estimator(
this.#provider,
this.#tokens,
i,
this.#target!,
targetToken,
this.#targetToken!,
).estimate(),
),
)
Expand All @@ -262,6 +247,31 @@ export class EVMOperation
}, [])
}

async #getTargetToken(): Promise<Token | undefined> {
const params = this.#target!

const targetTokenSymbol = getTargetTokenSymbol(
this.#getChainByID(params.chainId)!,
params.price.symbol,
)

if (!targetTokenSymbol) return

const internalToken = await this.getInternalTokenMapping(targetTokenSymbol)

if (!internalToken) return

const chain = internalToken?.chains.find(
i => toLow(i.id) === toLow(this.#chainFrom?.name),
)

if (!chain) return

return chain.token_address === NATIVE_TOKEN_ADDRESS
? Token.fromChain(this.#chainFrom!)
: this.#tokens.find(i => toLow(i.address) === toLow(chain.token_address))
}

async getInternalTokenMapping(
targetTokenSymbol: string,
): Promise<InternalToken | undefined> {
Expand Down Expand Up @@ -529,3 +539,16 @@ export class EVMOperation
})
}
}

const getTargetTokenSymbol = (chain: BridgeChain, symbol: TokenSymbol) => {
if (!chain.isTestnet) return symbol

return (
{
[ChainNames.Goerli]: '2',
[ChainNames.Sepolia]: '3',
[ChainNames.Fuji]: '4',
[ChainNames.Chapel]: '5',
}[chain.name] ?? ''
)
}
1 change: 1 addition & 0 deletions packages/nft-checkout/src/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type TokenSymbol = string // Token symbol, e.g. ETH, USDT, etc
export type BridgeChain = Chain & {
contractAddress: Address
contactVersion: SwapContractVersion
isTestnet: boolean
}

export { Chain }
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": "1.5.0-rc.25",
"version": "1.5.0",
"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": "1.5.0-rc.25",
"version": "1.5.0",
"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": "1.5.0-rc.25",
"version": "1.5.0",
"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": "1.5.0-rc.25",
"version": "1.5.0",
"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": "1.5.0-rc.25",
"version": "1.5.0",
"description": "React components that you can use in your UI to create cross-chain transactions with the Rarimo Protocol",
"license": "MIT",
"sideEffects": false,
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": "1.5.0-rc.25",
"version": "1.5.0",
"description": "Tools to connect to wallets in React applications through the Rarimo SDK.",
"repository": {
"type": "git",
Expand Down

0 comments on commit 5a9c212

Please sign in to comment.