Skip to content

Commit

Permalink
feat: pool retirement operation in block transaction endpoints (carda…
Browse files Browse the repository at this point in the history
  • Loading branch information
abarretoatix committed Apr 30, 2021
1 parent 4f8beef commit 8d33c69
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 3 deletions.
28 changes: 25 additions & 3 deletions cardano-rosetta-server/src/server/db/blockchain-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import Queries, {
FindTransactionPoolRelays,
FindTransactionPoolOwners,
FindTransactionWithdrawals,
FindPoolRetirements,
FindUtxo,
FindMaBalance
} from './queries/blockchain-queries';
Expand Down Expand Up @@ -145,7 +146,8 @@ const mapTransactionsToDict = (transactions: Transaction[]): TransactionsMap =>
registrations: [],
deregistrations: [],
delegations: [],
poolRegistrations: []
poolRegistrations: [],
poolRetirements: []
}
};
}, {});
Expand Down Expand Up @@ -304,6 +306,23 @@ const parseInputsRow = (
(updatedTransaction, updatedCollection) => ({ ...updatedTransaction, inputs: updatedCollection })
);

/**
* Parses a pool retirement row into a pool retirement object
*
* @param transaction
* @param deregistration
*/
const parsePoolRetirementRow = (
transaction: PopulatedTransaction,
poolRetirement: FindPoolRetirements
): PopulatedTransaction => ({
...transaction,
poolRetirements: transaction.poolRetirements.concat({
epoch: poolRetirement.epoch,
address: hexFormatter(poolRetirement.address)
})
});

/**
* Updates the transaction appending outputs
*
Expand Down Expand Up @@ -428,7 +447,8 @@ const populateTransactions = async (
Queries.findTransactionDelegations,
Queries.FindTransactionPoolRegistrationsData,
Queries.findTransactionPoolOwners,
Queries.findTransactionPoolRelays
Queries.findTransactionPoolRelays,
Queries.findPoolRetirements
];
const [
inputs,
Expand All @@ -439,7 +459,8 @@ const populateTransactions = async (
delegations,
poolsData,
poolsOwners,
poolsRelays
poolsRelays,
poolRetirements
] = await Promise.all(
operationsQueries.map(operationQuery => databaseInstance.query(operationQuery, [transactionsHashes]))
);
Expand All @@ -449,6 +470,7 @@ const populateTransactions = async (
transactionsMap = populateTransactionField(transactionsMap, registrations.rows, parseRegistrationsRow);
transactionsMap = populateTransactionField(transactionsMap, deregistrations.rows, parseDeregistrationsRow);
transactionsMap = populateTransactionField(transactionsMap, delegations.rows, parseDelegationsRow);
transactionsMap = populateTransactionField(transactionsMap, poolRetirements.rows, parsePoolRetirementRow);

const mappedPoolRegistrations = mapToTransactionPoolRegistrations(poolsData.rows, poolsOwners.rows, poolsRelays.rows);
transactionsMap = populateTransactionField(transactionsMap, mappedPoolRegistrations, parsePoolRegistrationsRows);
Expand Down
20 changes: 20 additions & 0 deletions cardano-rosetta-server/src/server/db/queries/blockchain-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ export interface FindMaBalance {
value: string;
}

export interface FindPoolRetirements extends FindTransactionFieldResult {
address: Buffer;
poolKeyHash: Buffer;
epoch: number;
}

const poolRegistrationQuery = `
WITH pool_registration AS (
SELECT
Expand Down Expand Up @@ -358,6 +364,19 @@ WITH utxo AS (
tx_in_tx.id IS NULL
)`;

const findPoolRetirements = `
SELECT
pr.retiring_epoch AS "epoch",
ph.hash_raw AS "address",
tx.hash as "txHash"
FROM pool_retire pr
INNER JOIN pool_hash ph
ON pr.hash_id = ph.id
INNER JOIN tx
ON tx.id = pr.announced_tx_id
WHERE tx.hash = ANY($1)
`;

const findUtxoByAddressAndBlock = (currencies?: CurrencyId[]): string => `
${utxoQuery}
SELECT
Expand Down Expand Up @@ -432,6 +451,7 @@ const Queries = {
findTransactionWithdrawals,
findTransactionsByBlock,
findTransactionsInputs,
findPoolRetirements,
findTransactionsOutputs,
findUtxoByAddressAndBlock,
findLatestMinFeeAAndMinFeeB
Expand Down
6 changes: 6 additions & 0 deletions cardano-rosetta-server/src/server/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ export interface PoolRegistration {
metadataHash?: string;
}

export interface PoolRetirement {
epoch: number;
address: string;
}

export interface Delegation {
stakeAddress: string;
poolHash: string;
Expand All @@ -140,6 +145,7 @@ export interface PopulatedTransaction extends Transaction {
deregistrations: Deregistration[];
delegations: Delegation[];
poolRegistrations: PoolRegistration[];
poolRetirements: PoolRetirement[];
}

export interface Network {
Expand Down
18 changes: 18 additions & 0 deletions cardano-rosetta-server/src/server/utils/data-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,24 @@ export const mapToRosettaTransaction = (
})
);
totalOperations.push(registrationsAsOperations);
const poolRetirementOperations: Components.Schemas.Operation[] = transaction.poolRetirements.map(
(poolRetirement, index) => ({
operation_identifier: {
index: getOperationCurrentIndex(totalOperations, index)
},
type: OperationType.POOL_RETIREMENT,
status: SUCCESS_STATUS,
account: {
address: poolRetirement.address
},
metadata: {
epoch: poolRetirement.epoch,
refundAmount: mapAmount((poolDeposit * -1).toString())
}
})
);
totalOperations.push(poolRetirementOperations);

const deregistrationsAsOperations: Components.Schemas.Operation[] = transaction.deregistrations.map(
(deregistration, index) => ({
operation_identifier: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
transactionBlock4490559WithDelegation,
transactionBlock4597861WithWithdrawals,
transactionBlock4853177WithDeregistration,
transactionBlock4853177WithPoolRetirement,
launchpad236643PoolRegistrationWithSeveralOwners
} from '../fixture-data';
import { setupDatabase, setupServer } from '../utils/test-utils';
Expand Down Expand Up @@ -258,6 +259,23 @@ describe('/block/transactions endpoint', () => {
expect(response.json()).toEqual(transactionBlock4853177WithDeregistration);
});

test('should return a pool retirement transaction', async () => {
const transaction = 'dcbff41c50c5b4012d49be5be75b11a0c5289515258ef4cf108eb6ec4ed5f37a';
const response = await serverWithMultiassetsSupport.inject({
method: 'post',
url: BLOCK_TRANSACTION_ENDPOINT,
payload: {
...generatePayload(236746, 'b389d1c4975563bf4199afeaa1434dfd1b406e30ac4eda884a03ecef8cd0a87a'),
// eslint-disable-next-line camelcase
transaction_identifier: {
hash: transaction
}
}
});
expect(response.statusCode).toEqual(StatusCodes.OK);
expect(response.json()).toEqual(transactionBlock4853177WithPoolRetirement);
});

test('should be able to return multiasset token transactions with several tokens in the bundle', async () => {
const transaction = '863783d4460647b8227411eb7b0cf8fac82c29f6f4ac52baf7e4d74fabb7884b';
const response = await serverWithMultiassetsSupport.inject({
Expand Down
133 changes: 133 additions & 0 deletions cardano-rosetta-server/test/e2e/fixture-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,139 @@ export const transactionBlock4490559WithDelegation = {
}
};

export const transactionBlock4853177WithPoolRetirement = {
transaction: {
transaction_identifier: {
hash: 'dcbff41c50c5b4012d49be5be75b11a0c5289515258ef4cf108eb6ec4ed5f37a'
},
operations: [
{
operation_identifier: { index: 0 },
type: 'input',
status: 'success',
account: {
address:
'addr_test1qzyh0zdfjmk997fkdrgcm4xmuhcqqd4qgphkmgm3shryrjhkjhp4qfyx33xada55u94c300knphrrgr577gdw5jpc39srpfmlp'
},
amount: { value: '-269377901300', currency: { symbol: 'ADA', decimals: 6 } },
coin_change: {
coin_identifier: {
identifier: '30ca1269e56ed17c97c202164c14e31a58c104f60927ed5de4252255fc624b6b:0'
},
coin_action: 'coin_spent'
},
metadata: {
tokenBundle: [
{
policyId: '202e0181ea963e2fcd206b1a794ce160afbe120dad5fd30a181d3a24',
tokens: [
{
value: '-1000000000000',
currency: { symbol: '41434c', decimals: 0 }
}
]
},
{
policyId: '34250edd1e9836f5378702fbf9416b709bc140e04f668cc355208518',
tokens: [
{
value: '-9952',
currency: { symbol: '4154414441636f696e', decimals: 0 }
},
{
value: '-8000000000',
currency: { symbol: '61646f736961', decimals: 0 }
},
{
value: '-2500',
currency: { symbol: '6d616368746c636f696e', decimals: 0 }
}
]
},
{
policyId: 'ecd07b4ef62f37a68d145de8efd60c53d288dd5ffc641215120cc3db',
tokens: [
{
value: '-500',
currency: { symbol: '6d616368746c32636f696e', decimals: 0 }
}
]
}
]
}
},
{
operation_identifier: { index: 1 },
type: 'poolRetirement',
status: 'success',
account: {
address: 'd6aafa5358b98373449434542e3da3564bc71635ae3247dc1a2b7b0e'
},
metadata: {
epoch: 676,
refundAmount: { value: '-500000000', currency: { symbol: 'ADA', decimals: 6 } }
}
},
{
operation_identifier: { index: 2, network_index: 0 },
related_operations: [{ index: 0 }],
type: 'output',
status: 'success',
account: {
address:
'addr_test1qzyh0zdfjmk997fkdrgcm4xmuhcqqd4qgphkmgm3shryrjhkjhp4qfyx33xada55u94c300knphrrgr577gdw5jpc39srpfmlp'
},
amount: { value: '269377714987', currency: { symbol: 'ADA', decimals: 6 } },
coin_change: {
coin_identifier: {
identifier: 'dcbff41c50c5b4012d49be5be75b11a0c5289515258ef4cf108eb6ec4ed5f37a:0'
},
coin_action: 'coin_created'
},
metadata: {
tokenBundle: [
{
policyId: '202e0181ea963e2fcd206b1a794ce160afbe120dad5fd30a181d3a24',
tokens: [
{
value: '1000000000000',
currency: { symbol: '41434c', decimals: 0 }
}
]
},
{
policyId: '34250edd1e9836f5378702fbf9416b709bc140e04f668cc355208518',
tokens: [
{
value: '9952',
currency: { symbol: '4154414441636f696e', decimals: 0 }
},
{
value: '8000000000',
currency: { symbol: '61646f736961', decimals: 0 }
},
{
value: '2500',
currency: { symbol: '6d616368746c636f696e', decimals: 0 }
}
]
},
{
policyId: 'ecd07b4ef62f37a68d145de8efd60c53d288dd5ffc641215120cc3db',
tokens: [
{
value: '500',
currency: { symbol: '6d616368746c32636f696e', decimals: 0 }
}
]
}
]
}
}
]
}
};

export const transactionBlock4853177WithDeregistration = {
transaction: {
operations: [
Expand Down

0 comments on commit 8d33c69

Please sign in to comment.