Skip to content

Commit

Permalink
#3533 Add catch of overflow error and min_to_loan > total_balance
Browse files Browse the repository at this point in the history
  • Loading branch information
ihorml authored and sschiessl-bcp committed Dec 17, 2022
1 parent 515c950 commit 097f4bb
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 66 deletions.
3 changes: 3 additions & 0 deletions app/assets/locales/locale-en.json
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,9 @@
}
},
"credit_offer": {
"min_loan_bigger_than_balance": "Min to loan > Total credit offer balance (%(min)s > %(balance)s)",
"number_is_to_big": "You have used a big number which caused overflow",
"invalid_min_bigger_amount": "Min cannot be bigger than amount",
"total_to_repay": "Repay Total",
"current_available_balance": "Current Available Balance",
"accepted_pawn": "Acceptable",
Expand Down
116 changes: 65 additions & 51 deletions app/components/Account/CreditOffer/CreateModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -351,58 +351,60 @@ class CreateModal extends React.Component {
let opData;

try {
if(parseInt(min_loan) > parseInt(amount)) {
throw new Error(
counterpart.translate('credit_offer.min_loan_bigger_than_balance', {
min: min_loan,
balance: amount
})
)
}

opData = {
owner_account: account.get("id"),
asset_type: asset_id,
balance: new Asset({
real: amount,
asset_id,
precision: asset.get("precision")
}).getAmount(),
fee_rate: (parseFloat(rate) * FEE_RATE_DENOM) / 100,
max_duration_seconds: repay_period,
min_deal_amount: new Asset({
real: min_loan,
asset_id,
precision: asset.get("precision")
}).getAmount(),
auto_disable_time: validity_period,
acceptable_collateral: pawn_assets.map(v => {
let va = ChainStore.getAsset(v.asset_id);
let p = new Price({
base: new Asset({
asset_id,
precision: asset.get("precision")
}),
quote: new Asset({
asset_id: v.asset_id,
precision: va.get("precision")
}),
// real: v.getAmount({real: true}),
real: 1 / v.getAmount({real: true}) // Keeping it consistent with the App, this may violate Graphene's price representation convention.
});
return [v.asset_id, p.toObject()];
}),
acceptable_borrowers: whitelist.map(v => {
return [
v.account.get("id"),
new Asset({
real: v.amount,
asset_id,
precision: asset.get("precision")
}).getAmount()
];
}),
fee_asset: feeAmount
};
} catch (err) {
this.setState({
createOfferError: err.toString()
})
return;
}
console.log("obj: ", opData);
CreditOfferActions.create(opData)
owner_account: account.get("id"),
asset_type: asset_id,
balance: new Asset({
real: amount,
asset_id,
precision: asset.get("precision")
}).getAmount(),
fee_rate: (parseFloat(rate) * FEE_RATE_DENOM) / 100,
max_duration_seconds: repay_period,
min_deal_amount: new Asset({
real: min_loan,
asset_id,
precision: asset.get("precision")
}).getAmount(),
auto_disable_time: validity_period,
acceptable_collateral: pawn_assets.map(v => {
let va = ChainStore.getAsset(v.asset_id);
let p = new Price({
base: new Asset({
asset_id,
precision: asset.get("precision")
}),
quote: new Asset({
asset_id: v.asset_id,
precision: va.get("precision")
}),
// real: v.getAmount({real: true}),
real: 1 / v.getAmount({real: true}) // Keeping it consistent with the App, this may violate Graphene's price representation convention.
});
return [v.asset_id, p.toObject()];
}),
acceptable_borrowers: whitelist.map(v => {
return [
v.account.get("id"),
new Asset({
real: v.amount,
asset_id,
precision: asset.get("precision")
}).getAmount()
];
}),
fee_asset: feeAmount
};
CreditOfferActions.create(opData)
.then(() => {
this.hideModal();
})
Expand All @@ -413,6 +415,18 @@ class CreateModal extends React.Component {
createOfferError: err.toString()
})
});
} catch (err) {
if(err.toString().indexOf('overflow') >= 0) {
this.setState({
createOfferError: counterpart.translate('credit_offer.number_is_to_big')
})
} else {
this.setState({
createOfferError: err.toString()
})
}
return;
}
}

_renderCreateModal() {
Expand Down
55 changes: 40 additions & 15 deletions app/components/Account/CreditOffer/EditModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ class EditModal extends React.Component {
asset_id: itemData.asset_type,
precision: asset_type_precision
});
let totalBalanceAsset = new Asset({
amount: itemData.total_balance,
asset_id: itemData.asset_type,
precision: asset_type_precision
});
let pawn_assets = itemData.acceptable_collateral.map(v => {
let bp = ChainStore.getAsset(v[1].base.asset_id).get("precision");
let qp = ChainStore.getAsset(v[1].quote.asset_id).get("precision");
Expand Down Expand Up @@ -120,6 +125,7 @@ class EditModal extends React.Component {
account: ChainStore.getAccount(itemData.owner_account, false),
amount: asset.getAmount({real: true}),
balanceAmount: asset.getAmount({real: true}),
totalBalanceAmount: totalBalanceAsset.getAmount({ real: true }),
asset_id: itemData.asset_type,
asset: null,
error: null,
Expand Down Expand Up @@ -424,7 +430,8 @@ class EditModal extends React.Component {
whitelist,
feeAmount,
offer_id,
balanceAmount
balanceAmount,
totalBalanceAmount
} = this.state;

this.setState({
Expand All @@ -434,6 +441,17 @@ class EditModal extends React.Component {
let opData;

try {
const updatedBalance = (totalBalanceAmount + (parseInt(amount) - parseInt(balanceAmount)))

if(parseInt(min_loan) > updatedBalance) {
throw new Error(
counterpart.translate('credit_offer.min_loan_bigger_than_balance', {
min: min_loan,
balance: updatedBalance.toFixed(4)
})
)
}

let asset_precision = ChainStore.getAsset(asset_id).get(
"precision"
);
Expand Down Expand Up @@ -481,22 +499,29 @@ class EditModal extends React.Component {
}),
fee_asset: feeAmount
};

if (opData.delta_amount.getAmount({real: true}) === 0) {
delete opData.delta_amount;
}
CreditOfferActions.update(opData)
.then(() => {
this.hideModal();
})
.catch(err => {
console.error(err);
});
} catch (err) {
this.setState({
submitErr: err.toString()
});
}
if (opData.delta_amount.getAmount({real: true}) === 0) {
delete opData.delta_amount;

if(err.toString().indexOf('overflow') >= 0) {
this.setState({
submitErr: counterpart.translate('credit_offer.number_is_to_big')
})
} else {
this.setState({
submitErr: err.toString()
});
}
}
CreditOfferActions.update(opData)
.then(() => {
this.hideModal();
})
.catch(err => {
// todo: visualize error somewhere
console.error(err);
});
}

_renderEditModal() {
Expand Down

0 comments on commit 097f4bb

Please sign in to comment.