@@ -68,6 +68,47 @@ func (vacc *VirtualAccount) Receive(asset string, sender Sender) []Posting {
68
68
return postings
69
69
}
70
70
71
+ func send (
72
+ source AccountValue ,
73
+ destination AccountValue ,
74
+ amount * big.Int ,
75
+ asset string ,
76
+ color string ,
77
+ ) []Posting {
78
+ switch source := source .(type ) {
79
+ case AccountAddress :
80
+
81
+ switch destination := destination .(type ) {
82
+ case AccountAddress :
83
+ return []Posting {{
84
+ Source : string (source ),
85
+ Destination : string (destination ),
86
+ Amount : amount ,
87
+ Asset : coloredAsset (asset , & color ),
88
+ }}
89
+ case VirtualAccount :
90
+ panic ("TODO2" )
91
+ }
92
+
93
+ case VirtualAccount :
94
+
95
+ switch dest := destination .(type ) {
96
+ case AccountAddress :
97
+ return source .Pull (asset , nil , Sender {
98
+ Account : dest ,
99
+ Amount : amount ,
100
+ Color : color ,
101
+ })
102
+
103
+ case VirtualAccount :
104
+ panic ("TODO4" )
105
+ }
106
+
107
+ }
108
+
109
+ panic ("non exhaustive match" )
110
+ }
111
+
71
112
// Treat this stack as debts and use the sender to repay debt.
72
113
// Return the emitted postings and the remaining amount
73
114
func repayWithSender (s * fundsStack , asset string , credit Sender ) ([]Posting , * big.Int ) {
@@ -76,31 +117,20 @@ func repayWithSender(s *fundsStack, asset string, credit Sender) ([]Posting, *bi
76
117
var postings []Posting
77
118
78
119
// Take away the debt that the credit allows for
79
- clearedDebt := s .PullColored (remainingAmt , credit .Color )
80
- for _ , receiver := range clearedDebt {
81
- switch creditAccount := credit .Account .(type ) {
82
- case VirtualAccount :
83
- pulled := creditAccount .Pull (asset , nil , receiver )
84
- postings = append (postings , pulled ... )
85
-
86
- case AccountAddress :
87
- // TODO do we need this in the other case?
88
- remainingAmt .Sub (remainingAmt , receiver .Amount )
89
-
90
- switch receiverAccount := receiver .Account .(type ) {
91
- case AccountAddress :
92
- postings = append (postings , Posting {
93
- Source : string (creditAccount ),
94
- Destination : string (receiverAccount ),
95
- Amount : receiver .Amount ,
96
- Asset : coloredAsset (asset , & credit .Color ),
97
- })
98
-
99
- case VirtualAccount :
100
- panic ("TODO repay vacc" )
101
- }
102
- }
120
+ pulled := s .PullColored (credit .Amount , credit .Color )
121
+ for _ , pulledSender := range pulled {
122
+ newPostings := send (
123
+ credit .Account ,
124
+ pulledSender .Account ,
125
+ pulledSender .Amount ,
126
+ asset ,
127
+ credit .Color ,
128
+ )
129
+ postings = append (postings , newPostings ... )
130
+ }
103
131
132
+ for _ , p := range postings {
133
+ remainingAmt .Sub (remainingAmt , p .Amount )
104
134
}
105
135
106
136
return postings , remainingAmt
@@ -128,36 +158,18 @@ func (vacc *VirtualAccount) Pull(asset string, overdraft *big.Int, receiver Send
128
158
pulled := credits .PullColored (receiver .Amount , receiver .Color )
129
159
130
160
remainingAmt := new (big.Int ).Set (receiver .Amount )
161
+
131
162
var postings []Posting
132
- for _ , pulledSender := range pulled {
133
- switch pulledSenderAccount := pulledSender .Account .(type ) {
134
- case VirtualAccount :
135
- recPostings := pulledSenderAccount .Pull (asset , overdraft , receiver )
136
- postings = append (postings , recPostings ... )
137
- continue
138
163
139
- case AccountAddress :
140
- remainingAmt .Sub (remainingAmt , pulledSender .Amount )
141
- switch receiverAccount := receiver .Account .(type ) {
142
- case AccountAddress :
143
- postings = append (postings , Posting {
144
- Source : string (pulledSenderAccount ),
145
- Destination : string (receiverAccount ),
146
- Amount : pulledSender .Amount ,
147
- Asset : coloredAsset (asset , & receiver .Color ),
148
- })
149
-
150
- case VirtualAccount :
151
- // TODO either include in coverage or simply this
152
- panic ("UNRECHED" )
153
-
154
- return receiverAccount .Receive (asset , Sender {
155
- vacc ,
156
- pulledSender .Amount ,
157
- receiver .Color ,
158
- })
159
- }
160
- }
164
+ for _ , pulledSender := range pulled {
165
+ newPostings := send (
166
+ pulledSender .Account ,
167
+ receiver .Account ,
168
+ pulledSender .Amount ,
169
+ asset ,
170
+ receiver .Color ,
171
+ )
172
+ postings = append (postings , newPostings ... )
161
173
}
162
174
163
175
// TODO it looks like we aren't using overdraft now. How's that possible?
0 commit comments