[#873] morph/client: Add function that calculates notary deposit

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-10-12 15:24:19 +03:00 committed by Alex Vanin
parent d55456f3ac
commit d2096b392c

View file

@ -715,3 +715,33 @@ const alreadyOnChainErrorMessage = "already on chain"
func alreadyOnChainError(err error) bool { func alreadyOnChainError(err error) bool {
return strings.Contains(err.Error(), alreadyOnChainErrorMessage) return strings.Contains(err.Error(), alreadyOnChainErrorMessage)
} }
// CalculateNotaryDepositAmount calculates notary deposit amount
// using the rule:
// IF notaryBalance < gasBalance * gasMul {
// DEPOSIT gasBalance / gasDiv
// } ELSE {
// DEPOSIT 1
// }
// gasMul and gasDiv must be positive.
func CalculateNotaryDepositAmount(c *Client, gasMul, gasDiv int64) (fixedn.Fixed8, error) {
notaryBalance, err := c.GetNotaryDeposit()
if err != nil {
return 0, fmt.Errorf("could not get notary balance: %w", err)
}
gasBalance, err := c.GasBalance()
if err != nil {
return 0, fmt.Errorf("could not get GAS balance: %w", err)
}
var depositAmount int64
if gasBalance*gasMul > notaryBalance {
depositAmount = gasBalance / gasDiv
} else {
depositAmount = 1
}
return fixedn.Fixed8(depositAmount), nil
}