Skip to content

Commit 7fbcc0f

Browse files
authored
Merge pull request #1085 from input-output-hk/merge-master
Merge master
2 parents f14fa5d + 622c3cc commit 7fbcc0f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+675
-104
lines changed

packages/cardano-services-client/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
All notable changes to this project will be documented in this file.
44
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
55

6+
## [0.17.3](https://github.com/input-output-hk/cardano-js-sdk/compare/@cardano-sdk/[email protected]...@cardano-sdk/[email protected]) (2024-02-02)
7+
8+
**Note:** Version bump only for package @cardano-sdk/cardano-services-client
9+
610
## [0.17.2](https://github.com/input-output-hk/cardano-js-sdk/compare/@cardano-sdk/[email protected]...@cardano-sdk/[email protected]) (2024-02-02)
711

812
**Note:** Version bump only for package @cardano-sdk/cardano-services-client

packages/cardano-services-client/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cardano-sdk/cardano-services-client",
3-
"version": "0.17.2",
3+
"version": "0.17.3",
44
"description": "Cardano Services Client",
55
"engines": {
66
"node": ">=16.20.2"

packages/cardano-services/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
All notable changes to this project will be documented in this file.
44
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
55

6+
## [0.25.4](https://github.com/input-output-hk/cardano-js-sdk/compare/@cardano-sdk/[email protected]...@cardano-sdk/[email protected]) (2024-02-02)
7+
8+
**Note:** Version bump only for package @cardano-sdk/cardano-services
9+
610
## [0.25.3](https://github.com/input-output-hk/cardano-js-sdk/compare/@cardano-sdk/[email protected]...@cardano-sdk/[email protected]) (2024-02-02)
711

812
**Note:** Version bump only for package @cardano-sdk/cardano-services

packages/cardano-services/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cardano-sdk/cardano-services",
3-
"version": "0.25.3",
3+
"version": "0.25.4",
44
"description": "Cardano GraphQL Services",
55
"engines": {
66
"node": ">=16.20.2"

packages/cardano-services/src/ChainHistory/DbSyncChainHistory/ChainHistoryBuilder.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export class ChainHistoryBuilder {
200200
public async queryTransactionInputsByIds(ids: string[], collateral = false): Promise<TxInput[]> {
201201
this.#logger.debug(`About to find inputs (collateral: ${collateral}) for transactions with ids:`, ids);
202202
const result: QueryResult<TxInputModel> = await this.#db.query({
203-
name: `tx_${collateral ? 'collateral' : 'inputs'}_by_tx_ids`,
203+
name: `tx_${collateral ? 'collateral_' : ''}inputs_by_tx_ids`,
204204
text: collateral ? Queries.findTxCollateralsByIds : Queries.findTxInputsByIds,
205205
values: [ids]
206206
});
@@ -217,11 +217,11 @@ export class ChainHistoryBuilder {
217217
return mapTxOutTokenMap(result.rows);
218218
}
219219

220-
public async queryTransactionOutputsByIds(ids: string[]): Promise<TxOutput[]> {
221-
this.#logger.debug('About to find outputs for transactions with ids:', ids);
220+
public async queryTransactionOutputsByIds(ids: string[], collateral = false): Promise<TxOutput[]> {
221+
this.#logger.debug(`About to find outputs (collateral: ${collateral}) for transactions with ids:`, ids);
222222
const result: QueryResult<TxOutputModel> = await this.#db.query({
223-
name: 'tx_outputs_by_tx_ids',
224-
text: Queries.findTxOutputsByIds,
223+
name: `tx_${collateral ? 'collateral_' : ''}outputs_by_tx_ids`,
224+
text: collateral ? Queries.findCollateralOutputsByTxIds : Queries.findTxOutputsByIds,
225225
values: [ids]
226226
});
227227
if (result.rows.length === 0) return [];

packages/cardano-services/src/ChainHistory/DbSyncChainHistory/DbSyncChainHistoryProvider.ts

+6
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export class DbSyncChainHistoryProvider extends DbSyncProvider() implements Chai
116116
metadata,
117117
collaterals,
118118
certificates,
119+
collateralOutputs,
119120
votingProcedures,
120121
proposalProcedures
121122
] = await Promise.all([
@@ -128,6 +129,7 @@ export class DbSyncChainHistoryProvider extends DbSyncProvider() implements Chai
128129
this.#metadataService.queryTxMetadataByRecordIds(ids),
129130
this.#builder.queryTransactionInputsByIds(ids, true),
130131
this.#builder.queryCertificatesByIds(ids),
132+
this.#builder.queryTransactionOutputsByIds(ids, true),
131133
this.#builder.queryVotingProceduresByIds(ids),
132134
this.#builder.queryProposalProceduresByIds(ids)
133135
]);
@@ -137,12 +139,16 @@ export class DbSyncChainHistoryProvider extends DbSyncProvider() implements Chai
137139
const txInputs = orderBy(inputs.filter((input) => input.txInputId === txId).map(mapTxIn), ['index']);
138140
const txCollaterals = orderBy(collaterals.filter((col) => col.txInputId === txId).map(mapTxIn), ['index']);
139141
const txOutputs = orderBy(outputs.filter((output) => output.txId === txId).map(mapTxOut), ['index']);
142+
const txCollateralOutputs = orderBy(collateralOutputs.filter((output) => output.txId === txId).map(mapTxOut), [
143+
'index'
144+
]);
140145
const inputSource: Cardano.InputSource = tx.valid_contract
141146
? Cardano.InputSource.inputs
142147
: Cardano.InputSource.collaterals;
143148

144149
return mapTxAlonzo(tx, {
145150
certificates: certificates.get(txId),
151+
collateralOutputs: txCollateralOutputs,
146152
collaterals: txCollaterals,
147153
inputSource,
148154
inputs: txInputs,

packages/cardano-services/src/ChainHistory/DbSyncChainHistory/mappers.ts

+3
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ interface TxAlonzoData {
365365
metadata?: Cardano.TxMetadata;
366366
collaterals?: Cardano.HydratedTxIn[];
367367
certificates?: Cardano.Certificate[];
368+
collateralOutputs?: Cardano.TxOut[];
368369
proposalProcedures?: Cardano.ProposalProcedure[];
369370
votingProcedures?: Cardano.VotingProcedures;
370371
}
@@ -374,6 +375,7 @@ export const mapTxAlonzo = (
374375
{
375376
certificates,
376377
collaterals,
378+
collateralOutputs = [],
377379
inputSource,
378380
inputs,
379381
metadata,
@@ -398,6 +400,7 @@ export const mapTxAlonzo = (
398400
},
399401
body: {
400402
certificates,
403+
collateralReturn: collateralOutputs.length > 0 ? collateralOutputs[0] : undefined,
401404
collaterals,
402405
fee: BigInt(txModel.fee),
403406
inputs,

packages/cardano-services/src/ChainHistory/DbSyncChainHistory/queries.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ const selectTxInput = (collateral?: boolean) => `
1515
JOIN tx AS source_tx
1616
ON tx_out.tx_id = source_tx.id`;
1717

18-
const selectTxOutput = `
18+
const selectTxOutput = (collateral = false) => `
1919
SELECT
2020
tx_out.id AS id,
2121
tx_out.address AS address,
2222
tx_out."index" AS "index",
2323
tx_out.value AS coin_value,
2424
tx_out.data_hash AS datum,
2525
tx.hash AS tx_id
26-
FROM tx_out
26+
FROM ${collateral ? 'collateral_tx_out' : 'tx_out'} AS tx_out
2727
JOIN tx ON tx_out.tx_id = tx.id`;
2828

2929
export const findTxInputsByIds = `
@@ -45,18 +45,23 @@ export const findTxInputsByAddresses = `
4545
ORDER BY tx_in.id ASC`;
4646

4747
export const findTxOutputsByIds = `
48-
${selectTxOutput}
48+
${selectTxOutput()}
4949
WHERE tx.id = ANY($1)
5050
ORDER BY tx_out.id ASC`;
5151

5252
export const findTxOutputsByAddresses = `
53-
${selectTxOutput}
53+
${selectTxOutput()}
5454
JOIN block ON tx.block_id = block.id
5555
WHERE tx_out.address = ANY($1)
5656
AND block.block_no >= $2
5757
AND block.block_no <= $3
5858
ORDER BY tx_out.id ASC`;
5959

60+
export const findCollateralOutputsByTxIds = `
61+
${selectTxOutput(true)}
62+
WHERE tx.id = ANY($1)
63+
ORDER BY tx_out.id ASC`;
64+
6065
export const findTip = `
6166
SELECT
6267
block_no,

packages/cardano-services/test/ChainHistory/ChainHistoryHttpService.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,17 @@ describe('ChainHistoryHttpService', () => {
326326
expect(tx.body.collaterals?.length).toEqual(1);
327327
});
328328

329+
// TODO LW-9182 REMOVE SKIP
330+
it.skip('has collateral outputs', async () => {
331+
const response = await provider.transactionsByHashes({
332+
ids: await fixtureBuilder.getTxHashes(1, { with: [TxWith.CollateralOutput] })
333+
});
334+
const tx: Cardano.HydratedTx = response[0];
335+
expect(response.length).toEqual(1);
336+
337+
expect(tx.body.collateralReturn).toMatchShapeOf(DataMocks.Tx.collateralReturn);
338+
});
339+
329340
it('has certificates', async () => {
330341
const response = await provider.transactionsByHashes({
331342
ids: await fixtureBuilder.getTxHashes(2, { with: [TxWith.DelegationCertificate] })

packages/cardano-services/test/ChainHistory/DbSyncChainHistoryProvider/ChainHistoryBuilder.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe('ChainHistoryBuilder', () => {
102102
const result = await builder.queryTransactionOutputsByIds(ids);
103103
expect(result.length).toBeGreaterThanOrEqual(2);
104104
expect(result[0]).toMatchShapeOf(DataMocks.Tx.txOut);
105-
checkLoggedTxIds('About to find outputs for transactions with ids', true);
105+
checkLoggedTxIds('About to find outputs \\(collateral: false\\) for transactions with ids', true);
106106
});
107107
test('query transaction outputs with empty array', async () => {
108108
const result = await builder.queryTransactionOutputsByIds([]);

packages/cardano-services/test/ChainHistory/DbSyncChainHistoryProvider/mappers.test.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ describe('chain history mappers', () => {
377377
test('map TxModel to Cardano.HydratedTx with extra data', () => {
378378
const result = mappers.mapTxAlonzo(txModel, {
379379
certificates,
380+
collateralOutputs: [txOutput],
380381
collaterals: inputs,
381382
inputSource,
382383
inputs,
@@ -389,7 +390,14 @@ describe('chain history mappers', () => {
389390
expect(result).toEqual<Cardano.HydratedTx>({
390391
...expected,
391392
auxiliaryData: { blob: metadata },
392-
body: { ...expected.body, certificates, collaterals: inputs, mint: assets, withdrawals },
393+
body: {
394+
...expected.body,
395+
certificates,
396+
collateralReturn: txOutput,
397+
collaterals: inputs,
398+
mint: assets,
399+
withdrawals
400+
},
393401
witness: { ...expected.witness, redeemers }
394402
});
395403
});

packages/cardano-services/test/ChainHistory/fixtures/FixtureBuilder.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export enum TxWith {
1717
Mint = 'mint',
1818
MultiAsset = 'multiAsset',
1919
Redeemer = 'redeemer',
20-
Withdrawal = 'withdrawal'
20+
Withdrawal = 'withdrawal',
21+
CollateralOutput = 'collateralOutput'
2122
}
2223

2324
export type AddressesInBlockRange = {
@@ -120,6 +121,7 @@ export class ChainHistoryFixtureBuilder {
120121
if (options.with.includes(TxWith.DelegationCertificate)) query += Queries.latestTxHashesWithDelegationCerts;
121122
if (options.with.includes(TxWith.MirCertificate)) query += Queries.latestTxHashesWithMirCerts;
122123
if (options.with.includes(TxWith.Withdrawal)) query += Queries.latestTxHashesWithWithdrawal;
124+
if (options.with.includes(TxWith.CollateralOutput)) query += Queries.latestTxHashesWithCollateralOutput;
123125

124126
query += Queries.endLatestTxHashes;
125127
}

packages/cardano-services/test/ChainHistory/fixtures/queries.ts

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ export const latestTxHashesWithMirCerts = `
5757
export const latestTxHashesWithWithdrawal = `
5858
JOIN withdrawal ON withdrawal.tx_id = tx.id`;
5959

60+
export const latestTxHashesWithCollateralOutput = `
61+
JOIN collateral_tx_out ON collateral_tx_out.tx_id = tx.id`;
62+
6063
export const endLatestTxHashes = `
6164
GROUP BY tx.id
6265
ORDER BY tx.id DESC
@@ -101,6 +104,7 @@ const Queries = {
101104
latestTxHashes,
102105
latestTxHashesWithAuxiliaryData,
103106
latestTxHashesWithCollateral,
107+
latestTxHashesWithCollateralOutput,
104108
latestTxHashesWithDelegationCerts,
105109
latestTxHashesWithMint,
106110
latestTxHashesWithMirCerts,

packages/cardano-services/test/data-mocks/tx.ts

+7
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ export const collateralInputs = [
114114
}
115115
];
116116

117+
export const collateralReturn: Cardano.TxOut = {
118+
address: Cardano.PaymentAddress('addr_test1wqnp362vmvr8jtc946d3a3utqgclfdl5y9d3kn849e359hst7hkqk'),
119+
value: {
120+
coins: 3_681_817_479_100_950n
121+
}
122+
};
123+
117124
export const withValidityInterval: Cardano.HydratedTx = merge(withAssets, {
118125
body: {
119126
validityInterval: {

packages/core/CHANGELOG.md

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
All notable changes to this project will be documented in this file.
44
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
55

6+
## [0.27.0](https://github.com/input-output-hk/cardano-js-sdk/compare/@cardano-sdk/[email protected]...@cardano-sdk/[email protected]) (2024-02-02)
7+
8+
### ⚠ BREAKING CHANGES
9+
10+
* **core:** tokenTransferInspector now takes an extra parameter AssetProvider
11+
- txSummaryInspector now takes an extra parameter AssetProvider
12+
- tokenTransferInspector now return AssetInfo rather than AssettId
13+
- txSummaryInspector now return AssetInfo rather than AssettId
14+
15+
### Features
16+
17+
* **core:** tokenTransferInspector and txSummaryInspector now return AssetInfo rather than AssettId ([219623f](https://github.com/input-output-hk/cardano-js-sdk/commit/219623fa1218c5f5e4c4cffffd43af7db04951f1))
18+
619
## [0.26.0](https://github.com/input-output-hk/cardano-js-sdk/compare/@cardano-sdk/[email protected]...@cardano-sdk/[email protected]) (2024-02-02)
720

821
### ⚠ BREAKING CHANGES

packages/core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cardano-sdk/core",
3-
"version": "0.26.0",
3+
"version": "0.27.0",
44
"description": "Core types and libraries for Cardano",
55
"engines": {
66
"node": ">=16.20.2"

packages/core/src/util/tokenTransferInspector.ts

+56-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
import * as Cardano from '../Cardano';
2+
import { AssetInfo } from '../Asset';
3+
import { AssetProvider } from '../Provider';
24
import { Inspector, resolveInputs } from './txInspector';
35
import { coalesceValueQuantities } from './coalesceValueQuantities';
46
import { subtractValueQuantities } from './subtractValueQuantities';
57
import uniq from 'lodash/uniq';
68

9+
export type AssetInfoWithAmount = { amount: Cardano.Lovelace; assetInfo: AssetInfo };
10+
11+
export type TokenTransferValue = {
12+
assets: Map<Cardano.AssetId, AssetInfoWithAmount>;
13+
coins: Cardano.Lovelace;
14+
};
15+
716
export type TokenTransferInspection = {
8-
fromAddress: Map<Cardano.PaymentAddress, Cardano.Value>;
9-
toAddress: Map<Cardano.PaymentAddress, Cardano.Value>;
17+
fromAddress: Map<Cardano.PaymentAddress, TokenTransferValue>;
18+
toAddress: Map<Cardano.PaymentAddress, TokenTransferValue>;
1019
};
1120

21+
/** Arguments for the token transfer inspector. */
1222
export interface TokenTransferInspectorArgs {
23+
/** The input resolver. */
1324
inputResolver: Cardano.InputResolver;
25+
26+
/** The asset provider to resolve AssetInfo for assets in the fromAddress field. */
27+
fromAddressAssetProvider: AssetProvider;
28+
29+
/** The asset provider to resolve AssetInfo for assets in the toAddress field. */
30+
toAddressAssetProvider: AssetProvider;
1431
}
1532

1633
export type TokenTransferInspector = (args: TokenTransferInspectorArgs) => Inspector<TokenTransferInspection>;
@@ -99,9 +116,41 @@ const removeZeroBalanceEntries = (addressMap: Map<Cardano.PaymentAddress, Cardan
99116
}
100117
};
101118

119+
const toTokenTransferValue = async (
120+
assetProvider: AssetProvider,
121+
addressMap: Map<Cardano.PaymentAddress, Cardano.Value>
122+
): Promise<Map<Cardano.PaymentAddress, TokenTransferValue>> => {
123+
const tokenTransferValue = new Map<Cardano.PaymentAddress, TokenTransferValue>();
124+
125+
for (const [address, value] of addressMap.entries()) {
126+
const coins = value.coins;
127+
const assetIds = uniq(value.assets && value.assets.size > 0 ? [...value.assets.keys()] : []);
128+
const assetInfos = new Map<Cardano.AssetId, AssetInfoWithAmount>();
129+
130+
if (assetIds.length > 0) {
131+
const assets = await assetProvider.getAssets({
132+
assetIds,
133+
extraData: { nftMetadata: true, tokenMetadata: true }
134+
});
135+
136+
for (const asset of assets) {
137+
const amount = value.assets?.get(asset.assetId) ?? 0n;
138+
assetInfos.set(asset.assetId, { amount, assetInfo: asset });
139+
}
140+
}
141+
142+
tokenTransferValue.set(address, {
143+
assets: assetInfos,
144+
coins
145+
});
146+
}
147+
148+
return tokenTransferValue;
149+
};
150+
102151
/** Inspect a transaction and return a map of addresses and their balances. */
103152
export const tokenTransferInspector: TokenTransferInspector =
104-
({ inputResolver }) =>
153+
({ inputResolver, fromAddressAssetProvider, toAddressAssetProvider }) =>
105154
async (tx) => {
106155
const { resolvedInputs } = await resolveInputs(tx.body.inputs, inputResolver);
107156

@@ -118,5 +167,8 @@ export const tokenTransferInspector: TokenTransferInspector =
118167
removeZeroBalanceEntries(fromAddress);
119168
removeZeroBalanceEntries(toAddress);
120169

121-
return { fromAddress, toAddress };
170+
return {
171+
fromAddress: await toTokenTransferValue(fromAddressAssetProvider, fromAddress),
172+
toAddress: await toTokenTransferValue(toAddressAssetProvider, toAddress)
173+
};
122174
};

0 commit comments

Comments
 (0)