Skip to content

Commit

Permalink
Merge pull request #102 from Cryptorubic/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ottebrut authored Jun 21, 2022
2 parents 7dbab18 + 8394d82 commit b5b4b6a
Show file tree
Hide file tree
Showing 87 changed files with 1,430 additions and 746 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/ci-docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Documentation generation

on:
push:
branches:
- master

jobs:
lint:
name: Generate docs and push to github pages
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Read .nvmrc
run: echo ::set-output name=NVMRC::$(cat .nvmrc)
id: nvm

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '${{ steps.nvm.outputs.NVMRC }}'

- name: Set up yarn
run: npm install --global yarn

- name: Set up dependencies
run: yarn

- name: Generate docs
run: yarn docs

- name: Deploy Github Page
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./docs
6 changes: 0 additions & 6 deletions .github/workflows/ci-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,3 @@ jobs:

- name: Build bundle
run: yarn build

# - name: Set up env.js
# run: node ./scripts/build-env.js --eth ${{ secrets.ETH_RPC }} --bsc ${{ secrets.BSC_RPC }} --polygon ${{ secrets.POLYGON_RPC }}
#
# - name: Run tests
# run: yarn test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
/dist/
/cache/
/scripts/node.log
/docker-compose.yml

/docs/
651 changes: 354 additions & 297 deletions README.md

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: "3.7"

services:
eth-node:
image: ottebrut/rubic-sdk-tools:eth-node-1.2
container_name: eth-node
ports:
- 8545:8545
- 1545:1545
polygon-node:
image: ottebrut/rubic-sdk-tools:polygon-node-1.2
container_name: polygon-node
ports:
- 8547:8545
- 1547:1545

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rubic-sdk",
"version": "2.0.0",
"version": "2.0.1",
"description": "Simplify dApp creation",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -55,7 +55,8 @@
"lint": "eslint src __tests__",
"test": "cd ./scripts && bash test-runner.sh",
"build:publish": "yarn compile && yarn build && npm publish --access public",
"analyze": "webpack --profile --json > stats.json && webpack-bundle-analyzer stats.json"
"analyze": "webpack --profile --json > stats.json && webpack-bundle-analyzer stats.json",
"docs": "typedoc"
},
"dependencies": {
"assert": "^2.0.0",
Expand Down Expand Up @@ -96,7 +97,7 @@
"ts-loader": "^9.3.0",
"tsconfig-paths-webpack-plugin": "^3.5.2",
"tscpaths": "^0.0.9",
"typedoc": "^0.22.15",
"typedoc": "^0.22.17",
"typescript": "^4.7.2",
"webpack": "^5.65.0",
"webpack-bundle-analyzer": "^4.5.0",
Expand Down
3 changes: 3 additions & 0 deletions src/common/decorators/cache.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ function CacheBuilder(cacheConfig: CacheConfig): DecoratorSignature {
};
}

/**
* Decorator, used to cache calculated result of functions.
*/
export function Cache(cacheConfigOrTarget: CacheConfig): DecoratorSignature;
export function Cache<T>(
cacheConfigOrTarget: Object,
Expand Down
11 changes: 11 additions & 0 deletions src/common/decorators/models/cache-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
/**
* Configuration, used for cache decorator.
*/
export interface CacheConfig {
/**
* Amount of time, during which cached result is relevant.
*/
maxAge?: number;

/**
* If true, then results must be of type {@link ConditionalResult},
* defining whether to cache calculated result.
*/
conditionalCache?: boolean;
}
4 changes: 4 additions & 0 deletions src/common/decorators/models/conditional-result.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* Used for {@link Cache} decorator.
* User `notSave` field to define whether to cache calculated result.
*/
export type ConditionalResult<T> = {
notSave: boolean;
value: T;
Expand Down
4 changes: 4 additions & 0 deletions src/common/errors/blockchain/healthcheck.error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { RubicSdkError } from '@common/errors/rubic-sdk.error';

/**
* @internal
* Thrown, if rpc provider has not passed healthcheck.
*/
export class HealthcheckError extends RubicSdkError {}
3 changes: 3 additions & 0 deletions src/common/errors/blockchain/low-gas.error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { RubicSdkError } from '@common/errors/rubic-sdk.error';

/**
* Thrown, when gas price is too low.
*/
export class LowGasError extends RubicSdkError {}
3 changes: 3 additions & 0 deletions src/common/errors/blockchain/transaction-reverted.error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { RubicSdkError } from '@common/errors/rubic-sdk.error';

/**
* Thrown, if transaction was reverted without specified error.
*/
export class TransactionRevertedError extends RubicSdkError {}
3 changes: 3 additions & 0 deletions src/common/errors/blockchain/user-reject.error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { RubicSdkError } from '@common/errors/rubic-sdk.error';

/**
* Thrown, when user cancels transaction.
*/
export class UserRejectError extends RubicSdkError {}
3 changes: 3 additions & 0 deletions src/common/errors/blockchain/web3.error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* Type of errors, thrown by web3 methods.
*/
export interface Web3Error extends Error {
code: number;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { RubicSdkError } from '@common/errors/rubic-sdk.error';

/**
* Thrown, when cross chain contracts are on pause or there is not enough crypto balance.
*/
export class CrossChainIsUnavailableError extends RubicSdkError {}
9 changes: 0 additions & 9 deletions src/common/errors/cross-chain/cross-chain-max-amount-error.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/common/errors/cross-chain/cross-chain-min-amount-error.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { RubicSdkError } from '@common/errors/rubic-sdk.error';

/**
* Thrown, when user doesn't have enough native token balance for gas fee plus `value`.
*/
export class InsufficientFundsGasPriceValueError extends RubicSdkError {}
4 changes: 4 additions & 0 deletions src/common/errors/cross-chain/max-gas-price-overflow.error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { RubicSdkError } from '@common/errors/rubic-sdk.error';

/**
* Thrown, when current gas price is higher, than max gas price on cross chain contract
* in target network.
*/
export class MaxGasPriceOverflowError extends RubicSdkError {}
2 changes: 1 addition & 1 deletion src/common/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export { InsufficientLiquidityError } from './swap/insufficient-liquidity.error'
export { LowSlippageError } from './swap/low-slippage.error';
export { LowSlippageDeflationaryTokenError } from './swap/low-slippage-deflationary-token.error';
export { NotSupportedBlockchain } from './swap/not-supported-blockchain';
export { UnnecessaryApprove } from './swap/unnecessary-approve';
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';
Expand Down
3 changes: 3 additions & 0 deletions src/common/errors/provider/wrong-chain-id.error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { RubicSdkError } from '@common/errors/rubic-sdk.error';

/**
* Thrown, when provided chain id does not match real one, set in wallet.
*/
export class WrongChainIdError extends RubicSdkError {}
3 changes: 3 additions & 0 deletions src/common/errors/rubic-sdk.error.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/**
* Base class for all errors that can be thrown in sdk.
*/
export class RubicSdkError extends Error {}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { RubicSdkError } from '@common/errors/rubic-sdk.error';

/**
* @internal
* Thrown, when transaction is passed, but web3 cannot retrieve transaction receipt.
*/
export class FailedToCheckForTransactionReceiptError extends RubicSdkError {}
3 changes: 3 additions & 0 deletions src/common/errors/swap/insufficient-funds-oneinch.error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { RubicSdkError } from '@common/errors/rubic-sdk.error';

/**
* Thrown by 1inch, if user doesn't have enough balance.
*/
export class InsufficientFundsOneinchError extends RubicSdkError {}
3 changes: 3 additions & 0 deletions src/common/errors/swap/insufficient-funds.error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { RubicSdkError } from '@common/errors/rubic-sdk.error';

/**
* Thrown, when user doesn't have enough balance.
*/
export class InsufficientFundsError extends RubicSdkError {}
3 changes: 3 additions & 0 deletions src/common/errors/swap/insufficient-liquidity.error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { RubicSdkError } from '@common/errors/rubic-sdk.error';

/**
* Thrown, when tokens' pair doesn't have enough liquidity.
*/
export class InsufficientLiquidityError extends RubicSdkError {}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { RubicSdkError } from '@common/errors/rubic-sdk.error';

/**
* Thrown, when user is selling deflationary token with too low slippage.
*/
export class LowSlippageDeflationaryTokenError extends RubicSdkError {}
3 changes: 3 additions & 0 deletions src/common/errors/swap/low-slippage.error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { RubicSdkError } from '@common/errors/rubic-sdk.error';

/**
* Thrown, when token cannot be swapped with provided options.
*/
export class LowSlippageError extends RubicSdkError {}
4 changes: 4 additions & 0 deletions src/common/errors/swap/not-supported-blockchain.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { RubicSdkError } from '@common/errors/rubic-sdk.error';

/**
* @internal
* Thrown, when provider does not support provided blockchain.
*/
export class NotSupportedBlockchain extends RubicSdkError {}
6 changes: 6 additions & 0 deletions src/common/errors/swap/unnecessary-approve.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { RubicSdkError } from '@common/errors/rubic-sdk.error';

/**
* Thrown, when approve method is called, but there is enough allowance.
*/
export class UnnecessaryApproveError extends RubicSdkError {}
3 changes: 0 additions & 3 deletions src/common/errors/swap/unnecessary-approve.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/common/errors/swap/wallet-not-connected.error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { RubicSdkError } from '@common/errors/rubic-sdk.error';

/**
* Thrown, when method, which requires connected wallet, is called without
* wallet being connected.
*/
export class WalletNotConnectedError extends RubicSdkError {}
4 changes: 4 additions & 0 deletions src/common/errors/swap/wrong-network.error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { RubicSdkError } from '@common/errors/rubic-sdk.error';

/**
* Thrown during swap, if user's selected network does not match source blockchain
* in swap parameters.
*/
export class WrongNetworkError extends RubicSdkError {}
3 changes: 3 additions & 0 deletions src/common/http/coingecko-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ type SupportedBlockchain = typeof supportedBlockchains[number];

const API_BASE_URL = 'https://api.coingecko.com/api/v3/';

/**
* Works with coingecko api to get tokens prices in usd.
*/
export class CoingeckoApi {
private static isSupportedBlockchain(
blockchain: BlockchainName
Expand Down
15 changes: 8 additions & 7 deletions src/common/http/gas-price-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import BigNumber from 'bignumber.js';
import { HttpClient } from '@common/models/http-client';
import { Web3Pure } from '@core/blockchain/web3-pure/web3-pure';

/**
* Uses different api or web3 to retrieve current gas price.
*/
export class GasPriceApi {
/**
* Gas price request interval in seconds.
Expand All @@ -15,9 +18,8 @@ export class GasPriceApi {
constructor(private readonly httpClient: HttpClient) {}

/**
* Gas price in Wei for selected blockchain.
* Gets gas price in Wei for selected blockchain.
* @param blockchain Blockchain to get gas price from.
* @return Promise<BigNumber> Average gas price in Wei.
*/
public getGasPrice(blockchain: BlockchainName): Promise<string> {
if (blockchain === BLOCKCHAIN_NAME.ETHEREUM) {
Expand All @@ -27,17 +29,16 @@ export class GasPriceApi {
}

/**
* Gas price in Eth units for selected blockchain.
* Gets gas price in Eth units for selected blockchain.
* @param blockchain Blockchain to get gas price from.
* @return Promise<BigNumber> Average gas price in Eth units.
*/
public async getGasPriceInEthUnits(blockchain: BlockchainName): Promise<BigNumber> {
return Web3Pure.fromWei(await this.getGasPrice(blockchain));
}

/**
* Gets Ethereum gas price from different APIs, sorted by priority.
* @return Promise<BigNumber> Average gas price in Wei.
* @returns Average gas price in Wei.
*/
@Cache({
maxAge: GasPriceApi.requestInterval
Expand Down Expand Up @@ -66,8 +67,8 @@ export class GasPriceApi {
}

/**
* Gets Avalanche gas price.
* @return Promise<BigNumber> Average gas price in Wei.
* Gets gas price from web3.
* @returns Average gas price in Wei.
*/
@Cache({
maxAge: GasPriceApi.requestInterval
Expand Down
3 changes: 3 additions & 0 deletions src/common/models/http-client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* Http client, used to get and send http requests.
*/
export interface HttpClient {
post<ResponseBody>(url: string, body: Object): Promise<ResponseBody>;
get<ResponseBody>(
Expand Down
3 changes: 3 additions & 0 deletions src/common/utils/blockchain.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* Compares provided addresses case insensitive.
*/
export function compareAddresses(address0: string, address1: string): boolean {
return address0.toLowerCase() === address1.toLowerCase();
}
Loading

0 comments on commit b5b4b6a

Please sign in to comment.