Skip to content

Commit

Permalink
Use the authorized addresses for getting swap preview
Browse files Browse the repository at this point in the history
  • Loading branch information
skubarenko committed Sep 12, 2022
1 parent 9dc4148 commit e9b606c
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions src/atomex/atomexSwapPreviewManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,24 +194,25 @@ export class AtomexSwapPreviewManager implements Disposable {
): Promise<{ fromAddress?: string; toAddress?: string; maxOrderPreview?: OrderPreview }> {
const [fromAddress, toAddress] = await Promise.all([
this.atomexContext.managers.walletsManager.getWallet(undefined, swapCurrencyInfos.fromCurrencyInfo.currency.blockchain)
.then(wallet => wallet?.getAddress())
.then(address => address && this.atomexContext.managers.authorizationManager.getAuthToken(address) ? address : undefined),

.then(wallet => wallet?.getAddress()),
this.atomexContext.managers.walletsManager.getWallet(undefined, swapCurrencyInfos.toCurrencyInfo.currency.blockchain)
.then(wallet => wallet?.getAddress()),
]);
let maxOrderPreview: OrderPreview | undefined;

const authorizedFromAddress = fromAddress && this.atomexContext.managers.authorizationManager.getAuthToken(fromAddress) ? fromAddress : undefined;
const authorizedToAddress = toAddress && this.atomexContext.managers.authorizationManager.getAuthToken(toAddress) ? toAddress : undefined;

const userInvolvedSwapsInfo = await this.getUserInvolvedSwapsInfo(
fromAddress,
toAddress,
authorizedFromAddress,
authorizedToAddress,
swapCurrencyInfos
);
if (fromAddress) {
let fromCurrencyBalance = await this.atomexContext.managers.balanceManager.getBalance(fromAddress, swapCurrencyInfos.fromCurrencyInfo.currency);
if (authorizedFromAddress) {
let fromCurrencyBalance = await this.atomexContext.managers.balanceManager.getBalance(authorizedFromAddress, swapCurrencyInfos.fromCurrencyInfo.currency);
let fromNativeCurrencyBalance = swapCurrencyInfos.isFromCurrencyNative
? fromCurrencyBalance
: await this.atomexContext.managers.balanceManager.getBalance(fromAddress, swapCurrencyInfos.fromNativeCurrencyInfo.currency);
: await this.atomexContext.managers.balanceManager.getBalance(authorizedFromAddress, swapCurrencyInfos.fromNativeCurrencyInfo.currency);
if (!fromCurrencyBalance || !fromNativeCurrencyBalance)
throw new Error('Can not get from currency balances');

Expand Down Expand Up @@ -307,8 +308,8 @@ export class AtomexSwapPreviewManager implements Disposable {
}
}

if (toAddress) {
let toNativeCurrencyBalance = await this.atomexContext.managers.balanceManager.getBalance(toAddress, swapCurrencyInfos.toNativeCurrencyInfo.currency);
if (authorizedToAddress) {
let toNativeCurrencyBalance = await this.atomexContext.managers.balanceManager.getBalance(authorizedToAddress, swapCurrencyInfos.toNativeCurrencyInfo.currency);
if (!toNativeCurrencyBalance)
throw new Error('Can not get to currency balance');

Expand Down Expand Up @@ -368,32 +369,32 @@ export class AtomexSwapPreviewManager implements Disposable {
}

protected async getUserInvolvedSwapsInfo(
fromAddress: string | undefined,
toAddress: string | undefined,
authorizedFromAddress: string | undefined,
authorizedToAddress: string | undefined,
swapCurrencyInfos: SwapCurrencyInfos
): Promise<UserInvolvedSwapsInfo | undefined> {
const addresses = fromAddress && toAddress
? fromAddress !== toAddress
? [fromAddress, toAddress].sort()
: [fromAddress]
: fromAddress
? [fromAddress]
: toAddress
? [toAddress]
const authorizedAddresses = authorizedFromAddress && authorizedToAddress
? authorizedFromAddress !== authorizedToAddress
? [authorizedFromAddress, authorizedToAddress].sort()
: [authorizedFromAddress]
: authorizedFromAddress
? [authorizedFromAddress]
: authorizedToAddress
? [authorizedToAddress]
: undefined;
if (!addresses)
if (!authorizedAddresses)
return undefined;

const cacheKey = this.getUserInvolvedSwapsCacheKey(
addresses,
authorizedAddresses,
swapCurrencyInfos.fromCurrencyInfo.currency.id,
swapCurrencyInfos.toCurrencyInfo.currency.id
);
let swapsInfo = this.userInvolvedSwapsCache.get<UserInvolvedSwapsInfo>(cacheKey);
if (swapsInfo)
return swapsInfo;

const involvedSwaps = await this.getInvolvedSwaps(addresses);
const involvedSwaps = await this.getInvolvedSwaps(authorizedAddresses);
swapsInfo = await this.getUserInvolvedSwapsInfoByActiveSwaps(involvedSwaps);
this.userInvolvedSwapsCache.set(cacheKey, swapsInfo);

Expand Down

0 comments on commit e9b606c

Please sign in to comment.