Skip to content

Commit

Permalink
function to store all remaining Mana on an output
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberphysic4l committed Nov 7, 2023
1 parent 99d6925 commit 3b2f6cc
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions builder/transaction_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,36 @@ func (b *TransactionBuilder) AddTaggedDataPayload(payload *iotago.TaggedData) *T
// TransactionFunc is a function which receives a SignedTransaction as its parameter.
type TransactionFunc func(tx *iotago.SignedTransaction)

func (b *TransactionBuilder) StoreRemainingManaInOutput(targetSlot iotago.SlotIndex, blockIssuerAccountID iotago.AccountID, storedManaOutputIndex int) *TransactionBuilder {
setBuildError := func(err error) *TransactionBuilder {
b.occurredBuildErr = err
return b
}

if storedManaOutputIndex >= len(b.transaction.Outputs) {
return setBuildError(ierrors.Errorf("given storedManaOutputIndex does not exist: %d", storedManaOutputIndex))
}

unboundManaInputsLeftoverBalance, err := b.calculateAvailableManaLeftover(targetSlot, 0, blockIssuerAccountID)
if err != nil {
return setBuildError(err)
}

// move the remaining mana to stored mana on the specified output index
switch output := b.transaction.Outputs[storedManaOutputIndex].(type) {
case *iotago.BasicOutput:
output.Mana += unboundManaInputsLeftoverBalance
case *iotago.AccountOutput:
output.Mana += unboundManaInputsLeftoverBalance
case *iotago.NFTOutput:
output.Mana += unboundManaInputsLeftoverBalance
default:
return setBuildError(ierrors.Wrapf(iotago.ErrUnknownOutputType, "output type %T does not support stored mana", output))
}

return b
}

func (b *TransactionBuilder) AllotRequiredManaAndStoreRemainingManaInOutput(targetSlot iotago.SlotIndex, rmc iotago.Mana, blockIssuerAccountID iotago.AccountID, storedManaOutputIndex int) *TransactionBuilder {
setBuildError := func(err error) *TransactionBuilder {
b.occurredBuildErr = err
Expand Down

0 comments on commit 3b2f6cc

Please sign in to comment.