Skip to content

Commit

Permalink
Merge pull request #9 from rarimo/feature/bsc
Browse files Browse the repository at this point in the history
Added slippage param, support bsc pancake swap, refactor, fixed swap for input native token
  • Loading branch information
napalmpapalam authored Feb 28, 2023
2 parents fe2be7b + b08b7a8 commit f1fb3ee
Show file tree
Hide file tree
Showing 51 changed files with 1,264 additions and 383 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `@rarimo/provider`: Support of Solfare wallet
- `@rarimo/provider`: Provider now implements `ProviderSubscriber` interface, which allows to subscribe to provider change state events
- `@rarimo/nft-checkout`: NFT checkout operation now implements `OperationSubscriber` interface, which allows to subscribe to operation change state events
- `@rarimo/nft-checkout`: `supportedTokens` method to `INFTCheckoutOperation` interface to get supported tokens for swap on the selected chain
- `@rarimo/nft-checkout`: PancakeSwap integration
- `@rarimo/nft-checkout`: Ability to provide slippage in target object

### Changed
- `root`: Added some more info about SDK to the readme
- `all`: Moved all changelogs to the one file in the root of the repo
- `all`: Operator errors moved into from `@rarimo/provider` to `@rarimo/nft-checkout`
- `@rarimo/nft-checkout`: Target object now has the required field `swapTargetTokenSymbol`, which need to determine output token for swap.
- `@rarimo/nft-checkout`: Price type refactored to class, which provides developer friendly interface to create price instance

### Removed
- `root`: `esbuild` from dependencies
- `root`: `@babel/*` from dependencies to simplify and speed up build process
- `all`: `@/ -> ./src` aliases to remove `@babel/*` and all related packages
- `@rarimo/nft-checkout`: Swap contract addresses removed from EVM mainnet chains till they will be deployed

### Fixed
- `@rarimo/nft-checkout`: Swap for input native token

## 1.1.0 - 2023-02-20
### Added
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"workspaces": [
"packages/*"
],
"gitHooks": {
"pre-commit": "yarn lint"
},
"scripts": {
"build": "yarn workspaces foreach --parallel --topological-dev --verbose run build",
"docs": "typedoc --options typedoc.json",
Expand All @@ -37,7 +40,8 @@
"prettier": "^2.7.1",
"ts-jest": "^29.0.3",
"typedoc": "^0.23.8",
"typescript": "^4.9.5"
"typescript": "^4.9.5",
"yorkie": "^2.0.0"
},
"dependenciesMeta": {
"[email protected]": {
Expand Down
4 changes: 4 additions & 0 deletions packages/nft-checkout/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
"@distributedlab/utils": "^3.2.1",
"@ethersproject/abstract-provider": "^5.7.0",
"@ethersproject/properties": "^5.7.0",
"@ethersproject/providers": "^5.7.2",
"@pancakeswap/sdk": "^3.1.4",
"@pancakeswap/smart-router": "^0.6.1",
"@pancakeswap/swap-sdk-core": "^0.0.1",
"@rarimo/provider": "workspace:*",
"@traderjoe-xyz/sdk": "2.0.7",
"@uniswap/router-sdk": "^1.4.0",
Expand Down
8 changes: 5 additions & 3 deletions packages/nft-checkout/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Config } from './types'

export const DEFAULT_CONFIG: Config = {
V3_TOKEN_LIST: 'https://ipfs.io/ipns/tokens.uniswap.org',
V2_TOKEN_LIST:
'https://raw.githubusercontent.com/traderjoe-xyz/joe-tokenlists/main/src/joe.tokenlist-v2.json',
UNISWAP_V3_TOKEN_LIST_URL: 'https://ipfs.io/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:
'https://tokens.pancakeswap.finance/pancakeswap-extended.json',
}
10 changes: 10 additions & 0 deletions packages/nft-checkout/src/const/abi/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import { SwapContractVersion } from '../../enums'
import { SWAP_V2_ABI } from './swap-v2'
import { SWAP_V3_ABI } from './swap-v3'

export * from './erc-20'
export * from './swap-v2'
export * from './swap-v3'

export const SWAP_CONTRACT_ABIS = {
[SwapContractVersion.TraderJoe]: SWAP_V2_ABI,
[SwapContractVersion.PancakeSwap]: SWAP_V2_ABI,
[SwapContractVersion.UniswapV3]: SWAP_V3_ABI,
}
2 changes: 1 addition & 1 deletion packages/nft-checkout/src/const/abi/swap-v2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const SWAP_V2 = [
export const SWAP_V2_ABI = [
{
inputs: [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/nft-checkout/src/const/abi/swap-v3.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const SWAP_V3 = [
export const SWAP_V3_ABI = [
{
inputs: [
{
Expand Down
1 change: 1 addition & 0 deletions packages/nft-checkout/src/const/bundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const BUNDLE_SALT_BYTES = 32
150 changes: 107 additions & 43 deletions packages/nft-checkout/src/const/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,86 +10,150 @@ const EVM_CHAIN_IDS = {
[ChainNames.Goerli]: 5,
[ChainNames.Sepolia]: 11155111,
[ChainNames.Mumbai]: 80001,
[ChainNames.BinanceSmartChain]: 56,
[ChainNames.Chapel]: 97,
}

const EVM_CHAINS: BridgeChain[] = [
const nativeToken = {
decimals: 18,
}

export const EVM_CHAINS: BridgeChain[] = [
{
id: EVM_CHAIN_IDS[ChainNames.Ethereum],
name: ChainNames.Ethereum,
rpcUrl: 'https://mainnet.infura.io/v3/',
symbol: 'ETH',
token: {
symbol: 'ETH',
name: 'Ethereum',
...nativeToken,
},
explorerUrl: 'https://etherscan.io',
type: ChainTypes.EVM,
icon: 'https://chainlist.org/_next/image?url=https%3A%2F%2Fdefillama.com%2Fchain-icons%2Frsz_ethereum.jpg&w=32&q=75',
contractAddress: '0xe3C6b16AFAB73D836f12252f376613ceF967B5e1',
contactVersion: SwapContractVersion.UniswapV3,
},
{
id: EVM_CHAIN_IDS[ChainNames.Polygon],
name: ChainNames.Polygon,
rpcUrl: 'https://polygon-mainnet.infura.io/v3/',
symbol: 'MATIC',
explorerUrl: 'https://polygonscan.com',
type: ChainTypes.EVM,
icon: 'https://chainlist.org/_next/image?url=https%3A%2F%2Fdefillama.com%2Fchain-icons%2Frsz_polygon.jpg&w=32&q=75',
contractAddress: '0xe3C6b16AFAB73D836f12252f376613ceF967B5e1',
icon: 'https://raw.githubusercontent.com/MetaMask/metamask-extension/develop/app/images/eth_logo.svg',
contractAddress: '',
contactVersion: SwapContractVersion.UniswapV3,
},
{
id: EVM_CHAIN_IDS[ChainNames.Avalanche],
name: ChainNames.Avalanche,
rpcUrl: 'https://avalanche-mainnet.infura.io/v3/',
symbol: 'AVAX',
explorerUrl: 'https://cchain.explorer.avax.network',
type: ChainTypes.EVM,
icon: 'https://chainlist.org/_next/image?url=https%3A%2F%2Fdefillama.com%2Fchain-icons%2Frsz_avalanche.jpg&w=64&q=75',
contractAddress: '0xA009003BA2c6Af4C77cD1E6c3512723EC2Bdb5eE',
contactVersion: SwapContractVersion.AvalancheV2,
},
{
id: EVM_CHAIN_IDS[ChainNames.Goerli],
name: 'Görli',
rpcUrl: 'https://goerli.infura.io/v3/',
symbol: 'ETH',
token: {
symbol: 'ETH',
name: 'Goerli Ethereum',
...nativeToken,
},
explorerUrl: 'https://goerli.etherscan.io',
type: ChainTypes.EVM,
icon: 'https://chainlist.org/_next/image?url=https%3A%2F%2Fdefillama.com%2Fchain-icons%2Frsz_ethereum.jpg&w=32&q=75',
icon: 'https://raw.githubusercontent.com/MetaMask/metamask-extension/develop/app/images/eth_logo.svg',
contractAddress: '0xe3C6b16AFAB73D836f12252f376613ceF967B5e1',
contactVersion: SwapContractVersion.UniswapV3,
},
{
id: EVM_CHAIN_IDS[ChainNames.Sepolia],
name: 'Sepolia',
rpcUrl: 'https://sepolia.infura.io/v3/',
symbol: 'ETH',
token: {
symbol: 'ETH',
name: 'Sepolia Ethereum',
...nativeToken,
},
explorerUrl: 'https://sepolia.etherscan.io',
type: ChainTypes.EVM,
icon: 'https://chainlist.org/_next/image?url=https%3A%2F%2Fdefillama.com%2Fchain-icons%2Frsz_ethereum.jpg&w=32&q=75',
contractAddress: '0xe3C6b16AFAB73D836f12252f376613ceF967B5e1',
icon: 'https://raw.githubusercontent.com/MetaMask/metamask-extension/develop/app/images/eth_logo.svg',
contractAddress: '',
contactVersion: SwapContractVersion.UniswapV3,
},
{
id: EVM_CHAIN_IDS[ChainNames.Fuji],
name: ChainNames.Fuji,
rpcUrl: 'https://avalanche-fuji.infura.io/v3/',
symbol: 'AVAX',
explorerUrl: 'https://cchain.explorer.avax-test.network',
id: EVM_CHAIN_IDS[ChainNames.Polygon],
name: ChainNames.Polygon,
rpcUrl: 'https://polygon-rpc.com/',
token: {
symbol: 'MATIC',
name: 'Polygon Matic',
...nativeToken,
},
explorerUrl: 'https://polygonscan.com',
type: ChainTypes.EVM,
icon: 'https://chainlist.org/_next/image?url=https%3A%2F%2Fdefillama.com%2Fchain-icons%2Frsz_avalanche.jpg&w=64&q=75',
contractAddress: '0xA009003BA2c6Af4C77cD1E6c3512723EC2Bdb5eE',
contactVersion: SwapContractVersion.AvalancheV2,
icon: 'https://raw.githubusercontent.com/MetaMask/metamask-extension/develop/app/images/matic-token.png',
contractAddress: '',
contactVersion: SwapContractVersion.UniswapV3,
},
{
id: EVM_CHAIN_IDS[ChainNames.Mumbai],
name: ChainNames.Mumbai,
rpcUrl: 'https://polygon-mumbai.infura.io/v3/',
symbol: 'MATIC',
rpcUrl: 'https://rpc-mumbai.maticvigil.com/',
token: {
symbol: 'MATIC',
name: 'Polygon Mumbai Matic',
...nativeToken,
},
explorerUrl: 'https://mumbai.polygonscan.com',
type: ChainTypes.EVM,
icon: 'https://chainlist.org/_next/image?url=%2Funknown-logo.png&w=64&q=75',
contractAddress: '0xe3C6b16AFAB73D836f12252f376613ceF967B5e1',
contractAddress: '',
icon: 'https://raw.githubusercontent.com/MetaMask/metamask-extension/develop/app/images/matic-token.png',
contactVersion: SwapContractVersion.UniswapV3,
},
{
id: EVM_CHAIN_IDS[ChainNames.Avalanche],
name: ChainNames.Avalanche,
rpcUrl: 'https://api.avax.network/ext/bc/C/rpc',
token: {
symbol: 'AVAX',
name: 'Avalanche',
...nativeToken,
},
explorerUrl: 'https://cchain.explorer.avax.network',
type: ChainTypes.EVM,
contractAddress: '',
icon: 'https://github.com/MetaMask/metamask-extension/blob/develop/app/images/avax-token.png',
contactVersion: SwapContractVersion.TraderJoe,
},
{
id: EVM_CHAIN_IDS[ChainNames.Fuji],
name: ChainNames.Fuji,
rpcUrl: 'https://api.avax-test.network/ext/bc/C/rpc',
token: {
symbol: 'AVAX',
name: 'Fuji Avalanche',
...nativeToken,
},
explorerUrl: 'https://cchain.explorer.avax-test.network',
type: ChainTypes.EVM,
icon: 'https://github.com/MetaMask/metamask-extension/blob/develop/app/images/avax-token.png',
contractAddress: '0x9f971C56B9Ce9Ff013c82A99daFA9c3C0a9585CA',
contactVersion: SwapContractVersion.TraderJoe,
},
{
id: EVM_CHAIN_IDS[ChainNames.BinanceSmartChain],
name: ChainNames.BinanceSmartChain,
rpcUrl: 'https://bsc-dataseed.binance.org/',
token: {
symbol: 'BNB',
name: 'Binance Coin',
...nativeToken,
},
explorerUrl: 'https://bscscan.com',
type: ChainTypes.EVM,
icon: 'https://raw.githubusercontent.com/MetaMask/metamask-extension/develop/app/images/bnb.png',
contractAddress: '',
contactVersion: SwapContractVersion.PancakeSwap,
},
{
id: EVM_CHAIN_IDS[ChainNames.Chapel],
name: ChainNames.Chapel,
rpcUrl: 'https://data-seed-prebsc-1-s1.binance.org:8545/',
token: {
symbol: 'tBNB',
name: 'Binance Testnet Coin',
...nativeToken,
},
explorerUrl: 'https://testnet.bscscan.com',
type: ChainTypes.EVM,
icon: 'https://raw.githubusercontent.com/MetaMask/metamask-extension/develop/app/images/bnb.png',
contractAddress: '0x08a87595f4423AaF591155aa2cEF31Fb904BcdE8',
contactVersion: SwapContractVersion.PancakeSwap,
},
]

export const CHAINS: Readonly<{ [key in ChainTypes]?: BridgeChain[] }> = {
Expand Down
3 changes: 3 additions & 0 deletions packages/nft-checkout/src/const/fee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const RARIMO_BRIDGE_FEE = 2.5

export const NATIVE_TOKEN_WRAP_SLIPPAGE_MULTIPLIER = 1.02
3 changes: 3 additions & 0 deletions packages/nft-checkout/src/const/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export * from './chains'
export * from './abi/'
export * from './fee'
export * from './bundle'
export * from './tokens'
57 changes: 57 additions & 0 deletions packages/nft-checkout/src/const/tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Token } from '../entities'
import { ChainNames } from '../enums'
import { EVM_CHAINS } from './chains'

const chapelChain = EVM_CHAINS.find(i => i.name === ChainNames.Chapel)!
const fujiChain = EVM_CHAINS.find(i => i.name === ChainNames.Fuji)!

export const PANCAKE_SWAP_TESTNET_TOKEN_LIST: Token[] = [
new Token({
chain: chapelChain,
address: '0x8BaBbB98678facC7342735486C851ABD7A0d17Ca',
name: 'Ethereum Token',
symbol: 'ETH',
decimals: 18,
logoURI:
'https://raw.githubusercontent.com/MetaMask/metamask-extension/develop/app/images/eth_logo.svg',
}),
new Token({
chain: chapelChain,
address: '0x8BaBbB98678facC7342735486C851ABD7A0d17Ca',
name: 'Ethereum Token',
symbol: 'ETH',
decimals: 18,
logoURI:
'https://raw.githubusercontent.com/MetaMask/metamask-extension/develop/app/images/eth_logo.svg',
}),
new Token({
chain: chapelChain,
address: '0xae13d989dac2f0debff460ac112a837c89baa7cd',
name: 'Wrapped Binance Testnet Token',
symbol: 'WBNB',
decimals: 18,
logoURI:
'https://raw.githubusercontent.com/MetaMask/metamask-extension/develop/app/images/bnb.png',
}),
]

export const TRADER_JOE_SWAP_TESTNET_TOKEN_LIST: Token[] = [
new Token({
chain: fujiChain,
address: '0xd00ae08403B9bbb9124bB305C09058E32C39A48c',
decimals: 18,
name: 'Wrapped AVAX',
symbol: 'WAVAX',
logoURI:
'https://raw.githubusercontent.com/traderjoe-xyz/joe-tokenlists/main/logos/0xd00ae08403B9bbb9124bB305C09058E32C39A48c/logo.png',
}),
new Token({
chain: fujiChain,
address: '0xB6076C93701D6a07266c31066B298AeC6dd65c2d',
decimals: 6,
name: 'USD Coin',
symbol: 'USDC',
logoURI:
'https://raw.githubusercontent.com/traderjoe-xyz/joe-tokenlists/main/logos/0xB6076C93701D6a07266c31066B298AeC6dd65c2d/logo.png',
}),
]
37 changes: 37 additions & 0 deletions packages/nft-checkout/src/entities/amount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { BN } from '@distributedlab/utils'

export class AmountBase {
readonly #value: string
readonly #decimals: number

protected constructor(value: string, decimals: number) {
this.#value = value
this.#decimals = decimals
}

get value(): string {
return this.#value
}

get decimals(): number {
return this.#decimals
}

toString(): string {
return new BN(this.#value).fromFraction(this.#decimals).toString()
}
}

export class Amount extends AmountBase {
protected constructor(value: string, decimals: number) {
super(value, decimals)
}

static fromRaw(value: string, decimals: number): Amount {
return new Amount(value, decimals)
}

static fromFraction(value: string, decimals: number): Amount {
return new Amount(new BN(value).fromFraction(decimals).toString(), decimals)
}
}
Loading

0 comments on commit f1fb3ee

Please sign in to comment.