Skip to content

Commit

Permalink
Merge pull request #183 from Cryptorubic/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ottebrut authored Sep 6, 2022
2 parents b568a98 + ef4f60d commit 3184e47
Show file tree
Hide file tree
Showing 34 changed files with 319 additions and 278 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": "2.14.0",
"version": "2.15.0",
"description": "Simplify dApp creation",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -85,7 +85,7 @@
"compression-webpack-plugin": "^9.2.0",
"crypto-browserify": "^3.12.0",
"delay": "^5.0.0",
"eslint": "^8.23.0",
"eslint": "8.22.0",
"eslint-config-airbnb-typescript": "^12.3.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.22.1",
Expand Down
25 changes: 19 additions & 6 deletions src/common/decorators/cache.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,17 @@ function modifyMethodCacheDescriptor<T>(

if (cacheConfig.conditionalCache) {
if (result instanceof Promise) {
const handledPromise = result.then((resolved: ConditionalResult<T>) => {
if (resolved.notSave) {
const handledPromise = result
.then((resolved: ConditionalResult<T>) => {
if (resolved.notSave) {
instanceStore.delete(key);
}
return resolved.value;
})
.catch(err => {
instanceStore.delete(key);
}
return resolved.value;
}) as unknown as T;
throw err;
}) as T;
saveResult(instanceStore, key, handledPromise, cacheConfig.maxAge);
return handledPromise;
}
Expand All @@ -97,6 +102,14 @@ function modifyMethodCacheDescriptor<T>(
return result.value;
}

if (result instanceof Promise) {
const handledPromise = result.catch(err => {
instanceStore.delete(key);
throw err;
}) as T;
saveResult(instanceStore, key, handledPromise, cacheConfig.maxAge);
return handledPromise;
}
saveResult(instanceStore, key, result as T, cacheConfig.maxAge);
return result;
} as unknown as T;
Expand Down Expand Up @@ -156,7 +169,7 @@ export function Cache<T>(
}

/**
* Decorated function should returns {@link ConditionalResult}.
* Decorated function should return {@link ConditionalResult}.
* You have to check types by yourself {@see https://github.com/microsoft/TypeScript/issues/4881}
*/
export function PConditionalCache<T>(
Expand Down
1 change: 0 additions & 1 deletion src/common/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export { NotSupportedBlockchain } from './swap/not-supported-blockchain';
export { UnnecessaryApproveError } from 'src/common/errors/swap/unnecessary-approve.error';
export { WalletNotConnectedError } from './swap/wallet-not-connected.error';
export { WrongNetworkError } from './swap/wrong-network.error';
export { WrongChainIdError } from './provider/wrong-chain-id.error';
export { CrossChainIsUnavailableError } from './cross-chain/cross-chain-is-unavailable.error';
export { InsufficientFundsGasPriceValueError } from './cross-chain/insufficient-funds-gas-price-value.error';
export { MaxGasPriceOverflowError } from './cross-chain/max-gas-price-overflow.error';
Expand Down
11 changes: 0 additions & 11 deletions src/common/errors/provider/wrong-chain-id.error.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/core/blockchain/constants/healthcheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const HEALTHCHECK = {
expected: 'USDT'
},
[BLOCKCHAIN_NAME.TELOS]: {
contractAddress: '0x4988a896b1227218e4A686fdE5EabdcAbd91571f',
contractAddress: '0xefaeee334f0fd1712f9a8cc375f427d9cdd40d73',
contractAbi: ERC20_TOKEN_ABI,
method: 'symbol',
expected: 'USDT'
Expand Down
2 changes: 0 additions & 2 deletions src/core/blockchain/models/wallet-connection-configuration.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { BlockchainName } from '@rsdk-core/blockchain/models/blockchain-name';
import Web3 from 'web3';

export interface WalletConnectionConfiguration {
web3: Web3;
address: string;
blockchainName: BlockchainName;
}
37 changes: 3 additions & 34 deletions src/core/blockchain/web3-private/web3-private-factory.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { WrongChainIdError } from '@rsdk-common/errors/provider/wrong-chain-id.error';
import { RubicSdkError } from '@rsdk-common/errors/rubic-sdk.error';
import { BlockchainsInfo } from '@rsdk-core/blockchain/blockchains-info';
import { BlockchainName } from '@rsdk-core/blockchain/models/blockchain-name';
import { WalletConnectionConfiguration } from '@rsdk-core/blockchain/models/wallet-connection-configuration';
import { Web3Private } from '@rsdk-core/blockchain/web3-private/web3-private';
import { WalletProvider } from '@rsdk-core/sdk/models/configuration';
import BigNumber from 'bignumber.js';
import Web3 from 'web3';
import { provider } from 'web3-core';

Expand All @@ -14,17 +10,14 @@ export class Web3PrivateFactory {

private address: string | undefined;

private blockchainName: BlockchainName | undefined;

public static async createWeb3Private(walletProvider?: WalletProvider): Promise<Web3Private> {
if (!walletProvider) {
return Web3PrivateFactory.createWeb3PrivateEmptyProxy();
}

const web3PrivateFactory = new Web3PrivateFactory(
walletProvider.core,
walletProvider.address,
new BigNumber(walletProvider.chainId).toNumber()
walletProvider.address
);
return web3PrivateFactory.createWeb3Private();
}
Expand All @@ -51,15 +44,10 @@ export class Web3PrivateFactory {
);
}

constructor(
private readonly core: provider | Web3,
private readonly walletAddress: string,
private readonly chainId: number
) {}
constructor(private readonly core: provider | Web3, private readonly walletAddress: string) {}

public async createWeb3Private(): Promise<Web3Private> {
this.createWeb3();
await this.parseChainId();
this.parseAddress();
return this.createWeb3PrivateInstance();
}
Expand All @@ -72,24 +60,6 @@ export class Web3PrivateFactory {
this.web3 = web3 as Web3;
}

private async parseChainId(): Promise<void | never> {
if (!this.web3) {
throw new RubicSdkError('Web3 is not initialized');
}

const realChainId = await this.web3.eth.getChainId();
if (!new BigNumber(realChainId).eq(this.chainId)) {
throw new WrongChainIdError();
}

const blockchainName = BlockchainsInfo.getBlockchainById(realChainId);
if (!blockchainName) {
throw new WrongChainIdError();
}

this.blockchainName = blockchainName.name;
}

private parseAddress(): void {
if (!this.web3) {
throw new RubicSdkError('Web3 is not initialized');
Expand All @@ -101,8 +71,7 @@ export class Web3PrivateFactory {
private createWeb3PrivateInstance(): Web3Private {
const walletConfiguration: WalletConnectionConfiguration = {
web3: this.web3!!,
address: this.address!!,
blockchainName: this.blockchainName!!
address: this.address!!
};

return new Web3Private(walletConfiguration);
Expand Down
63 changes: 31 additions & 32 deletions src/core/blockchain/web3-private/web3-private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { TransactionRevertedError } from '@rsdk-common/errors/blockchain/transac
import { WalletConnectionConfiguration } from '@rsdk-core/blockchain/models/wallet-connection-configuration';
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 { LowSlippageError } from 'src/common';
import { BlockchainName, BlockchainsInfo, Web3Pure } from 'src/core';
import { LowSlippageError, WrongNetworkError } from 'src/common';
import { parseError } from 'src/common/utils/errors';
import { TransactionConfig } from 'web3-core';

Expand All @@ -35,35 +35,6 @@ export class Web3Private {
return bnAmount.toFixed(0);
}

private readonly APPROVE_GAS_LIMIT = 60_000;

/**
* Instance of web3, initialized with ethereum wallet, e.g. Metamask, WalletConnect.
*/
private readonly web3: Web3;

/**
* Current wallet provider address.
*/
public get address(): string {
return this.walletConnectionConfiguration.address;
}

/**
* Current wallet blockchain name.
*/
public get blockchainName(): string {
return this.walletConnectionConfiguration.blockchainName;
}

/**
* @param walletConnectionConfiguration Provider that implements {@link WalletConnectionConfiguration} interface.
* The provider must contain an instance of web3, initialized with ethereum wallet, e.g. Metamask, WalletConnect.
*/
constructor(private readonly walletConnectionConfiguration: WalletConnectionConfiguration) {
this.web3 = walletConnectionConfiguration.web3;
}

/**
* Parses web3 error by its code or message.
* @param err Web3 error to parse.
Expand Down Expand Up @@ -93,6 +64,34 @@ export class Web3Private {
return parseError(err);
}

/**
* Instance of web3, initialized with ethereum wallet, e.g. Metamask, WalletConnect.
*/
private readonly web3: Web3;

/**
* Current wallet provider address.
*/
public get address(): string {
return this.walletConnectionConfiguration.address;
}

/**
* @param walletConnectionConfiguration Provider that implements {@link WalletConnectionConfiguration} interface.
* The provider must contain an instance of web3, initialized with ethereum wallet, e.g. Metamask, WalletConnect.
*/
constructor(private readonly walletConnectionConfiguration: WalletConnectionConfiguration) {
this.web3 = walletConnectionConfiguration.web3;
}

public async checkBlockchainCorrect(blockchainName: BlockchainName): Promise<void | never> {
const userChainId = await this.web3.eth.getChainId();
const userBlockchain = BlockchainsInfo.getBlockchainById(userChainId);
if (userBlockchain?.name !== blockchainName) {
throw new WrongNetworkError();
}
}

/**
* Sends ERC-20 tokens and resolves the promise when the transaction is included in the block.
* @param contractAddress Address of the smart-contract corresponding to the token.
Expand Down Expand Up @@ -438,7 +437,7 @@ export class Web3Private {
const test = ignoreCallErrors.some(err =>
error?.message?.toLowerCase().includes(err.toLowerCase())
);
console.log(test);
console.debug(test);
return test;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { RpcProvider } from 'src/core';
import { MarkRequired } from 'ts-essentials';

export type RpcListProvider = MarkRequired<Exclude<RpcProvider, 'mainRpc' | 'spareRpc'>, 'rpcList'>;
Loading

0 comments on commit 3184e47

Please sign in to comment.