@casual-simulation/aux-records 3.8.1 → 3.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AIChatInterface.d.ts +7 -43
- package/AIChatInterface.js +8 -6
- package/AIChatInterface.js.map +1 -1
- package/AIController.js +44 -49
- package/AIController.js.map +1 -1
- package/AIOpenAIRealtimeInterface.d.ts +1 -1
- package/AnthropicAIChatInterface.js +4 -4
- package/AnthropicAIChatInterface.js.map +1 -1
- package/AuthController.d.ts +78 -10
- package/AuthController.js +230 -166
- package/AuthController.js.map +1 -1
- package/AuthStore.d.ts +317 -4
- package/BigIntPatch.d.ts +1 -0
- package/BigIntPatch.js +24 -0
- package/BigIntPatch.js.map +1 -0
- package/BlockadeLabsGenerateSkyboxInterface.js +4 -4
- package/BlockadeLabsGenerateSkyboxInterface.js.map +1 -1
- package/CachingConfigStore.d.ts +8 -1
- package/CachingConfigStore.js +50 -0
- package/CachingConfigStore.js.map +1 -1
- package/ComIdConfig.d.ts +132 -86
- package/ComIdConfig.js +5 -5
- package/ComIdConfig.js.map +1 -1
- package/ConfigurationStore.d.ts +1393 -3
- package/ConfigurationStore.js +49 -0
- package/ConfigurationStore.js.map +1 -1
- package/DataRecordsController.js +28 -28
- package/DataRecordsController.js.map +1 -1
- package/EventRecordsController.js +9 -9
- package/EventRecordsController.js.map +1 -1
- package/FileRecordsController.js +18 -17
- package/FileRecordsController.js.map +1 -1
- package/GoogleAIChatInterface.js +4 -4
- package/GoogleAIChatInterface.js.map +1 -1
- package/HttpTestUtils.d.ts +48 -0
- package/HttpTestUtils.js +256 -0
- package/HttpTestUtils.js.map +1 -0
- package/LivekitController.js +2 -2
- package/LivekitController.js.map +1 -1
- package/LoomController.js +3 -4
- package/LoomController.js.map +1 -1
- package/MemoryStore.d.ts +60 -7
- package/MemoryStore.js +427 -59
- package/MemoryStore.js.map +1 -1
- package/MetricsStore.d.ts +11 -0
- package/ModerationConfiguration.d.ts +11 -85
- package/ModerationConfiguration.js +17 -17
- package/ModerationConfiguration.js.map +1 -1
- package/ModerationController.js +9 -11
- package/ModerationController.js.map +1 -1
- package/OpenAIChatInterface.js +8 -13
- package/OpenAIChatInterface.js.map +1 -1
- package/OpenAIImageInterface.js +4 -5
- package/OpenAIImageInterface.js.map +1 -1
- package/PolicyController.js +66 -73
- package/PolicyController.js.map +1 -1
- package/PolicyStore.d.ts +59 -33
- package/PolicyStore.js +35 -1
- package/PolicyStore.js.map +1 -1
- package/PrivoClient.d.ts +3 -1
- package/PrivoClient.js +2 -4
- package/PrivoClient.js.map +1 -1
- package/PrivoConfiguration.d.ts +6 -72
- package/PrivoConfiguration.js +30 -31
- package/PrivoConfiguration.js.map +1 -1
- package/README.md +276 -2
- package/RateLimitController.js +2 -2
- package/RateLimitController.js.map +1 -1
- package/RecordsClient.d.ts +3 -1
- package/RecordsClient.js +6 -6
- package/RecordsClient.js.map +1 -1
- package/RecordsController.d.ts +145 -3
- package/RecordsController.js +399 -61
- package/RecordsController.js.map +1 -1
- package/RecordsServer.d.ts +1932 -1109
- package/RecordsStore.d.ts +99 -14
- package/RecordsStore.js +8 -10
- package/RecordsStore.js.map +1 -1
- package/ServerConfig.d.ts +698 -9867
- package/ServerConfig.js +457 -377
- package/ServerConfig.js.map +1 -1
- package/SloydInterface.js +1 -1
- package/SloydInterface.js.map +1 -1
- package/StabilityAIImageInterface.js +6 -9
- package/StabilityAIImageInterface.js.map +1 -1
- package/StripeInterface.d.ts +431 -287
- package/StripeInterface.js +21 -1
- package/StripeInterface.js.map +1 -1
- package/SubscriptionConfigBuilder.d.ts +10 -1
- package/SubscriptionConfigBuilder.js +72 -41
- package/SubscriptionConfigBuilder.js.map +1 -1
- package/SubscriptionConfiguration.d.ts +606 -6334
- package/SubscriptionConfiguration.js +460 -277
- package/SubscriptionConfiguration.js.map +1 -1
- package/SubscriptionController.d.ts +677 -4
- package/SubscriptionController.js +2986 -186
- package/SubscriptionController.js.map +1 -1
- package/SystemNotificationMessenger.d.ts +14 -70
- package/SystemNotificationMessenger.js +17 -20
- package/SystemNotificationMessenger.js.map +1 -1
- package/TestUtils.d.ts +18 -3
- package/TestUtils.js +84 -8
- package/TestUtils.js.map +1 -1
- package/TypeUtils.d.ts +991 -0
- package/TypeUtils.js +2 -0
- package/TypeUtils.js.map +1 -0
- package/Utils.d.ts +59 -0
- package/Utils.js +507 -3
- package/Utils.js.map +1 -1
- package/Validations.d.ts +24 -108
- package/Validations.js +62 -45
- package/Validations.js.map +1 -1
- package/ViewTemplateRenderer.d.ts +39 -0
- package/ViewTemplateRenderer.js +19 -0
- package/ViewTemplateRenderer.js.map +1 -0
- package/contracts/ContractRecordsController.d.ts +58 -0
- package/contracts/ContractRecordsController.js +144 -0
- package/contracts/ContractRecordsController.js.map +1 -0
- package/contracts/ContractRecordsStore.d.ts +285 -0
- package/contracts/ContractRecordsStore.js +19 -0
- package/contracts/ContractRecordsStore.js.map +1 -0
- package/contracts/MemoryContractRecordsStore.d.ts +27 -0
- package/contracts/MemoryContractRecordsStore.js +124 -0
- package/contracts/MemoryContractRecordsStore.js.map +1 -0
- package/contracts/index.d.ts +4 -0
- package/contracts/index.js +21 -0
- package/contracts/index.js.map +1 -0
- package/crud/CrudHelpers.d.ts +25 -26
- package/crud/CrudHelpers.js +1 -1
- package/crud/CrudHelpers.js.map +1 -1
- package/crud/CrudRecordsController.js +13 -16
- package/crud/CrudRecordsController.js.map +1 -1
- package/crud/CrudRecordsControllerTests.d.ts +2 -2
- package/crud/CrudRecordsControllerTests.js +605 -580
- package/crud/CrudRecordsControllerTests.js.map +1 -1
- package/crud/MemoryCrudRecordsStore.js +1 -2
- package/crud/MemoryCrudRecordsStore.js.map +1 -1
- package/crud/sub/MemorySubCrudRecordsStore.js +4 -6
- package/crud/sub/MemorySubCrudRecordsStore.js.map +1 -1
- package/crud/sub/SubCrudRecordsController.js +8 -8
- package/crud/sub/SubCrudRecordsController.js.map +1 -1
- package/database/DatabaseRecordsController.js +1 -2
- package/database/DatabaseRecordsController.js.map +1 -1
- package/database/SqliteDatabaseInterface.js +1 -2
- package/database/SqliteDatabaseInterface.js.map +1 -1
- package/dns/DNSDomainNameValidator.d.ts +11 -0
- package/dns/DNSDomainNameValidator.js +59 -0
- package/dns/DNSDomainNameValidator.js.map +1 -0
- package/dns/DomainNameValidator.d.ts +36 -0
- package/dns/DomainNameValidator.js +19 -0
- package/dns/DomainNameValidator.js.map +1 -0
- package/dns/index.d.ts +3 -0
- package/dns/index.js +20 -0
- package/dns/index.js.map +1 -0
- package/financial/FinancialController.d.ts +272 -0
- package/financial/FinancialController.js +762 -0
- package/financial/FinancialController.js.map +1 -0
- package/financial/FinancialInterface.d.ts +352 -0
- package/financial/FinancialInterface.js +642 -0
- package/financial/FinancialInterface.js.map +1 -0
- package/financial/FinancialStore.d.ts +186 -0
- package/financial/FinancialStore.js +19 -0
- package/financial/FinancialStore.js.map +1 -0
- package/financial/MemoryFinancialInterface.d.ts +23 -0
- package/financial/MemoryFinancialInterface.js +592 -0
- package/financial/MemoryFinancialInterface.js.map +1 -0
- package/financial/TigerBeetleFinancialInterface.d.ts +46 -0
- package/financial/TigerBeetleFinancialInterface.js +109 -0
- package/financial/TigerBeetleFinancialInterface.js.map +1 -0
- package/financial/TigerBeetleTestUtils.d.ts +40 -0
- package/financial/TigerBeetleTestUtils.js +185 -0
- package/financial/TigerBeetleTestUtils.js.map +1 -0
- package/financial/Types.d.ts +1 -0
- package/financial/Types.js +801 -0
- package/financial/Types.js.map +1 -0
- package/financial/index.d.ts +6 -0
- package/financial/index.js +24 -0
- package/financial/index.js.map +1 -0
- package/index.d.ts +4 -0
- package/index.js +3 -0
- package/index.js.map +1 -1
- package/notifications/NotificationRecordsController.js +14 -14
- package/notifications/NotificationRecordsController.js.map +1 -1
- package/notifications/WebPushInterface.d.ts +24 -155
- package/notifications/WebPushInterface.js +2 -2
- package/notifications/WebPushInterface.js.map +1 -1
- package/package.json +72 -70
- package/packages/version/MemoryPackageVersionRecordsStore.js +12 -15
- package/packages/version/MemoryPackageVersionRecordsStore.js.map +1 -1
- package/packages/version/PackageVersionRecordsController.d.ts +19 -0
- package/packages/version/PackageVersionRecordsController.js +102 -22
- package/packages/version/PackageVersionRecordsController.js.map +1 -1
- package/packages/version/PackageVersionRecordsStore.js +6 -8
- package/packages/version/PackageVersionRecordsStore.js.map +1 -1
- package/purchasable-items/MemoryPurchasableItemRecordsStore.d.ts +7 -0
- package/purchasable-items/MemoryPurchasableItemRecordsStore.js +33 -0
- package/purchasable-items/MemoryPurchasableItemRecordsStore.js.map +1 -0
- package/purchasable-items/PurchasableItemRecordsController.d.ts +16 -0
- package/purchasable-items/PurchasableItemRecordsController.js +72 -0
- package/purchasable-items/PurchasableItemRecordsController.js.map +1 -0
- package/purchasable-items/PurchasableItemRecordsStore.d.ts +66 -0
- package/purchasable-items/PurchasableItemRecordsStore.js +2 -0
- package/purchasable-items/PurchasableItemRecordsStore.js.map +1 -0
- package/purchasable-items/index.d.ts +4 -0
- package/purchasable-items/index.js +21 -0
- package/purchasable-items/index.js.map +1 -0
- package/queue/MemoryQueue.js +1 -1
- package/queue/MemoryQueue.js.map +1 -1
- package/search/MemorySearchInterface.js +1 -2
- package/search/MemorySearchInterface.js.map +1 -1
- package/search/SearchRecordsController.d.ts +66 -244
- package/search/SearchRecordsController.js +35 -36
- package/search/SearchRecordsController.js.map +1 -1
- package/search/SearchSyncProcessor.d.ts +7 -83
- package/search/TypesenseSearchInterface.js +8 -11
- package/search/TypesenseSearchInterface.js.map +1 -1
- package/tracing/TracingDecorators.js +5 -8
- package/tracing/TracingDecorators.js.map +1 -1
- package/webhooks/WebhookEnvironment.d.ts +36 -560
- package/webhooks/WebhookEnvironment.js +1 -1
- package/webhooks/WebhookEnvironment.js.map +1 -1
- package/webhooks/WebhookRecordsController.js +14 -16
- package/webhooks/WebhookRecordsController.js.map +1 -1
- package/websockets/MemoryLockStore.d.ts +18 -0
- package/websockets/MemoryLockStore.js +2 -0
- package/websockets/MemoryLockStore.js.map +1 -0
- package/websockets/MemoryTempInstRecordsStore.js +15 -27
- package/websockets/MemoryTempInstRecordsStore.js.map +1 -1
- package/websockets/MemoryWebsocketConnectionStore.js +6 -8
- package/websockets/MemoryWebsocketConnectionStore.js.map +1 -1
- package/websockets/MemoryWebsocketMessenger.js +1 -2
- package/websockets/MemoryWebsocketMessenger.js.map +1 -1
- package/websockets/SplitInstRecordsStore.js +2 -2
- package/websockets/SplitInstRecordsStore.js.map +1 -1
- package/websockets/WebsocketController.d.ts +5 -0
- package/websockets/WebsocketController.js +122 -83
- package/websockets/WebsocketController.js.map +1 -1
- package/RecordsServer.js +0 -6435
- package/RecordsServer.js.map +0 -1
|
@@ -0,0 +1,762 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { AccountBalance } from '@casual-simulation/aux-common';
|
|
8
|
+
import { failure, isFailure, logErrors, success, unwrap, } from '@casual-simulation/aux-common';
|
|
9
|
+
import { ACCOUNT_IDS, AccountCodes, AMOUNT_MAX, CREDITS_DISPLAY_FACTOR, CURRENCIES, getAccountCurrency, getFlagsForAccountCode, getFlagsForTransferCode, getMessageForAccountError, LEDGERS, processAccountErrors, processTransferErrors, USD_DISPLAY_FACTOR, } from './FinancialInterface';
|
|
10
|
+
import { AccountFilterFlags, AccountFlags, CreateAccountError, TransferFlags, } from 'tigerbeetle-node';
|
|
11
|
+
import { traced } from '../tracing/TracingDecorators';
|
|
12
|
+
const TRACE_NAME = 'FinancialController';
|
|
13
|
+
export class FinancialController {
|
|
14
|
+
constructor(financialInterface, financialStore) {
|
|
15
|
+
this._idempotentKeyMaxAgeMS = 3600000; // 1 hour
|
|
16
|
+
this._financialInterface = financialInterface;
|
|
17
|
+
this._financialStore = financialStore;
|
|
18
|
+
}
|
|
19
|
+
async init() {
|
|
20
|
+
// Ensure that the financial interface has the correct accounts
|
|
21
|
+
const results = await this._financialInterface.createAccounts([
|
|
22
|
+
{
|
|
23
|
+
id: ACCOUNT_IDS.assets_cash,
|
|
24
|
+
code: AccountCodes.assets_cash,
|
|
25
|
+
flags: getFlagsForAccountCode(AccountCodes.assets_cash),
|
|
26
|
+
credits_pending: 0n,
|
|
27
|
+
credits_posted: 0n,
|
|
28
|
+
debits_pending: 0n,
|
|
29
|
+
debits_posted: 0n,
|
|
30
|
+
user_data_128: 0n,
|
|
31
|
+
user_data_64: 0n,
|
|
32
|
+
user_data_32: 0,
|
|
33
|
+
timestamp: 0n,
|
|
34
|
+
ledger: LEDGERS.usd,
|
|
35
|
+
reserved: 0,
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
id: ACCOUNT_IDS.assets_stripe,
|
|
39
|
+
code: AccountCodes.assets_stripe,
|
|
40
|
+
flags: getFlagsForAccountCode(AccountCodes.assets_stripe),
|
|
41
|
+
credits_pending: 0n,
|
|
42
|
+
credits_posted: 0n,
|
|
43
|
+
debits_pending: 0n,
|
|
44
|
+
debits_posted: 0n,
|
|
45
|
+
user_data_128: 0n,
|
|
46
|
+
user_data_64: 0n,
|
|
47
|
+
user_data_32: 0,
|
|
48
|
+
timestamp: 0n,
|
|
49
|
+
ledger: LEDGERS.usd,
|
|
50
|
+
reserved: 0,
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
id: ACCOUNT_IDS.revenue_xp_platform_fees,
|
|
54
|
+
code: AccountCodes.revenue_platform_fees,
|
|
55
|
+
flags: getFlagsForAccountCode(AccountCodes.revenue_platform_fees),
|
|
56
|
+
credits_pending: 0n,
|
|
57
|
+
credits_posted: 0n,
|
|
58
|
+
debits_pending: 0n,
|
|
59
|
+
debits_posted: 0n,
|
|
60
|
+
user_data_128: 0n,
|
|
61
|
+
user_data_64: 0n,
|
|
62
|
+
user_data_32: 0,
|
|
63
|
+
timestamp: 0n,
|
|
64
|
+
ledger: LEDGERS.usd,
|
|
65
|
+
reserved: 0,
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
id: ACCOUNT_IDS.revenue_store_platform_fees,
|
|
69
|
+
code: AccountCodes.revenue_platform_fees,
|
|
70
|
+
flags: getFlagsForAccountCode(AccountCodes.revenue_platform_fees),
|
|
71
|
+
credits_pending: 0n,
|
|
72
|
+
credits_posted: 0n,
|
|
73
|
+
debits_pending: 0n,
|
|
74
|
+
debits_posted: 0n,
|
|
75
|
+
user_data_128: 0n,
|
|
76
|
+
user_data_64: 0n,
|
|
77
|
+
user_data_32: 0,
|
|
78
|
+
timestamp: 0n,
|
|
79
|
+
ledger: LEDGERS.usd,
|
|
80
|
+
reserved: 0,
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
id: ACCOUNT_IDS.liquidity_usd,
|
|
84
|
+
code: AccountCodes.liquidity_pool,
|
|
85
|
+
flags: getFlagsForAccountCode(AccountCodes.liquidity_pool) |
|
|
86
|
+
AccountFlags.debits_must_not_exceed_credits,
|
|
87
|
+
credits_pending: 0n,
|
|
88
|
+
credits_posted: 0n,
|
|
89
|
+
debits_pending: 0n,
|
|
90
|
+
debits_posted: 0n,
|
|
91
|
+
user_data_128: 0n,
|
|
92
|
+
user_data_64: 0n,
|
|
93
|
+
user_data_32: 0,
|
|
94
|
+
timestamp: 0n,
|
|
95
|
+
ledger: LEDGERS.usd,
|
|
96
|
+
reserved: 0,
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
id: ACCOUNT_IDS.liquidity_credits,
|
|
100
|
+
code: AccountCodes.liquidity_pool,
|
|
101
|
+
flags: getFlagsForAccountCode(AccountCodes.liquidity_pool) |
|
|
102
|
+
AccountFlags.credits_must_not_exceed_debits,
|
|
103
|
+
credits_pending: 0n,
|
|
104
|
+
credits_posted: 0n,
|
|
105
|
+
debits_pending: 0n,
|
|
106
|
+
debits_posted: 0n,
|
|
107
|
+
user_data_128: 0n,
|
|
108
|
+
user_data_64: 0n,
|
|
109
|
+
user_data_32: 0,
|
|
110
|
+
timestamp: 0n,
|
|
111
|
+
ledger: LEDGERS.credits,
|
|
112
|
+
reserved: 0,
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
id: ACCOUNT_IDS.USD_SETUP,
|
|
116
|
+
code: AccountCodes.control,
|
|
117
|
+
flags: AccountFlags.none,
|
|
118
|
+
credits_pending: 0n,
|
|
119
|
+
credits_posted: 0n,
|
|
120
|
+
debits_pending: 0n,
|
|
121
|
+
debits_posted: 0n,
|
|
122
|
+
user_data_128: 0n,
|
|
123
|
+
user_data_64: 0n,
|
|
124
|
+
user_data_32: 0,
|
|
125
|
+
timestamp: 0n,
|
|
126
|
+
ledger: LEDGERS.usd,
|
|
127
|
+
reserved: 0,
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
id: ACCOUNT_IDS.USD_LIMIT_CREDITS,
|
|
131
|
+
code: AccountCodes.control,
|
|
132
|
+
flags: AccountFlags.credits_must_not_exceed_debits,
|
|
133
|
+
credits_pending: 0n,
|
|
134
|
+
credits_posted: 0n,
|
|
135
|
+
debits_pending: 0n,
|
|
136
|
+
debits_posted: 0n,
|
|
137
|
+
user_data_128: 0n,
|
|
138
|
+
user_data_64: 0n,
|
|
139
|
+
user_data_32: 0,
|
|
140
|
+
timestamp: 0n,
|
|
141
|
+
ledger: LEDGERS.usd,
|
|
142
|
+
reserved: 0,
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
id: ACCOUNT_IDS.USD_LIMIT_DEBITS,
|
|
146
|
+
code: AccountCodes.control,
|
|
147
|
+
flags: AccountFlags.debits_must_not_exceed_credits,
|
|
148
|
+
credits_pending: 0n,
|
|
149
|
+
credits_posted: 0n,
|
|
150
|
+
debits_pending: 0n,
|
|
151
|
+
debits_posted: 0n,
|
|
152
|
+
user_data_128: 0n,
|
|
153
|
+
user_data_64: 0n,
|
|
154
|
+
user_data_32: 0,
|
|
155
|
+
timestamp: 0n,
|
|
156
|
+
ledger: LEDGERS.usd,
|
|
157
|
+
reserved: 0,
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
id: ACCOUNT_IDS.CREDITS_SETUP,
|
|
161
|
+
code: AccountCodes.control,
|
|
162
|
+
flags: AccountFlags.none,
|
|
163
|
+
credits_pending: 0n,
|
|
164
|
+
credits_posted: 0n,
|
|
165
|
+
debits_pending: 0n,
|
|
166
|
+
debits_posted: 0n,
|
|
167
|
+
user_data_128: 0n,
|
|
168
|
+
user_data_64: 0n,
|
|
169
|
+
user_data_32: 0,
|
|
170
|
+
timestamp: 0n,
|
|
171
|
+
ledger: LEDGERS.credits,
|
|
172
|
+
reserved: 0,
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
id: ACCOUNT_IDS.CREDITS_LIMIT_CREDITS,
|
|
176
|
+
code: AccountCodes.control,
|
|
177
|
+
flags: AccountFlags.credits_must_not_exceed_debits,
|
|
178
|
+
credits_pending: 0n,
|
|
179
|
+
credits_posted: 0n,
|
|
180
|
+
debits_pending: 0n,
|
|
181
|
+
debits_posted: 0n,
|
|
182
|
+
user_data_128: 0n,
|
|
183
|
+
user_data_64: 0n,
|
|
184
|
+
user_data_32: 0,
|
|
185
|
+
timestamp: 0n,
|
|
186
|
+
ledger: LEDGERS.credits,
|
|
187
|
+
reserved: 0,
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
id: ACCOUNT_IDS.CREDITS_LIMIT_DEBITS,
|
|
191
|
+
code: AccountCodes.control,
|
|
192
|
+
flags: AccountFlags.debits_must_not_exceed_credits,
|
|
193
|
+
credits_pending: 0n,
|
|
194
|
+
credits_posted: 0n,
|
|
195
|
+
debits_pending: 0n,
|
|
196
|
+
debits_posted: 0n,
|
|
197
|
+
user_data_128: 0n,
|
|
198
|
+
user_data_64: 0n,
|
|
199
|
+
user_data_32: 0,
|
|
200
|
+
timestamp: 0n,
|
|
201
|
+
ledger: LEDGERS.credits,
|
|
202
|
+
reserved: 0,
|
|
203
|
+
},
|
|
204
|
+
]);
|
|
205
|
+
let failed = false;
|
|
206
|
+
for (let result of results) {
|
|
207
|
+
if (result.result !== CreateAccountError.ok) {
|
|
208
|
+
console.error(`[FinancialController] Failed to create default accounts (${result.index},${result.result})`, getMessageForAccountError(result.result));
|
|
209
|
+
failed = true;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (failed) {
|
|
213
|
+
return failure({
|
|
214
|
+
errorCode: 'server_error',
|
|
215
|
+
errorMessage: 'Failed to create default accounts.',
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
return success();
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
generateId() {
|
|
223
|
+
return this._financialInterface.generateId();
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Creates a new account with the given code.
|
|
227
|
+
* For internal use only.
|
|
228
|
+
* @param code The code for the new account.
|
|
229
|
+
* @param ledger The ledger for the new account.
|
|
230
|
+
*/
|
|
231
|
+
async createAccount(code, ledger) {
|
|
232
|
+
const id = this._financialInterface.generateId();
|
|
233
|
+
const results = await this._financialInterface.createAccount({
|
|
234
|
+
id: id,
|
|
235
|
+
code: code,
|
|
236
|
+
flags: getFlagsForAccountCode(code),
|
|
237
|
+
credits_pending: 0n,
|
|
238
|
+
credits_posted: 0n,
|
|
239
|
+
debits_pending: 0n,
|
|
240
|
+
debits_posted: 0n,
|
|
241
|
+
user_data_128: 0n,
|
|
242
|
+
user_data_64: 0n,
|
|
243
|
+
user_data_32: 0,
|
|
244
|
+
timestamp: 0n,
|
|
245
|
+
ledger: ledger,
|
|
246
|
+
reserved: 0,
|
|
247
|
+
});
|
|
248
|
+
const accountErrors = processAccountErrors(results);
|
|
249
|
+
if (isFailure(accountErrors)) {
|
|
250
|
+
logErrors(accountErrors.error, '[FinancialController] [createAccount]');
|
|
251
|
+
return failure({
|
|
252
|
+
errorCode: 'server_error',
|
|
253
|
+
errorMessage: 'Failed to create account.',
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
return success({
|
|
257
|
+
id: id.toString(),
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Gets the account with the given ID.
|
|
262
|
+
* @param accountId The ID of the account to get.
|
|
263
|
+
* @returns
|
|
264
|
+
*/
|
|
265
|
+
async getAccount(accountId) {
|
|
266
|
+
if (typeof accountId === 'string') {
|
|
267
|
+
accountId = BigInt(accountId);
|
|
268
|
+
}
|
|
269
|
+
const [account] = await this._financialInterface.lookupAccounts([
|
|
270
|
+
accountId,
|
|
271
|
+
]);
|
|
272
|
+
if (!account) {
|
|
273
|
+
return failure({
|
|
274
|
+
errorCode: 'not_found',
|
|
275
|
+
errorMessage: `The account with ID '${accountId}' does not exist.`,
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
return success(account);
|
|
279
|
+
}
|
|
280
|
+
async getTransfer(transferId) {
|
|
281
|
+
if (typeof transferId === 'string') {
|
|
282
|
+
transferId = BigInt(transferId);
|
|
283
|
+
}
|
|
284
|
+
const transfers = await this._financialInterface.lookupTransfers([
|
|
285
|
+
transferId,
|
|
286
|
+
]);
|
|
287
|
+
if (transfers.length === 0) {
|
|
288
|
+
return failure({
|
|
289
|
+
errorCode: 'not_found',
|
|
290
|
+
errorMessage: `The transfer with ID '${transferId}' does not exist.`,
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
return success(transfers[0]);
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Gets the details for the financial account with the given ID.
|
|
297
|
+
* @param accountId The ID of the account to get details for.
|
|
298
|
+
*/
|
|
299
|
+
async getAccountDetails(accountId) {
|
|
300
|
+
const financialAccount = await this._financialStore.getAccountById(accountId.toString());
|
|
301
|
+
if (!financialAccount) {
|
|
302
|
+
return failure({
|
|
303
|
+
errorCode: 'not_found',
|
|
304
|
+
errorMessage: `The financial account does not exist.`,
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
const account = await this.getAccount(financialAccount.id);
|
|
308
|
+
if (isFailure(account)) {
|
|
309
|
+
return account;
|
|
310
|
+
}
|
|
311
|
+
return success({
|
|
312
|
+
account: account.value,
|
|
313
|
+
financialAccount,
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Attempts to get the financial account for the given filter.
|
|
318
|
+
* @param filter The filter to use.
|
|
319
|
+
*/
|
|
320
|
+
async getFinancialAccount(filter) {
|
|
321
|
+
const financialAccount = await this._financialStore.getAccountByFilter(filter);
|
|
322
|
+
if (!financialAccount) {
|
|
323
|
+
return failure({
|
|
324
|
+
errorCode: 'not_found',
|
|
325
|
+
errorMessage: `The financial account does not exist.`,
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
const account = await this.getAccount(financialAccount.id);
|
|
329
|
+
if (isFailure(account)) {
|
|
330
|
+
return account;
|
|
331
|
+
}
|
|
332
|
+
return success({
|
|
333
|
+
account: account.value,
|
|
334
|
+
financialAccount,
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Gets or creates a financial account for the given filter.
|
|
339
|
+
* @param filter The filter to use.
|
|
340
|
+
*/
|
|
341
|
+
async getOrCreateFinancialAccount(filter) {
|
|
342
|
+
let account = await this._financialStore.getAccountByFilter(filter);
|
|
343
|
+
let accountResult;
|
|
344
|
+
if (!account) {
|
|
345
|
+
console.log(`[FinancialController] [getOrCreateFinancialAccount] Creating account for filter [userId: ${filter.userId}, studioId: ${filter.studioId}, contractId: ${filter.contractId}] on ledger ${filter.ledger}`);
|
|
346
|
+
let result;
|
|
347
|
+
if (filter.userId) {
|
|
348
|
+
result = await this.createAccount(AccountCodes.liabilities_user, filter.ledger);
|
|
349
|
+
}
|
|
350
|
+
else if (filter.studioId) {
|
|
351
|
+
result = await this.createAccount(AccountCodes.liabilities_studio, filter.ledger);
|
|
352
|
+
}
|
|
353
|
+
else if (filter.contractId) {
|
|
354
|
+
result = await this.createAccount(AccountCodes.liabilities_contract, filter.ledger);
|
|
355
|
+
}
|
|
356
|
+
if (isFailure(result)) {
|
|
357
|
+
return result;
|
|
358
|
+
}
|
|
359
|
+
account = {
|
|
360
|
+
id: result.value.id.toString(),
|
|
361
|
+
userId: filter.userId,
|
|
362
|
+
studioId: filter.studioId,
|
|
363
|
+
contractId: filter.contractId,
|
|
364
|
+
ledger: filter.ledger,
|
|
365
|
+
currency: CURRENCIES.get(filter.ledger),
|
|
366
|
+
};
|
|
367
|
+
await this._financialStore.createAccount(account);
|
|
368
|
+
accountResult = await this.getAccount(result.value.id);
|
|
369
|
+
}
|
|
370
|
+
else {
|
|
371
|
+
accountResult = await this.getAccount(account.id);
|
|
372
|
+
}
|
|
373
|
+
if (isFailure(accountResult)) {
|
|
374
|
+
return accountResult;
|
|
375
|
+
}
|
|
376
|
+
return success({
|
|
377
|
+
account: accountResult.value,
|
|
378
|
+
financialAccount: account,
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Gets the list of accounts for the given filter.
|
|
383
|
+
* @param filter The filter to use.
|
|
384
|
+
*/
|
|
385
|
+
async listAccounts(filter) {
|
|
386
|
+
const accounts = await this._financialStore.listAccounts(filter);
|
|
387
|
+
const results = await Promise.all(accounts.map((account) => this.getAccount(account.id)));
|
|
388
|
+
const errors = results.filter((result) => isFailure(result));
|
|
389
|
+
if (errors.length > 0) {
|
|
390
|
+
return failure({
|
|
391
|
+
errorCode: 'multi_error',
|
|
392
|
+
errorMessage: 'Some accounts could not be retrieved.',
|
|
393
|
+
errors: errors.map((e) => e.error),
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
return success({
|
|
397
|
+
accounts: results.map((r) => unwrap(r)),
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Gets the account balances for the given account filter.
|
|
402
|
+
* @param filter The filter to use.
|
|
403
|
+
*/
|
|
404
|
+
async getAccountBalances(filter) {
|
|
405
|
+
const [usdResult, creditsResult] = await Promise.all([
|
|
406
|
+
this.getAccountBalance({
|
|
407
|
+
...filter,
|
|
408
|
+
ledger: LEDGERS.usd,
|
|
409
|
+
}),
|
|
410
|
+
this.getAccountBalance({
|
|
411
|
+
...filter,
|
|
412
|
+
ledger: LEDGERS.credits,
|
|
413
|
+
}),
|
|
414
|
+
]);
|
|
415
|
+
if (isFailure(usdResult)) {
|
|
416
|
+
return usdResult;
|
|
417
|
+
}
|
|
418
|
+
else if (isFailure(creditsResult)) {
|
|
419
|
+
return creditsResult;
|
|
420
|
+
}
|
|
421
|
+
if (!usdResult.value && !creditsResult.value) {
|
|
422
|
+
return success(undefined);
|
|
423
|
+
}
|
|
424
|
+
return success({
|
|
425
|
+
usd: usdResult.value,
|
|
426
|
+
credits: creditsResult.value,
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Gets the balance for the account matching the given filter.
|
|
431
|
+
* @param filter The filter to use.
|
|
432
|
+
*/
|
|
433
|
+
async getAccountBalance(filter) {
|
|
434
|
+
const result = await this.getFinancialAccount(filter);
|
|
435
|
+
if (isFailure(result)) {
|
|
436
|
+
if (result.error.errorCode === 'not_found') {
|
|
437
|
+
return success(undefined);
|
|
438
|
+
}
|
|
439
|
+
else {
|
|
440
|
+
return result;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
return success(this.convertToAccountBalance(result.value.account));
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Converts the given account data to an account balance.
|
|
447
|
+
* @param account The account to convert.
|
|
448
|
+
*/
|
|
449
|
+
convertToAccountBalance(account) {
|
|
450
|
+
return new AccountBalance({
|
|
451
|
+
accountId: account.id.toString(),
|
|
452
|
+
credits: account.credits_posted,
|
|
453
|
+
debits: account.debits_posted,
|
|
454
|
+
pendingCredits: account.credits_pending,
|
|
455
|
+
pendingDebits: account.debits_pending,
|
|
456
|
+
currency: getAccountCurrency(account),
|
|
457
|
+
displayFactor: account.ledger === LEDGERS.credits
|
|
458
|
+
? CREDITS_DISPLAY_FACTOR
|
|
459
|
+
: USD_DISPLAY_FACTOR,
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Transfers funds between two internal accounts.
|
|
464
|
+
* Suitable for moving money between user accounts and contracts.
|
|
465
|
+
* Not suitable for interfacing with external systems such as Stripe.
|
|
466
|
+
*
|
|
467
|
+
* Not for public use.
|
|
468
|
+
* @param request The request for the transfer.
|
|
469
|
+
*/
|
|
470
|
+
async internalTransaction(request) {
|
|
471
|
+
let transfers = [];
|
|
472
|
+
const transactionId = typeof request.transactionId === 'string'
|
|
473
|
+
? BigInt(request.transactionId)
|
|
474
|
+
: typeof request.transactionId === 'bigint'
|
|
475
|
+
? request.transactionId
|
|
476
|
+
: this._financialInterface.generateId();
|
|
477
|
+
for (let i = 0; i < request.transfers.length; i++) {
|
|
478
|
+
const transfer = request.transfers[i];
|
|
479
|
+
const transferId = typeof transfer.transferId === 'string'
|
|
480
|
+
? BigInt(transfer.transferId)
|
|
481
|
+
: typeof transfer.transferId === 'bigint'
|
|
482
|
+
? transfer.transferId
|
|
483
|
+
: this._financialInterface.generateId();
|
|
484
|
+
const currency = transfer.currency.toLowerCase();
|
|
485
|
+
const ledger = LEDGERS[currency];
|
|
486
|
+
if (typeof ledger !== 'number') {
|
|
487
|
+
return failure({
|
|
488
|
+
errorCode: 'unsupported_currency',
|
|
489
|
+
errorMessage: `The currency '${currency}' is not supported.`,
|
|
490
|
+
errors: [],
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
let flags = getFlagsForTransferCode(transfer.code);
|
|
494
|
+
let timeout = 0;
|
|
495
|
+
if (i < request.transfers.length - 1) {
|
|
496
|
+
flags |= TransferFlags.linked;
|
|
497
|
+
}
|
|
498
|
+
if ('pending' in transfer && transfer.pending) {
|
|
499
|
+
flags |= TransferFlags.pending;
|
|
500
|
+
if (transfer.timeoutSeconds <= 0) {
|
|
501
|
+
return failure({
|
|
502
|
+
errorCode: 'invalid_transfer',
|
|
503
|
+
errorMessage: 'Pending transfers must have a positive timeoutSeconds value.',
|
|
504
|
+
errors: [],
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
timeout = transfer.timeoutSeconds;
|
|
508
|
+
}
|
|
509
|
+
if (transfer.balancingCredit) {
|
|
510
|
+
flags |= TransferFlags.balancing_credit;
|
|
511
|
+
}
|
|
512
|
+
if (transfer.balancingDebit) {
|
|
513
|
+
flags |= TransferFlags.balancing_debit;
|
|
514
|
+
}
|
|
515
|
+
if (transfer.closingCredit) {
|
|
516
|
+
flags |= TransferFlags.closing_credit | TransferFlags.pending;
|
|
517
|
+
}
|
|
518
|
+
if (transfer.closingDebit) {
|
|
519
|
+
flags |= TransferFlags.closing_debit | TransferFlags.pending;
|
|
520
|
+
}
|
|
521
|
+
transfers.push({
|
|
522
|
+
id: transferId,
|
|
523
|
+
amount: typeof transfer.amount === 'number'
|
|
524
|
+
? BigInt(transfer.amount)
|
|
525
|
+
: transfer.amount,
|
|
526
|
+
code: transfer.code,
|
|
527
|
+
credit_account_id: BigInt(transfer.creditAccountId),
|
|
528
|
+
debit_account_id: BigInt(transfer.debitAccountId),
|
|
529
|
+
flags: flags,
|
|
530
|
+
ledger: ledger,
|
|
531
|
+
pending_id: 0n,
|
|
532
|
+
timeout: 0,
|
|
533
|
+
timestamp: 0n,
|
|
534
|
+
user_data_128: transactionId,
|
|
535
|
+
user_data_64: 0n,
|
|
536
|
+
user_data_32: 0,
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
const results = await this._financialInterface.createTransfers(transfers);
|
|
540
|
+
const transferResult = processTransferErrors(results, transfers);
|
|
541
|
+
return this._processTransferResult(transactionId, transfers, transferResult);
|
|
542
|
+
}
|
|
543
|
+
async completePendingTransfers(request) {
|
|
544
|
+
let transfers = [];
|
|
545
|
+
const transactionId = typeof request.transactionId === 'string'
|
|
546
|
+
? BigInt(request.transactionId)
|
|
547
|
+
: typeof request.transactionId === 'bigint'
|
|
548
|
+
? request.transactionId
|
|
549
|
+
: this._financialInterface.generateId();
|
|
550
|
+
for (let i = 0; i < request.transfers.length; i++) {
|
|
551
|
+
const id = request.transfers[i];
|
|
552
|
+
const transferId = typeof id === 'string' ? BigInt(id) : id;
|
|
553
|
+
let flags = request.flags ?? TransferFlags.post_pending_transfer;
|
|
554
|
+
if (i < request.transfers.length - 1) {
|
|
555
|
+
flags |= TransferFlags.linked;
|
|
556
|
+
}
|
|
557
|
+
transfers.push({
|
|
558
|
+
id: this._financialInterface.generateId(),
|
|
559
|
+
amount: (flags & TransferFlags.void_pending_transfer) ===
|
|
560
|
+
TransferFlags.void_pending_transfer
|
|
561
|
+
? 0n
|
|
562
|
+
: AMOUNT_MAX,
|
|
563
|
+
code: 0,
|
|
564
|
+
credit_account_id: 0n,
|
|
565
|
+
debit_account_id: 0n,
|
|
566
|
+
pending_id: transferId,
|
|
567
|
+
flags,
|
|
568
|
+
ledger: 0,
|
|
569
|
+
timeout: 0,
|
|
570
|
+
timestamp: 0n,
|
|
571
|
+
user_data_128: transactionId,
|
|
572
|
+
user_data_64: 0n,
|
|
573
|
+
user_data_32: 0,
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
const results = await this._financialInterface.createTransfers(transfers);
|
|
577
|
+
const transferResult = processTransferErrors(results, transfers);
|
|
578
|
+
return this._processTransferResult(transactionId, transfers, transferResult);
|
|
579
|
+
}
|
|
580
|
+
async listTransfers(accountId) {
|
|
581
|
+
if (typeof accountId === 'string') {
|
|
582
|
+
accountId = BigInt(accountId);
|
|
583
|
+
}
|
|
584
|
+
const transfers = await this._financialInterface.getAccountTransfers({
|
|
585
|
+
account_id: accountId,
|
|
586
|
+
code: 0, // 0 means all codes
|
|
587
|
+
flags: AccountFilterFlags.credits | AccountFilterFlags.debits,
|
|
588
|
+
limit: 1000, // Limit to 1000 transfers
|
|
589
|
+
timestamp_max: 0n, // No timestamp limit
|
|
590
|
+
timestamp_min: 0n, // No timestamp limit
|
|
591
|
+
user_data_128: 0n, // No user data filter
|
|
592
|
+
user_data_64: 0n, // No user data filter
|
|
593
|
+
user_data_32: 0, // No user data filter
|
|
594
|
+
});
|
|
595
|
+
return success(transfers);
|
|
596
|
+
}
|
|
597
|
+
async queryTransfers(query) {
|
|
598
|
+
const transfers = await this._financialInterface.queryTransfers({
|
|
599
|
+
user_data_128: BigInt(query.transactionId ?? 0n),
|
|
600
|
+
code: query.code ?? 0,
|
|
601
|
+
flags: 0,
|
|
602
|
+
ledger: 0,
|
|
603
|
+
limit: 1000,
|
|
604
|
+
timestamp_max: 0n,
|
|
605
|
+
timestamp_min: 0n,
|
|
606
|
+
user_data_64: 0n,
|
|
607
|
+
user_data_32: 0,
|
|
608
|
+
});
|
|
609
|
+
return success(transfers);
|
|
610
|
+
}
|
|
611
|
+
_processTransferResult(transactionId, transfers, transferResult) {
|
|
612
|
+
if (isFailure(transferResult)) {
|
|
613
|
+
// logErrors(
|
|
614
|
+
// transferResult.error,
|
|
615
|
+
// `[XpController] [transfer transactionId: ${transactionId}]`
|
|
616
|
+
// );
|
|
617
|
+
const errors = transferResult.error.errors.map(({ transfer, ...e }) => e);
|
|
618
|
+
const exceedsDebits = transferResult.error.errors.find((e) => e.errorCode === 'exceeds_debits');
|
|
619
|
+
if (exceedsDebits) {
|
|
620
|
+
return failure({
|
|
621
|
+
errorCode: 'credits_exceed_debits',
|
|
622
|
+
errorMessage: 'The transfer would cause the account credits to exceed its debits.',
|
|
623
|
+
accountId: exceedsDebits.transfer.credit_account_id.toString(),
|
|
624
|
+
errors: errors,
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
const exceedsCredits = transferResult.error.errors.find((e) => e.errorCode === 'exceeds_credits');
|
|
628
|
+
if (exceedsCredits) {
|
|
629
|
+
return failure({
|
|
630
|
+
errorCode: 'debits_exceed_credits',
|
|
631
|
+
errorMessage: 'The transfer would cause the account debits to exceed its credits.',
|
|
632
|
+
accountId: exceedsCredits.transfer.debit_account_id.toString(),
|
|
633
|
+
errors: errors,
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
const exists = transferResult.error.errors.find((e) => e.errorCode === 'exists');
|
|
637
|
+
if (exists) {
|
|
638
|
+
return failure({
|
|
639
|
+
errorCode: 'transfer_already_exists',
|
|
640
|
+
errorMessage: `The transfer (${exists.transfer.id}) already exists.`,
|
|
641
|
+
errors: errors,
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
const completed = transferResult.error.errors.find((e) => e.errorCode === 'already_posted' ||
|
|
645
|
+
e.errorCode === 'already_voided');
|
|
646
|
+
if (completed) {
|
|
647
|
+
return failure({
|
|
648
|
+
errorCode: 'transfer_already_completed',
|
|
649
|
+
errorMessage: `The transfer (${completed.transfer.pending_id}) has already been posted/voided.`,
|
|
650
|
+
errors: errors,
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
return failure({
|
|
654
|
+
errorCode: 'server_error',
|
|
655
|
+
errorMessage: 'An error occurred while transferring the funds.',
|
|
656
|
+
errors: errors,
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
return success({
|
|
660
|
+
transactionId: transactionId.toString(),
|
|
661
|
+
transferIds: transfers.map((t) => t.id.toString()),
|
|
662
|
+
});
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
__decorate([
|
|
666
|
+
traced(TRACE_NAME)
|
|
667
|
+
], FinancialController.prototype, "init", null);
|
|
668
|
+
__decorate([
|
|
669
|
+
traced(TRACE_NAME)
|
|
670
|
+
], FinancialController.prototype, "createAccount", null);
|
|
671
|
+
__decorate([
|
|
672
|
+
traced(TRACE_NAME)
|
|
673
|
+
], FinancialController.prototype, "getAccount", null);
|
|
674
|
+
__decorate([
|
|
675
|
+
traced(TRACE_NAME)
|
|
676
|
+
], FinancialController.prototype, "getTransfer", null);
|
|
677
|
+
__decorate([
|
|
678
|
+
traced(TRACE_NAME)
|
|
679
|
+
], FinancialController.prototype, "getAccountDetails", null);
|
|
680
|
+
__decorate([
|
|
681
|
+
traced(TRACE_NAME)
|
|
682
|
+
], FinancialController.prototype, "getFinancialAccount", null);
|
|
683
|
+
__decorate([
|
|
684
|
+
traced(TRACE_NAME)
|
|
685
|
+
], FinancialController.prototype, "getOrCreateFinancialAccount", null);
|
|
686
|
+
__decorate([
|
|
687
|
+
traced(TRACE_NAME)
|
|
688
|
+
], FinancialController.prototype, "listAccounts", null);
|
|
689
|
+
__decorate([
|
|
690
|
+
traced(TRACE_NAME)
|
|
691
|
+
], FinancialController.prototype, "getAccountBalances", null);
|
|
692
|
+
__decorate([
|
|
693
|
+
traced(TRACE_NAME)
|
|
694
|
+
], FinancialController.prototype, "getAccountBalance", null);
|
|
695
|
+
__decorate([
|
|
696
|
+
traced(TRACE_NAME)
|
|
697
|
+
], FinancialController.prototype, "internalTransaction", null);
|
|
698
|
+
__decorate([
|
|
699
|
+
traced(TRACE_NAME)
|
|
700
|
+
], FinancialController.prototype, "completePendingTransfers", null);
|
|
701
|
+
__decorate([
|
|
702
|
+
traced(TRACE_NAME)
|
|
703
|
+
], FinancialController.prototype, "listTransfers", null);
|
|
704
|
+
__decorate([
|
|
705
|
+
traced(TRACE_NAME)
|
|
706
|
+
], FinancialController.prototype, "queryTransfers", null);
|
|
707
|
+
/**
|
|
708
|
+
* Gets the balance of the given account.
|
|
709
|
+
*
|
|
710
|
+
* For asset accounts, the balance is calculated as: debits_posted - credits_posted.
|
|
711
|
+
* For liability accounts, the balance is calculated as: credits_posted - debits_posted.
|
|
712
|
+
* For revenue accounts, the balance is calculated as: credits_posted - debits_posted.
|
|
713
|
+
*
|
|
714
|
+
* @param account The account to get the balance of.
|
|
715
|
+
* @returns
|
|
716
|
+
*/
|
|
717
|
+
export function getAccountBalance(account) {
|
|
718
|
+
switch (account.code) {
|
|
719
|
+
case AccountCodes.assets_cash:
|
|
720
|
+
return getAssetAccountBalance(account);
|
|
721
|
+
case AccountCodes.liabilities_user:
|
|
722
|
+
case AccountCodes.liabilities_contract:
|
|
723
|
+
case AccountCodes.revenue_platform_fees:
|
|
724
|
+
return getLiabilityAccountBalance(account);
|
|
725
|
+
case AccountCodes.liquidity_pool:
|
|
726
|
+
return getLiquidityPoolBalance(account);
|
|
727
|
+
// Add other cases for different account codes as needed
|
|
728
|
+
default:
|
|
729
|
+
throw new Error(`Unsupported account code: ${account.code}`);
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
/**
|
|
733
|
+
* Gets the balance of the given liability account.
|
|
734
|
+
* For liability accounts, the balance is calculated as: credits_posted - debits_posted.
|
|
735
|
+
* @param account The account to get the balance of.
|
|
736
|
+
*/
|
|
737
|
+
export function getLiabilityAccountBalance(account) {
|
|
738
|
+
return Number(account.credits_posted -
|
|
739
|
+
account.debits_posted -
|
|
740
|
+
(account.credits_pending - account.debits_pending));
|
|
741
|
+
}
|
|
742
|
+
/**
|
|
743
|
+
* Gets the balance of the given asset account.
|
|
744
|
+
* For asset accounts, the balance is calculated as: debits_posted - credits_posted.
|
|
745
|
+
* @param account The account to get the balance of.
|
|
746
|
+
*/
|
|
747
|
+
export function getAssetAccountBalance(account) {
|
|
748
|
+
return Number(account.debits_posted -
|
|
749
|
+
account.credits_posted -
|
|
750
|
+
(account.debits_pending - account.credits_pending));
|
|
751
|
+
}
|
|
752
|
+
/**
|
|
753
|
+
* Gets the balance of the given liquidity pool account.
|
|
754
|
+
* For liquidity accounts, the balance is calculated as: debits_posted - credits_posted.
|
|
755
|
+
* @param account The account to get the balance of.
|
|
756
|
+
*/
|
|
757
|
+
export function getLiquidityPoolBalance(account) {
|
|
758
|
+
return Number(account.debits_posted -
|
|
759
|
+
account.credits_posted -
|
|
760
|
+
(account.debits_pending - account.credits_pending));
|
|
761
|
+
}
|
|
762
|
+
//# sourceMappingURL=FinancialController.js.map
|