Skip to content

Commit

Permalink
Breaking change: add fee parameter to nanopay IncrementAmount method
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang committed Mar 14, 2022
1 parent 41b2534 commit 3295767
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/wallet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func main() {
return err
}
// Send 100 NKN into channel
tx, err := np.IncrementAmount("100")
tx, err := np.IncrementAmount("100", "0.1")
txHash := tx.Hash()
txHashRef := &txHash
if err != nil {
Expand Down
21 changes: 16 additions & 5 deletions nanopay.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,12 @@ func (np *NanoPay) Recipient() string {
}

// IncrementAmount increments the NanoPay amount by delta and returns the signed
// NanoPay transaction. Delta is the string representation of the amount in unit
// of NKN to avoid precision loss. For example, "0.1" will be parsed as 0.1 NKN.
func (np *NanoPay) IncrementAmount(delta string) (*transaction.Transaction, error) {
// NanoPay transaction. If length of fee is greater than zero, it will parsed as
// transaction fee, otherwise the default transaction fee (passed when creating
// nanopay) will be used. Delta and fee are the string representation of the
// amount in unit of NKN to avoid precision loss. For example, "0.1" will be
// parsed as 0.1 NKN.
func (np *NanoPay) IncrementAmount(delta, fee string) (*transaction.Transaction, error) {
height, err := np.rpcClient.GetHeight()

np.lock.Lock()
Expand Down Expand Up @@ -132,7 +135,15 @@ func (np *NanoPay) IncrementAmount(delta string) (*transaction.Transaction, erro
return nil, err
}

tx.UnsignedTx.Fee = int64(np.fee)
if len(fee) > 0 {
feeFixed64, err := common.StringToFixed64(fee)
if err != nil {
return nil, err
}
tx.UnsignedTx.Fee = int64(feeFixed64)
} else {
tx.UnsignedTx.Fee = int64(np.fee)
}

if err := np.senderWallet.SignTransaction(tx); err != nil {
return nil, err
Expand Down Expand Up @@ -396,7 +407,7 @@ func (npc *NanoPayClaimer) Claim(tx *transaction.Transaction) (*Amount, error) {
return nil, err
}

if senderBalance.ToFixed64()+npc.prevFlushAmount < common.Fixed64(npPayload.Amount) {
if senderBalance.ToFixed64()+npc.prevFlushAmount < common.Fixed64(npPayload.Amount+tx.UnsignedTx.Fee) {
return nil, npc.closeWithError(ErrInsufficientBalance)
}

Expand Down

0 comments on commit 3295767

Please sign in to comment.