forked from echovl/cardano-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
body_builder.go
68 lines (57 loc) · 1.44 KB
/
body_builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package cardano
import "time"
const (
shelleyStartTimestamp = 1596491091
shelleyStartSlot = 4924800
slotMargin = 1200
)
var ShelleyProtocol = ProtocolParams{
MinimumUtxoValue: 1000000,
MinFeeA: 44,
MinFeeB: 155381,
}
func LiveTTL() uint64 {
shelleyStart := time.Unix(shelleyStartTimestamp, 0)
return uint64(shelleyStartSlot + time.Since(shelleyStart).Seconds())
}
type TXBodyBuilder struct {
Protocol ProtocolParams
TTL uint64
}
func (builder TXBodyBuilder) Build(receiver Address, pickedUtxos []Utxo, amount uint64, change Address) (*TransactionBody, error) {
var inputAmount uint64
var inputs []TransactionInput
for _, utxo := range pickedUtxos {
inputs = append(inputs, TransactionInput{
ID: utxo.TxId.Bytes(),
Index: utxo.Index,
})
inputAmount += utxo.Amount
}
var outputs []TransactionOutput
outputs = append(outputs, TransactionOutput{
Address: receiver.Bytes(),
Amount: amount,
})
body := TransactionBody{
Inputs: inputs,
Outputs: outputs,
Ttl: builder.ttl(),
}
if err := body.addFee(inputAmount, change, builder.protocol()); err != nil {
return nil, err
}
return &body, nil
}
func (builder TXBodyBuilder) ttl() uint64 {
if builder.TTL == 0 {
return LiveTTL() + slotMargin
}
return builder.TTL
}
func (builder TXBodyBuilder) protocol() ProtocolParams {
if builder.Protocol == (ProtocolParams{}) {
return ShelleyProtocol
}
return builder.Protocol
}