From d2096b392c6d87e869ecac5eb261422d2032213c Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 12 Oct 2021 15:24:19 +0300 Subject: [PATCH] [#873] morph/client: Add function that calculates notary deposit Signed-off-by: Pavel Karpy --- pkg/morph/client/notary.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pkg/morph/client/notary.go b/pkg/morph/client/notary.go index 21dfb8fd8..858ca3741 100644 --- a/pkg/morph/client/notary.go +++ b/pkg/morph/client/notary.go @@ -715,3 +715,33 @@ const alreadyOnChainErrorMessage = "already on chain" func alreadyOnChainError(err error) bool { 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 +}