Move ExchangeTransferSimulator into contract-exchange tests since that's the only place it's still used and we no longer want to expose it to external developers

This commit is contained in:
fabioberger
2019-11-07 12:46:34 +00:00
parent 2915ee08ea
commit fba3870ef1
20 changed files with 56 additions and 2140 deletions

View File

@@ -0,0 +1,119 @@
import { BigNumber } from '@0x/utils';
import * as _ from 'lodash';
import { AbstractBalanceAndProxyAllowanceFetcher } from '../abstract/abstract_balance_and_proxy_allowance_fetcher';
import { AbstractBalanceAndProxyAllowanceLazyStore } from '../abstract/abstract_balance_and_proxy_allowance_lazy_store';
/**
* Copy on read store for balances/proxyAllowances of tokens/accounts
*/
export class BalanceAndProxyAllowanceLazyStore implements AbstractBalanceAndProxyAllowanceLazyStore {
private readonly _balanceAndProxyAllowanceFetcher: AbstractBalanceAndProxyAllowanceFetcher;
private _balance: {
[assetData: string]: {
[userAddress: string]: BigNumber;
};
};
private _proxyAllowance: {
[assetData: string]: {
[userAddress: string]: BigNumber;
};
};
/**
* Instantiates a BalanceAndProxyAllowanceLazyStore
* @param balanceAndProxyAllowanceFetcher Class the implements the AbstractBalanceAndProxyAllowanceFetcher
* @return Instance of BalanceAndProxyAllowanceLazyStore
*/
constructor(balanceAndProxyAllowanceFetcher: AbstractBalanceAndProxyAllowanceFetcher) {
this._balanceAndProxyAllowanceFetcher = balanceAndProxyAllowanceFetcher;
this._balance = {};
this._proxyAllowance = {};
}
/**
* Get a users balance of an asset
* @param assetData AssetData of interest
* @param userAddress Ethereum address of interest
*/
public async getBalanceAsync(assetData: string, userAddress: string): Promise<BigNumber> {
if (this._balance[assetData] === undefined || this._balance[assetData][userAddress] === undefined) {
const balance = await this._balanceAndProxyAllowanceFetcher.getBalanceAsync(assetData, userAddress);
this.setBalance(assetData, userAddress, balance);
}
const cachedBalance = this._balance[assetData][userAddress];
return cachedBalance;
}
/**
* Set the balance of an asset for a user
* @param assetData AssetData of interest
* @param userAddress Ethereum address of interest
*/
public setBalance(assetData: string, userAddress: string, balance: BigNumber): void {
if (this._balance[assetData] === undefined) {
this._balance[assetData] = {};
}
this._balance[assetData][userAddress] = balance;
}
/**
* Clear the balance of an asset for a user
* @param assetData AssetData of interest
* @param userAddress Ethereum address of interest
*/
public deleteBalance(assetData: string, userAddress: string): void {
if (this._balance[assetData] !== undefined) {
delete this._balance[assetData][userAddress];
if (_.isEmpty(this._balance[assetData])) {
delete this._balance[assetData];
}
}
}
/**
* Get the 0x asset proxy allowance
* @param assetData AssetData of interest
* @param userAddress Ethereum address of interest
*/
public async getProxyAllowanceAsync(assetData: string, userAddress: string): Promise<BigNumber> {
if (
this._proxyAllowance[assetData] === undefined ||
this._proxyAllowance[assetData][userAddress] === undefined
) {
const proxyAllowance = await this._balanceAndProxyAllowanceFetcher.getProxyAllowanceAsync(
assetData,
userAddress,
);
this.setProxyAllowance(assetData, userAddress, proxyAllowance);
}
const cachedProxyAllowance = this._proxyAllowance[assetData][userAddress];
return cachedProxyAllowance;
}
/**
* Set the 0x asset proxy allowance
* @param assetData AssetData of interest
* @param userAddress Ethereum address of interest
*/
public setProxyAllowance(assetData: string, userAddress: string, proxyAllowance: BigNumber): void {
if (this._proxyAllowance[assetData] === undefined) {
this._proxyAllowance[assetData] = {};
}
this._proxyAllowance[assetData][userAddress] = proxyAllowance;
}
/**
* Clear the 0x asset proxy allowance
* @param assetData AssetData of interest
* @param userAddress Ethereum address of interest
*/
public deleteProxyAllowance(assetData: string, userAddress: string): void {
if (this._proxyAllowance[assetData] !== undefined) {
delete this._proxyAllowance[assetData][userAddress];
if (_.isEmpty(this._proxyAllowance[assetData])) {
delete this._proxyAllowance[assetData];
}
}
}
/**
* Delete all balances & allowances
*/
public deleteAll(): void {
this._balance = {};
this._proxyAllowance = {};
}
}

View File

@@ -0,0 +1,90 @@
import { BigNumber } from '@0x/utils';
import * as _ from 'lodash';
import { AbstractOrderFilledCancelledFetcher } from '../abstract/abstract_order_filled_cancelled_fetcher';
import { AbstractOrderFilledCancelledLazyStore } from '../abstract/abstract_order_filled_cancelled_lazy_store';
/**
* Copy on read store for balances/proxyAllowances of tokens/accounts
*/
export class OrderFilledCancelledLazyStore implements AbstractOrderFilledCancelledLazyStore {
private readonly _orderFilledCancelledFetcher: AbstractOrderFilledCancelledFetcher;
private _filledTakerAmount: {
[orderHash: string]: BigNumber;
};
private _isCancelled: {
[orderHash: string]: boolean;
};
/**
* Instantiate a OrderFilledCancelledLazyStore
* @param orderFilledCancelledFetcher Class instance that implements the AbstractOrderFilledCancelledFetcher
* @returns An instance of OrderFilledCancelledLazyStore
*/
constructor(orderFilledCancelledFetcher: AbstractOrderFilledCancelledFetcher) {
this._orderFilledCancelledFetcher = orderFilledCancelledFetcher;
this._filledTakerAmount = {};
this._isCancelled = {};
}
/**
* Get the filledTakerAssetAmount of an order
* @param orderHash OrderHash from order of interest
* @return filledTakerAssetAmount
*/
public async getFilledTakerAmountAsync(orderHash: string): Promise<BigNumber> {
if (this._filledTakerAmount[orderHash] === undefined) {
const filledTakerAmount = await this._orderFilledCancelledFetcher.getFilledTakerAmountAsync(orderHash);
this.setFilledTakerAmount(orderHash, filledTakerAmount);
}
const cachedFilledTakerAmount = this._filledTakerAmount[orderHash];
return cachedFilledTakerAmount;
}
/**
* Set the filledTakerAssetAmount of an order
* @param orderHash OrderHash from order of interest
* @param filledTakerAmount Desired filledTakerAssetAmount
*/
public setFilledTakerAmount(orderHash: string, filledTakerAmount: BigNumber): void {
this._filledTakerAmount[orderHash] = filledTakerAmount;
}
/**
* Clear the filledTakerAssetAmount of an order
* @param orderHash OrderHash from order of interest
*/
public deleteFilledTakerAmount(orderHash: string): void {
delete this._filledTakerAmount[orderHash];
}
/**
* Set whether an order has been cancelled or not
* @param orderHash OrderHash from order of interest
* @param isCancelled Whether this order should be cancelled or not
*/
public setIsCancelled(orderHash: string, isCancelled: boolean): void {
this._isCancelled[orderHash] = isCancelled;
}
/**
* Clear whether the order has been cancelled if already set
* @param orderHash OrderHash from order of interest
*/
public deleteIsCancelled(orderHash: string): void {
delete this._isCancelled[orderHash];
}
/**
* Clear all filled/cancelled state
*/
public deleteAll(): void {
this.deleteAllFilled();
this.deleteAllIsCancelled();
}
/**
* Clear all cancelled state
*/
public deleteAllIsCancelled(): void {
this._isCancelled = {};
}
/**
* Clear all filled state
*/
public deleteAllFilled(): void {
this._filledTakerAmount = {};
}
}