Comments and cleanup

This commit is contained in:
Fabio Berger
2018-06-19 17:25:08 +02:00
parent 6641f34d76
commit 1a0b9e4612

View File

@@ -12,6 +12,10 @@ interface ProxyIdToAssetWrappers {
[proxyId: number]: AbstractAssetWrapper; [proxyId: number]: AbstractAssetWrapper;
} }
/**
* This class abstracts away the differences between ERC20 and ERC721 tokens so that
* the logic that uses it does not need to care what standard a token belongs to.
*/
export class AssetWrapper { export class AssetWrapper {
private _proxyIdToAssetWrappers: ProxyIdToAssetWrappers; private _proxyIdToAssetWrappers: ProxyIdToAssetWrappers;
constructor(assetWrappers: AbstractAssetWrapper[]) { constructor(assetWrappers: AbstractAssetWrapper[]) {
@@ -29,7 +33,6 @@ export class AssetWrapper {
const balance = await assetWrapper.getBalanceAsync(userAddress, assetData); const balance = await assetWrapper.getBalanceAsync(userAddress, assetData);
return balance; return balance;
} }
case constants.ERC721_PROXY_ID: { case constants.ERC721_PROXY_ID: {
const assetWrapper = this._proxyIdToAssetWrappers[proxyId] as ERC721Wrapper; const assetWrapper = this._proxyIdToAssetWrappers[proxyId] as ERC721Wrapper;
const assetProxyData = assetProxyUtils.decodeERC721AssetData(assetData); const assetProxyData = assetProxyUtils.decodeERC721AssetData(assetData);
@@ -41,7 +44,6 @@ export class AssetWrapper {
const balance = isOwner ? new BigNumber(1) : new BigNumber(0); const balance = isOwner ? new BigNumber(1) : new BigNumber(0);
return balance; return balance;
} }
default: default:
throw errorUtils.spawnSwitchErr('proxyId', proxyId); throw errorUtils.spawnSwitchErr('proxyId', proxyId);
} }
@@ -54,7 +56,6 @@ export class AssetWrapper {
await assetWrapper.setBalanceAsync(userAddress, assetData, desiredBalance); await assetWrapper.setBalanceAsync(userAddress, assetData, desiredBalance);
return; return;
} }
case constants.ERC721_PROXY_ID: { case constants.ERC721_PROXY_ID: {
if (!desiredBalance.eq(0) && !desiredBalance.eq(1)) { if (!desiredBalance.eq(0) && !desiredBalance.eq(1)) {
throw new Error(`Balance for ERC721 token can only be set to 0 or 1. Got: ${desiredBalance}`); throw new Error(`Balance for ERC721 token can only be set to 0 or 1. Got: ${desiredBalance}`);
@@ -82,11 +83,6 @@ export class AssetWrapper {
tokenOwner, tokenOwner,
userAddress, userAddress,
); );
} else if (
(userAddress !== tokenOwner && desiredBalance.eq(0)) ||
(tokenOwner === userAddress && desiredBalance.eq(1))
) {
return; // noop
} else if (tokenOwner === userAddress && desiredBalance.eq(0)) { } else if (tokenOwner === userAddress && desiredBalance.eq(0)) {
// Burn token // Burn token
await erc721Wrapper.burnAsync(assetProxyData.tokenAddress, assetProxyData.tokenId, userAddress); await erc721Wrapper.burnAsync(assetProxyData.tokenAddress, assetProxyData.tokenId, userAddress);
@@ -94,7 +90,6 @@ export class AssetWrapper {
} }
break; break;
} }
default: default:
throw errorUtils.spawnSwitchErr('proxyId', proxyId); throw errorUtils.spawnSwitchErr('proxyId', proxyId);
} }
@@ -107,7 +102,6 @@ export class AssetWrapper {
const allowance = await assetWrapper.getProxyAllowanceAsync(userAddress, assetData); const allowance = await assetWrapper.getProxyAllowanceAsync(userAddress, assetData);
return allowance; return allowance;
} }
case constants.ERC721_PROXY_ID: { case constants.ERC721_PROXY_ID: {
const assetWrapper = this._proxyIdToAssetWrappers[proxyId] as ERC721Wrapper; const assetWrapper = this._proxyIdToAssetWrappers[proxyId] as ERC721Wrapper;
const erc721ProxyData = assetProxyUtils.decodeERC721AssetData(assetData); const erc721ProxyData = assetProxyUtils.decodeERC721AssetData(assetData);
@@ -123,7 +117,6 @@ export class AssetWrapper {
const allowance = isProxyApproved || isProxyApprovedForAllAsync ? new BigNumber(1) : new BigNumber(0); const allowance = isProxyApproved || isProxyApprovedForAllAsync ? new BigNumber(1) : new BigNumber(0);
return allowance; return allowance;
} }
default: default:
throw errorUtils.spawnSwitchErr('proxyId', proxyId); throw errorUtils.spawnSwitchErr('proxyId', proxyId);
} }
@@ -140,7 +133,6 @@ export class AssetWrapper {
await assetWrapper.setAllowanceAsync(userAddress, assetData, desiredAllowance); await assetWrapper.setAllowanceAsync(userAddress, assetData, desiredAllowance);
return; return;
} }
case constants.ERC721_PROXY_ID: { case constants.ERC721_PROXY_ID: {
if (!desiredAllowance.eq(0) && !desiredAllowance.eq(1)) { if (!desiredAllowance.eq(0) && !desiredAllowance.eq(1)) {
throw new Error(`Allowance for ERC721 token can only be set to 0 or 1. Got: ${desiredAllowance}`); throw new Error(`Allowance for ERC721 token can only be set to 0 or 1. Got: ${desiredAllowance}`);
@@ -164,8 +156,9 @@ export class AssetWrapper {
assetProxyData.tokenAddress, assetProxyData.tokenAddress,
assetProxyData.tokenId, assetProxyData.tokenId,
); );
// TODO: We should have a way to deal with this. Things get hairier once we are testing // HACK: We do not currently support ApprovedForAll when setting proxy allowance
// batch fills // This was intentional since unsetting ApprovedForAll, will unset approval for unrelated
// tokens other then the one specified in the call to this method.
if (isProxyApprovedForAll) { if (isProxyApprovedForAll) {
throw new Error(`We don't currently support the use of "approveAll" functionality for ERC721.`); throw new Error(`We don't currently support the use of "approveAll" functionality for ERC721.`);
} }
@@ -177,21 +170,16 @@ export class AssetWrapper {
if (!isProxyApproved && desiredAllowance.eq(1)) { if (!isProxyApproved && desiredAllowance.eq(1)) {
await erc721Wrapper.approveProxyAsync(assetProxyData.tokenAddress, assetProxyData.tokenId); await erc721Wrapper.approveProxyAsync(assetProxyData.tokenAddress, assetProxyData.tokenId);
} else if (isProxyApproved && desiredAllowance.eq(0)) { } else if (isProxyApproved && desiredAllowance.eq(0)) {
// Remove approval
await erc721Wrapper.approveAsync( await erc721Wrapper.approveAsync(
constants.NULL_ADDRESS, constants.NULL_ADDRESS,
assetProxyData.tokenAddress, assetProxyData.tokenAddress,
assetProxyData.tokenId, assetProxyData.tokenId,
); );
} else if (
(!isProxyApproved && desiredAllowance.eq(0)) ||
(isProxyApproved && desiredAllowance.eq(1))
) {
return; // noop
} }
break; break;
} }
default: default:
throw errorUtils.spawnSwitchErr('proxyId', proxyId); throw errorUtils.spawnSwitchErr('proxyId', proxyId);
} }