-
Notifications
You must be signed in to change notification settings - Fork 29
/
payments.go
325 lines (286 loc) · 10.7 KB
/
payments.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package keeper
import (
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/ixofoundation/ixo-blockchain/v3/x/claims/types"
"github.com/ixofoundation/ixo-blockchain/v3/x/token/types/contracts/ixo1155"
)
// --------------------------
// PAYMENT HELPERS
// --------------------------
func processPayment(ctx sdk.Context, k Keeper, receiver sdk.AccAddress, payment *types.Payment, paymentType types.PaymentType, claimId string) error {
// check that there is outcome payment to make, otherwise skip this with no error as no payment for action
paymentExists := false
if !payment.Amount.IsZero() {
paymentExists = true
}
if payment.Contract_1155Payment != nil && payment.Contract_1155Payment.Amount != 0 {
paymentExists = true
}
if !paymentExists {
return nil
}
// Get payer address
payerAddress, err := sdk.AccAddressFromBech32(payment.Account)
if err != nil {
return err
}
// Not sure if needed as authz can still be created even if sender has no fees
// check that sender has enough tokens to make the payment
// if !types.HasBalances(ctx, k.BankKeeper, payerAddress, payment.Amount) {
// return sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, "sender has insufficient funds")
// }
// get claim and collection payment is for
claim, err := k.GetClaim(ctx, claimId)
if err != nil {
return err
}
collection, err := k.GetCollection(ctx, claim.CollectionId)
if err != nil {
return err
}
var inputs []banktypes.Input
var outputs []banktypes.Output
// if there is cosmos coins in payment get input/outputs for multisend
if !payment.Amount.IsZero() {
// if evaluation payment then do split based on params
if paymentType == types.PaymentType_evaluation {
params := k.GetParams(ctx)
// Get node address
entity, exists := k.EntityKeeper.GetEntity(ctx, []byte(collection.Entity))
if !exists {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "entity did doesn't exist for did %s", collection.Entity)
}
relayerDidDoc, exists := k.IidKeeper.GetDidDocument(ctx, []byte(entity.RelayerNode))
if !exists {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "relayer node did doesn't exist for did %s", entity.RelayerNode)
}
relayerAddr, err := relayerDidDoc.GetVerificationMethodBlockchainAddress(entity.RelayerNode)
if err != nil {
return sdkerrors.Wrapf(err, "did not found in iid doc verification methods for %s", entity.RelayerNode)
}
// Calculate evaluator pay share (totals to 100) for ixo, node, and oracle
nodeFeePercentage := params.NodeFeePercentage
ixoFeePercentage := params.NetworkFeePercentage
// check that the 2 preset percentages dont go over 100%
if nodeFeePercentage.Add(ixoFeePercentage).GT(types.OneHundred) {
return types.ErrPaymentPresetPercentagesOverflow
}
oracleFeePercentage := types.OneHundred.Sub(nodeFeePercentage).Sub(ixoFeePercentage)
// Get ixo network address
ixoAddress, err := sdk.AccAddressFromBech32(params.IxoAccount)
if err != nil {
return err
}
recipients := types.NewDistribution(
types.NewDistributionShare(ixoAddress, ixoFeePercentage),
types.NewDistributionShare(relayerAddr, nodeFeePercentage),
types.NewDistributionShare(receiver, oracleFeePercentage))
// Calculate list of outputs and calculate the total output to payees based
// on the calculated wallet distributions
distributions, err := recipients.GetDistributionsFor(payment.Amount)
if err != nil {
return err
}
var countOutputs sdk.Coins
for i, share := range distributions {
// Get integer output
outputAmt, _ := share.TruncateDecimal()
address, err := recipients[i].GetAddress()
if err != nil {
return err
}
// If receiver address(last address in the distribution), then add the remainder to the receiver
if address.Equals(receiver) {
outputAmt = payment.Amount.Sub(countOutputs)
outputs = append(outputs, banktypes.NewOutput(address, outputAmt))
} else if !outputAmt.IsZero() {
// If amount not zero, add as output, for network and node
outputs = append(outputs, banktypes.NewOutput(address, outputAmt))
countOutputs = countOutputs.Sort().Add(outputAmt.Sort()...)
}
}
} else {
// if no split then recipient gets 100% payment
outputs = append(outputs, banktypes.NewOutput(receiver, payment.Amount))
}
inputs = append(inputs, banktypes.NewInput(payerAddress, payment.Amount))
}
// if no timeout in payment make payout immidiately
if payment.TimeoutNs == 0 {
if err := payout(ctx, k, inputs, outputs, paymentType, claimId, &time.Time{}, payment.Contract_1155Payment, payerAddress, receiver); err != nil {
return err
}
} else {
// get admin address
adminAddress, err := sdk.AccAddressFromBech32(collection.Admin)
if err != nil {
return err
}
// else create authz WithdrawPaymentAuthorization for receiver to execute to receive payout once timout has passed
if err := createAuthz(ctx, k, receiver, adminAddress, inputs, outputs, paymentType, claimId, payment.TimeoutNs, payment.Contract_1155Payment, payerAddress, receiver); err != nil {
return err
}
}
return nil
}
func payout(ctx sdk.Context, k Keeper, inputs []banktypes.Input, outputs []banktypes.Output, paymentType types.PaymentType, claimId string, releaseDate *time.Time, payment1155 *types.Contract1155Payment, fromAddress, toAddress sdk.AccAddress) error {
// get entity payout is for to validate if from address is valid entity module account
claim, err := k.GetClaim(ctx, claimId)
if err != nil {
return err
}
collection, err := k.GetCollection(ctx, claim.CollectionId)
if err != nil {
return err
}
_, entity, err := k.EntityKeeper.ResolveEntity(ctx, collection.Entity)
if err != nil {
return err
}
// check that fromAddress and input addresses is entity module accounts
if !entity.ContainsAccountAddress(fromAddress.String()) {
return types.ErrCollNotEntityAcc
}
for _, i := range inputs {
if !entity.ContainsAccountAddress(i.Address) {
return types.ErrCollNotEntityAcc
}
}
// distribute the payment according to the outputs for Cosmos Coins if has inputs and outputs
if len(inputs) != 0 && len(outputs) != 0 {
if err := k.BankKeeper.InputOutputCoins(ctx, inputs, outputs); err != nil {
return sdkerrors.Wrapf(types.ErrPaymentWithdrawFailed, "%s", err)
}
}
// pay 1155 contract payment if has one
if payment1155 != nil && payment1155.Amount != 0 {
encodedTransferMessage, err := ixo1155.Marshal(ixo1155.WasmSendFrom{
SendFrom: ixo1155.SendFrom{
From: fromAddress.String(),
To: toAddress.String(),
Token_id: payment1155.TokenId,
Value: fmt.Sprint(payment1155.Amount),
},
})
if err != nil {
return err
}
contractAddress, err := sdk.AccAddressFromBech32(payment1155.Address)
if err != nil {
return err
}
_, err = k.WasmKeeper.Execute(
ctx,
contractAddress,
fromAddress,
encodedTransferMessage,
sdk.NewCoins(sdk.NewCoin("uixo", sdk.ZeroInt())),
)
if err != nil {
return err
}
}
// update payment status to success
updatePaymentStatus(ctx, k, paymentType, claimId, types.PaymentStatus_paid)
// emit the events
if err := ctx.EventManager().EmitTypedEvents(
&types.PaymentWithdrawnEvent{
Withdraw: &types.WithdrawPaymentConstraints{
ClaimId: claimId,
Inputs: inputs,
Outputs: outputs,
PaymentType: paymentType,
ReleaseDate: releaseDate,
Contract_1155Payment: payment1155,
FromAddress: fromAddress.String(),
ToAddress: toAddress.String(),
},
},
); err != nil {
return err
}
return nil
}
func createAuthz(ctx sdk.Context, k Keeper, receiver, admin sdk.AccAddress, inputs []banktypes.Input, outputs []banktypes.Output, paymentType types.PaymentType, claimId string, timeoutNs time.Duration, payment1155 *types.Contract1155Payment, fromAddress, toAddress sdk.AccAddress) error {
// get users current WithdrawPaymentAuthorization authorization
authzMsgType := sdk.MsgTypeURL(&types.MsgWithdrawPayment{})
auth, _ := k.AuthzKeeper.GetCleanAuthorization(ctx, receiver, admin, authzMsgType)
// making expiration date for authz grant one year from now (until indefinite time fix for cosmos version)
expiration := ctx.BlockTime().Add(time.Hour * 24 * 365)
releaseDate := ctx.BlockTime().Add(timeoutNs)
var constraints []*types.WithdrawPaymentConstraints
constraint := types.WithdrawPaymentConstraints{
ClaimId: claimId,
Inputs: inputs,
Outputs: outputs,
PaymentType: paymentType,
ReleaseDate: &releaseDate,
Contract_1155Payment: payment1155,
FromAddress: fromAddress.String(),
ToAddress: toAddress.String(),
}
// if have a WithdrawPaymentAuthorization authz use current constaints to append new one to
if auth != nil {
switch k := auth.(type) {
case *types.WithdrawPaymentAuthorization:
constraints = k.Constraints
default:
return fmt.Errorf("existing Authorizations for route %s is not of type WithdrawPaymentAuthorization", authzMsgType)
}
}
constraints = append(constraints, &constraint)
// persist new grant
if err := k.AuthzKeeper.SaveGrant(ctx, receiver, admin, types.NewWithdrawPaymentAuthorization(admin.String(), constraints), expiration); err != nil {
return err
}
// update payment status to authorized
updatePaymentStatus(ctx, k, paymentType, claimId, types.PaymentStatus_authorized)
// emit the events
if err := ctx.EventManager().EmitTypedEvents(
&types.PaymentWithdrawCreatedEvent{
Withdraw: &types.WithdrawPaymentConstraints{
ClaimId: claimId,
Inputs: inputs,
Outputs: outputs,
PaymentType: paymentType,
ReleaseDate: &releaseDate,
Contract_1155Payment: payment1155,
FromAddress: fromAddress.String(),
ToAddress: toAddress.String(),
},
},
); err != nil {
return err
}
return nil
}
func updatePaymentStatus(ctx sdk.Context, k Keeper, paymentType types.PaymentType, claimId string, paymentStatus types.PaymentStatus) error {
// get claim and payment is for
claim, err := k.GetClaim(ctx, claimId)
if err != nil {
return err
}
switch paymentType {
case types.PaymentType_approval:
claim.PaymentsStatus.Approval = paymentStatus
case types.PaymentType_evaluation:
claim.PaymentsStatus.Evaluation = paymentStatus
case types.PaymentType_submission:
claim.PaymentsStatus.Submission = paymentStatus
}
// persist claim changes
k.SetClaim(ctx, claim)
// emit the events
if err := ctx.EventManager().EmitTypedEvents(
&types.ClaimUpdatedEvent{
Claim: &claim,
},
); err != nil {
return err
}
return nil
}