From 53673f5dc1b3d7c38a62d8e4a0769fe91845e22f Mon Sep 17 00:00:00 2001 From: Andrew Skubarenko Date: Fri, 12 Aug 2022 05:29:23 +0300 Subject: [PATCH] Use the blockchain provider instead of currencies provider in the atomex protocol implementations; Extract the common functionality into a separated abstract class --- src/blockchain/atomexBlockchainProvider.ts | 4 +- .../erc20EthereumWeb3AtomexProtocolV1.ts | 21 +++---- .../ethereumWeb3AtomexProtocolV1.ts | 27 ++++----- src/ethereum/config/defaultOptions.ts | 4 +- src/evm/atomexProtocol/index.ts | 1 + .../atomexProtocol/web3AtomexProtocolV1.ts | 58 +++++++++++++++++++ src/evm/index.ts | 1 + .../fa12TezosTaquitoAtomexProtocolV1.ts | 21 +++---- .../fa2TezosTaquitoAtomexProtocolV1.ts | 21 +++---- .../atomexProtocol/taquitoAtomexProtocolV1.ts | 58 +++++++++++++++++++ .../tezosTaquitoAtomexProtocolV1.ts | 21 +++---- src/tezos/config/defaultOptions.ts | 6 +- 12 files changed, 181 insertions(+), 62 deletions(-) create mode 100644 src/evm/atomexProtocol/index.ts create mode 100644 src/evm/atomexProtocol/web3AtomexProtocolV1.ts create mode 100644 src/tezos/atomexProtocol/taquitoAtomexProtocolV1.ts diff --git a/src/blockchain/atomexBlockchainProvider.ts b/src/blockchain/atomexBlockchainProvider.ts index 3c0516f3..76903ca3 100644 --- a/src/blockchain/atomexBlockchainProvider.ts +++ b/src/blockchain/atomexBlockchainProvider.ts @@ -51,7 +51,7 @@ export class AtomexBlockchainProvider implements CurrenciesProvider { return this.getCurrencyInfo(currencyId)?.currency; } - async getReadonlyToolkit(toolkitId: string, blockchain?: string): Promise { + async getReadonlyToolkit(toolkitId: string, blockchain?: string): Promise { const providerToolkitPromises: Array> = []; for (const provider of this.blockchainToolkitProviders) { if (provider.toolkitId === toolkitId) @@ -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); diff --git a/src/ethereum/atomexProtocol/erc20EthereumWeb3AtomexProtocolV1.ts b/src/ethereum/atomexProtocol/erc20EthereumWeb3AtomexProtocolV1.ts index 3301bccb..1d3a681b 100644 --- a/src/ethereum/atomexProtocol/erc20EthereumWeb3AtomexProtocolV1.ts +++ b/src/ethereum/atomexProtocol/erc20EthereumWeb3AtomexProtocolV1.ts @@ -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, - protected readonly currenciesProvider: CurrenciesProvider, - protected readonly walletsManager: WalletsManager + atomexBlockchainProvider: AtomexBlockchainProvider, + walletsManager: WalletsManager ) { + super('ethereum', atomexNetwork, atomexProtocolOptions, atomexBlockchainProvider, walletsManager); } get currencyId() { diff --git a/src/ethereum/atomexProtocol/ethereumWeb3AtomexProtocolV1.ts b/src/ethereum/atomexProtocol/ethereumWeb3AtomexProtocolV1.ts index e6eed072..487ed575 100644 --- a/src/ethereum/atomexProtocol/ethereumWeb3AtomexProtocolV1.ts +++ b/src/ethereum/atomexProtocol/ethereumWeb3AtomexProtocolV1.ts @@ -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, - 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 { throw new Error('Method not implemented.'); } - getEstimatedInitiateFees(_params: Partial): Promise { + async getEstimatedInitiateFees(_params: Partial): Promise { throw new Error('Method not implemented.'); } diff --git a/src/ethereum/config/defaultOptions.ts b/src/ethereum/config/defaultOptions.ts index a78502fb..8e524ccd 100644 --- a/src/ethereum/config/defaultOptions.ts +++ b/src/ethereum/config/defaultOptions.ts @@ -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: diff --git a/src/evm/atomexProtocol/index.ts b/src/evm/atomexProtocol/index.ts new file mode 100644 index 00000000..7cb4620b --- /dev/null +++ b/src/evm/atomexProtocol/index.ts @@ -0,0 +1 @@ +export { Web3AtomexProtocolV1 } from './web3AtomexProtocolV1'; diff --git a/src/evm/atomexProtocol/web3AtomexProtocolV1.ts b/src/evm/atomexProtocol/web3AtomexProtocolV1.ts new file mode 100644 index 00000000..a0acfe17 --- /dev/null +++ b/src/evm/atomexProtocol/web3AtomexProtocolV1.ts @@ -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, + protected readonly atomexBlockchainProvider: AtomexBlockchainProvider, + protected readonly walletsManager: WalletsManager + ) { + } + + get currencyId() { + return this.atomexProtocolOptions.currencyId; + } + + abstract initiate(_params: AtomexProtocolV1InitiateParameters): Promise; + + abstract getEstimatedInitiateFees(_params: Partial): Promise; + + abstract redeem(_params: AtomexProtocolV1RedeemParameters): Promise; + + abstract getRedeemReward(_nativeTokenPriceInUsd: number, _nativeTokenPriceInCurrency: number): Promise; + + abstract getEstimatedRedeemFees(_params: Partial): Promise; + + abstract refund(_params: AtomexProtocolV1RefundParameters): Promise; + + abstract getEstimatedRefundFees(_params: Partial): Promise; + + protected async getReadonlyWeb3() { + const toolkit = this.atomexBlockchainProvider.getReadonlyToolkit('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(address, this.blockchain, 'web3'); + if (!web3Wallet) + throw new Error(`${this.blockchain} Web3 wallet not found`); + + return web3Wallet; + } +} diff --git a/src/evm/index.ts b/src/evm/index.ts index 5343bdd2..f336785c 100644 --- a/src/evm/index.ts +++ b/src/evm/index.ts @@ -1,3 +1,4 @@ +export { Web3AtomexProtocolV1 } from './atomexProtocol/index'; export { Web3BlockchainWallet } from './wallets/index'; export { Web3BlockchainToolkitProvider } from './blockchainToolkitProviders/index'; diff --git a/src/tezos/atomexProtocol/fa12TezosTaquitoAtomexProtocolV1.ts b/src/tezos/atomexProtocol/fa12TezosTaquitoAtomexProtocolV1.ts index 786e3708..07d5b693 100644 --- a/src/tezos/atomexProtocol/fa12TezosTaquitoAtomexProtocolV1.ts +++ b/src/tezos/atomexProtocol/fa12TezosTaquitoAtomexProtocolV1.ts @@ -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, - protected readonly currenciesProvider: CurrenciesProvider, - protected readonly walletsManager: WalletsManager + atomexBlockchainProvider: AtomexBlockchainProvider, + walletsManager: WalletsManager ) { + super('tezos', atomexNetwork, atomexProtocolOptions, atomexBlockchainProvider, walletsManager); } get currencyId() { diff --git a/src/tezos/atomexProtocol/fa2TezosTaquitoAtomexProtocolV1.ts b/src/tezos/atomexProtocol/fa2TezosTaquitoAtomexProtocolV1.ts index 10ea47b8..c98dbd39 100644 --- a/src/tezos/atomexProtocol/fa2TezosTaquitoAtomexProtocolV1.ts +++ b/src/tezos/atomexProtocol/fa2TezosTaquitoAtomexProtocolV1.ts @@ -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, - protected readonly currenciesProvider: CurrenciesProvider, - protected readonly walletsManager: WalletsManager + atomexBlockchainProvider: AtomexBlockchainProvider, + walletsManager: WalletsManager ) { + super('tezos', atomexNetwork, atomexProtocolOptions, atomexBlockchainProvider, walletsManager); } get currencyId() { diff --git a/src/tezos/atomexProtocol/taquitoAtomexProtocolV1.ts b/src/tezos/atomexProtocol/taquitoAtomexProtocolV1.ts new file mode 100644 index 00000000..326d6d2f --- /dev/null +++ b/src/tezos/atomexProtocol/taquitoAtomexProtocolV1.ts @@ -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, + protected readonly atomexBlockchainProvider: AtomexBlockchainProvider, + protected readonly walletsManager: WalletsManager + ) { + } + + get currencyId() { + return this.atomexProtocolOptions.currencyId; + } + + abstract initiate(_params: AtomexProtocolV1InitiateParameters): Promise; + + abstract getEstimatedInitiateFees(_params: Partial): Promise; + + abstract redeem(_params: AtomexProtocolV1RedeemParameters): Promise; + + abstract getRedeemReward(_nativeTokenPriceInUsd: number, _nativeTokenPriceInCurrency: number): Promise; + + abstract getEstimatedRedeemFees(_params: Partial): Promise; + + abstract refund(_params: AtomexProtocolV1RefundParameters): Promise; + + abstract getEstimatedRefundFees(_params: Partial): Promise; + + protected async getReadonlyTezosToolkit() { + const toolkit = this.atomexBlockchainProvider.getReadonlyToolkit('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(address, this.blockchain, 'taquito'); + if (!taquitoWallet) + throw new Error(`${this.blockchain} Taqutio wallet not found`); + + return taquitoWallet; + } +} diff --git a/src/tezos/atomexProtocol/tezosTaquitoAtomexProtocolV1.ts b/src/tezos/atomexProtocol/tezosTaquitoAtomexProtocolV1.ts index fbc1f546..08d57161 100644 --- a/src/tezos/atomexProtocol/tezosTaquitoAtomexProtocolV1.ts +++ b/src/tezos/atomexProtocol/tezosTaquitoAtomexProtocolV1.ts @@ -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, - protected readonly currenciesProvider: CurrenciesProvider, - protected readonly walletsManager: WalletsManager + atomexBlockchainProvider: AtomexBlockchainProvider, + walletsManager: WalletsManager ) { + super('tezos', atomexNetwork, atomexProtocolOptions, atomexBlockchainProvider, walletsManager); } get currencyId() { diff --git a/src/tezos/config/defaultOptions.ts b/src/tezos/config/defaultOptions.ts index bc27f89a..624525e4 100644 --- a/src/tezos/config/defaultOptions.ts +++ b/src/tezos/config/defaultOptions.ts @@ -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: