-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransaction.js
290 lines (231 loc) · 11.2 KB
/
Transaction.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
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
// Copyright (c) 2020 Cryptogogue, Inc. All Rights Reserved.
import * as entitlements from './entitlements';
import * as util from './util';
import * as fgc from 'fgc';
import { action, computed, observable } from 'mobx';
//const debugLog = function () {}
const debugLog = function ( ...args ) { console.log ( '@TX:', ...args ); }
export const TRANSACTION_TYPE = {
ACCOUNT_POLICY: 'ACCOUNT_POLICY',
AFFIRM_KEY: 'AFFIRM_KEY',
BETA_GET_ASSETS: 'BETA_GET_ASSETS',
BETA_GET_DECK: 'BETA_GET_DECK',
BUY_ASSETS: 'BUY_ASSETS',
CANCEL_OFFER: 'CANCEL_OFFER',
HARD_RESET: 'HARD_RESET',
IDENTIFY_ACCOUNT: 'IDENTIFY_ACCOUNT',
KEY_POLICY: 'KEY_POLICY',
NEW_ACCOUNT: 'NEW_ACCOUNT',
OPEN_ACCOUNT: 'OPEN_ACCOUNT',
OFFER_ASSETS: 'OFFER_ASSETS',
PUBLISH_SCHEMA: 'PUBLISH_SCHEMA',
PUBLISH_SCHEMA_AND_RESET: 'PUBLISH_SCHEMA_AND_RESET',
REGISTER_MINER: 'REGISTER_MINER',
RENAME_ACCOUNT: 'RENAME_ACCOUNT',
RESERVE_ACCOUNT_NAME: 'RESERVE_ACCOUNT_NAME',
RUN_SCRIPT: 'RUN_SCRIPT',
SELECT_REWARD: 'SELECT_REWARD',
SEND_ASSETS: 'SEND_ASSETS',
SEND_VOL: 'SEND_VOL',
STAMP_ASSETS: 'STAMP_ASSETS',
SET_ENTITLEMENTS: 'SET_ENTITLEMENTS',
SET_IDENTITY_PROVIDER: 'SET_IDENTITY_PROVIDER',
SET_MINIMUM_GRATUITY: 'SET_MINIMUM_GRATUITY',
SET_TERMS_OF_SERVICE: 'SET_TERMS_OF_SERVICE',
UPGRADE_ASSETS: 'UPGRADE_ASSETS',
UPDATE_MINER_INFO: 'UPDATE_MINER_INFO',
};
//================================================================//
// Transaction
//================================================================//
export class Transaction {
get accountID () { return this.maker.accountName; }
get cost () { return ( this.body.maker.gratuity || 0 ) + ( this.body.maker.transferTax || 0 ) + this.virtual_getCost (); }
get friendlyName () { return Transaction.friendlyNameForType ( this.body.type ); }
get maker () { return this.body.maker; }
get nonce () { return this.maker.nonce; }
get type () { return this.body.type; }
get uuid () { return this.body.uuid || ''; }
get vol () { return this.virtual_getTaxableVOL (); }
get weight () { return this.virtual_getWeight (); }
//----------------------------------------------------------------//
static checkEntitlement ( type, policy ) {
if ( policy ) {
if ( entitlements.check ( policy, type )) return true;
switch ( type ) {
case TRANSACTION_TYPE.REGISTER_MINER:
return entitlements.check ( policy, 'SELF_REGISTER_MINER' );
case TRANSACTION_TYPE.SET_ENTITLEMENTS:
return entitlements.check ( policy, 'PUBLISH_SCHEMA' );
}
}
return false;
}
//----------------------------------------------------------------//
constructor ( body ) {
this.assetsFiltered = {};
this.offerID = false;
this.body = body;
}
//----------------------------------------------------------------//
static friendlyNameForType ( type ) {
switch ( type ) {
case TRANSACTION_TYPE.ACCOUNT_POLICY: return 'Account Policy';
case TRANSACTION_TYPE.AFFIRM_KEY: return 'Affirm Key';
case TRANSACTION_TYPE.BETA_GET_DECK: return 'BETA Get Deck';
case TRANSACTION_TYPE.BETA_GET_ASSETS: return 'BETA Get Assets';
case TRANSACTION_TYPE.BUY_ASSETS: return 'Buy Assets';
case TRANSACTION_TYPE.CANCEL_OFFER: return 'Cancel Offer';
case TRANSACTION_TYPE.IDENTIFY_ACCOUNT: return 'Identify Account';
case TRANSACTION_TYPE.KEY_POLICY: return 'Key Policy';
case TRANSACTION_TYPE.OFFER_ASSETS: return 'Sell Assets';
case TRANSACTION_TYPE.OPEN_ACCOUNT: return 'Sponsor Account';
case TRANSACTION_TYPE.PUBLISH_SCHEMA: return 'Publish Schema';
case TRANSACTION_TYPE.PUBLISH_SCHEMA_AND_RESET: return 'Publish Schema and Reset';
case TRANSACTION_TYPE.REGISTER_MINER: return 'Register Miner';
case TRANSACTION_TYPE.RENAME_ACCOUNT: return 'Rename Account';
case TRANSACTION_TYPE.RESERVE_ACCOUNT_NAME: return 'Reserve Account Name';
case TRANSACTION_TYPE.RUN_SCRIPT: return 'Run Script';
case TRANSACTION_TYPE.SELECT_REWARD: return 'Select Reward';
case TRANSACTION_TYPE.SEND_ASSETS: return 'Send Assets';
case TRANSACTION_TYPE.SEND_VOL: return 'Send VOL';
case TRANSACTION_TYPE.STAMP_ASSETS: return 'Stamp Assets';
case TRANSACTION_TYPE.SET_ENTITLEMENTS: return 'Set Entitlements';
case TRANSACTION_TYPE.SET_IDENTITY_PROVIDER: return 'Set Identity Provider';
case TRANSACTION_TYPE.SET_MINIMUM_GRATUITY: return 'Set Minimum Gratuity';
case TRANSACTION_TYPE.SET_TERMS_OF_SERVICE: return 'Set Terms of Service';
case TRANSACTION_TYPE.UPGRADE_ASSETS: return 'Upgrade Assets';
case TRANSACTION_TYPE.UPDATE_MINER_INFO: return 'Update Miner Info';
}
return 'UNKNOWN';
}
//----------------------------------------------------------------//
static fromBody ( body ) {
switch ( body.type ) {
case TRANSACTION_TYPE.BUY_ASSETS: return new Transaction_BuyAssets ( body );
case TRANSACTION_TYPE.IDENTIFY_ACCOUNT: return new Transaction_IdentifyAccount ( body );
case TRANSACTION_TYPE.NEW_ACCOUNT: return new Transaction_NewAccount ( body );
case TRANSACTION_TYPE.OPEN_ACCOUNT: return new Transaction_OpenAccount ( body );
case TRANSACTION_TYPE.RUN_SCRIPT: return new Transaction_RunScript ( body );
case TRANSACTION_TYPE.SEND_VOL: return new Transaction_SendVOL ( body );
case TRANSACTION_TYPE.STAMP_ASSETS: return new Transaction_StampAssets ( body );
default: return new Transaction ( body );
}
}
//----------------------------------------------------------------//
@action
static load ( transaction ) {
return Transaction.fromBody ( transaction.body );
}
//----------------------------------------------------------------//
@action
setAssetsFiltered ( assetIDs, filterStatus ) {
this.assetsFiltered = this.assetsFiltered || {};
for ( let assetID of assetIDs ) {
this.assetsFiltered [ assetID ] = filterStatus;
}
}
//----------------------------------------------------------------//
@action
setOfferID ( offerID ) {
this.offerID = offerID;
}
//----------------------------------------------------------------//
@action
setBody ( body ) {
this.body = body;
}
//----------------------------------------------------------------//
@action
setFees ( feeSchedule ) {
const maker = this.body.maker;
const fees = util.calculateTransactionFees ( feeSchedule, this.type, maker.gratuity, this.vol );
maker.profitShare = fees.profitShare;
maker.transferTax = fees.transferTax;
}
//----------------------------------------------------------------//
@action
setUUID ( uuid ) {
this.body.uuid = uuid || fgc.util.generateUUIDV4 ();
}
//----------------------------------------------------------------//
@action
setWeight ( weight ) {
this.body.weight = weight;
}
//----------------------------------------------------------------//
virtual_getCost () {
return 0;
}
//----------------------------------------------------------------//
virtual_getTaxableVOL () {
return Math.abs ( this.virtual_getCost ());
}
//----------------------------------------------------------------//
virtual_getWeight () {
return 1;
}
};
//================================================================//
// Transaction_BuyAssets
//================================================================//
class Transaction_BuyAssets extends Transaction {
//----------------------------------------------------------------//
virtual_getCost () {
return this.body.price || 0;
}
};
//================================================================//
// Transaction_IdentifyAccount
//================================================================//
class Transaction_IdentifyAccount extends Transaction {
//----------------------------------------------------------------//
virtual_getCost () {
return -this.body.grant || 0;
}
};
//================================================================//
// Transaction_NewAccount
//================================================================//
class Transaction_NewAccount extends Transaction {
//----------------------------------------------------------------//
virtual_getCost () {
return -this.body.grant || 0;
}
};
//================================================================//
// Transaction_OpenAccount
//================================================================//
class Transaction_OpenAccount extends Transaction {
//----------------------------------------------------------------//
virtual_getCost () {
return this.body.grant || 0;
}
};
//================================================================//
// Transaction_RunScript
//================================================================//
class Transaction_RunScript extends Transaction {
//----------------------------------------------------------------//
virtual_getWeight () {
return ( this.body.weight || 1 );
}
};
//================================================================//
// Transaction_SendVOL
//================================================================//
class Transaction_SendVOL extends Transaction {
//----------------------------------------------------------------//
virtual_getCost () {
return this.body.amount || 0;
}
};
//================================================================//
// Transaction_StampAssets
//================================================================//
class Transaction_StampAssets extends Transaction {
//----------------------------------------------------------------//
virtual_getCost () {
return this.body.price * this.body.assetIdentifiers.length;
}
};