Skip to content

Commit

Permalink
Use the blockchain provider instead of currencies provider in the ato…
Browse files Browse the repository at this point in the history
…mex protocol implementations;

Extract the common functionality into a separated abstract class
  • Loading branch information
skubarenko committed Aug 12, 2022
1 parent a93021b commit 53673f5
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 62 deletions.
4 changes: 2 additions & 2 deletions src/blockchain/atomexBlockchainProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class AtomexBlockchainProvider implements CurrenciesProvider {
return this.getCurrencyInfo(currencyId)?.currency;
}

async getReadonlyToolkit(toolkitId: string, blockchain?: string): Promise<unknown | undefined> {
async getReadonlyToolkit<Toolkit = unknown>(toolkitId: string, blockchain?: string): Promise<Toolkit | undefined> {
const providerToolkitPromises: Array<Promise<unknown | undefined>> = [];
for (const provider of this.blockchainToolkitProviders) {
if (provider.toolkitId === toolkitId)
Expand All @@ -61,7 +61,7 @@ export class AtomexBlockchainProvider implements CurrenciesProvider {
const providerToolkitResults = await Promise.all(providerToolkitPromises);
for (const providerResult of providerToolkitResults) {
if (providerResult)
return providerResult;
return providerResult as Toolkit;
}

return Promise.resolve(undefined);
Expand Down
21 changes: 11 additions & 10 deletions src/ethereum/atomexProtocol/erc20EthereumWeb3AtomexProtocolV1.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import type BigNumber from 'bignumber.js';

import type {
AtomexProtocolV1, AtomexProtocolV1InitiateParameters, AtomexProtocolV1RedeemParameters, AtomexProtocolV1RefundParameters,
AtomexBlockchainProvider,
AtomexProtocolV1InitiateParameters, AtomexProtocolV1RedeemParameters, AtomexProtocolV1RefundParameters,
Transaction, WalletsManager
} from '../../blockchain/index';
import type { AtomexNetwork, CurrenciesProvider } from '../../common/index';
import type { DeepReadonly } from '../../core';
import type { ERC20EthereumWeb3AtomexProtocolV1Options } from '../models/atomexProtocolOptions';

export class ERC20EthereumWeb3AtomexProtocolV1 implements AtomexProtocolV1 {
readonly version = 1;
import type { AtomexNetwork } from '../../common/index';
import type { DeepReadonly } from '../../core/index';
import { Web3AtomexProtocolV1 } from '../../evm/index';
import type { ERC20EthereumWeb3AtomexProtocolV1Options } from '../models/index';

export class ERC20EthereumWeb3AtomexProtocolV1 extends Web3AtomexProtocolV1 {
constructor(
readonly atomexNetwork: AtomexNetwork,
atomexNetwork: AtomexNetwork,
protected readonly atomexProtocolOptions: DeepReadonly<ERC20EthereumWeb3AtomexProtocolV1Options>,
protected readonly currenciesProvider: CurrenciesProvider,
protected readonly walletsManager: WalletsManager
atomexBlockchainProvider: AtomexBlockchainProvider,
walletsManager: WalletsManager
) {
super('ethereum', atomexNetwork, atomexProtocolOptions, atomexBlockchainProvider, walletsManager);
}

get currencyId() {
Expand Down
27 changes: 12 additions & 15 deletions src/ethereum/atomexProtocol/ethereumWeb3AtomexProtocolV1.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
import type BigNumber from 'bignumber.js';

import type {
AtomexProtocolV1, AtomexProtocolV1InitiateParameters, AtomexProtocolV1RedeemParameters, AtomexProtocolV1RefundParameters,
AtomexBlockchainProvider,
AtomexProtocolV1InitiateParameters, AtomexProtocolV1RedeemParameters, AtomexProtocolV1RefundParameters,
Transaction, WalletsManager
} from '../../blockchain/index';
import type { AtomexNetwork, CurrenciesProvider } from '../../common/index';
import type { DeepReadonly } from '../../core';
import type { EthereumWeb3AtomexProtocolV1Options } from '../models/atomexProtocolOptions';

export class EthereumWeb3AtomexProtocolV1 implements AtomexProtocolV1 {
readonly version = 1;
import type { AtomexNetwork } from '../../common/index';
import type { DeepReadonly } from '../../core/index';
import { Web3AtomexProtocolV1 } from '../../evm/index';
import type { EthereumWeb3AtomexProtocolV1Options } from '../models/index';

export class EthereumWeb3AtomexProtocolV1 extends Web3AtomexProtocolV1 {
constructor(
readonly atomexNetwork: AtomexNetwork,
atomexNetwork: AtomexNetwork,
protected readonly atomexProtocolOptions: DeepReadonly<EthereumWeb3AtomexProtocolV1Options>,
protected readonly currenciesProvider: CurrenciesProvider,
protected readonly walletsManager: WalletsManager
atomexBlockchainProvider: AtomexBlockchainProvider,
walletsManager: WalletsManager
) {
}

get currencyId() {
return this.atomexProtocolOptions.currencyId;
super('ethereum', atomexNetwork, atomexProtocolOptions, atomexBlockchainProvider, walletsManager);
}

initiate(_params: AtomexProtocolV1InitiateParameters): Promise<Transaction> {
throw new Error('Method not implemented.');
}

getEstimatedInitiateFees(_params: Partial<AtomexProtocolV1InitiateParameters>): Promise<BigNumber> {
async getEstimatedInitiateFees(_params: Partial<AtomexProtocolV1InitiateParameters>): Promise<BigNumber> {
throw new Error('Method not implemented.');
}

Expand Down
4 changes: 2 additions & 2 deletions src/ethereum/config/defaultOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ const createAtomexProtocol = (
return new EthereumWeb3AtomexProtocolV1(
atomexContext.atomexNetwork,
atomexProtocolOptions,
atomexContext.providers.currenciesProvider,
atomexContext.providers.blockchainProvider,
atomexContext.managers.walletsManager
);
case 'erc-20':
return new ERC20EthereumWeb3AtomexProtocolV1(
atomexContext.atomexNetwork,
atomexProtocolOptions,
atomexContext.providers.currenciesProvider,
atomexContext.providers.blockchainProvider,
atomexContext.managers.walletsManager
);
default:
Expand Down
1 change: 1 addition & 0 deletions src/evm/atomexProtocol/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Web3AtomexProtocolV1 } from './web3AtomexProtocolV1';
58 changes: 58 additions & 0 deletions src/evm/atomexProtocol/web3AtomexProtocolV1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type BigNumber from 'bignumber.js';
import type Web3 from 'web3';

import type {
AtomexBlockchainProvider,
AtomexProtocolV1, AtomexProtocolV1InitiateParameters, AtomexProtocolV1RedeemParameters, AtomexProtocolV1RefundParameters,
Transaction, WalletsManager
} from '../../blockchain/index';
import type { AtomexNetwork } from '../../common/index';
import type { DeepReadonly } from '../../core/index';
import type { Web3AtomexProtocolV1Options } from '../models/index';

export abstract class Web3AtomexProtocolV1 implements AtomexProtocolV1 {
readonly version = 1;

constructor(
protected readonly blockchain: string,
readonly atomexNetwork: AtomexNetwork,
protected readonly atomexProtocolOptions: DeepReadonly<Web3AtomexProtocolV1Options>,
protected readonly atomexBlockchainProvider: AtomexBlockchainProvider,
protected readonly walletsManager: WalletsManager
) {
}

get currencyId() {
return this.atomexProtocolOptions.currencyId;
}

abstract initiate(_params: AtomexProtocolV1InitiateParameters): Promise<Transaction>;

abstract getEstimatedInitiateFees(_params: Partial<AtomexProtocolV1InitiateParameters>): Promise<BigNumber>;

abstract redeem(_params: AtomexProtocolV1RedeemParameters): Promise<Transaction>;

abstract getRedeemReward(_nativeTokenPriceInUsd: number, _nativeTokenPriceInCurrency: number): Promise<BigNumber>;

abstract getEstimatedRedeemFees(_params: Partial<AtomexProtocolV1InitiateParameters>): Promise<BigNumber>;

abstract refund(_params: AtomexProtocolV1RefundParameters): Promise<Transaction>;

abstract getEstimatedRefundFees(_params: Partial<AtomexProtocolV1InitiateParameters>): Promise<BigNumber>;

protected async getReadonlyWeb3() {
const toolkit = this.atomexBlockchainProvider.getReadonlyToolkit<Web3>('web3', this.blockchain);
if (!toolkit)
throw new Error('Web3 toolkit not found');

return toolkit;
}

protected async getWallet(address?: string) {
const web3Wallet = await this.walletsManager.getWallet<Web3>(address, this.blockchain, 'web3');
if (!web3Wallet)
throw new Error(`${this.blockchain} Web3 wallet not found`);

return web3Wallet;
}
}
1 change: 1 addition & 0 deletions src/evm/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { Web3AtomexProtocolV1 } from './atomexProtocol/index';
export { Web3BlockchainWallet } from './wallets/index';
export { Web3BlockchainToolkitProvider } from './blockchainToolkitProviders/index';

Expand Down
21 changes: 11 additions & 10 deletions src/tezos/atomexProtocol/fa12TezosTaquitoAtomexProtocolV1.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import type BigNumber from 'bignumber.js';

import type {
AtomexProtocolV1, AtomexProtocolV1InitiateParameters, AtomexProtocolV1RedeemParameters, AtomexProtocolV1RefundParameters,
AtomexBlockchainProvider,
AtomexProtocolV1InitiateParameters, AtomexProtocolV1RedeemParameters, AtomexProtocolV1RefundParameters,
Transaction, WalletsManager
} from '../../blockchain/index';
import type { AtomexNetwork, CurrenciesProvider } from '../../common/index';
import type { DeepReadonly } from '../../core';
import type { FA12TezosTaquitoAtomexProtocolV1Options } from '../models/atomexProtocolOptions';

export class FA12TezosTaquitoAtomexProtocolV1 implements AtomexProtocolV1 {
readonly version = 1;
import type { AtomexNetwork } from '../../common/index';
import type { DeepReadonly } from '../../core/index';
import type { FA12TezosTaquitoAtomexProtocolV1Options } from '../models/index';
import { TaquitoAtomexProtocolV1 } from './taquitoAtomexProtocolV1';

export class FA12TezosTaquitoAtomexProtocolV1 extends TaquitoAtomexProtocolV1 {
constructor(
readonly atomexNetwork: AtomexNetwork,
atomexNetwork: AtomexNetwork,
protected readonly atomexProtocolOptions: DeepReadonly<FA12TezosTaquitoAtomexProtocolV1Options>,
protected readonly currenciesProvider: CurrenciesProvider,
protected readonly walletsManager: WalletsManager
atomexBlockchainProvider: AtomexBlockchainProvider,
walletsManager: WalletsManager
) {
super('tezos', atomexNetwork, atomexProtocolOptions, atomexBlockchainProvider, walletsManager);
}

get currencyId() {
Expand Down
21 changes: 11 additions & 10 deletions src/tezos/atomexProtocol/fa2TezosTaquitoAtomexProtocolV1.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import type BigNumber from 'bignumber.js';

import type {
AtomexProtocolV1, AtomexProtocolV1InitiateParameters, AtomexProtocolV1RedeemParameters, AtomexProtocolV1RefundParameters,
AtomexBlockchainProvider,
AtomexProtocolV1InitiateParameters, AtomexProtocolV1RedeemParameters, AtomexProtocolV1RefundParameters,
Transaction, WalletsManager
} from '../../blockchain/index';
import type { AtomexNetwork, CurrenciesProvider } from '../../common/index';
import type { DeepReadonly } from '../../core';
import type { FA2TezosTaquitoAtomexProtocolV1Options } from '../models/atomexProtocolOptions';

export class FA2TezosTaquitoAtomexProtocolV1 implements AtomexProtocolV1 {
readonly version = 1;
import type { AtomexNetwork } from '../../common/index';
import type { DeepReadonly } from '../../core/index';
import type { FA2TezosTaquitoAtomexProtocolV1Options } from '../models/index';
import { TaquitoAtomexProtocolV1 } from './taquitoAtomexProtocolV1';

export class FA2TezosTaquitoAtomexProtocolV1 extends TaquitoAtomexProtocolV1 {
constructor(
readonly atomexNetwork: AtomexNetwork,
atomexNetwork: AtomexNetwork,
protected readonly atomexProtocolOptions: DeepReadonly<FA2TezosTaquitoAtomexProtocolV1Options>,
protected readonly currenciesProvider: CurrenciesProvider,
protected readonly walletsManager: WalletsManager
atomexBlockchainProvider: AtomexBlockchainProvider,
walletsManager: WalletsManager
) {
super('tezos', atomexNetwork, atomexProtocolOptions, atomexBlockchainProvider, walletsManager);
}

get currencyId() {
Expand Down
58 changes: 58 additions & 0 deletions src/tezos/atomexProtocol/taquitoAtomexProtocolV1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type { TezosToolkit } from '@taquito/taquito';
import type BigNumber from 'bignumber.js';

import type {
AtomexBlockchainProvider,
AtomexProtocolV1, AtomexProtocolV1InitiateParameters, AtomexProtocolV1RedeemParameters, AtomexProtocolV1RefundParameters,
Transaction, WalletsManager
} from '../../blockchain/index';
import type { AtomexNetwork } from '../../common/index';
import type { DeepReadonly } from '../../core/index';
import type { TaquitoAtomexProtocolV1Options } from '../models/index';

export abstract class TaquitoAtomexProtocolV1 implements AtomexProtocolV1 {
readonly version = 1;

constructor(
protected readonly blockchain: string,
readonly atomexNetwork: AtomexNetwork,
protected readonly atomexProtocolOptions: DeepReadonly<TaquitoAtomexProtocolV1Options>,
protected readonly atomexBlockchainProvider: AtomexBlockchainProvider,
protected readonly walletsManager: WalletsManager
) {
}

get currencyId() {
return this.atomexProtocolOptions.currencyId;
}

abstract initiate(_params: AtomexProtocolV1InitiateParameters): Promise<Transaction>;

abstract getEstimatedInitiateFees(_params: Partial<AtomexProtocolV1InitiateParameters>): Promise<BigNumber>;

abstract redeem(_params: AtomexProtocolV1RedeemParameters): Promise<Transaction>;

abstract getRedeemReward(_nativeTokenPriceInUsd: number, _nativeTokenPriceInCurrency: number): Promise<BigNumber>;

abstract getEstimatedRedeemFees(_params: Partial<AtomexProtocolV1InitiateParameters>): Promise<BigNumber>;

abstract refund(_params: AtomexProtocolV1RefundParameters): Promise<Transaction>;

abstract getEstimatedRefundFees(_params: Partial<AtomexProtocolV1InitiateParameters>): Promise<BigNumber>;

protected async getReadonlyTezosToolkit() {
const toolkit = this.atomexBlockchainProvider.getReadonlyToolkit<TezosToolkit>('taquito', this.blockchain);
if (!toolkit)
throw new Error('Tezos toolkit not found');

return toolkit;
}

protected async getWallet(address?: string) {
const taquitoWallet = await this.walletsManager.getWallet<TezosToolkit>(address, this.blockchain, 'taquito');
if (!taquitoWallet)
throw new Error(`${this.blockchain} Taqutio wallet not found`);

return taquitoWallet;
}
}
21 changes: 11 additions & 10 deletions src/tezos/atomexProtocol/tezosTaquitoAtomexProtocolV1.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import type BigNumber from 'bignumber.js';

import type {
AtomexProtocolV1, AtomexProtocolV1InitiateParameters, AtomexProtocolV1RedeemParameters, AtomexProtocolV1RefundParameters,
AtomexBlockchainProvider,
AtomexProtocolV1InitiateParameters, AtomexProtocolV1RedeemParameters, AtomexProtocolV1RefundParameters,
Transaction, WalletsManager
} from '../../blockchain/index';
import type { AtomexNetwork, CurrenciesProvider } from '../../common/index';
import type { DeepReadonly } from '../../core';
import type { TezosTaquitoAtomexProtocolV1Options } from '../models/atomexProtocolOptions';

export class TezosTaquitoAtomexProtocolV1 implements AtomexProtocolV1 {
readonly version = 1;
import type { AtomexNetwork } from '../../common/index';
import type { DeepReadonly } from '../../core/index';
import type { TezosTaquitoAtomexProtocolV1Options } from '../models/index';
import { TaquitoAtomexProtocolV1 } from './taquitoAtomexProtocolV1';

export class TezosTaquitoAtomexProtocolV1 extends TaquitoAtomexProtocolV1 {
constructor(
readonly atomexNetwork: AtomexNetwork,
atomexNetwork: AtomexNetwork,
protected readonly atomexProtocolOptions: DeepReadonly<TezosTaquitoAtomexProtocolV1Options>,
protected readonly currenciesProvider: CurrenciesProvider,
protected readonly walletsManager: WalletsManager
atomexBlockchainProvider: AtomexBlockchainProvider,
walletsManager: WalletsManager
) {
super('tezos', atomexNetwork, atomexProtocolOptions, atomexBlockchainProvider, walletsManager);
}

get currencyId() {
Expand Down
6 changes: 3 additions & 3 deletions src/tezos/config/defaultOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ const createAtomexProtocol = (
return new TezosTaquitoAtomexProtocolV1(
atomexContext.atomexNetwork,
atomexProtocolOptions,
atomexContext.providers.currenciesProvider,
atomexContext.providers.blockchainProvider,
atomexContext.managers.walletsManager
);
case 'fa1.2':
return new FA12TezosTaquitoAtomexProtocolV1(
atomexContext.atomexNetwork,
atomexProtocolOptions,
atomexContext.providers.currenciesProvider,
atomexContext.providers.blockchainProvider,
atomexContext.managers.walletsManager
);
case 'fa2':
return new FA2TezosTaquitoAtomexProtocolV1(
atomexContext.atomexNetwork,
atomexProtocolOptions,
atomexContext.providers.currenciesProvider,
atomexContext.providers.blockchainProvider,
atomexContext.managers.walletsManager
);
default:
Expand Down

0 comments on commit 53673f5

Please sign in to comment.