diff --git a/db/migrations/1_currency_as_asset.sql b/db/migrations/1_currency_as_asset.sql new file mode 100644 index 00000000..946dfe39 --- /dev/null +++ b/db/migrations/1_currency_as_asset.sql @@ -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; \ No newline at end of file diff --git a/package.json b/package.json index 97fac597..7f09d2c3 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/schema.graphql b/schema.graphql index 10027302..902f60df 100644 --- a/schema.graphql +++ b/schema.graphql @@ -2037,7 +2037,7 @@ type Distribution @entity { localId: Int! @index(unique: false) asset: Asset! @index(unique: false) portfolio: Portfolio! - currency: String! + currency: Asset! perShare: BigInt! amount: BigInt! remaining: BigInt! diff --git a/src/mappings/entities/assets/mapCorporateActions.ts b/src/mappings/entities/assets/mapCorporateActions.ts index 46408ce5..b4e0d087 100644 --- a/src/mappings/entities/assets/mapCorporateActions.ts +++ b/src/mappings/entities/assets/mapCorporateActions.ts @@ -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}`, diff --git a/src/utils/distributions.ts b/src/utils/distributions.ts index a62f1281..55b49576 100644 --- a/src/utils/distributions.ts +++ b/src/utils/distributions.ts @@ -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),