/
flow_transaction.go
175 lines (157 loc) · 4.94 KB
/
flow_transaction.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) 2020-2024 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package etl
import (
"blockwatch.cc/tzgo/micheline"
"blockwatch.cc/tzindex/etl/model"
"blockwatch.cc/tzindex/rpc"
)
func (b *Builder) NewTransactionFlows(
src, dst *model.Account,
sbkr, dbkr *model.Baker,
srccon, dstcon *model.Contract,
fees, bal rpc.BalanceUpdates,
block *model.Block,
id model.OpRef) []*model.Flow {
// apply fees
flows, feespaid := b.NewFeeFlows(src, fees, id)
// transaction may burn and transfer
var moved, burnedAndMoved int64
for _, u := range bal {
// we only consider contract in/out flows and calculate
// the burn even though Ithaca makes this explict
if u.Kind == "contract" {
if u.Change < 0 {
burnedAndMoved += -u.Change
} else {
moved += u.Change
}
}
}
// create move and burn flows when necessary
if moved > 0 && dst != nil {
// debit from source
f := model.NewFlow(b.block, src, dst, id)
f.Kind = model.FlowKindBalance
f.Type = model.FlowTypeTransaction
f.AmountOut = moved
f.TokenAge = block.Age(src.LastIn)
f.IsUnshielded = srccon != nil && srccon.Features.Contains(micheline.FeatureSapling)
flows = append(flows, f)
// credit to dest
f = model.NewFlow(b.block, dst, src, id)
f.Kind = model.FlowKindBalance
f.Type = model.FlowTypeTransaction
f.AmountIn = moved
f.TokenAge = block.Age(src.LastIn)
f.IsShielded = dstcon != nil && dstcon.Features.Contains(micheline.FeatureSapling)
flows = append(flows, f)
}
if burnedAndMoved-moved > 0 {
// debit burn from source
f := model.NewFlow(b.block, src, nil, id)
f.Kind = model.FlowKindBalance
f.Type = model.FlowTypeTransaction
f.AmountOut = burnedAndMoved - moved
f.IsBurned = true
flows = append(flows, f)
}
// debit from source delegation unless source is a baker
if sbkr != nil && !src.IsBaker && feespaid+burnedAndMoved > 0 {
f := model.NewFlow(b.block, sbkr.Account, src, id)
f.Kind = model.FlowKindDelegation
f.Type = model.FlowTypeTransaction
f.AmountOut = feespaid + burnedAndMoved // fees and amount moved
flows = append(flows, f)
}
// credit to destination baker unless dest is a baker
if dbkr != nil && !dst.IsBaker && moved > 0 {
f := model.NewFlow(b.block, dbkr.Account, src, id)
f.Kind = model.FlowKindDelegation
f.Type = model.FlowTypeTransaction
f.AmountIn = moved
flows = append(flows, f)
}
b.block.Flows = append(b.block.Flows, flows...)
return flows
}
// fees are already paid by outer tx
// burn is attributed to outer source
func (b *Builder) NewInternalTransactionFlows(
origsrc, src, dst *model.Account,
origbkr, srcbkr, dstbkr *model.Baker,
srccon, dstcon *model.Contract,
bal rpc.BalanceUpdates,
block *model.Block,
id model.OpRef,
) []*model.Flow {
flows := make([]*model.Flow, 0)
// transaction may burn and transfer
var moved, burnedAndMoved int64
for _, u := range bal {
// we only consider contract in/out flows and calculate
// the burn even though Ithaca makes this explict
if u.Kind == "contract" {
if u.Change < 0 {
burnedAndMoved += -u.Change
} else {
moved += u.Change
}
}
}
// create move and burn flows when necessary
if moved > 0 && dst != nil {
// deducted from source
f := model.NewFlow(b.block, src, dst, id)
f.Kind = model.FlowKindBalance
f.Type = model.FlowTypeTransaction
f.AmountOut = moved
f.TokenAge = block.Age(src.LastIn)
f.IsUnshielded = srccon != nil && srccon.Features.Contains(micheline.FeatureSapling)
flows = append(flows, f)
// credit to dest
f = model.NewFlow(b.block, dst, src, id)
f.Kind = model.FlowKindBalance
f.Type = model.FlowTypeTransaction
f.AmountIn = moved
f.TokenAge = block.Age(src.LastIn)
f.IsShielded = dstcon != nil && dstcon.Features.Contains(micheline.FeatureSapling)
flows = append(flows, f)
}
if burnedAndMoved-moved > 0 {
// Note: use outer source to burn
f := model.NewFlow(b.block, origsrc, nil, id)
f.Kind = model.FlowKindBalance
f.Type = model.FlowTypeTransaction
f.AmountOut = burnedAndMoved - moved
f.IsBurned = true
flows = append(flows, f)
// adjust delegated balance if not a baker
if origbkr != nil && !origsrc.IsBaker {
f = model.NewFlow(b.block, origbkr.Account, origsrc, id)
f.Kind = model.FlowKindDelegation
f.Type = model.FlowTypeTransaction
f.AmountOut = burnedAndMoved - moved
flows = append(flows, f)
}
}
// handle delegation updates
// debut moved amount from source delegation if not baker
if srcbkr != nil && !src.IsBaker && moved > 0 {
f := model.NewFlow(b.block, srcbkr.Account, src, id)
f.Kind = model.FlowKindDelegation
f.Type = model.FlowTypeTransaction
f.AmountOut = moved
flows = append(flows, f)
}
// credit to dest delegate unless its a baker
if dstbkr != nil && !dst.IsBaker && moved > 0 {
f := model.NewFlow(b.block, dstbkr.Account, src, id)
f.Kind = model.FlowKindDelegation
f.Type = model.FlowTypeTransaction
f.AmountIn = moved
flows = append(flows, f)
}
b.block.Flows = append(b.block.Flows, flows...)
return flows
}