Skip to content

Commit

Permalink
Tron and bridgers refactoring, add meson and orbiter (5.43.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
axtezy authored Nov 14, 2024
2 parents 1c26492 + 2fb4bc6 commit de4380d
Show file tree
Hide file tree
Showing 42 changed files with 1,283 additions and 848 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": "5.42.6",
"version": "5.43.0",
"description": "Simplify dApp creation",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ export interface TronTransactionOptions extends BasicTransactionOptions {
feeLimit?: number;

callValue?: number | string;

rawParameter?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { TronParameters } from 'src/core/blockchain/web3-pure/typed-web3-pure/tr
import { TronTransactionConfig } from 'src/core/blockchain/web3-pure/typed-web3-pure/tron-web3-pure/models/tron-transaction-config';
import { TronWeb3Pure } from 'src/core/blockchain/web3-pure/typed-web3-pure/tron-web3-pure/tron-web3-pure';
import { WalletProviderCore } from 'src/core/sdk/models/wallet-provider';
import { AbiItem } from 'web3-utils';

export class TronWeb3Private extends Web3Private {
/**
Expand Down Expand Up @@ -91,42 +90,16 @@ export class TronWeb3Private extends Web3Private {
tokenAddress,
TRC20_CONTRACT_ABI,
'approve',
[spenderAddress, rawValue.toFixed(0)],
[
{ type: 'address', value: spenderAddress },
{ type: 'uint256', value: rawValue.toFixed(0) }
],
'0',
options.feeLimit
);
}

public async executeContractMethod(
contractAddress: string,
contractAbi: AbiItem[],
methodName: string,
methodArguments: unknown[],
options: TronTransactionOptions = {}
): Promise<string> {
try {
const contract = await this.tronWeb.contract(contractAbi, contractAddress);

const transactionHash = await contract[methodName](...methodArguments).send({
from: this.address,
...(options.callValue && {
callValue: Web3Private.stringifyAmount(options.callValue)
}),
...(options.feeLimit && {
feeLimit: Web3Private.stringifyAmount(options.feeLimit)
})
});
if (options.onTransactionHash) {
options.onTransactionHash(transactionHash);
}
return transactionHash;
} catch (err) {
console.error('Method execution error: ', err);
throw TronWeb3Private.parseError(err);
}
}

public async triggerContract(
public async sendTransaction(
contractAddress: string,
methodSignature: string,
parameters: TronParameters,
Expand Down Expand Up @@ -154,4 +127,27 @@ export class TronWeb3Private extends Web3Private {
throw TronWeb3Private.parseError(err);
}
}

public async trySendTransaction(
contractAddress: string,
methodSignature: string,
parameters: TronParameters,
options: TronTransactionOptions = {}
): Promise<string> {
try {
await this.tronWeb.transactionBuilder.estimateEnergy(
contractAddress,
methodSignature,
{},
parameters,
this.address
);
} catch (err) {
if (err !== 'this node does not support estimate energy') {
throw new Error('Tron transaction simulation error');
}
}

return this.sendTransaction(contractAddress, methodSignature, parameters, options);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { TronParameters } from 'src/core/blockchain/web3-pure/typed-web3-pure/tron-web3-pure/models/tron-parameters';

export interface TronTransactionConfig {
signature: string;
arguments: TronParameters;
to: string;
data: string;
callValue?: string;

feeLimit?: number;
callValue?: string;

rawParameter?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,25 @@ export class TronWeb3Pure {
contractAddress: string,
contractAbi: AbiItem[],
methodName: string,
methodArguments: unknown[] = [],
methodArguments: TronParameters,
callValue?: string,
feeLimit?: number
): TronTransactionConfig {
const data = this.encodeFunctionCall(contractAbi, methodName, methodArguments);
const methodAbi = contractAbi.find(abiItem => abiItem.name === methodName);
if (!methodAbi) {
throw new Error('Encode fail. No method in ABI');
}

const signature = `${methodAbi.name!}(${this.flattenTypesToString(methodAbi.inputs!).join(
','
)})`;

return {
to: contractAddress,
data,
callValue,
feeLimit
arguments: methodArguments,
signature,
...(callValue && { callValue }),
...(feeLimit && { feeLimit })
};
}

Expand Down
1 change: 1 addition & 0 deletions src/features/common/constants/fake-wallet-address.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* used for passing fromAddress in swap/quote requests with disabled wallet */
export const FAKE_WALLET_ADDRESS = '0xe388Ed184958062a2ea29B7fD049ca21244AE02e';
export const FAKE_SOLANA_WALLET_ADDRESS = '7cwWhuCJUHc27Dq4nQRhggwgeuVHEeS3NWv7BY6yY9Bk';
export const FAKE_TRON_WALLET_ADDRESS = 'TFwy9q45E5kK6HKzmc3gAcJY4rVD96VNi5';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BridgersSourceFlag } from 'src/features/common/providers/bridgers/models/bridgers-source-flag';
import { EvmBridgersTransactionData } from 'src/features/cross-chain/calculation-manager/providers/bridgers-provider/evm-bridgers-trade/models/evm-bridgers-transaction-data';
import { TronBridgersTransactionData } from 'src/features/cross-chain/calculation-manager/providers/bridgers-provider/tron-bridgers-trade/models/tron-bridgers-transaction-data';
import { EvmBridgersTransactionData } from 'src/features/cross-chain/calculation-manager/providers/bridgers-provider/models/evm-bridgers-transaction-data';
import { TronBridgersTransactionData } from 'src/features/cross-chain/calculation-manager/providers/bridgers-provider/models/tron-bridgers-transaction-data';

export interface BridgersSwapRequest {
fromTokenAddress: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
BlockchainName,
EvmBlockchainName,
TronBlockchainName
} from 'src/core/blockchain/models/blockchain-name';
import { BlockchainsInfo } from 'src/core/blockchain/utils/blockchains-info/blockchains-info';
import {
BridgersCrossChainGasParams,
BridgersCrossChainParams,
BridgersEvmCrossChainParams,
BridgersTronCrossChainParams
} from 'src/features/cross-chain/calculation-manager/providers/bridgers-provider/models/bridgers-cross-chain-trade-types';
import { EvmBridgersCrossChainTrade } from 'src/features/cross-chain/calculation-manager/providers/bridgers-provider/networks/evm-bridgers-cross-chain-trade';
import { TronBridgersCrossChainTrade } from 'src/features/cross-chain/calculation-manager/providers/bridgers-provider/networks/tron-bridgers-cross-chain-trade';
import { GasData } from 'src/features/cross-chain/calculation-manager/providers/common/evm-cross-chain-trade/models/gas-data';

export class BridgersCrossChainProviderFactory {
public static createTrade(
params: BridgersCrossChainParams<TronBlockchainName | EvmBlockchainName>
): EvmBridgersCrossChainTrade | TronBridgersCrossChainTrade {
const fromBlockchain = params.crossChainTrade.from.blockchain;
if (BlockchainsInfo.isTronBlockchainName(fromBlockchain)) {
return new TronBridgersCrossChainTrade(params as BridgersTronCrossChainParams);
}

if (BlockchainsInfo.isEvmBlockchainName(fromBlockchain)) {
return new EvmBridgersCrossChainTrade(params as BridgersEvmCrossChainParams);
}
throw new Error('Can not create trade instance');
}

public static async getGasData(
calculateGas: boolean,
params: BridgersCrossChainGasParams<BlockchainName>
): Promise<GasData | null> {
if (calculateGas) {
const fromBlockchain = params.from.blockchain;
if (BlockchainsInfo.isEvmBlockchainName(fromBlockchain)) {
return EvmBridgersCrossChainTrade.getGasData(
params as BridgersCrossChainGasParams<EvmBlockchainName>
);
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { PriceToken, PriceTokenAmount } from 'src/common/tokens';
import {
BLOCKCHAIN_NAME,
BlockchainName,
EvmBlockchainName,
TronBlockchainName
} from 'src/core/blockchain/models/blockchain-name';
import { BlockchainsInfo } from 'src/core/blockchain/utils/blockchains-info/blockchains-info';
import { Web3PublicSupportedBlockchain } from 'src/core/blockchain/web3-public-service/models/web3-public-storage';
import { EvmEncodeConfig } from 'src/core/blockchain/web3-pure/typed-web3-pure/evm-web3-pure/models/evm-encode-config';
import { TronTransactionConfig } from 'src/core/blockchain/web3-pure/typed-web3-pure/tron-web3-pure/models/tron-transaction-config';
Expand All @@ -26,13 +26,11 @@ import { getFromWithoutFee } from 'src/features/common/utils/get-from-without-fe
import { createTokenNativeAddressProxy } from 'src/features/common/utils/token-native-address-proxy';
import { RequiredCrossChainOptions } from 'src/features/cross-chain/calculation-manager/models/cross-chain-options';
import { CROSS_CHAIN_TRADE_TYPE } from 'src/features/cross-chain/calculation-manager/models/cross-chain-trade-type';
import { BridgersCrossChainProviderFactory } from 'src/features/cross-chain/calculation-manager/providers/bridgers-provider/bridgers-cross-chain-provider-factory';
import {
BridgersCrossChainSupportedBlockchain,
bridgersCrossChainSupportedBlockchains,
BridgersEvmCrossChainSupportedBlockchain
bridgersCrossChainSupportedBlockchains
} from 'src/features/cross-chain/calculation-manager/providers/bridgers-provider/constants/bridgers-cross-chain-supported-blockchain';
import { EvmBridgersCrossChainTrade } from 'src/features/cross-chain/calculation-manager/providers/bridgers-provider/evm-bridgers-trade/evm-bridgers-cross-chain-trade';
import { TronBridgersCrossChainTrade } from 'src/features/cross-chain/calculation-manager/providers/bridgers-provider/tron-bridgers-trade/tron-bridgers-cross-chain-trade';
import { CrossChainProvider } from 'src/features/cross-chain/calculation-manager/providers/common/cross-chain-provider';
import { CalculationResult } from 'src/features/cross-chain/calculation-manager/providers/common/models/calculation-result';
import { FeeInfo } from 'src/features/cross-chain/calculation-manager/providers/common/models/fee-info';
Expand Down Expand Up @@ -152,49 +150,34 @@ export class BridgersCrossChainProvider extends CrossChainProvider {
toToken.decimals
);

if (BlockchainsInfo.isEvmBlockchainName(fromBlockchain)) {
const gasData =
options.gasCalculation === 'enabled' && options.receiverAddress
? await EvmBridgersCrossChainTrade.getGasData(
from as PriceTokenAmount<BridgersEvmCrossChainSupportedBlockchain>,
to as PriceTokenAmount<TronBlockchainName>,
options.receiverAddress,
options.providerAddress,
feeInfo
)
: null;

const evmTrade = new EvmBridgersCrossChainTrade(
{
from: from as PriceTokenAmount<BridgersEvmCrossChainSupportedBlockchain>,
to: to as PriceTokenAmount<TronBlockchainName>,
toTokenAmountMin,
feeInfo,
gasData,
slippage: options.slippageTolerance
},
options.providerAddress,
await this.getRoutePath(from, to),
useProxy
);

return this.getCalculationResponse(from, transactionData, evmTrade);
}

const tronTrade = new TronBridgersCrossChainTrade(
const gasData = await BridgersCrossChainProviderFactory.getGasData(
options.gasCalculation === 'enabled' && Boolean(options.receiverAddress),
{
from: from as PriceTokenAmount<TronBlockchainName>,
to: to as PriceTokenAmount<BridgersEvmCrossChainSupportedBlockchain>,
from,
to,
receiverAddress: options.receiverAddress!,
providerAddress: options.providerAddress,
feeInfo
}
);

const trade = BridgersCrossChainProviderFactory.createTrade({
crossChainTrade: {
from: from as
| PriceTokenAmount<EvmBlockchainName>
| PriceTokenAmount<TronBlockchainName>,
to,
toTokenAmountMin,
feeInfo,
slippage: options.slippageTolerance,
contractAddress: transactionData.contractAddress
gasData,
slippage: options.slippageTolerance
},
options.providerAddress,
await this.getRoutePath(from, to)
);
providerAddress: options.providerAddress,
routePath: await this.getRoutePath(from, to),
useProxy
});

return this.getCalculationResponse(from, transactionData, tronTrade);
return this.getCalculationResponse(from, transactionData, trade);
} catch (err: unknown) {
return {
trade: null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import BigNumber from 'bignumber.js';
import { PriceTokenAmount } from 'src/common/tokens';
import {
BlockchainName,
EvmBlockchainName,
TronBlockchainName
} from 'src/core/blockchain/models/blockchain-name';

import { GasData } from '../../common/evm-cross-chain-trade/models/gas-data';
import { FeeInfo } from '../../common/models/fee-info';
import { RubicStep } from '../../common/models/rubicStep';

export interface BridgersCrossChainGasParams<Blockchain extends BlockchainName> {
from: PriceTokenAmount<Blockchain>;
to: PriceTokenAmount;
receiverAddress: string;
providerAddress: string;
feeInfo: FeeInfo;
}

export interface BridgersCrossChainParams<Blockchain extends BlockchainName> {
crossChainTrade: {
from: PriceTokenAmount<Blockchain>;
to: PriceTokenAmount;
toTokenAmountMin: BigNumber;
feeInfo: FeeInfo;
gasData: GasData;
slippage: number;
};
providerAddress: string;
routePath: RubicStep[];
useProxy: boolean;
}

export type BridgersEvmCrossChainParams = BridgersCrossChainParams<EvmBlockchainName>;
export type BridgersTronCrossChainParams = BridgersCrossChainParams<TronBlockchainName>;
Loading

0 comments on commit de4380d

Please sign in to comment.