-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathmappers.ts
457 lines (425 loc) · 15.6 KB
/
mappers.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import * as Crypto from '@cardano-sdk/crypto';
import { BigIntMath } from '@cardano-sdk/util';
import {
BlockModel,
BlockOutputModel,
CertificateModel,
MultiAssetModel,
RedeemerModel,
TipModel,
TxIdModel,
TxInput,
TxInputModel,
TxModel,
TxOutMultiAssetModel,
TxOutTokenMap,
TxOutput,
TxOutputModel,
TxTokenMap,
WithCertIndex,
WithCertType,
WithdrawalModel
} from './types';
import { Cardano } from '@cardano-sdk/core';
import { Hash28ByteBase16, Hash32ByteBase16 } from '@cardano-sdk/crypto';
import {
isAuthorizeCommitteeHotCertModel,
isDelegationCertModel,
isDrepRegistrationCertModel,
isDrepUnregistrationCertModel,
isMirCertModel,
isPoolRegisterCertModel,
isPoolRetireCertModel,
isResignCommitteeColdCertModel,
isStakeCertModel,
isStakeRegistrationDelegationCertModel,
isStakeVoteDelegationCertModel,
isStakeVoteRegistrationDelegationCertModel,
isUpdateDrepCertModel,
isVoteDelegationCertModel,
isVoteRegistrationDelegationCertModel
} from './util';
const addMultiAssetToTokenMap = (multiAsset: MultiAssetModel, tokenMap: Cardano.TokenMap): Cardano.TokenMap => {
const tokens = new Map(tokenMap);
const assetId = Cardano.AssetId.fromParts(
multiAsset.policy_id.toString('hex') as unknown as Cardano.PolicyId,
multiAsset.asset_name.toString('hex') as unknown as Cardano.AssetName
);
const currentQuantity = tokens.get(assetId) ?? 0n;
tokens.set(assetId, BigIntMath.sum([currentQuantity, BigInt(multiAsset.quantity)]));
return tokens;
};
export const mapTxTokenMap = (multiAssetModels: MultiAssetModel[]): TxTokenMap => {
const txTokenMap: TxTokenMap = new Map();
for (const multiAsset of multiAssetModels) {
const txId = multiAsset.tx_id.toString('hex') as unknown as Cardano.TransactionId;
const currentTokenMap = txTokenMap.get(txId) ?? new Map();
const tokenMap = addMultiAssetToTokenMap(multiAsset, currentTokenMap);
txTokenMap.set(txId, tokenMap);
}
return txTokenMap;
};
export const mapTxOutTokenMap = (multiAssetModels: TxOutMultiAssetModel[]): TxOutTokenMap => {
const txTokenMap: TxOutTokenMap = new Map();
for (const multiAsset of multiAssetModels) {
const currentTokenMap = txTokenMap.get(multiAsset.tx_out_id) ?? new Map();
const tokenMap = addMultiAssetToTokenMap(multiAsset, currentTokenMap);
txTokenMap.set(multiAsset.tx_out_id, tokenMap);
}
return txTokenMap;
};
export const mapTxIn = (txIn: TxInput): Cardano.HydratedTxIn => ({
address: txIn.address,
index: txIn.index,
txId: txIn.txSourceId
});
export const mapTxInModel = (txInModel: TxInputModel): TxInput => ({
address: txInModel.address as unknown as Cardano.PaymentAddress,
id: txInModel.id,
index: txInModel.index,
txInputId: txInModel.tx_input_id.toString('hex') as unknown as Cardano.TransactionId,
txSourceId: txInModel.tx_source_id.toString('hex') as unknown as Cardano.TransactionId
});
export const mapTxOut = (txOut: TxOutput): Cardano.TxOut => ({
address: txOut.address,
datum: txOut.datum,
datumHash: txOut.datumHash,
scriptReference: txOut.scriptReference,
value: txOut.value
});
export const mapTxOutModel = (txOutModel: TxOutputModel, assets?: Cardano.TokenMap): TxOutput => ({
address: txOutModel.address as unknown as Cardano.PaymentAddress,
// Inline datums are missing, but for now it's ok on ChainHistoryProvider
datumHash: txOutModel.datum ? (txOutModel.datum.toString('hex') as unknown as Hash32ByteBase16) : undefined,
index: txOutModel.index,
txId: txOutModel.tx_id.toString('hex') as unknown as Cardano.TransactionId,
value: {
assets: assets && assets.size > 0 ? assets : undefined,
coins: BigInt(txOutModel.coin_value)
}
});
export const mapWithdrawal = (withdrawalModel: WithdrawalModel): Cardano.Withdrawal => ({
quantity: BigInt(withdrawalModel.quantity),
stakeAddress: withdrawalModel.stake_address as unknown as Cardano.RewardAccount
});
// TODO: unfortunately this is not nullable and not implemented.
// Remove this and select the actual redeemer data from `redeemer_data` table.
const stubRedeemerData = Buffer.from('not implemented');
export const mapRedeemer = (redeemerModel: RedeemerModel): Cardano.Redeemer => {
let purpose: Cardano.RedeemerPurpose;
// It can be one of 'spend', 'mint', 'cert', 'reward'
switch (redeemerModel.purpose) {
case 'cert':
purpose = Cardano.RedeemerPurpose.certificate;
break;
case 'reward':
purpose = Cardano.RedeemerPurpose.withdrawal;
break;
default:
// spend or mint
purpose = redeemerModel.purpose as Cardano.RedeemerPurpose;
}
return {
data: stubRedeemerData,
executionUnits: {
memory: Number(redeemerModel.unit_mem),
steps: Number(redeemerModel.unit_steps)
},
index: redeemerModel.index,
purpose
};
};
export const mapAnchor = (anchorUrl: string, anchorDataHash: string): Cardano.Anchor | null => {
if (!!anchorUrl && !!anchorDataHash) {
return {
dataHash: anchorDataHash as Hash32ByteBase16,
url: anchorUrl
};
}
return null;
};
// eslint-disable-next-line complexity
export const mapCertificate = (
certModel: WithCertType<CertificateModel>
// eslint-disable-next-line sonarjs/cognitive-complexity
): WithCertIndex<Cardano.Certificate> | null => {
if (isPoolRetireCertModel(certModel))
return {
__typename: Cardano.CertificateType.PoolRetirement,
cert_index: certModel.cert_index,
epoch: Cardano.EpochNo(certModel.retiring_epoch),
poolId: certModel.pool_id as unknown as Cardano.PoolId
} as WithCertIndex<Cardano.PoolRetirementCertificate>;
if (isPoolRegisterCertModel(certModel))
return {
__typename: Cardano.CertificateType.PoolRegistration,
cert_index: certModel.cert_index,
poolParameters: null as unknown as Cardano.PoolParameters
} as WithCertIndex<Cardano.PoolRegistrationCertificate>;
if (isMirCertModel(certModel)) {
const credential = Cardano.Address.fromString(certModel.address)?.asReward()?.getPaymentCredential();
return {
__typename: Cardano.CertificateType.MIR,
cert_index: certModel.cert_index,
kind: credential ? Cardano.MirCertificateKind.ToStakeCreds : Cardano.MirCertificateKind.ToOtherPot,
pot: certModel.pot === 'reserve' ? Cardano.MirCertificatePot.Reserves : Cardano.MirCertificatePot.Treasury,
quantity: BigInt(certModel.amount),
stakeCredential: credential
} as WithCertIndex<Cardano.MirCertificate>;
}
if (isStakeCertModel(certModel))
return {
__typename: certModel.registration
? Cardano.CertificateType.Registration
: Cardano.CertificateType.Unregistration,
cert_index: certModel.cert_index,
deposit: BigInt(certModel.deposit),
stakeCredential: {
hash: Cardano.RewardAccount.toHash(
Cardano.RewardAccount(certModel.address)
) as unknown as Crypto.Hash28ByteBase16,
type: Cardano.CredentialType.KeyHash
}
} as WithCertIndex<Cardano.NewStakeAddressCertificate>;
if (isDelegationCertModel(certModel))
return {
__typename: Cardano.CertificateType.StakeDelegation,
cert_index: certModel.cert_index,
poolId: certModel.pool_id as unknown as Cardano.PoolId,
stakeCredential: {
hash: Cardano.RewardAccount.toHash(
Cardano.RewardAccount(certModel.address)
) as unknown as Crypto.Hash28ByteBase16,
type: Cardano.CredentialType.KeyHash
}
} as WithCertIndex<Cardano.StakeDelegationCertificate>;
if (isDrepRegistrationCertModel(certModel))
return {
__typename: Cardano.CertificateType.RegisterDelegateRepresentative,
anchor: mapAnchor(certModel.url, certModel.data_hash),
cert_index: certModel.cert_index,
dRepCredential: {
hash: certModel.drep_hash as Hash28ByteBase16,
type: Number(certModel.has_script) ? Cardano.CredentialType.ScriptHash : Cardano.CredentialType.KeyHash
},
deposit: BigInt(certModel.deposit)
};
if (isDrepUnregistrationCertModel(certModel))
return {
__typename: Cardano.CertificateType.UnregisterDelegateRepresentative,
cert_index: certModel.cert_index,
dRepCredential: {
hash: certModel.drep_hash as Hash28ByteBase16,
type: Number(certModel.has_script) ? Cardano.CredentialType.ScriptHash : Cardano.CredentialType.KeyHash
},
deposit: BigInt(certModel.deposit)
};
if (isUpdateDrepCertModel(certModel))
return {
__typename: Cardano.CertificateType.UpdateDelegateRepresentative,
anchor: mapAnchor(certModel.url, certModel.data_hash),
cert_index: certModel.cert_index,
dRepCredential: {
hash: certModel.drep_hash as Hash28ByteBase16,
type: Number(certModel.has_script) ? Cardano.CredentialType.ScriptHash : Cardano.CredentialType.KeyHash
}
};
if (isVoteDelegationCertModel(certModel))
return {
__typename: Cardano.CertificateType.VoteDelegation,
cert_index: certModel.cert_index,
dRep: {
hash: certModel.drep_hash as Hash28ByteBase16,
type: Number(certModel.has_script) ? Cardano.CredentialType.ScriptHash : Cardano.CredentialType.KeyHash
},
stakeCredential: {
hash: Cardano.RewardAccount.toHash(
Cardano.RewardAccount(certModel.address)
) as unknown as Crypto.Hash28ByteBase16,
type: Cardano.CredentialType.KeyHash
}
};
if (isVoteRegistrationDelegationCertModel(certModel))
return {
__typename: Cardano.CertificateType.VoteRegistrationDelegation,
cert_index: certModel.cert_index,
dRep: {
hash: certModel.drep_hash as Hash28ByteBase16,
type: Number(certModel.has_script) ? Cardano.CredentialType.ScriptHash : Cardano.CredentialType.KeyHash
},
deposit: BigInt(certModel.deposit),
stakeCredential: {
hash: Cardano.RewardAccount.toHash(
Cardano.RewardAccount(certModel.address)
) as unknown as Crypto.Hash28ByteBase16,
type: Cardano.CredentialType.KeyHash
}
};
if (isStakeVoteDelegationCertModel(certModel))
return {
__typename: Cardano.CertificateType.StakeVoteDelegation,
cert_index: certModel.cert_index,
dRep: {
hash: certModel.drep_hash as Hash28ByteBase16,
type: Number(certModel.has_script) ? Cardano.CredentialType.ScriptHash : Cardano.CredentialType.KeyHash
},
poolId: certModel.pool_id as unknown as Cardano.PoolId,
stakeCredential: {
hash: Cardano.RewardAccount.toHash(
Cardano.RewardAccount(certModel.address)
) as unknown as Crypto.Hash28ByteBase16,
type: Cardano.CredentialType.KeyHash
}
};
if (isStakeRegistrationDelegationCertModel(certModel))
return {
__typename: Cardano.CertificateType.StakeRegistrationDelegation,
cert_index: certModel.cert_index,
deposit: BigInt(certModel.deposit),
poolId: certModel.pool_id as unknown as Cardano.PoolId,
stakeCredential: {
hash: Cardano.RewardAccount.toHash(
Cardano.RewardAccount(certModel.address)
) as unknown as Crypto.Hash28ByteBase16,
type: Cardano.CredentialType.KeyHash
}
};
if (isStakeVoteRegistrationDelegationCertModel(certModel))
return {
__typename: Cardano.CertificateType.StakeVoteRegistrationDelegation,
cert_index: certModel.cert_index,
dRep: {
hash: certModel.drep_hash as Hash28ByteBase16,
type: Number(certModel.has_script) ? Cardano.CredentialType.ScriptHash : Cardano.CredentialType.KeyHash
},
deposit: BigInt(certModel.deposit),
poolId: certModel.pool_id as unknown as Cardano.PoolId,
stakeCredential: {
hash: Cardano.RewardAccount.toHash(
Cardano.RewardAccount(certModel.address)
) as unknown as Crypto.Hash28ByteBase16,
type: Cardano.CredentialType.KeyHash
}
};
if (isAuthorizeCommitteeHotCertModel(certModel))
return {
__typename: Cardano.CertificateType.AuthorizeCommitteeHot,
cert_index: certModel.cert_index,
coldCredential: {
hash: certModel.cold_key.toString('hex') as unknown as Crypto.Hash28ByteBase16,
type: Cardano.CredentialType.KeyHash
},
hotCredential: {
hash: certModel.hot_key.toString('hex') as unknown as Crypto.Hash28ByteBase16,
type: Cardano.CredentialType.KeyHash
}
};
if (isResignCommitteeColdCertModel(certModel))
return {
__typename: Cardano.CertificateType.ResignCommitteeCold,
anchor: mapAnchor(certModel.url, certModel.data_hash),
cert_index: certModel.cert_index,
coldCredential: {
hash: certModel.cold_key.toString('hex') as unknown as Crypto.Hash28ByteBase16,
type: Cardano.CredentialType.KeyHash
}
};
return null;
};
interface TxAlonzoData {
inputSource: Cardano.InputSource;
inputs: Cardano.HydratedTxIn[];
outputs: Cardano.TxOut[];
mint?: Cardano.TokenMap;
withdrawals?: Cardano.Withdrawal[];
redeemers?: Cardano.Redeemer[];
metadata?: Cardano.TxMetadata;
collaterals?: Cardano.HydratedTxIn[];
certificates?: Cardano.Certificate[];
collateralOutputs?: Cardano.TxOut[];
proposalProcedures?: Cardano.ProposalProcedure[];
votingProcedures?: Cardano.VotingProcedures;
}
export const mapTxAlonzo = (
txModel: TxModel,
{
certificates,
collaterals,
collateralOutputs = [],
inputSource,
inputs,
metadata,
mint,
outputs,
proposalProcedures,
redeemers,
votingProcedures,
withdrawals
}: TxAlonzoData
): Cardano.HydratedTx => ({
auxiliaryData:
metadata && metadata.size > 0
? {
blob: metadata
}
: undefined,
blockHeader: {
blockNo: Cardano.BlockNo(Number(txModel.block_no)),
hash: txModel.block_hash.toString('hex') as unknown as Cardano.BlockId,
slot: Cardano.Slot(Number(txModel.block_slot_no))
},
body: {
certificates,
collateralReturn: collateralOutputs.length > 0 ? collateralOutputs[0] : undefined,
collaterals,
fee: BigInt(txModel.fee),
inputs,
mint,
outputs,
proposalProcedures,
validityInterval: {
invalidBefore: Cardano.Slot(Number(txModel.invalid_before)) || undefined,
invalidHereafter: Cardano.Slot(Number(txModel.invalid_hereafter)) || undefined
},
votingProcedures,
withdrawals
},
id: txModel.id.toString('hex') as unknown as Cardano.TransactionId,
index: txModel.index,
inputSource,
txSize: txModel.size,
witness: {
// TODO: fetch bootstrap witnesses, datums and scripts
redeemers,
// TODO: fetch signatures
signatures: new Map()
}
});
export const mapBlock = (
blockModel: BlockModel,
blockOutputModel: BlockOutputModel,
tip: TipModel
): Cardano.ExtendedBlockInfo => ({
confirmations: tip.block_no - blockModel.block_no,
date: new Date(blockModel.time),
epoch: Cardano.EpochNo(blockModel.epoch_no),
epochSlot: blockModel.epoch_slot_no,
fees: BigInt(blockOutputModel?.fees ?? 0),
header: {
blockNo: Cardano.BlockNo(blockModel.block_no),
hash: blockModel.hash.toString('hex') as unknown as Cardano.BlockId,
slot: Cardano.Slot(Number(blockModel.slot_no))
},
nextBlock: blockModel.next_block ? (blockModel.next_block.toString('hex') as unknown as Cardano.BlockId) : undefined,
previousBlock: blockModel.previous_block
? (blockModel.previous_block.toString('hex') as unknown as Cardano.BlockId)
: undefined,
size: Cardano.BlockSize(blockModel.size),
slotLeader: blockModel.slot_leader_pool
? Cardano.SlotLeader(blockModel.slot_leader_pool)
: Cardano.SlotLeader(blockModel.slot_leader_hash.toString('hex')),
totalOutput: BigInt(blockOutputModel?.output ?? 0),
txCount: Number(blockModel.tx_count),
vrf: blockModel.vrf as unknown as Cardano.VrfVkBech32
});
export const mapTxId = ({ tx_id }: TxIdModel) => tx_id;