Skip to content

Commit

Permalink
Hide ton keys (#735)
Browse files Browse the repository at this point in the history
  • Loading branch information
PseudoElement authored Oct 18, 2024
2 parents 00b2424 + a11729b commit 4f49865
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 97 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rubic-sdk",
"version": "5.40.2",
"version": "5.40.3",
"description": "Simplify dApp creation",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -84,7 +84,7 @@
"@solana/web3.js": "^1.89.1",
"@solflare-wallet/utl-sdk": "^1.4.0",
"@ston-fi/api": "^0.12.1",
"@ston-fi/sdk": "2.0.0-rc.7",
"@ston-fi/sdk": "2.0.0-rc.8",
"@ton/core": "^0.52.0",
"@ton/crypto": "^3.3.0",
"@ton/ton": "^15.0.0",
Expand Down
2 changes: 0 additions & 2 deletions src/core/blockchain/services/constants/ton-constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export const TONAPI_API_KEY =
'Bearer AHSDXSKDRDYQSIYAAAAPKX74RH4QUUEZGUDSR4LJI3YA5HXWCXKC34236WTHJVNQCJTT53Q';
export const TONAPI_API_URL = 'https://tonapi.io/v2';
export const TONCENTER_API_V3_URL = 'https://toncenter.com/api/v3';
42 changes: 23 additions & 19 deletions src/core/blockchain/services/ton/tonapi-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@ import {
TonApiTxDataByBocResp
} from '../../models/ton/tonapi-types';
import { Web3PrimitiveType } from '../../models/web3-primitive-type';
import { TONAPI_API_KEY, TONAPI_API_URL, TONCENTER_API_V3_URL } from '../constants/ton-constants';
import { TONCENTER_API_V3_URL } from '../constants/ton-constants';

export class TonApiService {
private readonly xApiUrl = 'https://x-api.rubic.exchange/tonapi';

private readonly xApiKey = 'sndfje3u4b3fnNSDNFUSDNVSunw345842hrnfd3b4nt4';

/**
*
* @param walletAddress in any format: raw or friendly
*/
public async fetchWalletSeqno(walletAddress: string): Promise<number> {
const res = await Injector.httpClient.get<TonApiResp<TonApiSeqnoResp>>(
`${TONAPI_API_URL}/wallet/${walletAddress}/seqno`,
{ headers: { Authorization: TONAPI_API_KEY } }
`${this.xApiUrl}/v2/wallet/${walletAddress}/seqno`,
{ headers: { apikey: this.xApiKey } }
);
if ('error' in res) {
throw new RubicSdkError(`[TonApiService] Error in fetchWalletSeqno - ${res.error}`);
Expand All @@ -40,8 +44,8 @@ export class TonApiService {

public async fetchTxInfo(txHash: string): Promise<TonApiTxDataByBocResp> {
const res = await Injector.httpClient.get<TonApiResp<TonApiTxDataByBocResp>>(
`${TONAPI_API_URL}/blockchain/messages/${txHash}/transaction`,
{ headers: { Authorization: TONAPI_API_KEY } }
`${this.xApiUrl}/v2/blockchain/messages/${txHash}/transaction`,
{ headers: { apikey: this.xApiKey } }
);
if ('error' in res) {
throw new RubicSdkError(`[TonApiService] Error in fetchTxInfo - ${res.error}`);
Expand All @@ -53,8 +57,8 @@ export class TonApiService {
public async checkIsTxCompleted(txHash: string): Promise<boolean> {
try {
const res = await Injector.httpClient.get<TonApiResp<TonApiStatusByBocResp>>(
`${TONAPI_API_URL}/events/${txHash}`,
{ headers: { Authorization: TONAPI_API_KEY } }
`${this.xApiUrl}/v2/events/${txHash}`,
{ headers: { apikey: this.xApiKey } }
);
if ('error' in res) {
throw new RubicSdkError(
Expand All @@ -70,8 +74,8 @@ export class TonApiService {

public async healthcheck(): Promise<boolean> {
const res = await Injector.httpClient.get<TonApiResp<TonApiHealthcheckResp>>(
`${TONAPI_API_URL}/status`,
{ headers: { Authorization: TONAPI_API_KEY } }
`${this.xApiUrl}/v2/status`,
{ headers: { apikey: this.xApiKey } }
);
if ('error' in res || !res.rest_online) {
return false;
Expand Down Expand Up @@ -113,8 +117,8 @@ export class TonApiService {
argsParam += '?' + methodArgs.map(param => `args=${param}`).join('&');
}
const res = await Injector.httpClient.get<TonApiResp<TonApiCallContractCommonResp<T>>>(
`${TONAPI_API_URL}/blockchain/accounts/${walletAddress}/methods/${methodName}${argsParam}`,
{ headers: { Authorization: TONAPI_API_KEY } }
`${this.xApiUrl}/v2/blockchain/accounts/${walletAddress}/methods/${methodName}${argsParam}`,
{ headers: { apikey: this.xApiKey } }
);
if ('error' in res) {
throw new RubicSdkError(`[TonApiService] Error in callContractMethod - ${res.error}`);
Expand All @@ -125,8 +129,8 @@ export class TonApiService {

public async fetchTokenInfo(tokenAddress: string): Promise<TonApiTokenInfoResp['metadata']> {
const res = await Injector.httpClient.get<TonApiResp<TonApiTokenInfoResp>>(
`${TONAPI_API_URL}/jettons/${tokenAddress}`,
{ headers: { Authorization: TONAPI_API_KEY } }
`${this.xApiUrl}/v2/jettons/${tokenAddress}`,
{ headers: { apikey: this.xApiKey } }
);
if ('error' in res) {
throw new RubicSdkError(`[TonApiService] Error in fetchTokenInfo - ${res.error}`);
Expand All @@ -145,8 +149,8 @@ export class TonApiService {
tokenAddress: string
): Promise<TonApiTokenInfoForWalletResp> {
const res = await Injector.httpClient.get<TonApiResp<TonApiTokenInfoForWalletResp>>(
`${TONAPI_API_URL}/accounts/${walletAddress}/jettons/${tokenAddress}`,
{ headers: { Authorization: TONAPI_API_KEY } }
`${this.xApiUrl}/v2/accounts/${walletAddress}/jettons/${tokenAddress}`,
{ headers: { apikey: this.xApiKey } }
);
if ('error' in res) {
throw new RubicSdkError(
Expand All @@ -162,8 +166,8 @@ export class TonApiService {
): Promise<TonApiAllNonNullableTokenInfoForWalletResp['balances']> {
const res = await Injector.httpClient.get<
TonApiResp<TonApiAllNonNullableTokenInfoForWalletResp>
>(`${TONAPI_API_URL}/accounts/${walletAddress}/jettons`, {
headers: { Authorization: TONAPI_API_KEY }
>(`${this.xApiUrl}/v2/accounts/${walletAddress}/jettons`, {
headers: { apikey: this.xApiKey }
});
if ('error' in res) {
throw new RubicSdkError(
Expand All @@ -179,8 +183,8 @@ export class TonApiService {
*/
public async fetchAccountInfo(walletAddress: string): Promise<TonApiAccountInfoResp> {
const res = await Injector.httpClient.get<TonApiResp<TonApiAccountInfoResp>>(
`${TONAPI_API_URL}/accounts/${walletAddress}`,
{ headers: { Authorization: TONAPI_API_KEY } }
`${this.xApiUrl}/v2/accounts/${walletAddress}`,
{ headers: { apikey: this.xApiKey } }
);
if ('error' in res) {
throw new RubicSdkError(`[TonApiService] Error in fetchAccountInfo - ${res.error}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { TonClient } from '@ton/ton';

export class TonClientInstance {
private static instance: TonClient | null = null;

private constructor() {}

public static getInstance(): TonClient {
if (!this.instance) {
this.instance = new TonClient({
endpoint: 'https://x-api.rubic.exchange/toncenter/api/v2/jsonRPC',
apiKey: 'sndfje3u4b3fnNSDNFUSDNVSunw345842hrnfd3b4nt4'
});
}

return this.instance;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { TonClient } from '@ton/ton';
import { TonConnectUI } from '@tonconnect/ui';
import { RubicSdkError, UserRejectError } from 'src/common/errors';
import { parseError } from 'src/common/utils/errors';
Expand All @@ -18,12 +17,6 @@ export class TonWeb3Private extends Web3Private {

private readonly tonConnectUI: TonConnectUI;

private readonly _tonClient: TonClient;

public get tonClient(): TonClient {
return this._tonClient;
}

public getBlockchainName(): Promise<BlockchainName> {
return Promise.resolve(BLOCKCHAIN_NAME.TON);
}
Expand Down Expand Up @@ -72,9 +65,5 @@ export class TonWeb3Private extends Web3Private {
constructor(tonProviderCore: TonWalletProviderCore) {
super(tonProviderCore.address);
this.tonConnectUI = tonProviderCore.core;
this._tonClient = new TonClient({
endpoint: 'https://toncenter.com/api/v2/jsonRPC',
apiKey: '44176ed3735504c6fb1ed3b91715ba5272cdd2bbb304f78d1ae6de6aed47d284'
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { RubicSdkError } from 'src/common/errors';
import { compareAddresses } from 'src/common/utils/blockchain';
import { staticImplements } from 'src/common/utils/decorators';
import { TonApiParseAddressResp, TonApiResp } from 'src/core/blockchain/models/ton/tonapi-types';
import {
TONAPI_API_KEY,
TONAPI_API_URL
} from 'src/core/blockchain/services/constants/ton-constants';
import { TypedWeb3Pure } from 'src/core/blockchain/web3-pure/typed-web3-pure/typed-web3-pure';
import { Injector } from 'src/core/injector/injector';

@staticImplements<TypedWeb3Pure>()
export class TonWeb3Pure {
private static readonly xApiUrl = 'https://x-api.rubic.exchange/tonapi';

private static readonly xApiKey = 'sndfje3u4b3fnNSDNFUSDNVSunw345842hrnfd3b4nt4';

public static readonly EMPTY_ADDRESS = '0x0000000000000000000000000000000000000000';

public static get nativeTokenAddress(): string {
Expand Down Expand Up @@ -51,10 +51,10 @@ export class TonWeb3Pure {
walletAddress: string
): Promise<TonApiParseAddressResp> {
const res = await Injector.httpClient.get<TonApiResp<TonApiParseAddressResp>>(
`${TONAPI_API_URL}/address/${walletAddress}/parse`,
`${this.xApiUrl}/v2/address/${walletAddress}/parse`,
{
headers: {
Authorization: TONAPI_API_KEY
apikey: this.xApiKey
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { LowSlippageError, RubicSdkError } from 'src/common/errors';
import { PriceToken, PriceTokenAmount, Token } from 'src/common/tokens';
import { BLOCKCHAIN_NAME } from 'src/core/blockchain/models/blockchain-name';
import { CHAIN_TYPE } from 'src/core/blockchain/models/chain-type';
import { TonClientInstance } from 'src/core/blockchain/web3-private-service/web3-private/ton-web3-private/ton-client/ton-client';
import { Injector } from 'src/core/injector/injector';
import { RubicStep } from 'src/features/cross-chain/calculation-manager/providers/common/models/rubicStep';

Expand All @@ -39,12 +40,7 @@ export class DedustSwapService {
}

constructor() {
this.tonClient =
Injector.web3PrivateService.getWeb3Private(CHAIN_TYPE.TON).tonClient ||
new TonClient({
endpoint: 'https://toncenter.com/api/v2/jsonRPC',
apiKey: '44176ed3735504c6fb1ed3b91715ba5272cdd2bbb304f78d1ae6de6aed47d284'
});
this.tonClient = TonClientInstance.getInstance();
this.factory = this.tonClient.open(Factory.createFromAddress(this.mainnetFactoryAddress));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,72 +1,72 @@
import { DEX, pTON } from '@ston-fi/sdk';
import { TonClient } from '@ton/ton';
import { PriceTokenAmount } from 'src/common/tokens';
import { CHAIN_TYPE } from 'src/core/blockchain/models/chain-type';
import { TonEncodedConfig } from 'src/core/blockchain/web3-private-service/web3-private/ton-web3-private/models/ton-types';
import { Injector } from 'src/core/injector/injector';
import { TonClientInstance } from 'src/core/blockchain/web3-private-service/web3-private/ton-web3-private/ton-client/ton-client';

import { STONFI_REFERRAL_ADDRESS } from '../constants/addresses';
import { StonfiTxParamsProvider } from '../models/stonfi-abstract';
import { convertTxParamsToTonConfig } from '../utils/convert-params-to-ton-config';

export class StonfiSwapServiceV2 implements StonfiTxParamsProvider {
private get tonClient(): TonClient {
return (
Injector.web3PrivateService.getWeb3Private(CHAIN_TYPE.TON).tonClient ||
new TonClient({
endpoint: 'https://toncenter.com/api/v2/jsonRPC',
apiKey: '44176ed3735504c6fb1ed3b91715ba5272cdd2bbb304f78d1ae6de6aed47d284'
})
);
return TonClientInstance.getInstance();
}

private readonly stonfiRouter = this.tonClient.open(
DEX.v2_1.Router.create('kQALh-JBBIKK7gr0o4AVf9JZnEsFndqO0qTCyT-D-yBsWk0v')
);

public async getTxParams(
from: PriceTokenAmount,
to: PriceTokenAmount,
walletAddress: string,
minAmountOutWei: string
): Promise<TonEncodedConfig> {
const stonfiRouter = this.tonClient.open(
DEX.v2_2.Router.create('kQALh-JBBIKK7gr0o4AVf9JZnEsFndqO0qTCyT-D-yBsWk0v')
);
const proxyTon = pTON.v2_1.create('kQACS30DNoUQ7NfApPvzh7eBmSZ9L4ygJ-lkNWtba8TQT-Px');

if (from.isNative) {
const txParams = await this.stonfiRouter.getSwapTonToJettonTxParams({
userWalletAddress: walletAddress,
proxyTon,
offerAmount: from.stringWeiAmount,
askJettonAddress: to.address,
minAskAmount: minAmountOutWei,
referralAddress: STONFI_REFERRAL_ADDRESS
});
try {
if (from.isNative) {
const txParams = await stonfiRouter.getSwapTonToJettonTxParams({
userWalletAddress: walletAddress,
proxyTon,
offerAmount: from.stringWeiAmount,
askJettonAddress: to.address,
minAskAmount: minAmountOutWei,
referralValue: 100,
referralAddress: STONFI_REFERRAL_ADDRESS
});

return convertTxParamsToTonConfig(txParams);
}
return convertTxParamsToTonConfig(txParams);
}

if (to.isNative) {
const txParams = await stonfiRouter.getSwapJettonToTonTxParams({
userWalletAddress: walletAddress,
offerJettonAddress: from.address,
offerAmount: from.stringWeiAmount,
proxyTon,
minAskAmount: minAmountOutWei,
referralValue: 100,
referralAddress: STONFI_REFERRAL_ADDRESS
});

if (to.isNative) {
const txParams = await this.stonfiRouter.getSwapJettonToTonTxParams({
return convertTxParamsToTonConfig(txParams);
}

const txParams = await stonfiRouter.getSwapJettonToJettonTxParams({
userWalletAddress: walletAddress,
offerJettonAddress: from.address,
offerAmount: from.stringWeiAmount,
proxyTon,
askJettonAddress: to.address,
minAskAmount: minAmountOutWei,
referralValue: 100,
referralAddress: STONFI_REFERRAL_ADDRESS
});

return convertTxParamsToTonConfig(txParams);
} catch (err) {
console.log('getTxParams_ERROR ==> ', err);
throw err;
}

const txParams = await this.stonfiRouter.getSwapJettonToJettonTxParams({
userWalletAddress: walletAddress,
offerJettonAddress: from.address,
offerAmount: from.stringWeiAmount,
askJettonAddress: to.address,
minAskAmount: minAmountOutWei,
referralAddress: STONFI_REFERRAL_ADDRESS
});

return convertTxParamsToTonConfig(txParams);
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
import { DEX, pTON } from '@ston-fi/sdk';
import { TonClient } from '@ton/ton';
import { PriceTokenAmount } from 'src/common/tokens';
import { CHAIN_TYPE } from 'src/core/blockchain/models/chain-type';
import { TonEncodedConfig } from 'src/core/blockchain/web3-private-service/web3-private/ton-web3-private/models/ton-types';
import { Injector } from 'src/core/injector/injector';
import { TonClientInstance } from 'src/core/blockchain/web3-private-service/web3-private/ton-web3-private/ton-client/ton-client';

import { STONFI_REFERRAL_ADDRESS } from '../constants/addresses';
import { StonfiTxParamsProvider } from '../models/stonfi-abstract';
import { convertTxParamsToTonConfig } from '../utils/convert-params-to-ton-config';

export class StonfiSwapService implements StonfiTxParamsProvider {
private get tonClient(): TonClient {
return (
Injector.web3PrivateService.getWeb3Private(CHAIN_TYPE.TON).tonClient ||
new TonClient({
endpoint: 'https://toncenter.com/api/v2/jsonRPC',
apiKey: '44176ed3735504c6fb1ed3b91715ba5272cdd2bbb304f78d1ae6de6aed47d284'
})
);
return TonClientInstance.getInstance();
}

private readonly stonfiRouter = this.tonClient.open(new DEX.v1.Router());
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2887,10 +2887,10 @@
decamelize-keys "2.0.1"
ofetch "1.3.4"

"@ston-fi/[email protected].7":
version "2.0.0-rc.7"
resolved "https://registry.yarnpkg.com/@ston-fi/sdk/-/sdk-2.0.0-rc.7.tgz#479bfd6ebc00cb749d755c00b6c3c2d7199a784b"
integrity sha512-5a04aX7FWq9Sm2/Ox2WayOpeoJ5VQH6Fnn/hiL8rW4wn7obwzAJgllnodwOQQNWw/z/Vb5BJHo2Ur5o7tymfXg==
"@ston-fi/[email protected].8":
version "2.0.0-rc.8"
resolved "https://registry.yarnpkg.com/@ston-fi/sdk/-/sdk-2.0.0-rc.8.tgz#c6b7260dfe29c27ec5e63f88af84ac24a730accd"
integrity sha512-9+9qBhdGzTVsC3iDHVQnrEFU7N7mdlGKbn57pHJrVfZiGm/55X5IPl4DKclbdtl9Gtjf9Kf8bBLweMEwzHntGw==

"@supercharge/promise-pool@^2.1.0":
version "2.4.0"
Expand Down

0 comments on commit 4f49865

Please sign in to comment.