From f50537513992ba0ac6444034ce042040722de525 Mon Sep 17 00:00:00 2001 From: Dan Miller Date: Tue, 12 May 2020 20:17:42 -0230 Subject: [PATCH 1/3] Fix validating of amount when sending a collectible --- app/components/Views/SendFlow/Confirm/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/components/Views/SendFlow/Confirm/index.js b/app/components/Views/SendFlow/Confirm/index.js index 8b74e40ae4a..a5f560eb3c4 100644 --- a/app/components/Views/SendFlow/Confirm/index.js +++ b/app/components/Views/SendFlow/Confirm/index.js @@ -632,7 +632,7 @@ class Confirm extends PureComponent { weiBalance = toWei(Number(selectedAsset.assetBalance)); weiInput = toWei(transaction.value); errorMessage = weiBalance.gte(weiInput) ? undefined : strings('transaction.insufficient'); - } else if (selectedAsset.isETH) { + } else if (selectedAsset.isETH || selectedAsset.tokenId) { const totalGas = gas ? gas.mul(gasPrice) : toBN('0x0'); weiBalance = hexToBN(accounts[selectedAddress].balance); weiInput = hexToBN(transaction.value).add(totalGas); From 0951e548e3a6053292b028eb558148645080cfcc Mon Sep 17 00:00:00 2001 From: Dan Miller Date: Tue, 12 May 2020 21:32:10 -0230 Subject: [PATCH 2/3] Validate collectible ownership on amount screen. --- app/components/Views/SendFlow/Amount/index.js | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/app/components/Views/SendFlow/Amount/index.js b/app/components/Views/SendFlow/Amount/index.js index 2c9913d0d68..a4e293d047c 100644 --- a/app/components/Views/SendFlow/Amount/index.js +++ b/app/components/Views/SendFlow/Amount/index.js @@ -414,6 +414,26 @@ class Amount extends PureComponent { } }; + validateCollectibleOwnership = async () => { + const { AssetsContractController } = Engine.context; + const { + transactionState: { + selectedAsset: { address, tokenId } + }, + selectedAddress + } = this.props; + try { + const owner = await AssetsContractController.getOwnerOf(address, tokenId); + const isOwner = owner.toLowerCase() === selectedAddress.toLowerCase(); + if (!isOwner) { + return strings('transaction.invalid_collectible_ownership'); + } + return undefined; + } catch (e) { + return false; + } + }; + onNext = async () => { const { navigation, @@ -425,7 +445,14 @@ class Amount extends PureComponent { } = this.props; const { inputValue, inputValueConversion, internalPrimaryCurrencyIsCrypto, hasExchangeRate } = this.state; const value = internalPrimaryCurrencyIsCrypto || !hasExchangeRate ? inputValue : inputValueConversion; - if (!selectedAsset.tokenId && this.validateAmount(value)) return; + if (!selectedAsset.tokenId && this.validateAmount(value)) { + return; + } else if (selectedAsset.tokenId) { + const invalidCollectibleOwnership = await this.validateCollectibleOwnership(); + if (invalidCollectibleOwnership) { + this.setState({ amountError: invalidCollectibleOwnership }); + } + } if (transaction.value !== undefined) { this.updateTransaction(value); From 42fb8972ede4e4c833164344c322cc52afc5bae9 Mon Sep 17 00:00:00 2001 From: Dan Miller Date: Tue, 12 May 2020 21:32:59 -0230 Subject: [PATCH 3/3] Ensure correct updating of collectible transaction after edit on the amount screen --- app/components/Views/SendFlow/Amount/index.js | 62 ++++++++++++------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/app/components/Views/SendFlow/Amount/index.js b/app/components/Views/SendFlow/Amount/index.js index a4e293d047c..40540d9403c 100644 --- a/app/components/Views/SendFlow/Amount/index.js +++ b/app/components/Views/SendFlow/Amount/index.js @@ -471,7 +471,37 @@ class Amount extends PureComponent { } }; - updateTransaction = value => { + getCollectibleTranferTransactionProperties() { + const { + selectedAsset, + transactionState: { transaction, transactionTo } + } = this.props; + + const collectibleTransferTransactionProperties = {}; + + const collectibleTransferInformation = collectiblesTransferInformation[selectedAsset.address.toLowerCase()]; + if ( + !collectibleTransferInformation || + (collectibleTransferInformation.tradable && collectibleTransferInformation.method === 'transferFrom') + ) { + collectibleTransferTransactionProperties.data = generateTransferData('transferFrom', { + fromAddress: transaction.from, + toAddress: transactionTo, + tokenId: selectedAsset.tokenId + }); + } else if (collectibleTransferInformation.tradable && collectibleTransferInformation.method === 'transfer') { + collectibleTransferTransactionProperties.data = generateTransferData('transfer', { + toAddress: transactionTo, + amount: selectedAsset.tokenId.toString(16) + }); + } + collectibleTransferTransactionProperties.to = selectedAsset.address; + collectibleTransferTransactionProperties.value = '0x0'; + + return collectibleTransferTransactionProperties; + } + + updateTransaction = (value = 0) => { const { selectedAsset, transactionState: { transaction, paymentChannelTransaction, transactionTo }, @@ -493,6 +523,11 @@ class Amount extends PureComponent { amount: BNToHex(tokenAmount) }); transactionObject.value = '0x0'; + } else if (selectedAsset.tokenId) { + const collectibleTransferTransactionProperties = this.getCollectibleTranferTransactionProperties(); + transactionObject.data = collectibleTransferTransactionProperties.data; + transactionObject.to = collectibleTransferTransactionProperties.to; + transactionObject.value = collectibleTransferTransactionProperties.value; } if (paymentChannelTransaction || selectedAsset.erc20) { @@ -515,27 +550,10 @@ class Amount extends PureComponent { transaction.to = transactionTo; transaction.value = BNToHex(toWei(value)); } else if (selectedAsset.tokenId) { - const collectibleTransferInformation = collectiblesTransferInformation[selectedAsset.address.toLowerCase()]; - if ( - !collectibleTransferInformation || - (collectibleTransferInformation.tradable && collectibleTransferInformation.method === 'transferFrom') - ) { - transaction.data = generateTransferData('transferFrom', { - fromAddress: transaction.from, - toAddress: transactionTo, - tokenId: selectedAsset.tokenId - }); - } else if ( - collectibleTransferInformation.tradable && - collectibleTransferInformation.method === 'transfer' - ) { - transaction.data = generateTransferData('transfer', { - toAddress: transactionTo, - amount: selectedAsset.tokenId.toString(16) - }); - } - transaction.to = selectedAsset.address; - transaction.value = '0x0'; + const collectibleTransferTransactionProperties = this.getCollectibleTranferTransactionProperties(); + transaction.data = collectibleTransferTransactionProperties.data; + transaction.to = collectibleTransferTransactionProperties.to; + transaction.value = collectibleTransferTransactionProperties.value; } else { const tokenAmount = toTokenMinimalUnit(value, selectedAsset.decimals); transaction.data = generateTransferData('transfer', {