forked from datcsv/tez-tax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_IS_Generation.R
102 lines (94 loc) · 4.42 KB
/
02_IS_Generation.R
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
################################################################################
# #
# Copyright 2022 datcsv #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# #
################################################################################
# Load operations data
load(file="data/operations.RData")
# Split nested features in operations data (Better way using tidyverse?)
operations$initiatorAlias <- operations$initiator$alias
operations$initiatorAddress <- operations$initiator$address
operations$senderAlias <- operations$sender$alias
operations$SenderAddress <- operations$sender$address
operations$targetAlias <- operations$target$alias
operations$targetAddress <- operations$target$address
operations$parameterEntry <- operations$parameter$entrypoint
operations$parameterValue <- operations$parameter$value
operations$quote <- operations$quote[[1]]
# Clean operations data
operations %<>%
group_by(., hash) %>%
mutate(.,
sumBakerFee = sum(bakerFee, na.rm=TRUE),
sumStorageFee = sum(storageFee, na.rm=TRUE),
sumAllocationFee = sum(allocationFee, na.rm=TRUE)
) %>%
ungroup(.)
operations %<>%
filter(., (type == "transaction") | (SenderAddress %in% wallets)) %>%
distinct(., id, hash, .keep_all=TRUE) %>%
mutate(.,
xtzAmount = ifelse(
(status != "backtracked") & (status != "failed") &
(!is.na(amount)) & (type == "transaction"),
amount / 1000000, 0
),
xtzFee = ifelse(
(status != "backtracked") & (status != "failed"),
(sumBakerFee + sumStorageFee + sumAllocationFee) / 1000000,
(sumBakerFee) / 1000000
),
xtzSent = ifelse(SenderAddress %in% wallets, xtzAmount + xtzFee, 0),
xtzReceived = ifelse(targetAddress %in% wallets, xtzAmount, 0),
parameterValue = ifelse(parameterValue == "NULL", NA, parameterValue),
tokenID = NA,
tokenAmount = 0,
tokenSender = NA,
tokenReceiver = NA,
tokenSent = 0,
tokenReceived = 0,
walletTx = TRUE,
xtzBuy = FALSE,
xtzProceeds = 0, # Proceeds on xtz sent
xtzGainLoss = 0, # Gain (loss) on xtz sent
tokenProceeds = 0, # Proceeds on token sent
tokenGainLoss = 0, # Gain (loss) on token sent
costBasis = NA # Cost basis of all xtz/tokens received
) %>%
select(., any_of(c(
"id", "level", "timestamp", "hash", "type", "status", "quote",
"initiatorAddress", "SenderAddress", "targetAddress", "parameterEntry",
"parameterValue", "xtzAmount", "xtzFee", "xtzSent", "xtzReceived",
"tokenID", "tokenAmount", "tokenSender", "tokenReceiver", "tokenSent",
"tokenReceived", "walletTx", "xtzBuy", "xtzProceeds", "xtzGainLoss",
"tokenProceeds", "tokenGainLoss", "costBasis"
)))
# Generate income statement from operations data:
source("functions/classify_tx.R")
# Adjust data
is %<>%
mutate(.,
timestamp = as_datetime(timestamp),
tokenSent = ifelse(tokenSender %in% wallets, tokenAmount, 0),
tokenReceived = ifelse(tokenReceiver %in% wallets, tokenAmount, 0)
) %>%
select(., -xtzAmount, -tokenAmount) %>%
arrange(., timestamp)
# Add exchange data:
save(is, file="data/is_exchange.RData")
if (!is.na(cb_path)) source("functions/cb_import.R")
# Save income statement data
save(is, file="data/is.RData")
save(is, file="data/is_original.RData")