Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix validating of amount when sending a collectible #1565

Merged
merged 4 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 68 additions & 23 deletions app/components/Views/SendFlow/Amount/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -444,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 },
Expand All @@ -466,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) {
Expand All @@ -488,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', {
Expand Down
2 changes: 1 addition & 1 deletion app/components/Views/SendFlow/Confirm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

@rickycodes rickycodes May 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we be checking for either or, or both?

or should we just be checking for selectedAsset.isETH?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think checking both is okay. When sending a collectible, we only need to validate that the user's balance exceeds the total gas, which is what weiInput will equal in the case of a collectible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const totalGas = gas ? gas.mul(gasPrice) : toBN('0x0');
weiBalance = hexToBN(accounts[selectedAddress].balance);
weiInput = hexToBN(transaction.value).add(totalGas);
Expand Down