forked from Salvionied/apollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModels.go
156 lines (140 loc) · 3.92 KB
/
Models.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package apollo
import (
"github.com/SundaeSwap-finance/apollo/serialization"
"github.com/SundaeSwap-finance/apollo/serialization/Address"
"github.com/SundaeSwap-finance/apollo/serialization/AssetName"
"github.com/SundaeSwap-finance/apollo/serialization/MultiAsset"
"github.com/SundaeSwap-finance/apollo/serialization/PlutusData"
"github.com/SundaeSwap-finance/apollo/serialization/Policy"
"github.com/SundaeSwap-finance/apollo/serialization/TransactionOutput"
"github.com/SundaeSwap-finance/apollo/serialization/Value"
"github.com/SundaeSwap-finance/apollo/txBuilding/Backend/Base"
"github.com/SundaeSwap-finance/apollo/txBuilding/Utils"
)
type Unit struct {
PolicyId string
Name string
Quantity int
}
func (u *Unit) ToValue() Value.Value {
val := Value.Value{}
policyId := Policy.PolicyId{Value: u.PolicyId}
ma := MultiAsset.MultiAsset[int64]{}
aname := AssetName.NewAssetNameFromString(u.Name)
ma[policyId] = map[AssetName.AssetName]int64{aname: int64(u.Quantity)}
val.AddAssets(ma)
return val
}
func NewUnit(policyId string, name string, quantity int) Unit {
return Unit{
PolicyId: policyId,
Name: name,
Quantity: quantity,
}
}
type PaymentI interface {
EnsureMinUTXO(cc Base.ChainContext)
ToTxOut() *TransactionOutput.TransactionOutput
ToValue() Value.Value
}
type Payment struct {
Lovelace int
Receiver Address.Address
Units []Unit
Datum *PlutusData.PlutusData
DatumHash []byte
IsInline bool
}
func PaymentFromTxOut(txOut *TransactionOutput.TransactionOutput) *Payment {
payment := &Payment{
Receiver: txOut.GetAddress(),
Lovelace: int(txOut.GetAmount().GetCoin()),
IsInline: false,
}
hasDatumHash := false
hasInlineDatum := false
if txOut.GetDatumHash() != nil {
payment.DatumHash = txOut.GetDatumHash().Payload
hasDatumHash = true
}
if !txOut.GetDatum().Equal(PlutusData.PlutusData{}) {
payment.Datum = txOut.GetDatum()
hasInlineDatum = true
}
if hasDatumHash && hasInlineDatum {
payment.IsInline = true
}
for policyId, assets := range txOut.GetAmount().GetAssets() {
for assetName, quantity := range assets {
payment.Units = append(payment.Units, Unit{
PolicyId: policyId.Value,
Name: assetName.String(),
Quantity: int(quantity),
})
}
}
return payment
}
func NewPayment(receiver string, lovelace int, units []Unit) *Payment {
decoded_address, _ := Address.DecodeAddress(receiver)
return &Payment{
Lovelace: lovelace,
Receiver: decoded_address,
Units: units,
}
}
func NewPaymentFromValue(receiver Address.Address, value Value.Value) *Payment {
payment := &Payment{
Receiver: receiver,
Lovelace: int(value.GetCoin()),
}
for policyId, assets := range value.GetAssets() {
for assetName, quantity := range assets {
payment.Units = append(payment.Units, Unit{
PolicyId: policyId.Value,
Name: assetName.String(),
Quantity: int(quantity),
})
}
}
return payment
}
func (p *Payment) ToValue() Value.Value {
v := Value.Value{}
for _, unit := range p.Units {
v.AddAssets(unit.ToValue().GetAssets())
}
v.AddLovelace(int64(p.Lovelace))
return v
}
func (p *Payment) EnsureMinUTXO(cc Base.ChainContext) {
if len(p.Units) == 0 && p.Lovelace >= 1_000_000 {
return
}
txOut := p.ToTxOut()
coins := Utils.MinLovelacePostAlonzo(*txOut, cc)
if int64(p.Lovelace) < coins {
p.Lovelace = int(coins)
}
}
func (p *Payment) ToTxOut() *TransactionOutput.TransactionOutput {
txOut := TransactionOutput.SimpleTransactionOutput(p.Receiver, p.ToValue())
if p.IsInline {
txO := TransactionOutput.TransactionOutput{}
txO.IsPostAlonzo = true
l := PlutusData.DatumOptionInline(p.Datum)
txO.PostAlonzo.Datum = &l
txO.PostAlonzo.Address = p.Receiver
txO.PostAlonzo.Amount = p.ToValue().ToAlonzoValue()
if p.Datum != nil {
txOut.SetDatum(p.Datum)
}
return &txO
} else {
if p.DatumHash != nil {
txOut.PreAlonzo.DatumHash = serialization.DatumHash{Payload: p.DatumHash}
txOut.PreAlonzo.HasDatum = true
}
}
return &txOut
}