Skip to content

Commit

Permalink
Merge pull request #158 from Cryptorubic/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
axtezy authored Aug 15, 2022
2 parents b4ddc98 + 7731bd2 commit 0a8bcec
Show file tree
Hide file tree
Showing 20 changed files with 185 additions and 111 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rubic-sdk",
"version": "2.9.1",
"version": "2.9.9",
"description": "Simplify dApp creation",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
11 changes: 11 additions & 0 deletions src/common/errors/blockchain/wrong-receiver-address.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { RubicSdkError } from 'src/common';

/**
* Thrown, when passed wrong receiver address.
*/
export class WrongReceiverAddressError extends RubicSdkError {
constructor() {
super();
Object.setPrototypeOf(this, WrongReceiverAddressError.prototype);
}
}
5 changes: 1 addition & 4 deletions src/core/blockchain/web3-private/web3-private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { WalletConnectionConfiguration } from '@rsdk-core/blockchain/models/wall
import { RubicSdkError } from '@rsdk-common/errors/rubic-sdk.error';
import { FailedToCheckForTransactionReceiptError } from '@rsdk-common/errors/swap/failed-to-check-for-transaction-receipt.error';
import { Web3Pure } from 'src/core';
import { InsufficientFundsGasPriceValueError, LowSlippageError } from 'src/common';
import { LowSlippageError } from 'src/common';
import { parseError } from 'src/common/utils/errors';
import { TransactionConfig } from 'web3-core';

Expand Down Expand Up @@ -78,9 +78,6 @@ export class Web3Private {
if (err.message.includes('Failed to check for transaction receipt')) {
return new FailedToCheckForTransactionReceiptError();
}
if (err.message.includes('Ok(OutOfFund)')) {
return new InsufficientFundsGasPriceValueError();
}
if (err.code === -32603) {
return new LowGasError();
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ export { Web3Pure } from './blockchain/web3-pure/web3-pure';
export { BlockchainsInfo } from './blockchain/blockchains-info';
export { BlockchainName, BLOCKCHAIN_NAME } from './blockchain/models/blockchain-name';
export { Configuration, RpcProvider, WalletProvider } from './sdk/models/configuration';
export { Token } from './blockchain/tokens/token';
export { PriceToken } from './blockchain/tokens/price-token';
export { PriceTokenAmount } from './blockchain/tokens/price-token-amount';

export type { Token } from './blockchain/tokens/token';
export type { PriceToken } from './blockchain/tokens/price-token';
export type { PriceTokenAmount } from './blockchain/tokens/price-token-amount';
export type { BasicTransactionOptions } from './blockchain/models/basic-transaction-options';
export type { Blockchain } from './blockchain/models/blockchain';
export type { TokenBaseStruct } from './blockchain/models/token-base-struct';
Expand Down
8 changes: 6 additions & 2 deletions src/core/sdk/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ export class SDK {
/**
* Can be used to get `Web3Public` instance by blockchain name to get public information from blockchain.
*/
public readonly web3PublicService = Injector.web3PublicService;
public get web3PublicService(): Web3PublicService {
return Injector.web3PublicService;
}

/**
* Can be used to send transactions and execute smart contracts methods.
*/
public readonly web3Private = Injector.web3Private;
public get web3Private(): Web3Private {
return Injector.web3Private;
}

/**
* Use it to get gas price information.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ import { SushiSwapEthereumProvider } from '@rsdk-features/instant-trades/dexes/e
import { UniSwapV3EthereumProvider } from '@rsdk-features/instant-trades/dexes/ethereum/uni-swap-v3-ethereum/uni-swap-v3-ethereum-provider';
import { OneinchEthereumProvider } from '@rsdk-features/instant-trades/dexes/ethereum/oneinch-ethereum/oneinch-ethereum-provider';
import { PancakeSwapProvider } from '@rsdk-features/instant-trades/dexes/bsc/pancake-swap/pancake-swap-provider';
import { SushiSwapBscProvider } from '@rsdk-features/instant-trades/dexes/bsc/sushi-swap-bsc/sushi-swap-bsc-provider';
import { OneinchBscProvider } from '@rsdk-features/instant-trades/dexes/bsc/oneinch-bsc/oneinch-bsc-provider';
import { QuickSwapProvider } from '@rsdk-features/instant-trades/dexes/polygon/quick-swap/quick-swap-provider';
import { SushiSwapPolygonProvider } from '@rsdk-features/instant-trades/dexes/polygon/sushi-swap-polygon/sushi-swap-polygon-provider';
import { UniSwapV3PolygonProvider } from '@rsdk-features/instant-trades/dexes/polygon/uni-swap-v3-polygon/uni-swap-v3-polygon-provider';
import { AlgebraProvider } from '@rsdk-features/instant-trades/dexes/polygon/algebra/algebra-provider';
import { OneinchPolygonProvider } from '@rsdk-features/instant-trades/dexes/polygon/oneinch-polygon/oneinch-polygon-provider';
import { PangolinProvider } from '@rsdk-features/instant-trades/dexes/avalanche/pangolin/pangolin-provider';
import { JoeProvider } from '@rsdk-features/instant-trades/dexes/avalanche/joe/joe-provider';
import { SushiSwapAvalancheProvider } from '@rsdk-features/instant-trades/dexes/avalanche/sushi-swap-avalanche/sushi-swap-avalanche-provider';
Expand All @@ -32,6 +26,12 @@ import { SushiSwapTelosProvider } from '@rsdk-features/instant-trades/dexes/telo
import { ZappyProvider } from '@rsdk-features/instant-trades/dexes/telos/zappy/trisolaris-aurora-provider';
import { OneinchFantomProvider } from 'src/features/instant-trades/dexes/fantom/oneinch-fantom/oneinch-fantom-provider';
import { OneinchAvalancheProvider } from 'src/features/instant-trades/dexes/avalanche/oneinch-avalanche/oneinch-avalanche-provider';
import { UniSwapV3PolygonProvider } from 'src/features/instant-trades/dexes/polygon/uni-swap-v3-polygon/uni-swap-v3-polygon-provider';
import { SushiSwapBscProvider } from 'src/features/instant-trades/dexes/bsc/sushi-swap-bsc/sushi-swap-bsc-provider';
import { OneinchBscProvider } from 'src/features/instant-trades/dexes/bsc/oneinch-bsc/oneinch-bsc-provider';
import { AlgebraProvider } from 'src/features/instant-trades/dexes/polygon/algebra/algebra-provider';
import { OneinchPolygonProvider } from 'src/features/instant-trades/dexes/polygon/oneinch-polygon/oneinch-polygon-provider';
import { SushiSwapPolygonProvider } from 'src/features/instant-trades/dexes/polygon/sushi-swap-polygon/sushi-swap-polygon-provider';

/**
* Stores contracts info.
Expand Down
2 changes: 1 addition & 1 deletion src/features/cross-chain/cross-chain-status-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export class CrossChainStatusManager {
return CrossChainTxStatus.FAIL;
}

if (dstTxStatus === CelerSwapStatus.SUCСESS) {
if (dstTxStatus === CelerSwapStatus.SUCCESS) {
return CrossChainTxStatus.SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { CROSS_CHAIN_TRADE_TYPE, TRADE_TYPE, TradeType } from 'src/features';
import {
CROSS_CHAIN_TRADE_TYPE,
TRADE_TYPE,
TradeType,
UniswapV2AbstractProvider
} from 'src/features';
import { BlockchainName, BlockchainsInfo, Web3Pure } from 'src/core';
import { PriceToken } from '@rsdk-core/blockchain/tokens/price-token';
import { PriceTokenAmount } from '@rsdk-core/blockchain/tokens/price-token-amount';
Expand Down Expand Up @@ -27,6 +32,10 @@ import { FeeInfo } from 'src/features/cross-chain/providers/common/models/fee';
import { celerCrossChainContractAbi } from 'src/features/cross-chain/providers/celer-trade-provider/constants/celer-cross-chain-contract-abi';
import { celerTransitTokens } from 'src/features/cross-chain/providers/celer-trade-provider/constants/celer-transit-tokens';

interface CelerCrossChainOptions extends RequiredCrossChainOptions {
isUniV2?: boolean;
}

export class CelerCrossChainTradeProvider extends CelerRubicCrossChainTradeProvider {
public static isSupportedBlockchain(
blockchain: BlockchainName
Expand All @@ -53,7 +62,7 @@ export class CelerCrossChainTradeProvider extends CelerRubicCrossChainTradeProvi
public async calculate(
from: PriceTokenAmount,
to: PriceToken,
options: RequiredCrossChainOptions
options: CelerCrossChainOptions
): Promise<Omit<WrappedCrossChainTrade, 'tradeType'> | null> {
const fromBlockchain = from.blockchain;
const toBlockchain = to.blockchain;
Expand Down Expand Up @@ -86,7 +95,8 @@ export class CelerCrossChainTradeProvider extends CelerRubicCrossChainTradeProvi
fromBlockchain,
from,
fromTransitToken,
slippages.fromSlippageTolerance
slippages.fromSlippageTolerance,
options.isUniV2
);

const celerSlippage = await this.fetchCelerSlippage(
Expand Down Expand Up @@ -132,6 +142,7 @@ export class CelerCrossChainTradeProvider extends CelerRubicCrossChainTradeProvi
toTransit,
to,
toSlippageTolerance,
options.isUniV2,
[TRADE_TYPE.ONE_INCH]
);

Expand Down Expand Up @@ -283,6 +294,7 @@ export class CelerCrossChainTradeProvider extends CelerRubicCrossChainTradeProvi
from: PriceTokenAmount,
toToken: PriceToken,
slippageTolerance: number,
isUniV2?: boolean,
disabledProviders?: TradeType[]
): Promise<CelerCrossChainContractTrade> {
if (compareAddresses(from.address, toToken.address)) {
Expand All @@ -299,6 +311,7 @@ export class CelerCrossChainTradeProvider extends CelerRubicCrossChainTradeProvi
from,
toToken,
slippageTolerance,
isUniV2,
disabledProviders
);
}
Expand Down Expand Up @@ -330,11 +343,13 @@ export class CelerCrossChainTradeProvider extends CelerRubicCrossChainTradeProvi
from: PriceTokenAmount,
toToken: PriceToken,
slippageTolerance: number,
isUniV2?: boolean,
disabledProviders?: TradeType[]
): Promise<CelerItCrossChainContractTrade> {
const contract = this.contracts(blockchain);
const promises: Promise<ItCalculatedTrade>[] = contract.providersData
.filter(data => !disabledProviders?.some(provider => provider === data.provider.type))
.filter(data => !isUniV2 || data.provider instanceof UniswapV2AbstractProvider)
.map(async (_, providerIndex) => {
return this.getItCalculatedTrade(
contract,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ import {
} from '@rsdk-features/cross-chain/providers/celer-trade-provider/constants/celer-cross-chain-fee-multipliers';
import { CelerCrossChainContractTrade } from '@rsdk-features/cross-chain/providers/celer-trade-provider/celer-cross-chain-contract-trade/celer-cross-chain-contract-trade';
import { CelerItCrossChainContractTrade } from '@rsdk-features/cross-chain/providers/celer-trade-provider/celer-cross-chain-contract-trade/celer-it-cross-chain-contract-trade/celer-it-cross-chain-contract-trade';
import { CROSS_CHAIN_TRADE_TYPE, TradeType } from 'src/features';
import { CROSS_CHAIN_TRADE_TYPE, CrossChainTrade, TradeType } from 'src/features';
import { FeeInfo } from 'src/features/cross-chain/providers/common/models/fee';
import { CelerDirectCrossChainContractTrade } from 'src/features/cross-chain/providers/celer-trade-provider/celer-cross-chain-contract-trade/celer-direct-cross-chain-trade/celer-direct-cross-chain-contract-trade';

/**
* Calculated Celer cross chain trade.
*/
export class CelerCrossChainTrade extends CelerRubicCrossChainTrade {
/** @internal */
public readonly type = CROSS_CHAIN_TRADE_TYPE.CELER;

public readonly itType: { from: TradeType | undefined; to: TradeType | undefined };
Expand Down Expand Up @@ -180,6 +179,7 @@ export class CelerCrossChainTrade extends CelerRubicCrossChainTrade {
public async swap(options: SwapTransactionOptions = {}): Promise<string | never> {
await this.checkTradeErrors();
await this.checkAllowanceAndApprove(options);
CrossChainTrade.checkReceiverAddress(options?.receiverAddress);

const { onConfirm, gasLimit, gasPrice } = options;

Expand Down Expand Up @@ -230,7 +230,7 @@ export class CelerCrossChainTrade extends CelerRubicCrossChainTrade {
throw err;
}

protected async getContractParams(
public async getContractParams(
options: {
fromAddress?: string;
receiverAddress?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export enum CelerSwapStatus {
NULL = 0,
SUCСESS = 1,
SUCCESS = 1,
FAILED = 2,
FALLBACK = 3
}
17 changes: 16 additions & 1 deletion src/features/cross-chain/providers/common/cross-chain-trade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,26 @@ import { TransactionReceipt } from 'web3-eth';
import { ContractParams } from '@rsdk-features/cross-chain/models/contract-params';
import { TransactionConfig } from 'web3-core';
import { FeeInfo } from 'src/features/cross-chain/providers/common/models/fee';
import { WrongReceiverAddressError } from 'src/common/errors/blockchain/wrong-receiver-address.error';

/**
* Abstract class for all cross chain providers' trades.
*/
export abstract class CrossChainTrade {
/**
* Checks receiver address for correctness.
* @param receiverAddress
*/
public static checkReceiverAddress(receiverAddress: string | undefined): void {
if (!receiverAddress) {
return;
}
if (Web3Pure.isAddressCorrect(receiverAddress)) {
return;
}
throw new WrongReceiverAddressError();
}

/**
* Type of calculated cross chain trade.
*/
Expand Down Expand Up @@ -101,7 +116,7 @@ export abstract class CrossChainTrade {
*/
public abstract swap(options?: SwapTransactionOptions): Promise<string | never>;

protected abstract getContractParams(options: {
public abstract getContractParams(options: {
fromAddress?: string;
receiverAddress?: string;
}): Promise<ContractParams>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class DebridgeCrossChainTrade extends CrossChainTrade {
cryptoFeeToken: from
},
EMPTY_ADDRESS
).getContractParams();
).getContractParams({});

const web3Public = Injector.web3PublicService.getWeb3Public(fromBlockchain);
const [gasLimit, gasPrice] = await Promise.all([
Expand Down Expand Up @@ -162,10 +162,11 @@ export class DebridgeCrossChainTrade extends CrossChainTrade {
public async swap(options: SwapTransactionOptions = {}): Promise<string | never> {
await this.checkTradeErrors();
await this.checkAllowanceAndApprove(options);
CrossChainTrade.checkReceiverAddress(options?.receiverAddress);

const { onConfirm, gasLimit, gasPrice } = options;
const { contractAddress, contractAbi, methodName, methodArguments, value } =
await this.getContractParams();
await this.getContractParams(options);

let transactionHash: string;
const onTransactionHash = (hash: string) => {
Expand Down Expand Up @@ -193,8 +194,8 @@ export class DebridgeCrossChainTrade extends CrossChainTrade {
}
}

public async getContractParams(): Promise<ContractParams> {
const data = await this.getTransactionRequest();
public async getContractParams(options: SwapTransactionOptions): Promise<ContractParams> {
const data = await this.getTransactionRequest(options?.receiverAddress);
const toChainId = BlockchainsInfo.getBlockchainByName(this.to.blockchain).id;
const fromContracts =
DE_BRIDGE_CONTRACT_ADDRESS[
Expand All @@ -207,7 +208,7 @@ export class DebridgeCrossChainTrade extends CrossChainTrade {
toChainId,
this.to.address,
Web3Pure.toWei(this.toTokenAmountMin, this.to.decimals),
this.walletAddress,
options?.receiverAddress || this.walletAddress,
this.providerAddress,
fromContracts.providerRouter
];
Expand Down Expand Up @@ -239,12 +240,15 @@ export class DebridgeCrossChainTrade extends CrossChainTrade {
return this.transitAmount.plus(usdCryptoFee).dividedBy(this.to.tokenAmount);
}

private async getTransactionRequest(): Promise<BytesLike> {
private async getTransactionRequest(receiverAddress?: string): Promise<BytesLike> {
const params = {
...this.transactionRequest,
...(receiverAddress && { dstChainTokenOutRecipient: receiverAddress })
};

const { tx } = await Injector.httpClient.get<TransactionResponse>(
DebridgeCrossChainTradeProvider.apiEndpoint,
{
params: this.transactionRequest as unknown as {}
}
{ params }
);
return tx.data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ export const lifiCrossChainSupportedBlockchains = [
BLOCKCHAIN_NAME.MOONRIVER,
BLOCKCHAIN_NAME.ARBITRUM,
BLOCKCHAIN_NAME.OPTIMISM,
BLOCKCHAIN_NAME.CRONOS,
BLOCKCHAIN_NAME.OKE_X_CHAIN,
BLOCKCHAIN_NAME.GNOSIS,
BLOCKCHAIN_NAME.FUSE,
BLOCKCHAIN_NAME.MOONBEAM
BLOCKCHAIN_NAME.MOONBEAM,
BLOCKCHAIN_NAME.CELO
] as const;

export type LifiCrossChainSupportedBlockchain = typeof lifiCrossChainSupportedBlockchains[number];
Loading

0 comments on commit 0a8bcec

Please sign in to comment.