rubic-sdk
Version:
Simplify dApp creation
152 lines • 8.25 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StargateV2CrossChainTrade = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const errors_1 = require("../../../../../common/errors");
const evm_web3_pure_1 = require("../../../../../core/blockchain/web3-pure/typed-web3-pure/evm-web3-pure/evm-web3-pure");
const injector_1 = require("../../../../../core/injector/injector");
const get_from_without_fee_1 = require("../../../../common/utils/get-from-without-fee");
const cross_chain_trade_type_1 = require("../../models/cross-chain-trade-type");
const rubic_proxy_contract_address_1 = require("../common/constants/rubic-proxy-contract-address");
const evm_common_cross_chain_abi_1 = require("../common/evm-cross-chain-trade/constants/evm-common-cross-chain-abi");
const gateway_rubic_cross_chain_abi_1 = require("../common/evm-cross-chain-trade/constants/gateway-rubic-cross-chain-abi");
const evm_cross_chain_trade_1 = require("../common/evm-cross-chain-trade/evm-cross-chain-trade");
const bridge_type_1 = require("../common/models/bridge-type");
const proxy_cross_chain_evm_trade_1 = require("../common/proxy-cross-chain-evm-facade/proxy-cross-chain-evm-trade");
const stargate_v2_contract_address_1 = require("./constants/stargate-v2-contract-address");
const stargate_v2_pool_abi_1 = require("./constants/stargate-v2-pool-abi");
const stargate_v2_token_address_1 = require("./constants/stargate-v2-token-address");
class StargateV2CrossChainTrade extends evm_cross_chain_trade_1.EvmCrossChainTrade {
get methodName() {
return 'startBridgeTokensViaGenericCrossChain';
}
get web3Private() {
return injector_1.Injector.web3PrivateService.getWeb3PrivateByBlockchain(this.from.blockchain);
}
get fromBlockchain() {
return this.from.blockchain;
}
get fromContractAddress() {
const fromTokenSymbol = stargate_v2_token_address_1.stargateV2TokenAddress[this.from.blockchain][this.fromTokenAddress];
return this.isProxyTrade
? rubic_proxy_contract_address_1.rubicProxyContractAddress[this.fromBlockchain].gateway
: stargate_v2_contract_address_1.stargateV2ContractAddress?.[this.fromBlockchain]?.[fromTokenSymbol];
}
constructor(crossChainTrade, providerAddress, routePath, useProxy) {
super(providerAddress, routePath, useProxy);
this.type = cross_chain_trade_type_1.CROSS_CHAIN_TRADE_TYPE.STARGATE_V2;
this.isAggregator = false;
this.onChainSubtype = { from: undefined, to: undefined };
this.bridgeType = bridge_type_1.BRIDGE_TYPE.STARGATE_V2;
this.from = crossChainTrade.from;
this.to = crossChainTrade.to;
this.feeInfo = crossChainTrade.feeInfo;
this.gasData = crossChainTrade.gasData;
this.slippageTolerance = crossChainTrade.slippageTolerance;
this.stargateV2SendParams = crossChainTrade.sendParams;
this.messagingFee = crossChainTrade.messagingFee;
this.priceImpact = crossChainTrade.priceImpact;
this.toTokenAmountMin = crossChainTrade.toTokenAmountMin;
this.fromTokenAddress = this.from.address.toLowerCase();
this.fromWithoutFee = (0, get_from_without_fee_1.getFromWithoutFee)(this.from, this.feeInfo.rubicProxy?.platformFee?.percent);
}
async getContractParams(options) {
const { data, value: providerValue, to } = await this.setTransactionConfig(false, options?.useCacheData || false, options?.receiverAddress);
try {
const bridgeData = proxy_cross_chain_evm_trade_1.ProxyCrossChainEvmTrade.getBridgeData({ ...options }, {
walletAddress: this.walletAddress,
fromTokenAmount: this.from,
toTokenAmount: this.to,
toAddress: undefined,
srcChainTrade: null,
providerAddress: this.providerAddress,
type: `native:${this.type}`,
fromAddress: this.walletAddress
});
const extraNativeFee = this.from.isNative
? new bignumber_js_1.default(providerValue).minus(this.fromWithoutFee.stringWeiAmount).toFixed()
: new bignumber_js_1.default(providerValue).toFixed();
const providerData = await proxy_cross_chain_evm_trade_1.ProxyCrossChainEvmTrade.getGenericProviderData(to, data, this.fromBlockchain, to, extraNativeFee);
const methodArguments = [bridgeData, providerData];
const value = this.getSwapValue(providerValue);
const transactionConfiguration = evm_web3_pure_1.EvmWeb3Pure.encodeMethodCall(rubic_proxy_contract_address_1.rubicProxyContractAddress[this.from.blockchain].router, evm_common_cross_chain_abi_1.evmCommonCrossChainAbi, this.methodName, methodArguments, value);
const sendingToken = this.from.isNative ? [] : [this.from.address];
const sendingAmount = this.from.isNative ? [] : [this.from.stringWeiAmount];
return {
contractAddress: rubic_proxy_contract_address_1.rubicProxyContractAddress[this.from.blockchain].gateway,
contractAbi: gateway_rubic_cross_chain_abi_1.gatewayRubicCrossChainAbi,
methodName: 'startViaRubic',
methodArguments: [sendingToken, sendingAmount, transactionConfiguration.data],
value
};
}
catch (err) {
console.log(err?.message);
throw err;
}
}
async getTransactionConfigAndAmount(_receiverAddress) {
const fromBlockchain = this.from.blockchain;
const fromTokenSymbol = stargate_v2_token_address_1.stargateV2TokenAddress[fromBlockchain][this.fromTokenAddress];
const contractAddress = stargate_v2_contract_address_1.stargateV2ContractAddress?.[fromBlockchain]?.[fromTokenSymbol];
if (!contractAddress) {
throw new errors_1.RubicSdkError();
}
const nativeFee = new bignumber_js_1.default(this.messagingFee.nativeFee);
const value = this.from.isNative
? new bignumber_js_1.default(this.fromWithoutFee.stringWeiAmount).plus(nativeFee).toFixed()
: this.messagingFee.nativeFee;
const calldata = await evm_web3_pure_1.EvmWeb3Pure.encodeMethodCall(contractAddress, stargate_v2_pool_abi_1.stargateV2SendTokenAbi, 'sendToken', [this.stargateV2SendParams, this.messagingFee, this.walletAddress], value);
return {
config: calldata,
amount: this.to.stringWeiAmount
};
}
async swapDirect(options = {}) {
this.checkWalletConnected();
await this.checkTradeErrors();
await this.checkAllowanceAndApprove(options);
const { onConfirm, gasLimit, gasPriceOptions } = options;
let transactionHash;
const onTransactionHash = (hash) => {
if (onConfirm) {
onConfirm(hash);
}
transactionHash = hash;
};
try {
const { data, value, to } = await this.setTransactionConfig(false, options?.useCacheData || false, options?.receiverAddress);
await this.web3Private.trySendTransaction(to, {
data,
value,
onTransactionHash,
gas: gasLimit,
gasPriceOptions
});
return transactionHash;
}
catch (err) {
if (err instanceof errors_1.FailedToCheckForTransactionReceiptError) {
return transactionHash;
}
throw err;
}
}
getTradeAmountRatio(fromUsd) {
return fromUsd.dividedBy(this.to.tokenAmount);
}
getTradeInfo() {
return {
estimatedGas: this.estimatedGas,
feeInfo: this.feeInfo,
priceImpact: this.priceImpact,
slippage: this.slippageTolerance * 100,
routePath: this.routePath
};
}
}
exports.StargateV2CrossChainTrade = StargateV2CrossChainTrade;
//# sourceMappingURL=stargate-v2-cross-chain-trade.js.map