-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathmodel.ts
244 lines (214 loc) · 6.69 KB
/
model.ts
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
import { Model, ModelOptions, QueryContext } from 'objection'
import { DbErrors } from 'objection-db-errors'
import { LiquidityAccount } from '../../../accounting/service'
import { Asset } from '../../../asset/model'
import { ConnectorAccount } from '../../../connector/core/rafiki'
import {
PaymentPointerSubresource,
PaymentPointer
} from '../../payment_pointer/model'
import { Quote } from '../../quote/model'
import { Amount, AmountJSON, serializeAmount } from '../../amount'
import { WebhookEvent } from '../../../webhook/model'
import { OutgoingPayment as OpenPaymentsOutgoingPayment } from 'open-payments'
export class OutgoingPaymentGrant extends DbErrors(Model) {
public static get modelPaths(): string[] {
return [__dirname]
}
public static readonly tableName = 'outgoingPaymentGrants'
public id!: string
}
export class OutgoingPayment
extends PaymentPointerSubresource
implements ConnectorAccount, LiquidityAccount
{
public static readonly tableName = 'outgoingPayments'
public static readonly urlPath = '/outgoing-payments'
static get virtualAttributes(): string[] {
return ['sendAmount', 'receiveAmount', 'quote', 'sentAmount', 'receiver']
}
public state!: OutgoingPaymentState
// The "| null" is necessary so that `$beforeUpdate` can modify a patch to remove the error. If `$beforeUpdate` set `error = undefined`, the patch would ignore the modification.
public error?: string | null
public stateAttempts!: number
public grantId?: string
public get receiver(): string {
return this.quote.receiver
}
public get sendAmount(): Amount {
return this.quote.sendAmount
}
private sentAmountValue?: bigint
public get sentAmount(): Amount {
return {
value: this.sentAmountValue || BigInt(0),
assetCode: this.asset.code,
assetScale: this.asset.scale
}
}
public set sentAmount(amount: Amount) {
this.sentAmountValue = amount.value
}
public get receiveAmount(): Amount {
return this.quote.receiveAmount
}
public description?: string
public externalRef?: string
public quote!: Quote
public get assetId(): string {
return this.quote.assetId
}
public getUrl(paymentPointer: PaymentPointer): string {
return `${paymentPointer.url}${OutgoingPayment.urlPath}/${this.id}`
}
public get asset(): Asset {
return this.quote.asset
}
public get failed(): boolean {
return this.state === OutgoingPaymentState.Failed
}
// Outgoing peer
public peerId?: string
static get relationMappings() {
return {
...super.relationMappings,
quote: {
relation: Model.HasOneRelation,
modelClass: Quote,
join: {
from: 'outgoingPayments.id',
to: 'quotes.id'
}
}
}
}
$beforeUpdate(opts: ModelOptions, queryContext: QueryContext): void {
super.$beforeUpdate(opts, queryContext)
if (opts.old && this.state) {
if (!this.stateAttempts) {
this.stateAttempts = 0
}
}
}
public toData({
amountSent,
balance
}: {
amountSent: bigint
balance: bigint
}): PaymentData {
const data: PaymentData = {
payment: {
id: this.id,
paymentPointerId: this.paymentPointerId,
state: this.state,
receiver: this.receiver,
sendAmount: {
...this.sendAmount,
value: this.sendAmount.value.toString()
},
receiveAmount: {
...this.receiveAmount,
value: this.receiveAmount.value.toString()
},
sentAmount: {
...this.sendAmount,
value: amountSent.toString()
},
stateAttempts: this.stateAttempts,
createdAt: new Date(+this.createdAt).toISOString(),
updatedAt: new Date(+this.updatedAt).toISOString(),
balance: balance.toString()
}
}
if (this.description) {
data.payment.description = this.description
}
if (this.externalRef) {
data.payment.externalRef = this.externalRef
}
if (this.error) {
data.payment.error = this.error
}
if (this.peerId) {
data.payment.peerId = this.peerId
}
return data
}
public toOpenPaymentsType(
paymentPointer: PaymentPointer
): OpenPaymentsOutgoingPayment {
return {
id: this.getUrl(paymentPointer),
paymentPointer: paymentPointer.url,
quoteId: this.quote?.getUrl(paymentPointer) ?? undefined,
receiveAmount: serializeAmount(this.receiveAmount),
sendAmount: serializeAmount(this.sendAmount),
sentAmount: serializeAmount(this.sentAmount),
receiver: this.receiver,
failed: this.failed,
externalRef: this.externalRef ?? undefined,
description: this.description ?? undefined,
createdAt: this.createdAt.toISOString(),
updatedAt: this.updatedAt.toISOString()
}
}
}
export enum OutgoingPaymentState {
// Initial state.
// Awaiting money from the user's wallet account to be deposited to the payment account to reserve it for the payment.
// On success, transition to `SENDING`.
// On failure, transition to `FAILED`.
Funding = 'FUNDING',
// Pay from the account to the destination.
// On success, transition to `COMPLETED`.
Sending = 'SENDING',
// The payment failed. (Though some money may have been delivered).
Failed = 'FAILED',
// Successful completion.
Completed = 'COMPLETED'
}
export enum PaymentDepositType {
PaymentCreated = 'outgoing_payment.created'
}
export enum PaymentWithdrawType {
PaymentFailed = 'outgoing_payment.failed',
PaymentCompleted = 'outgoing_payment.completed'
}
export const PaymentEventType = {
...PaymentDepositType,
...PaymentWithdrawType
}
export type PaymentEventType = PaymentDepositType | PaymentWithdrawType
export interface OutgoingPaymentResponse {
id: string
paymentPointerId: string
createdAt: string
receiver: string
sendAmount: AmountJSON
receiveAmount: AmountJSON
description?: string
externalRef?: string
failed: boolean
updatedAt: string
sentAmount: AmountJSON
}
export type PaymentData = {
payment: Omit<OutgoingPaymentResponse, 'failed'> & {
error?: string
state: OutgoingPaymentState
stateAttempts: number
balance: string
peerId?: string
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
export const isPaymentEventType = (o: any): o is PaymentEventType =>
Object.values(PaymentEventType).includes(o)
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
export const isPaymentEvent = (o: any): o is PaymentEvent =>
o instanceof WebhookEvent && isPaymentEventType(o.type)
export class PaymentEvent extends WebhookEvent {
public type!: PaymentEventType
public data!: PaymentData
}