Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🎸 Make Distribution.currency of type Asset #261

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions db/migrations/1_currency_as_asset.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- rename the column currency to currency_id
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM pg_attribute
WHERE attname = 'currency'
AND attrelid = (
SELECT oid
FROM pg_class
WHERE relname = 'distributions'
)
) THEN
EXECUTE 'ALTER TABLE distributions RENAME COLUMN currency TO currency_id';
EXECUTE 'CREATE INDEX "0xfa49555fd3055365" ON distributions USING GIST (currency_id, _block_range)';
END IF;
END $$;

-- first update distribution currency to the one as received from events (either ticker or asset ID)
update
distributions d
set
currency_id = e.corporate_action_ticker
from
events e
where e.block_id = d.created_block_id
and e.module_id = 'capitaldistribution'
and e.event_id = 'Created';

-- then update the distribution currency to asset_id value from the assets table
update
distributions d
set
currency_id = a.id
from
assets a
where a.ticker = d.currency_id;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "polymesh-subquery",
"version": "16.1.0",
"version": "17.0.0",
"author": "Polymesh Association",
"license": "Apache-2.0",
"description": "A Polymesh Chain Indexer, providing a GraphQL interface",
Expand Down
2 changes: 1 addition & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2037,7 +2037,7 @@ type Distribution @entity {
localId: Int! @index(unique: false)
asset: Asset! @index(unique: false)
portfolio: Portfolio!
currency: String!
polymath-eric marked this conversation as resolved.
Show resolved Hide resolved
currency: Asset!
perShare: BigInt!
amount: BigInt!
remaining: BigInt!
Expand Down
2 changes: 1 addition & 1 deletion src/mappings/entities/assets/mapCorporateActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const handleDistributionCreated = async (event: SubstrateEvent): Promise<
const [rawDid, rawCaId, rawDistribution] = params;

const { localId, assetId } = await getCaIdValue(rawCaId, block);
const distributionDetails = getDistributionValue(rawDistribution);
const distributionDetails = await getDistributionValue(rawDistribution, block);

await Distribution.create({
id: `${assetId}/${localId}`,
Expand Down
21 changes: 13 additions & 8 deletions src/utils/distributions.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { Codec } from '@polkadot/types/types';
import { END_OF_TIME, extractBigInt, getBigIntValue, hexToString } from './common';
import { meshPortfolioToPortfolio } from './portfolios';
import { SubstrateBlock } from '@subql/types';
import { Distribution } from 'src/types';
import { getAssetId } from './assets';
import { END_OF_TIME, extractBigInt, getBigIntValue } from './common';
import { meshPortfolioToPortfolio } from './portfolios';

export const getDistributionValue = (
item: Codec
): Pick<
Distribution,
'portfolioId' | 'currency' | 'perShare' | 'amount' | 'remaining' | 'paymentAt' | 'expiresAt'
export const getDistributionValue = async (
item: Codec,
block: SubstrateBlock
): Promise<
Pick<
Distribution,
'portfolioId' | 'currencyId' | 'perShare' | 'amount' | 'remaining' | 'paymentAt' | 'expiresAt'
>
> => {
const { from, currency, amount, remaining, ...rest } = JSON.parse(item.toString());
const { identityId, number } = meshPortfolioToPortfolio(from);
return {
portfolioId: `${identityId}/${number}`,
currency: hexToString(currency),
currencyId: await getAssetId(currency, block),
perShare: BigInt(extractBigInt(rest, 'per_share') || 0),
amount: getBigIntValue(amount),
remaining: getBigIntValue(remaining),
Expand Down
Loading