-
Notifications
You must be signed in to change notification settings - Fork 12
/
mappers.ts
175 lines (166 loc) · 4.93 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
import { extractAssets } from "../utils";
import {
rowToCertificate,
BlockEra,
BlockFrag,
Certificate,
TransInputFrag,
TransOutputFrag,
TransactionFrag,
Asset,
} from "../Transactions/types";
import {
GeneralTransactionMetadata,
TransactionMetadatum,
BigNum,
MetadataMap,
} from "@emurgo/cardano-serialization-lib-nodejs";
const MAX_INT = "2147483647";
export const mapTxRowsToTransactionFrags = (rows: any[]): TransactionFrag[] => {
return rows.map((row: any): TransactionFrag => {
const inputs = row.inAddrValPairs
? row.inAddrValPairs.map(
(obj: any): TransInputFrag => ({
address: obj.f1,
amount: obj.f2.toString(),
id: obj.f3.concat(obj.f4.toString()),
index: obj.f4,
txHash: obj.f3,
assets: extractAssets(obj.f5),
})
)
: [];
const collateralInputs = row.collateralInAddrValPairs
? row.collateralInAddrValPairs.map(
(obj: any): TransInputFrag => ({
address: obj.f1,
amount: obj.f2.toString(),
id: obj.f3.concat(obj.f4.toString()),
index: obj.f4,
txHash: obj.f3,
assets: extractAssets(obj.f5),
})
)
: [];
const outputs = row.outAddrValPairs
? row.outAddrValPairs.map(
(obj: any): TransOutputFrag => ({
address: obj.f1,
amount: obj.f2.toString(),
assets: extractAssets(obj.f3),
})
)
: [];
const withdrawals: TransOutputFrag[] = row.withdrawals
? row.withdrawals.map(
(obj: any): TransOutputFrag => ({
address: obj.f1,
amount: obj.f2.toString(),
assets: [] as Asset[],
})
)
: [];
const certificates =
row.certificates !== null
? row.certificates
.map(rowToCertificate)
.filter((i: Certificate | null) => i !== null)
: [];
const blockFrag: BlockFrag = {
number: row.blockNumber,
hash: row.blockHash.toString("hex"),
epochNo: row.blockEpochNo,
slotNo: row.blockSlotInEpoch,
};
return {
hash: row.hash.toString("hex"),
block: blockFrag,
validContract: row.valid_contract,
scriptSize: row.script_size,
fee: row.fee.toString(),
metadata: buildMetadataObj(row.metadata),
includedAt: row.includedAt,
inputs: inputs,
collateralInputs: collateralInputs,
outputs: outputs,
ttl: MAX_INT, // https://github.com/input-output-hk/cardano-db-sync/issues/212
blockEra: row.blockEra === "byron" ? BlockEra.Byron : BlockEra.Shelley,
txIndex: row.txIndex,
withdrawals: withdrawals,
certificates: certificates,
};
});
};
export const mapTransactionFragsToResponse = (txs: TransactionFrag[]) => {
return txs.map((tx) => mapTransactionFragToResponse(tx));
};
export const mapTransactionFragToResponse = (tx: TransactionFrag) => {
return {
hash: tx.hash,
fee: tx.fee,
metadata: tx.metadata,
valid_contract: tx.validContract,
script_size: tx.scriptSize,
//ttl: tx.ttl,
type: tx.blockEra,
withdrawals: tx.withdrawals,
certificates: tx.certificates,
tx_ordinal: tx.txIndex,
tx_state: "Successful", // graphql doesn't handle pending/failed txs
last_update: tx.includedAt,
block_num: tx.block.number,
block_hash: tx.block.hash,
time: tx.includedAt,
epoch: tx.block.epochNo,
slot: tx.block.slotNo,
inputs: tx.inputs,
collateral_inputs: tx.collateralInputs,
outputs: tx.outputs,
};
};
function buildMetadataObj(
metadataMap: null | Record<string, string>
): null | string {
if (metadataMap == null) return null;
const metadataWasm = GeneralTransactionMetadata.new();
for (const key of Object.keys(metadataMap)) {
const keyWasm = BigNum.from_str(key);
// the cbor inserted into SQL is not the full metadata for the transaction
// instead, each row is a CBOR map with a single entry <transaction_metadatum_label, transaction_metadatum>
let singletonMap;
let map;
try {
singletonMap = TransactionMetadatum.from_bytes(
Buffer.from(
// need to cutoff the \\x prefix added by SQL
metadataMap[key].substring(2),
"hex"
)
);
map = singletonMap.as_map();
} catch {
map = MetadataMap.new();
map.insert_str(
"error",
TransactionMetadatum.new_text("failed to deserialize")
);
}
const keys = map.keys();
for (let i = 0; i < keys.len(); i++) {
const cborKey = keys.get(i);
const datumWasm = map.get(cborKey);
metadataWasm.insert(keyWasm, datumWasm);
datumWasm.free();
cborKey.free();
}
keyWasm.free();
if (singletonMap) {
singletonMap.free();
}
map.free();
keys.free();
}
const result = Buffer.from(metadataWasm.to_bytes()).toString("hex");
metadataWasm.free();
return result;
}