@@ -4,14 +4,13 @@ import * as Cardano from '../../src/Cardano';
4
4
import {
5
5
Asset ,
6
6
AssetInfoWithAmount ,
7
- AssetProvider ,
8
- HealthCheckResponse ,
9
7
Milliseconds ,
10
8
TokenTransferValue ,
11
9
createTxInspector ,
12
10
tokenTransferInspector
13
11
} from '../../src' ;
14
12
import { Ed25519KeyHashHex , Ed25519PublicKeyHex , Ed25519SignatureHex } from '@cardano-sdk/crypto' ;
13
+ import { createMockAssetProvider , createMockInputResolver } from './mocks' ;
15
14
import { jsonToMetadatum } from '../../src/util/metadatum' ;
16
15
import { logger } from '@cardano-sdk/util-dev' ;
17
16
@@ -22,27 +21,6 @@ const buildTokenTransferValue = (coins: bigint, assets: Array<[Asset.AssetInfo,
22
21
coins
23
22
} ) ;
24
23
25
- const createMockInputResolver = ( historicalTxs : Cardano . HydratedTx [ ] , resolveDelay = 0 ) : Cardano . InputResolver => ( {
26
- async resolveInput ( input : Cardano . TxIn ) {
27
- const tx = historicalTxs . find ( ( historicalTx ) => historicalTx . id === input . txId ) ;
28
-
29
- if ( ! tx || tx . body . outputs . length <= input . index ) return Promise . resolve ( null ) ;
30
-
31
- return await new Promise ( ( resolve ) => {
32
- setTimeout ( ( ) => {
33
- resolve ( tx . body . outputs [ input . index ] ) ;
34
- } , resolveDelay ) ;
35
- } ) ;
36
- }
37
- } ) ;
38
-
39
- const createMockAssetProvider = ( assets : Asset . AssetInfo [ ] ) : AssetProvider => ( {
40
- getAsset : async ( { assetId } ) =>
41
- assets . find ( ( asset ) => asset . assetId === assetId ) ?? Promise . reject ( 'Asset not found' ) ,
42
- getAssets : async ( { assetIds } ) => assets . filter ( ( asset ) => assetIds . includes ( asset . assetId ) ) ,
43
- healthCheck : async ( ) => Promise . resolve ( { } as HealthCheckResponse )
44
- } ) ;
45
-
46
24
// eslint-disable-next-line max-statements
47
25
describe ( 'txInspector' , ( ) => {
48
26
const sendingAddress = Cardano . PaymentAddress (
@@ -1074,7 +1052,7 @@ describe('txInspector', () => {
1074
1052
) ;
1075
1053
} ) ;
1076
1054
1077
- it ( 'has no resolved fromAddresses on timeout' , async ( ) => {
1055
+ it ( 'has no resolved fromAddresses on inputResolver timeout' , async ( ) => {
1078
1056
const resolveDelay = Milliseconds ( 5 ) ;
1079
1057
const timeoutAfter = Milliseconds ( 1 ) ;
1080
1058
@@ -1143,5 +1121,155 @@ describe('txInspector', () => {
1143
1121
// Assert
1144
1122
expect ( tokenTransfer . fromAddress ) . toEqual ( new Map ( [ ] ) ) ;
1145
1123
} ) ;
1124
+
1125
+ it ( 'should have fromAddress and toAddress without metadata on AssetProvider.getAssets error' , async ( ) => {
1126
+ const failingAssetProvider = {
1127
+ ...assetProvider ,
1128
+ getAssets : ( ) => Promise . reject ( 'Failed to get assets' )
1129
+ } ;
1130
+
1131
+ const tx = buildMockTx ( {
1132
+ inputs : [
1133
+ {
1134
+ address : addresses [ 1 ] ,
1135
+ index : 0 ,
1136
+ txId : Cardano . TransactionId ( 'bb217abaca60fc0ca68c1555eca6a96d2478547818ae76ce6836133f3cc546e0' )
1137
+ }
1138
+ ] ,
1139
+ outputs : [
1140
+ {
1141
+ address : addresses [ 0 ] ,
1142
+ value : {
1143
+ assets : new Map ( [
1144
+ [ AssetIds . TSLA , 100n ] ,
1145
+ [ AssetIds . PXL , 50n ] ,
1146
+ [ AssetIds . Unit , 30n ]
1147
+ ] ) ,
1148
+ coins : 3_000_000n
1149
+ }
1150
+ }
1151
+ ]
1152
+ } ) ;
1153
+
1154
+ const histTx : Cardano . HydratedTx [ ] = [
1155
+ {
1156
+ body : {
1157
+ outputs : [
1158
+ {
1159
+ address : addresses [ 1 ] ,
1160
+ value : {
1161
+ assets : new Map ( [
1162
+ [ AssetIds . TSLA , 25n ] ,
1163
+ [ AssetIds . PXL , 10n ] ,
1164
+ [ AssetIds . Unit , 5n ]
1165
+ ] ) ,
1166
+ coins : 3_000_000n
1167
+ }
1168
+ }
1169
+ ]
1170
+ } ,
1171
+ id : Cardano . TransactionId ( 'bb217abaca60fc0ca68c1555eca6a96d2478547818ae76ce6836133f3cc546e0' )
1172
+ } as unknown as Cardano . HydratedTx
1173
+ ] ;
1174
+
1175
+ const inspectTx = createTxInspector ( {
1176
+ tokenTransfer : tokenTransferInspector ( {
1177
+ fromAddressAssetProvider : failingAssetProvider ,
1178
+ inputResolver : createMockInputResolver ( histTx ) ,
1179
+ logger,
1180
+ timeout,
1181
+ toAddressAssetProvider : failingAssetProvider
1182
+ } )
1183
+ } ) ;
1184
+
1185
+ // Act
1186
+ const { tokenTransfer } = await inspectTx ( tx ) ;
1187
+
1188
+ // Assert
1189
+ expect ( tokenTransfer . fromAddress ) . toMatchInlineSnapshot ( `
1190
+ Map {
1191
+ "addr1z8phkx6acpnf78fuvxn0mkew3l0fd058hzquvz7w36x4gten0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgs9yc0hh" => Object {
1192
+ "assets": Map {
1193
+ "659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba8254534c41" => Object {
1194
+ "amount": -25n,
1195
+ "assetInfo": Object {
1196
+ "assetId": "659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba8254534c41",
1197
+ "fingerprint": "asset1rqluyux4nxv6kjashz626c8usp8g88unmqwnyh",
1198
+ "name": "54534c41",
1199
+ "policyId": "659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba82",
1200
+ "quantity": 0n,
1201
+ "supply": 0n,
1202
+ },
1203
+ },
1204
+ "1ec85dcee27f2d90ec1f9a1e4ce74a667dc9be8b184463223f9c960150584c" => Object {
1205
+ "amount": -10n,
1206
+ "assetInfo": Object {
1207
+ "assetId": "1ec85dcee27f2d90ec1f9a1e4ce74a667dc9be8b184463223f9c960150584c",
1208
+ "fingerprint": "asset1376eat26n9qnucrkkcy4pjps8d536nh5gds0gt",
1209
+ "name": "50584c",
1210
+ "policyId": "1ec85dcee27f2d90ec1f9a1e4ce74a667dc9be8b184463223f9c9601",
1211
+ "quantity": 0n,
1212
+ "supply": 0n,
1213
+ },
1214
+ },
1215
+ "a5425bd7bc4182325188af2340415827a73f845846c165d9e14c5aed556e6974" => Object {
1216
+ "amount": -5n,
1217
+ "assetInfo": Object {
1218
+ "assetId": "a5425bd7bc4182325188af2340415827a73f845846c165d9e14c5aed556e6974",
1219
+ "fingerprint": "asset1947w6qxce5w35ur5q4s62z9fml2np9st3v20d9",
1220
+ "name": "556e6974",
1221
+ "policyId": "a5425bd7bc4182325188af2340415827a73f845846c165d9e14c5aed",
1222
+ "quantity": 0n,
1223
+ "supply": 0n,
1224
+ },
1225
+ },
1226
+ },
1227
+ "coins": -3000000n,
1228
+ },
1229
+ }
1230
+ ` ) ;
1231
+ expect ( tokenTransfer . toAddress ) . toMatchInlineSnapshot ( `
1232
+ Map {
1233
+ "addr1qx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3n0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgse35a3x" => Object {
1234
+ "assets": Map {
1235
+ "659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba8254534c41" => Object {
1236
+ "amount": 100n,
1237
+ "assetInfo": Object {
1238
+ "assetId": "659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba8254534c41",
1239
+ "fingerprint": "asset1rqluyux4nxv6kjashz626c8usp8g88unmqwnyh",
1240
+ "name": "54534c41",
1241
+ "policyId": "659f2917fb63f12b33667463ee575eeac1845bbc736b9c0bbc40ba82",
1242
+ "quantity": 0n,
1243
+ "supply": 0n,
1244
+ },
1245
+ },
1246
+ "1ec85dcee27f2d90ec1f9a1e4ce74a667dc9be8b184463223f9c960150584c" => Object {
1247
+ "amount": 50n,
1248
+ "assetInfo": Object {
1249
+ "assetId": "1ec85dcee27f2d90ec1f9a1e4ce74a667dc9be8b184463223f9c960150584c",
1250
+ "fingerprint": "asset1376eat26n9qnucrkkcy4pjps8d536nh5gds0gt",
1251
+ "name": "50584c",
1252
+ "policyId": "1ec85dcee27f2d90ec1f9a1e4ce74a667dc9be8b184463223f9c9601",
1253
+ "quantity": 0n,
1254
+ "supply": 0n,
1255
+ },
1256
+ },
1257
+ "a5425bd7bc4182325188af2340415827a73f845846c165d9e14c5aed556e6974" => Object {
1258
+ "amount": 30n,
1259
+ "assetInfo": Object {
1260
+ "assetId": "a5425bd7bc4182325188af2340415827a73f845846c165d9e14c5aed556e6974",
1261
+ "fingerprint": "asset1947w6qxce5w35ur5q4s62z9fml2np9st3v20d9",
1262
+ "name": "556e6974",
1263
+ "policyId": "a5425bd7bc4182325188af2340415827a73f845846c165d9e14c5aed",
1264
+ "quantity": 0n,
1265
+ "supply": 0n,
1266
+ },
1267
+ },
1268
+ },
1269
+ "coins": 3000000n,
1270
+ },
1271
+ }
1272
+ ` ) ;
1273
+ } ) ;
1146
1274
} ) ;
1147
1275
} ) ;
0 commit comments