Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split custom amounts #77

Closed
wants to merge 10 commits into from
Closed

Split custom amounts #77

wants to merge 10 commits into from

Conversation

gandlafbtc
Copy link
Collaborator

When minting, we can select to give custom instructions to the mint as for what token amounts we want to mint.

With the following approach we can pass an AmountPreference:

  • give a list of tokens we would like to mint [{amount: <number>, count: <number>}, ....]
  • amount has to be a power of 2 number, and the count represents how many tokens of this instance should be minted
  • if the provided ln payment is greater than the AmountPreference the rest of the tokens will be created in the most optimized way (as one-each powers of 2)
  • if the provided payment is smaller than the AmountPreference it will cut off before overflowing
  • if the cut off leaves a gap between payment and AmountPreference, the gap will be refilled with the optimal powers of 2s

@gandlafbtc gandlafbtc marked this pull request as ready for review June 26, 2023 08:21
@callebtc
Copy link
Contributor

callebtc commented Jul 8, 2023

In Nutshell, I implemented this as an explicit list of each amount requested. In my opinion this could be better suited here as it allows the implementer of the library to add helpers like (amount, count). It would also result in less code with token selection logic etc...

# in case the user wants a specific split, we create a list of amounts
optional_split = None
if split:
    assert amount % split == 0, "split must be divisor or amount"
    assert amount >= split, "split must smaller or equal amount"
    n_splits = amount // split
    optional_split = [split] * n_splits
    logger.debug(f"Requesting split with {n_splits} * {split} sat tokens.")

 r = await wallet.mint(amount, split=optional_split)

In wallet.mint:

# specific split
if split:
    logger.trace(f"Mint with split: {split}")
    assert sum(split) == amount, "split must sum to amount"
    allowed_amounts = [2**i for i in range(settings.max_order)]
    for a in split:
        if a not in allowed_amounts:
            raise Exception(
                f"Can only mint amounts with 2^n up to {2**settings.max_order}."
            )

# if no split was specified, we use the canonical split
amounts = split or amount_split(amount)
proofs = await super().mint(amounts, hash) 

@gandlafbtc gandlafbtc mentioned this pull request Sep 21, 2023
4 tasks
@gandlafbtc gandlafbtc closed this Sep 21, 2023
@gandlafbtc
Copy link
Collaborator Author

merged with #89

@gandlafbtc gandlafbtc deleted the split-custom-amounts branch February 19, 2024 05:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants