-
Notifications
You must be signed in to change notification settings - Fork 5
/
open_swap_builder.js
164 lines (148 loc) · 5.72 KB
/
open_swap_builder.js
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
import isUndefined from 'lodash/isUndefined'
import xdr from '../generated/xdr_generated'
import { BaseOperation } from './base_operation'
import { Keypair } from '../keypair'
import { Hyper } from 'js-xdr'
import { Hasher } from '../util/hasher'
import {
validateAmount,
validateBalanceKey,
validateCreatorDetails,
validateUint64
} from '../../utils/validators'
export class OpenSwapBuilder {
static prepareAttrs (opts) {
let attrs = {}
if (!Keypair.isValidBalanceKey(opts.sourceBalance)) {
throw new TypeError('sourceBalance is invalid')
}
if (Keypair.isValidPublicKey(opts.destination)) {
attrs.destination = new xdr.OpenSwapOpDestination.account(
Keypair.fromAccountId(opts.destination).xdrAccountId()
)
} else if (Keypair.isValidBalanceKey(opts.destination)) {
attrs.destination = new xdr.OpenSwapOpDestination.balance(
Keypair.fromBalanceId(opts.destination).xdrBalanceId()
)
} else {
throw new TypeError('opts.destination is invalid')
}
if (isUndefined(opts.feeData)) {
throw new Error('feeData argument must be defined')
}
try {
OpenSwapBuilder.ensureFeeValid(opts.feeData.sourceFee)
} catch (e) {
throw new TypeError('sourceFee.' + e.message)
}
try {
OpenSwapBuilder.ensureFeeValid(opts.feeData.destinationFee)
} catch (e) {
throw new TypeError('destination.' + e.message)
}
let sourceFee = new xdr.Fee({
percent: BaseOperation._toUnsignedXDRAmount(opts.feeData.sourceFee.percent),
fixed: BaseOperation._toUnsignedXDRAmount(opts.feeData.sourceFee.fixed),
ext: new xdr.FeeExt(xdr.LedgerVersion.emptyVersion())
})
let destinationFee = new xdr.Fee({
percent: BaseOperation._toUnsignedXDRAmount(
opts.feeData.destinationFee.percent
),
fixed: BaseOperation._toUnsignedXDRAmount(
opts.feeData.destinationFee.fixed
),
ext: new xdr.FeeExt(xdr.LedgerVersion.emptyVersion())
})
attrs.feeData = new xdr.PaymentFeeData({
sourceFee,
destinationFee,
sourcePaysForDest: opts.feeData.sourcePaysForDest,
ext: new xdr.PaymentFeeDataExt(xdr.LedgerVersion.emptyVersion())
})
attrs.details = JSON.stringify(opts.details)
attrs.sourceBalance = Keypair.fromBalanceId(opts.sourceBalance).xdrBalanceId()
attrs.amount = BaseOperation._toUnsignedXDRAmount(opts.amount)
attrs.secretHash = Hasher.hash(opts.secretHash)
attrs.lockTime = Hyper.fromString(opts.lockTime)
attrs.ext = new xdr.EmptyExt(xdr.LedgerVersion.emptyVersion())
return attrs
}
static ensureFeeValid (fee) {
if (!BaseOperation.isValidAmount(fee.fixed, true)) {
throw new TypeError('fixed fee must be of type String and represent a positive number')
}
if (!BaseOperation.isValidAmount(fee.percent, true)) {
throw new TypeError('fixed fee must be of type String and represent a positive number')
}
}
/**
* Creates OpenSwap operation where destination is AccountID or BalanceID
* @param {object} opts
* @param {string} opts.sourceBalance
* @param {string} opts.destination
* @param {number|string} opts.amount
* @param {object} opts.feeData
* * @param {object} opts.feeData.sourceFee
* * * @param {number|string} opts.feeData.sourceFee.percent
* * * @param {number|string} opts.feeData.sourceFee.fixed
* * @param {object} opts.feeData.destinationFee
* * * @param {number|string} opts.feeData.destinationFee.percent
* * * @param {number|string} opts.feeData.destinationFee.fixed
* * @param {bool} opts.feeData.sourcePaysForDest
* @param {string} opts.secretHash
* @param {string} opts.lockTime
* @param {object} opts.details
* @param {string} [opts.source] - The source account for the payment. Defaults to the transaction's source account.
* @returns {xdr.OpenSwapOp}
*/
static openSwap (opts) {
validateBalanceKey({ value: opts.sourceBalance, fieldName: 'opts.sourceBalance' })
validateCreatorDetails({ value: opts.details, fieldName: 'opts.details' })
validateUint64({ value: opts.lockTime, fieldName: 'opts.lockTime' })
validateAmount({ value: opts.amount, fieldName: 'opts.amount' })
let attrs = OpenSwapBuilder.prepareAttrs(opts)
let openSwapOp = new xdr.OpenSwapOp(attrs)
let opAttrs = {}
opAttrs.body = xdr.OperationBody.openSwap(openSwapOp)
BaseOperation.setSourceAccount(opAttrs, opts)
return new xdr.Operation(opAttrs)
}
static openSwapToObject (result, attrs) {
result.sourceBalance = BaseOperation.balanceIdtoString(attrs.sourceBalance())
switch (attrs.destination().switch()) {
case xdr.PaymentDestinationType.account(): {
result.destination = BaseOperation.accountIdtoAddress(attrs.destination().accountId())
break
}
case xdr.PaymentDestinationType.balance(): {
result.destination = BaseOperation.balanceIdtoString(attrs.destination().balanceId())
break
}
}
result.amount = BaseOperation._fromXDRAmount(attrs.amount())
result.feeData = {
sourceFee: {
fixed: BaseOperation._fromXDRAmount(
attrs.feeData().sourceFee().fixed()
),
percent: BaseOperation._fromXDRAmount(
attrs.feeData().sourceFee().percent()
)
},
destinationFee: {
fixed: BaseOperation._fromXDRAmount(
attrs.feeData().destinationFee().fixed()
),
percent: BaseOperation._fromXDRAmount(
attrs.feeData().destinationFee().percent()
)
},
sourcePaysForDest: attrs.feeData().sourcePaysForDest()
}
result.details = JSON.parse(attrs.details())
result.secretHash = attrs.secretHash().toString('hex')
result.lockTime = attrs.lockTime().toString()
return result
}
}