lukechampine/us

Output splitting

jkawamoto opened this issue · 4 comments

We can form only about 10~20 contracts with one output. (Actually, we can form more contracts but we get Lock: host rejected LoopLock request: no record of that contract if we try to use such contracts) To form more contracts stably, we need to split outputs. This commit 72a1e2b looks like it implements something related to output splitting, but it looks like DistributeFunds is not used anywhere. Is that still WIP or do we need to implement something?

Currently, you will need to call DistributeFunds in your own code. I suppose it should probably be added to walrus-cli so that you don't need to write any custom code, though.

I'm not sure how we should use DistributeFunds. Could you give examples?

Yeah, it's not the most user-friendly interface. DistributeFunds takes a set of inputs and a desired output value size, and tells you how many outputs you can create along with the leftover change. The use case is: you have a small number of high-value outputs, and you want a large number of small outputs.

Say you have a wallet with two outputs, each worth 1 million SC. You want to form lots of file contracts, and you know that no contract will cost more than 500 SC. You would construct the output-splitting transaction like so (simplified somewhat):

inputs, _ := wc.UnspentOutputs(false)
splitAddr, _ := wc.NextAddress()
scPerOutput := types.SiacoinPrecision.Mul64(500)
feePerByte, _ := wc.TransactionFee()
n, fee, change := wallet.DistributeFunds(inputs, scPerOutput, feePerByte)
outputs := make([]types.SiacoinOutput, n)
for i := range outputs {
    outputs[i].Value = scPerOutput
    outputs[i].UnlockHash = splitAddr
}
outputs = append(outputs, types.SiacoinOutput{
    Value:      change,
    UnlockHash: splitAddr,
}
txn := types.Transaction{
    SiacoinInputs:  inputs,
    SiacoinOutputs: outputs,
    MinerFees:      []types.Currency{fee},
}

...ok, I'll add a walrus-cli command for it 😅

Okay, split command added in lukechampine/walrus-cli@c9a1688