Skip to content

Commit c74c66e

Browse files
committed
The best version ever
1 parent 4c4993c commit c74c66e

File tree

1 file changed

+94
-103
lines changed

1 file changed

+94
-103
lines changed

src/apps/chifra/pkg/ledger1/get_statements.go

Lines changed: 94 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ func (r *Reconciler1) GetStatements1(pos *types.AppPosition, trans *types.Transa
1919
if ledger10.AssetOfInterest(r.opts.AssetFilters, base.FAKE_ETH_ADDRESS) {
2020
var err error
2121
reconciled := false
22-
stmt := r.getStatementFromTransaction(trans)
2322
if !r.opts.UseTraces {
23+
stmt := r.getStatementFromTransaction(trans)
2424
if reconciled, err = r.trialBalance(pos, trans, stmt); err != nil {
2525
return nil, err
2626
} else {
@@ -31,14 +31,15 @@ func (r *Reconciler1) GetStatements1(pos *types.AppPosition, trans *types.Transa
3131
}
3232

3333
if r.opts.UseTraces || !reconciled {
34-
results = make([]types.Statement, 0, 20) /* reset this */
35-
if stmt, err := r.getStatementFromTraces(pos, trans, stmt); err != nil {
34+
if stmt, err := r.getStatementFromTraces(trans); err != nil {
3635
logger.Warn(colors.Yellow+"Statement at ", fmt.Sprintf("%d.%d", trans.BlockNumber, trans.TransactionIndex), " does not reconcile."+colors.Off)
3736
} else {
3837
if _, err = r.trialBalance(pos, trans, stmt); err != nil {
3938
return nil, err
4039
} else {
41-
results = append(results, *stmt)
40+
if stmt.IsMaterial() {
41+
results = append(results, *stmt)
42+
}
4243
}
4344
}
4445
}
@@ -49,21 +50,24 @@ func (r *Reconciler1) GetStatements1(pos *types.AppPosition, trans *types.Transa
4950
return nil, err
5051
} else {
5152
receiptStatements := make([]types.Statement, 0, len(statements))
52-
for _, s := range statements {
53-
reconciled, _ := r.trialBalance(pos, trans, &s)
54-
if reconciled {
55-
id := fmt.Sprintf(" %d.%d.%d", s.BlockNumber, s.TransactionIndex, s.LogIndex)
56-
logger.Progress(true, colors.Green+"Transaction", id, "reconciled "+colors.Off)
53+
for _, stmt := range statements {
54+
if reconciled, err := r.trialBalance(pos, trans, &stmt); err != nil {
55+
// TODO: Silent fail?
56+
continue
5757
} else {
58-
if os.Getenv("TEST_MODE") != "true" {
59-
id := fmt.Sprintf(" %d.%d.%d", s.BlockNumber, s.TransactionIndex, s.LogIndex)
60-
logger.Warn("Log statement at ", id, " does not reconcile.")
58+
if reconciled {
59+
id := fmt.Sprintf(" %d.%d.%d", stmt.BlockNumber, stmt.TransactionIndex, stmt.LogIndex)
60+
logger.Progress(true, colors.Green+"Transaction", id, "reconciled "+colors.Off)
61+
} else {
62+
if os.Getenv("TEST_MODE") != "true" {
63+
id := fmt.Sprintf(" %d.%d.%d", stmt.BlockNumber, stmt.TransactionIndex, stmt.LogIndex)
64+
logger.Warn("Log statement at ", id, " does not reconcile.")
65+
}
66+
}
67+
// order matters
68+
if stmt.IsMaterial() {
69+
receiptStatements = append(receiptStatements, stmt)
6170
}
62-
}
63-
64-
// order matters
65-
if s.IsMaterial() {
66-
receiptStatements = append(receiptStatements, s)
6771
}
6872
}
6973
results = append(results, receiptStatements...)
@@ -126,40 +130,19 @@ func (r *Reconciler1) getStatementFromTransaction(trans *types.Transaction) *typ
126130
return stmt
127131
}
128132

129-
func (r *Reconciler1) getStatementFromTraces(pos *types.AppPosition, trans *types.Transaction, s *types.Statement) (*types.Statement, error) {
130-
131-
stmt := *s
132-
// clear all the internal accounting values. Keeps AmountIn, AmountOut and GasOut because
133-
// those are at the top level (both the transaction itself and trace '0' have them). We
134-
// skip trace '0' because it's the same as the transaction.
135-
// stmt.AmountIn.SetUint64(0)
136-
stmt.InternalIn.SetUint64(0)
137-
stmt.MinerBaseRewardIn.SetUint64(0)
138-
stmt.MinerNephewRewardIn.SetUint64(0)
139-
stmt.MinerTxFeeIn.SetUint64(0)
140-
stmt.MinerUncleRewardIn.SetUint64(0)
141-
stmt.CorrectingIn.SetUint64(0)
142-
stmt.PrefundIn.SetUint64(0)
143-
stmt.SelfDestructIn.SetUint64(0)
144-
145-
// stmt.AmountOut.SetUint64(0)
146-
// stmt.GasOut.SetUint64(0)
147-
stmt.InternalOut.SetUint64(0)
148-
stmt.CorrectingOut.SetUint64(0)
149-
stmt.SelfDestructOut.SetUint64(0)
150-
133+
func (r *Reconciler1) getStatementFromTraces(trans *types.Transaction) (*types.Statement, error) {
151134
if traces, err := r.opts.Connection.GetTracesByTransactionHash(trans.Hash.Hex(), trans); err != nil {
152135
return nil, err
153136

154137
} else {
155-
// These values accumulate...so we use += instead of =
138+
stmt := r.getStatementFromTransaction(trans)
156139
for i, trace := range traces {
157140
if i == 0 {
158141
// the first trace is identical to the transaction itself, so we can skip it
159142
continue
160143
}
161144

162-
if trace.Action.CallType == "delegatecall" && trace.Action.To != s.AccountedFor {
145+
if trace.Action.CallType == "delegatecall" && trace.Action.To != stmt.AccountedFor {
163146
// delegate calls are not included in the transaction's gas cost, so we skip them
164147
continue
165148
}
@@ -169,7 +152,7 @@ func (r *Reconciler1) getStatementFromTraces(pos *types.AppPosition, trans *type
169152
}
170153

171154
// Do not collapse, more than one of these can be true at the same time
172-
if trace.Action.From == s.AccountedFor {
155+
if trace.Action.From == stmt.AccountedFor {
173156
stmt.InternalOut = plusEq(&stmt.InternalOut, &trace.Action.Value)
174157
stmt.Sender = trace.Action.From
175158
if trace.Action.To.IsZero() {
@@ -181,13 +164,13 @@ func (r *Reconciler1) getStatementFromTraces(pos *types.AppPosition, trans *type
181164
}
182165
}
183166

184-
if trace.Action.To == s.AccountedFor {
167+
if trace.Action.To == stmt.AccountedFor {
185168
stmt.InternalIn = plusEq(&stmt.InternalIn, &trace.Action.Value)
186169
stmt.Sender = trace.Action.From
187170
stmt.Recipient = trace.Action.To
188171
}
189172

190-
if trace.Action.SelfDestructed == s.AccountedFor {
173+
if trace.Action.SelfDestructed == stmt.AccountedFor {
191174
stmt.SelfDestructOut = plusEq(&stmt.SelfDestructOut, &trace.Action.Balance)
192175
stmt.Sender = trace.Action.SelfDestructed
193176
if stmt.Sender.IsZero() {
@@ -196,7 +179,7 @@ func (r *Reconciler1) getStatementFromTraces(pos *types.AppPosition, trans *type
196179
stmt.Recipient = trace.Action.RefundAddress
197180
}
198181

199-
if trace.Action.RefundAddress == s.AccountedFor {
182+
if trace.Action.RefundAddress == stmt.AccountedFor {
200183
stmt.SelfDestructIn = plusEq(&stmt.SelfDestructIn, &trace.Action.Balance)
201184
stmt.Sender = trace.Action.SelfDestructed
202185
if stmt.Sender.IsZero() {
@@ -205,90 +188,98 @@ func (r *Reconciler1) getStatementFromTraces(pos *types.AppPosition, trans *type
205188
stmt.Recipient = trace.Action.RefundAddress
206189
}
207190

208-
if trace.Action.Address == s.AccountedFor && !trace.Action.RefundAddress.IsZero() {
191+
if trace.Action.Address == stmt.AccountedFor && !trace.Action.RefundAddress.IsZero() {
209192
stmt.SelfDestructOut = plusEq(&stmt.SelfDestructOut, &trace.Action.Balance)
210193
// self destructed send
211194
stmt.Sender = trace.Action.Address
212195
stmt.Recipient = trace.Action.RefundAddress
213196
}
214197

215198
if trace.Result != nil {
216-
if trace.Result.Address == s.AccountedFor {
199+
if trace.Result.Address == stmt.AccountedFor {
217200
stmt.InternalIn = plusEq(&stmt.InternalIn, &trace.Action.Value)
218201
stmt.Sender = trace.Action.From
219202
stmt.Recipient = trace.Result.Address
220203
}
221204
}
222205
}
206+
return stmt, nil
223207
}
224-
225-
return &stmt, nil
226208
}
227209

228210
func (r *Reconciler1) getStatementsFromLogs(logs []types.Log) ([]types.Statement, error) {
229-
receiptStatements := make([]types.Statement, 0, 20)
211+
statements := make([]types.Statement, 0, 20)
230212
for _, log := range logs {
231-
if log.Topics[0] != topics.TransferTopic {
213+
pass1 := log.Topics[0] == topics.TransferTopic
214+
if !pass1 {
232215
continue
233216
}
234-
addrArray := []base.Address{r.opts.AccountFor}
235-
if r.opts.AppFilters.ApplyLogFilter(&log, addrArray) && ledger10.AssetOfInterest(r.opts.AssetFilters, log.Address) {
236-
normalized, err := normalize.NormalizeKnownLogs(&log)
237-
if err != nil {
238-
continue
239-
} else if normalized.IsNFT() {
217+
218+
pass2 := r.opts.AppFilters.ApplyLogFilter(&log, []base.Address{r.opts.AccountFor})
219+
if !pass2 {
220+
continue
221+
}
222+
223+
pass3 := ledger10.AssetOfInterest(r.opts.AssetFilters, log.Address)
224+
if !pass3 {
225+
continue
226+
}
227+
228+
normalized, err := normalize.NormalizeKnownLogs(&log)
229+
if err != nil {
230+
// TODO: silent fail?
231+
continue
232+
233+
} else if normalized.IsNFT() {
234+
continue
235+
236+
} else {
237+
sender := base.HexToAddress(normalized.Topics[1].Hex())
238+
recipient := base.HexToAddress(normalized.Topics[2].Hex())
239+
isSender, isRecipient := r.opts.AccountFor == sender, r.opts.AccountFor == recipient
240+
if !isSender && !isRecipient {
240241
continue
241-
} else {
242-
sender := base.HexToAddress(normalized.Topics[1].Hex())
243-
recipient := base.HexToAddress(normalized.Topics[2].Hex())
244-
isSender, isRecipient := r.opts.AccountFor == sender, r.opts.AccountFor == recipient
245-
if !isSender && !isRecipient {
246-
continue
247-
}
242+
}
248243

249-
sym := normalized.Address.DefaultSymbol()
250-
decimals := base.Value(18)
251-
name := r.names[normalized.Address]
252-
if name.Address == normalized.Address {
253-
if name.Symbol != "" {
254-
sym = name.Symbol
255-
}
256-
if name.Decimals != 0 {
257-
decimals = base.Value(name.Decimals)
258-
}
259-
}
244+
var amountIn, amountOut base.Wei
245+
amount, _ := new(base.Wei).SetString(strings.ReplaceAll(normalized.Data, "0x", ""), 16)
246+
if amount == nil {
247+
amount = base.ZeroWei
248+
}
249+
if r.opts.AccountFor == sender {
250+
amountOut = *amount
251+
}
252+
if r.opts.AccountFor == recipient {
253+
amountIn = *amount
254+
}
255+
stmt := &types.Statement{
256+
AccountedFor: r.opts.AccountFor,
257+
Sender: sender,
258+
Recipient: recipient,
259+
BlockNumber: normalized.BlockNumber,
260+
TransactionIndex: normalized.TransactionIndex,
261+
LogIndex: normalized.LogIndex,
262+
TransactionHash: normalized.TransactionHash,
263+
Timestamp: normalized.Timestamp,
264+
Asset: normalized.Address,
265+
PriceSource: "not-priced",
266+
AmountIn: amountIn,
267+
AmountOut: amountOut,
268+
}
260269

261-
var amountIn, amountOut base.Wei
262-
amount, _ := new(base.Wei).SetString(strings.ReplaceAll(normalized.Data, "0x", ""), 16)
263-
if amount == nil {
264-
amount = base.NewWei(0)
265-
}
266-
if r.opts.AccountFor == sender {
267-
amountOut = *amount
268-
}
269-
if r.opts.AccountFor == recipient {
270-
amountIn = *amount
270+
name := r.names[stmt.Asset]
271+
stmt.Symbol = stmt.Asset.DefaultSymbol()
272+
stmt.Decimals = base.Value(18)
273+
if name.Address == stmt.Asset {
274+
if name.Symbol != "" {
275+
stmt.Symbol = name.Symbol
271276
}
272-
s := types.Statement{
273-
AccountedFor: r.opts.AccountFor,
274-
Sender: sender,
275-
Recipient: recipient,
276-
BlockNumber: normalized.BlockNumber,
277-
TransactionIndex: normalized.TransactionIndex,
278-
LogIndex: normalized.LogIndex,
279-
TransactionHash: normalized.TransactionHash,
280-
Timestamp: normalized.Timestamp,
281-
Asset: normalized.Address,
282-
Symbol: sym,
283-
Decimals: decimals,
284-
SpotPrice: 0.0,
285-
PriceSource: "not-priced",
286-
AmountIn: amountIn,
287-
AmountOut: amountOut,
277+
if name.Decimals != 0 {
278+
stmt.Decimals = base.Value(name.Decimals)
288279
}
289-
receiptStatements = append(receiptStatements, s)
290280
}
281+
statements = append(statements, *stmt)
291282
}
292283
}
293-
return receiptStatements, nil
284+
return statements, nil
294285
}

0 commit comments

Comments
 (0)