aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2018-06-20 19:56:27 +0800
committerFabio Berger <me@fabioberger.com>2018-06-20 19:56:27 +0800
commitfb7d425244fd754c478f432c5a64d52917071fbc (patch)
tree6a2659b02c2220346cdcde31eedb83b9d661ec15
parent3fab40efe553446770c8858afd0020f613ca4d4c (diff)
downloaddexon-sol-tools-fb7d425244fd754c478f432c5a64d52917071fbc.tar
dexon-sol-tools-fb7d425244fd754c478f432c5a64d52917071fbc.tar.gz
dexon-sol-tools-fb7d425244fd754c478f432c5a64d52917071fbc.tar.bz2
dexon-sol-tools-fb7d425244fd754c478f432c5a64d52917071fbc.tar.lz
dexon-sol-tools-fb7d425244fd754c478f432c5a64d52917071fbc.tar.xz
dexon-sol-tools-fb7d425244fd754c478f432c5a64d52917071fbc.tar.zst
dexon-sol-tools-fb7d425244fd754c478f432c5a64d52917071fbc.zip
Add support for setting allowance to unlimited
-rw-r--r--packages/contracts/src/utils/asset_wrapper.ts31
-rw-r--r--packages/contracts/src/utils/erc721_wrapper.ts11
2 files changed, 35 insertions, 7 deletions
diff --git a/packages/contracts/src/utils/asset_wrapper.ts b/packages/contracts/src/utils/asset_wrapper.ts
index 11697eca8..fd543621f 100644
--- a/packages/contracts/src/utils/asset_wrapper.ts
+++ b/packages/contracts/src/utils/asset_wrapper.ts
@@ -139,8 +139,14 @@ export class AssetWrapper {
return;
}
case constants.ERC721_PROXY_ID: {
- if (!desiredAllowance.eq(0) && !desiredAllowance.eq(1)) {
- throw new Error(`Allowance for ERC721 token can only be set to 0 or 1. Got: ${desiredAllowance}`);
+ if (
+ !desiredAllowance.eq(0) &&
+ !desiredAllowance.eq(1) &&
+ !desiredAllowance.eq(constants.UNLIMITED_ALLOWANCE_IN_BASE_UNITS)
+ ) {
+ throw new Error(
+ `Allowance for ERC721 token can only be set to 0, 1 or 2^256-1. Got: ${desiredAllowance}`,
+ );
}
const erc721Wrapper = this._proxyIdToAssetWrappers[proxyId] as ERC721Wrapper;
const assetProxyData = assetProxyUtils.decodeERC721AssetData(assetData);
@@ -161,11 +167,22 @@ export class AssetWrapper {
assetProxyData.tokenAddress,
assetProxyData.tokenId,
);
- // HACK: We do not currently support ApprovedForAll when setting proxy allowance
- // 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) {
- throw new Error(`We don't currently support the use of "approveAll" functionality for ERC721.`);
+ if (!isProxyApprovedForAll && desiredAllowance.eq(constants.UNLIMITED_ALLOWANCE_IN_BASE_UNITS)) {
+ const isApproved = true;
+ await erc721Wrapper.approveProxyForAllAsync(
+ assetProxyData.tokenAddress,
+ assetProxyData.tokenId,
+ isApproved,
+ );
+ } else if (isProxyApprovedForAll && desiredAllowance.eq(0)) {
+ const isApproved = false;
+ await erc721Wrapper.approveProxyForAllAsync(
+ assetProxyData.tokenAddress,
+ assetProxyData.tokenId,
+ isApproved,
+ );
+ } else if (isProxyApprovedForAll && desiredAllowance.eq(constants.UNLIMITED_ALLOWANCE_IN_BASE_UNITS)) {
+ return; // Noop
}
const isProxyApproved = await erc721Wrapper.isProxyApprovedAsync(
diff --git a/packages/contracts/src/utils/erc721_wrapper.ts b/packages/contracts/src/utils/erc721_wrapper.ts
index 7da3b6781..6df9fa12e 100644
--- a/packages/contracts/src/utils/erc721_wrapper.ts
+++ b/packages/contracts/src/utils/erc721_wrapper.ts
@@ -88,6 +88,17 @@ export class ERC721Wrapper {
const proxyAddress = (this._proxyContract as ERC721ProxyContract).address;
await this.approveAsync(proxyAddress, tokenAddress, tokenId);
}
+ public async approveProxyForAllAsync(tokenAddress: string, tokenId: BigNumber, isApproved: boolean): Promise<void> {
+ const tokenContract = this._getTokenContractFromAssetData(tokenAddress);
+ const tokenOwner = await this.ownerOfAsync(tokenAddress, tokenId);
+ const proxyAddress = (this._proxyContract as ERC721ProxyContract).address;
+ await this._web3Wrapper.awaitTransactionSuccessAsync(
+ await tokenContract.setApprovalForAll.sendTransactionAsync(proxyAddress, isApproved, {
+ from: tokenOwner,
+ }),
+ constants.AWAIT_TRANSACTION_MINED_MS,
+ );
+ }
public async approveAsync(to: string, tokenAddress: string, tokenId: BigNumber): Promise<void> {
const tokenContract = this._getTokenContractFromAssetData(tokenAddress);
const tokenOwner = await this.ownerOfAsync(tokenAddress, tokenId);