Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bases of the Atomex protocol for supported chains #66

Merged
merged 17 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"atomex",
"tezos",
"outdir",
"esbuild"
"esbuild",
"taquito"
]
}
7 changes: 6 additions & 1 deletion src/atomex/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export { Atomex } from './atomex';
export { AtomexContext } from './atomexContext';
export { SwapOperationCompleteStage } from './models/index';

export type { AtomexOptions, NewSwapRequest } from './models/index';
export type {
NewSwapRequest,
AtomexOptions, AtomexBlockchainNetworkOptions, AtomexBlockchainOptions, AtomexCurrencyOptions,
AtomexManagers, AtomexServices,
} from './models/index';
5 changes: 4 additions & 1 deletion src/atomex/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export type { AtomexOptions, AtomexBlockchainOptions, AtomexServices } from './atomexOptions';
export type {
AtomexOptions, AtomexBlockchainOptions, AtomexBlockchainNetworkOptions, AtomexCurrencyOptions,
AtomexManagers, AtomexServices
} from './atomexOptions';
export type { NewSwapRequest } from './newSwapRequest';

export { SwapOperationCompleteStage } from './swapOperationCompleteStage';
4 changes: 2 additions & 2 deletions src/atomexBuilder/atomexBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ export class AtomexBuilder {

protected createDefaultBlockchainOptions(): Record<string, AtomexBlockchainOptions> {
return {
tezos: createDefaultTezosBlockchainOptions(),
ethereum: createDefaultEthereumBlockchainOptions()
tezos: createDefaultTezosBlockchainOptions(this.atomexContext),
ethereum: createDefaultEthereumBlockchainOptions(this.atomexContext)
};
}
}
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
2 changes: 1 addition & 1 deletion src/blockchain/atomexProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import type { AtomexNetwork } from '../common/index';

export interface AtomexProtocol {
readonly version: number;
readonly network: AtomexNetwork;
readonly atomexNetwork: AtomexNetwork;
}
21 changes: 11 additions & 10 deletions src/blockchain/atomexProtocolV1/atomexProtocolV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ import type { BigNumber } from 'bignumber.js';
import type { Currency } from '../../common/index';
import type { AtomexProtocol } from '../atomexProtocol';
import type { Transaction } from '../models/index';
import type { InitiateParametersAtomexProtocolV1 } from './initiateParametersAtomexProtocolV1';
import type { RedeemParametersAtomexProtocolV1 } from './redeemParametersAtomexProtocolV1';
import type { RefundParametersAtomexProtocolV1 } from './refundParametersAtomexProtocolV1';
import type { AtomexProtocolV1InitiateParameters } from './initiateParameters';
import type { AtomexProtocolV1RedeemParameters } from './redeemParameters';
import type { AtomexProtocolV1RefundParameters } from './refundParameters';

export interface AtomexProtocolV1 extends AtomexProtocol {
readonly version: 1;
readonly currency: Currency;
readonly currencyId: Currency['id'];

initiate(params: InitiateParametersAtomexProtocolV1): Promise<Transaction>;
getEstimatedInitiateFees(params: Partial<InitiateParametersAtomexProtocolV1>): Promise<BigNumber>;
initiate(params: AtomexProtocolV1InitiateParameters): Promise<Transaction>;
getEstimatedInitiateFees(params: Partial<AtomexProtocolV1InitiateParameters>): Promise<BigNumber>;

redeem(params: RedeemParametersAtomexProtocolV1): Promise<Transaction>;
getEstimatedRedeemFees(params: Partial<InitiateParametersAtomexProtocolV1>): Promise<BigNumber>;
redeem(params: AtomexProtocolV1RedeemParameters): Promise<Transaction>;
getEstimatedRedeemFees(params: Partial<AtomexProtocolV1InitiateParameters>): Promise<BigNumber>;
getRedeemReward(nativeTokenPriceInUsd: number, nativeTokenPriceInCurrency: number): Promise<BigNumber>;

refund(params: RefundParametersAtomexProtocolV1): Promise<Transaction>;
getEstimatedRefundFees(params: Partial<InitiateParametersAtomexProtocolV1>): Promise<BigNumber>;
refund(params: AtomexProtocolV1RefundParameters): Promise<Transaction>;
getEstimatedRefundFees(params: Partial<AtomexProtocolV1InitiateParameters>): Promise<BigNumber>;
}
6 changes: 6 additions & 0 deletions src/blockchain/atomexProtocolV1/guards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { AtomexProtocol } from '../atomexProtocol';
import type { AtomexProtocolV1 } from './atomexProtocolV1';

export const isAtomexProtocolV1 = (atomexProtocol: AtomexProtocol): atomexProtocol is AtomexProtocolV1 => {
return atomexProtocol.version === 1;
};
9 changes: 6 additions & 3 deletions src/blockchain/atomexProtocolV1/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export { isAtomexProtocolV1 } from './guards';

export type { AtomexProtocolV1 } from './atomexProtocolV1';
export type { InitiateParametersAtomexProtocolV1 } from './initiateParametersAtomexProtocolV1';
export type { RedeemParametersAtomexProtocolV1 } from './redeemParametersAtomexProtocolV1';
export type { RefundParametersAtomexProtocolV1 } from './refundParametersAtomexProtocolV1';
export type { AtomexProtocolV1Options } from './options';
export type { AtomexProtocolV1InitiateParameters } from './initiateParameters';
export type { AtomexProtocolV1RedeemParameters } from './redeemParameters';
export type { AtomexProtocolV1RefundParameters } from './refundParameters';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { BigNumber } from 'bignumber.js';

export interface InitiateParametersAtomexProtocolV1 {
export interface AtomexProtocolV1InitiateParameters {
readonly amount: BigNumber;
readonly secretHash: string;
readonly receivingAddress: string;
Expand Down
8 changes: 8 additions & 0 deletions src/blockchain/atomexProtocolV1/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { Currency } from '../../common/index';
import type { AtomexProtocolOptions } from '../models/index';

export interface AtomexProtocolV1Options extends AtomexProtocolOptions {
currencyId: Currency['id'];
swapContractAddress: string;
swapContractBlockId?: string;
}
3 changes: 3 additions & 0 deletions src/blockchain/atomexProtocolV1/redeemParameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface AtomexProtocolV1RedeemParameters {
readonly secretHash: string;
}

This file was deleted.

3 changes: 3 additions & 0 deletions src/blockchain/atomexProtocolV1/refundParameters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface AtomexProtocolV1RefundParameters {
readonly secretHash: string;
}

This file was deleted.

7 changes: 4 additions & 3 deletions src/blockchain/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
export { WalletsManager } from './walletsManager';
export { isAtomexProtocolV1 } from './atomexProtocolV1/index';

export type { AtomexSignature, Transaction } from './models/index';
export type { AtomexProtocolOptions, AtomexSignature, Transaction } from './models/index';
export type { AtomexProtocol } from './atomexProtocol';
export type {
AtomexProtocolV1, InitiateParametersAtomexProtocolV1,
RedeemParametersAtomexProtocolV1, RefundParametersAtomexProtocolV1
AtomexProtocolV1, AtomexProtocolV1Options,
AtomexProtocolV1InitiateParameters, AtomexProtocolV1RedeemParameters, AtomexProtocolV1RefundParameters
} from './atomexProtocolV1/index';
export type { BalancesProvider } from './balancesProvider';
export type { CurrencyBalanceProvider } from './currencyBalanceProvider';
Expand Down
3 changes: 3 additions & 0 deletions src/blockchain/models/atomexProtocolOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface AtomexProtocolOptions {
atomexProtocolVersion: number;
}
1 change: 1 addition & 0 deletions src/blockchain/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export type { AtomexProtocolOptions } from './atomexProtocolOptions';
export type { AtomexSignature } from './atomexSignature';
export type { Transaction } from './transaction';
10 changes: 7 additions & 3 deletions src/blockchain/walletsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ export class WalletsManager {
return Promise.resolve(result);
}

async getWallet(address?: string, blockchain?: string, toolkit?: string): Promise<BlockchainWallet | undefined> {
async getWallet<Toolkit = unknown>(address?: string, blockchain?: string, toolkit?: string): Promise<BlockchainWallet<Toolkit> | undefined> {
if (!this.wallets.size || (!address && !blockchain && !toolkit))
return undefined;

const walletPromises: Array<Promise<[wallet: BlockchainWallet, address: string | undefined, blockchain: string | undefined]>> = [];
const walletPromises: Array<Promise<readonly [
wallet: BlockchainWallet<Toolkit>,
address: string | undefined,
blockchain: string | undefined
]>> = [];

for (const wallet of this.wallets) {
for (const wallet of this.wallets as Set<BlockchainWallet<Toolkit>>) {
if (toolkit && wallet.id !== toolkit)
continue;

Expand Down
54 changes: 54 additions & 0 deletions src/ethereum/atomexProtocol/erc20EthereumWeb3AtomexProtocolV1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type BigNumber from 'bignumber.js';

import type {
AtomexBlockchainProvider,
AtomexProtocolV1InitiateParameters, AtomexProtocolV1RedeemParameters, AtomexProtocolV1RefundParameters,
Transaction, WalletsManager
} from '../../blockchain/index';
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(
atomexNetwork: AtomexNetwork,
protected readonly atomexProtocolOptions: DeepReadonly<ERC20EthereumWeb3AtomexProtocolV1Options>,
atomexBlockchainProvider: AtomexBlockchainProvider,
walletsManager: WalletsManager
) {
super('ethereum', atomexNetwork, atomexProtocolOptions, atomexBlockchainProvider, walletsManager);
}

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

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

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

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

getRedeemReward(_nativeTokenPriceInUsd: number, _nativeTokenPriceInCurrency: number): Promise<BigNumber> {
throw new Error('Method not implemented.');
}

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

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

getEstimatedRefundFees(_params: Partial<AtomexProtocolV1InitiateParameters>): Promise<BigNumber> {
throw new Error('Method not implemented.');
}
}
50 changes: 50 additions & 0 deletions src/ethereum/atomexProtocol/ethereumWeb3AtomexProtocolV1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type BigNumber from 'bignumber.js';

import type {
AtomexBlockchainProvider,
AtomexProtocolV1InitiateParameters, AtomexProtocolV1RedeemParameters, AtomexProtocolV1RefundParameters,
Transaction, WalletsManager
} from '../../blockchain/index';
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(
atomexNetwork: AtomexNetwork,
protected readonly atomexProtocolOptions: DeepReadonly<EthereumWeb3AtomexProtocolV1Options>,
atomexBlockchainProvider: AtomexBlockchainProvider,
walletsManager: WalletsManager
) {
super('ethereum', atomexNetwork, atomexProtocolOptions, atomexBlockchainProvider, walletsManager);
}

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

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

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

getRedeemReward(_nativeTokenPriceInUsd: number, _nativeTokenPriceInCurrency: number): Promise<BigNumber> {
throw new Error('Method not implemented.');
}

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

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

getEstimatedRefundFees(_params: Partial<AtomexProtocolV1InitiateParameters>): Promise<BigNumber> {
throw new Error('Method not implemented.');
}
}
2 changes: 2 additions & 0 deletions src/ethereum/atomexProtocol/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { EthereumWeb3AtomexProtocolV1 } from './ethereumWeb3AtomexProtocolV1';
export { ERC20EthereumWeb3AtomexProtocolV1 } from './erc20EthereumWeb3AtomexProtocolV1';
Loading