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: Upgrade parachain 1.21.0 #61

Merged
merged 9 commits into from
Jan 17, 2023
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,12 @@ yarn gen:types:testnet

The GraphQL entities are defined in `schema.graphql`. When the schema is changed:

1. Ensure your DB is up to date (e.g. you recently ran `yarn db:migrate`)
2. Run `yarn db:create-migration`, and enter a name for the migration. This will auto-generate it.
3. Run `yarn db:migrate` to apply your new migration. (Or `yarn db:reset` if you wish to restart processing on the newly migrated DB, which is often a new idea - `db:reset` automatically applies runs `migrate` as well.)
4. Run `yarn gen:code` to regenerate Typescript types for the GraphQL entities.
5. You can now develop the mappings against the new entities. Run `yarn build` as usual to proceed.
1. Ensure your DB is running and up to date (e.g. you recently ran `yarn db:migrate`)
2. Run `yarn gen:code` to regenerate TypeScript types for the GraphQL entities.
3. Run `yarn build` to compile the newly generated code.
4. Run `yarn db:create-migration`, and enter a name for the migration. This will auto-generate it.
5. Run `yarn db:migrate` to apply your new migration.
6. You can now develop the mappings against the new entities.
Comment on lines +153 to +158
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great update of the readme! 👏


### Workflow - connecting to a local chain

Expand Down
35 changes: 35 additions & 0 deletions combined.typegen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"outDir": "src/types",
"specVersions": "indexer/combined.chainVersions.jsonl",
"typesBundle": "indexer/typesBundle.json",
"events": [
"BTCRelay.StoreMainChainHeader",
"Escrow.Deposit",
"Escrow.Withdraw",
"Issue.CancelIssue",
"Issue.ExecuteIssue",
"Issue.IssuePeriodChange",
"Issue.RequestIssue",
"Oracle.FeedValues",
"Redeem.CancelRedeem",
"Redeem.ExecuteRedeem",
"Redeem.RequestRedeem",
"Redeem.RedeemPeriodChange",
"Security.UpdateActiveBlock",
"Tokens.Transfer",
"VaultRegistry.RegisterVault",
"VaultRegistry.IncreaseLockedCollateral",
"VaultRegistry.DecreaseLockedCollateral",
"ZenlinkProtocol.AssetSwap",
"ZenlinkProtocol.LiquidityAdded",
"ZenlinkProtocol.LiquidityRemoved"
],
"calls": [
"BTCRelay.store_block_header",
"System.set_storage"
],
"storage": [
"Issue.IssuePeriod",
"Redeem.RedeemPeriod"
]
}
153 changes: 153 additions & 0 deletions db/migrations/1669651371135-Data.js

Large diffs are not rendered by default.

153 changes: 0 additions & 153 deletions db/migrations/1673456801077-Data.js

This file was deleted.

17 changes: 17 additions & 0 deletions db/migrations/1673950446387-Data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = class Data1673950446387 {
name = 'Data1673950446387'

async up(db) {
await db.query(`ALTER TABLE "cumulative_volume" DROP COLUMN "type"`)
await db.query(`ALTER TABLE "cumulative_volume" ADD "type" character varying(12) NOT NULL`)
await db.query(`ALTER TABLE "cumulative_volume_per_currency_pair" DROP COLUMN "type"`)
await db.query(`ALTER TABLE "cumulative_volume_per_currency_pair" ADD "type" character varying(12) NOT NULL`)
}

async down(db) {
await db.query(`ALTER TABLE "cumulative_volume" ADD "type" character varying(10) NOT NULL`)
await db.query(`ALTER TABLE "cumulative_volume" DROP COLUMN "type"`)
await db.query(`ALTER TABLE "cumulative_volume_per_currency_pair" ADD "type" character varying(10) NOT NULL`)
await db.query(`ALTER TABLE "cumulative_volume_per_currency_pair" DROP COLUMN "type"`)
}
}
25 changes: 25 additions & 0 deletions indexer/combined.chainVersions.jsonl

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions indexer/kintnet.chainVersions.jsonl

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions indexer/kintsugi.chainVersions.jsonl

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions indexer/localtest.chainVersions.jsonl

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "interbtc-indexer",
"private": "true",
"version": "0.11.1",
"version": "0.12.0",
"description": "GraphQL server and Substrate indexer for the interBTC parachain",
"author": "",
"license": "ISC",
Expand All @@ -11,6 +11,7 @@
"gen:explore:interlay": "squid-substrate-metadata-explorer --chain wss://api.interlay.io/parachain --archive https://interlay.archive.subsquid.io/graphql --out indexer/interlay.chainVersions.jsonl",
"gen:explore:kintsugi": "squid-substrate-metadata-explorer --chain wss://api-kusama.interlay.io/parachain --archive https://kintsugi.archive.subsquid.io/graphql --out indexer/kintsugi.chainVersions.jsonl",
"gen:explore:testnet": "squid-substrate-metadata-explorer --chain wss://api-testnet.interlay.io/parachain --archive https://api-testnet.interlay.io/subsquid-explorer/graphql --out indexer/testnet.chainVersions.jsonl",
"gen:explore:kintent": "squid-substrate-metadata-explorer --chain wss://api-dev-kintsugi.interlay.io/parachain --archive https://api-dev-kintsugi.interlay.io/subsquid-explorer/graphql --out indexer/kintnet.chainVersions.jsonl",
"gen:types:interlay": "squid-substrate-typegen interlay.typegen.json",
"gen:types:kintsugi": "squid-substrate-typegen kintsugi.typegen.json",
"gen:types:testnet": "squid-substrate-typegen testnet.typegen.json",
Expand All @@ -22,9 +23,8 @@
"query-node:start": "squid-graphql-server"
},
"dependencies": {
"@interlay/interbtc-api": "1.5.20",
"@interlay/interbtc-types": "1.1.1",
"@polkadot/util-crypto": "^10.1.7",
"@interlay/interbtc-api": "1.20.0",
"@polkadot/util-crypto": "^10.2.3",
"@subsquid/archive-registry": "^1.0.12",
"@subsquid/graphql-server": "^2.1.0",
"@subsquid/ss58": "^0.1.0",
Expand All @@ -38,6 +38,7 @@
"typeorm": "^0.3.9"
},
"devDependencies": {
"@interlay/interbtc-types": "1.1.1",
"@subsquid/cli": "^0.7.4",
"@subsquid/substrate-metadata-explorer": "^1.0.9",
"@subsquid/substrate-typegen": "^1.5.1",
Expand Down
13 changes: 12 additions & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,18 @@ type LendToken {
lendTokenId: Int!
}

union Currency = NativeToken | ForeignAsset | LendToken
type StableLpToken {
poolId: Int!
}

union PooledToken = NativeToken | ForeignAsset | StableLpToken

type LpToken {
token0: PooledToken!
token1: PooledToken!
}

union Currency = NativeToken | ForeignAsset | LendToken | LpToken | StableLpToken

type Vault @entity {
id: ID!
Expand Down
3 changes: 2 additions & 1 deletion src/mappings/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { VaultId as VaultIdV15 } from "../types/v15";
import { VaultId as VaultIdV17 } from "../types/v17";
import { VaultId as VaultIdV6 } from "../types/v6";
import { VaultId as VaultIdV1020000 } from "../types/v1020000";
import { VaultId as VaultIdV1021000 } from "../types/v1021000";
import { encodeLegacyVaultId, encodeVaultId } from "./encoding";

export type eventArgs = {
Expand All @@ -24,7 +25,7 @@ export async function getVaultIdLegacy(
});
}

export async function getVaultId(store: Store, vaultId: VaultIdV1020000) {
export async function getVaultId(store: Store, vaultId: VaultIdV1020000 | VaultIdV1021000) {
return store.get(Vault, {
where: { id: encodeVaultId(vaultId) },
});
Expand Down
91 changes: 73 additions & 18 deletions src/mappings/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
ForeignAsset,
Currency,
LendToken,
LpToken,
PooledToken,
StableLpToken,
} from "../model";
import {
VaultId as VaultIdV1020000,
Expand All @@ -21,6 +24,12 @@ import {
CurrencyId_Token as CurrencyId_TokenV6,
VaultId as VaultIdV6,
} from "../types/v6";
import {
LpToken as LpToken_V1021000,
VaultId as VaultIdV1021000,
CurrencyId as CurrencyId_V1021000,
} from "../types/v1021000";

import { CurrencyId_Token as CurrencyId_TokenV10 } from "../types/v10";
import { encodeBtcAddress, getBtcNetwork } from "./bitcoinUtils";

Expand Down Expand Up @@ -55,33 +64,79 @@ export const legacyCurrencyId = {
},
};

export const currencyId = {
encode: (asset: CurrencyId_V1020000): Currency => {
if (asset.__kind === "LendToken") {
return new LendToken({
lendTokenId: asset.value,
export const lpTokenId = {
encode: (lpToken: LpToken_V1021000): PooledToken => {
if (lpToken.__kind === "StableLpToken") {
return new StableLpToken({
poolId: lpToken.value,
});
} else if (asset.__kind === "ForeignAsset") {
// TODO: add asset-registry event decoding for more metadata?
} else if (lpToken.__kind === "ForeignAsset") {
return new ForeignAsset({
asset: asset.value,
asset: lpToken.value,
});
} else {
} else if (lpToken.__kind === "Token"){
return new NativeToken({
token: Token[asset.value.__kind],
token: Token[lpToken.value.__kind],
});
}

// throw if unhandled
throw new Error(`Unknown LpToken type to encode: ${JSON.stringify(lpToken)}`);
}
};

export const currencyId = {
encode: (asset: CurrencyId_V1020000 | CurrencyId_V1021000): Currency => {
switch(asset.__kind) {
case "LendToken":
return new LendToken({
lendTokenId: asset.value,
});
case "ForeignAsset":
return new ForeignAsset({
asset: asset.value,
});
case "Token":
return new NativeToken({
token: Token[asset.value.__kind],
});
case "StableLpToken":
return lpTokenId.encode(asset);
case "LpToken":
return new LpToken({
token0: lpTokenId.encode(asset.value[0]),
token1: lpTokenId.encode(asset.value[1])
});

default:
// throw if not handled
throw new Error(`Unknown currency type to encode: ${JSON.stringify(asset)}`);
}
},
};

// Note: At the moment, this method is primarily used to encode vault_ids.
// So adding lend tokens, lp token pairs, etc is kinda overkill
// and mainly done for future proofing.
// Very much unlike the currencyId.encode and lpTokenId.encode methods.
function currencyToString(currency: Currency): string {
if (currency.isTypeOf === "LendToken") {
// TODO: decide how we want to distinguish lend tokens from foreign assets
return `lendToken_${currency.lendTokenId.toString()}`;
} else if (currency.isTypeOf === "ForeignAsset") {
return currency.asset.toString();
} else {
return currency.token.toString();
switch(currency.isTypeOf) {
case "LendToken":
return `lendToken_${currency.lendTokenId.toString()}`;
case "ForeignAsset":
return currency.asset.toString();
case "NativeToken":
return currency.token.toString();
case "StableLpToken":
return `poolId_${currency.poolId.toString()}`;
case "LpToken":
const token0string = currencyToString(currency.token0);
const token1string = currencyToString(currency.token1);
return `lpToken__${token0string}__${token1string}`;

default:
// fallback throw if unhandled
throw new Error(`Unknown currency type to stringify: ${JSON.stringify(currency)}`);
}
}

Expand All @@ -94,7 +149,7 @@ export function encodeLegacyVaultId(vaultId: VaultIdV6 | VaultIdV15) {
)}`;
}

export function encodeVaultId(vaultId: VaultIdV1020000) {
export function encodeVaultId(vaultId: VaultIdV1020000 | VaultIdV1021000) {
const addressStr = address.interlay.encode(vaultId.accountId).toString();
const wrapped = currencyId.encode(vaultId.currencies.wrapped);
const collateral = currencyId.encode(vaultId.currencies.collateral);
Expand Down
11 changes: 5 additions & 6 deletions src/mappings/event/btcRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ export async function storeMainChainHeader(
const rawEvent = new BtcRelayStoreMainChainHeaderEvent(ctx, item.event);
let e;

if (rawEvent.isV4) e = rawEvent.asV4;
else {
e = rawEvent.asV1019000;
if (!rawEvent.isV1019000)
ctx.log.warn(`UNKOWN EVENT VERSION: BTCRelay.storeMainChainHeader`);
}
if (!rawEvent.isV4) {
ctx.log.warn("UNKOWN EVENT VERSION: BTCRelay.storeMainChainHeader");
return;
}
e = rawEvent.asV4;

const relayedAtHeight = await blockToHeight(
ctx,
Expand Down
23 changes: 11 additions & 12 deletions src/mappings/event/escrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export async function deposit(
const rawEvent = new EscrowDepositEvent(ctx, item.event);
let e;

if (rawEvent.isV6) e = rawEvent.asV6;
else {
e = rawEvent.asV1019000;
if (!rawEvent.isV1019000)
ctx.log.warn(`UNKOWN EVENT VERSION: Escrow.deposit`);
}
if (!rawEvent.isV6) {
ctx.log.warn("UNKOWN EVENT VERSION: Escrow.deposit");
return;
}
e = rawEvent.asV6;

const timestamp = new Date(block.timestamp);

if (e.amount === 0n) return;
Expand All @@ -44,12 +44,11 @@ export async function withdraw(
): Promise<void> {
const rawEvent = new EscrowWithdrawEvent(ctx, item.event);
let e;
if (rawEvent.isV6) e = rawEvent.asV6;
else {
e = rawEvent.asV1019000;
if (!rawEvent.isV1019000)
ctx.log.warn(`UNKNOWN EVENT VERSION: Escrow.withdraw`);
}
if (!rawEvent.isV6) {
ctx.log.warn(`UNKNOWN EVENT VERSION: Escrow.withdraw`);
return;
}
e = rawEvent.asV6;

const timestamp = new Date(block.timestamp);

Expand Down
36 changes: 17 additions & 19 deletions src/mappings/event/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ export async function requestIssue(
vaultIdString = encodeLegacyVaultId(e.vaultId);
} else {
if (rawEvent.isV17) e = rawEvent.asV17;
else if (rawEvent.isV1019000) e = rawEvent.asV1019000;
else if (rawEvent.isV1020000) e = rawEvent.asV1020000;
else if (rawEvent.isV1021000) e = rawEvent.asV1021000;
else {
e = rawEvent.asV1020000;
if (!rawEvent.isV1020000)
ctx.log.warn(`UNKOWN EVENT VERSION: Issue.requestIssue`);
ctx.log.warn(`UNKOWN EVENT VERSION: Issue.requestIssue`);
return;
}
vault = await getVaultId(ctx.store, e.vaultId);
vaultIdString = encodeVaultId(e.vaultId);
Expand Down Expand Up @@ -135,11 +135,11 @@ export async function executeIssue(
wrappedCurrency = legacyCurrencyId.encode(e.vaultId.currencies.wrapped);
} else {
if (rawEvent.isV17) e = rawEvent.asV17;
else if (rawEvent.isV1019000) e = rawEvent.asV1019000;
else if (rawEvent.isV1020000) e = rawEvent.asV1020000;
else if (rawEvent.isV1021000) e = rawEvent.asV1021000;
else {
e = rawEvent.asV1020000;
if (!rawEvent.isV1020000)
ctx.log.warn(`UNKOWN EVENT VERSION: Issue.executeIssue`);
ctx.log.warn(`UNKOWN EVENT VERSION: Issue.executeIssue`);
return;
}
collateralCurrency = currencyId.encode(e.vaultId.currencies.collateral);
wrappedCurrency = currencyId.encode(e.vaultId.currencies.wrapped);
Expand Down Expand Up @@ -205,12 +205,11 @@ export async function cancelIssue(
): Promise<void> {
const rawEvent = new IssueCancelIssueEvent(ctx, item.event);
let e;
if (rawEvent.isV4) e = rawEvent.asV4;
else {
e = rawEvent.asV1019000;
if (!rawEvent.isV1019000)
ctx.log.warn(`UNKOWN EVENT VERSION: Issue.cancelIssue`);
if (!rawEvent.isV4) {
ctx.log.warn(`UNKOWN EVENT VERSION: Issue.cancelIssue`);
return;
}
e = rawEvent.asV4;
const issue = await ctx.store.get(Issue, {
where: { id: toHex(e.issueId) },
});
Expand Down Expand Up @@ -241,12 +240,11 @@ export async function issuePeriodChange(
): Promise<void> {
const rawEvent = new IssueIssuePeriodChangeEvent(ctx, item.event);
let e;
if (rawEvent.isV16) e = rawEvent.asV16;
else {
e = rawEvent.asV1019000;
if (!rawEvent.isV1019000)
ctx.log.warn(`UNKOWN EVENT VERSION: Issue.issuePeriodChange`);
}
if (!rawEvent.isV16) {
ctx.log.warn(`UNKOWN EVENT VERSION: Issue.issuePeriodChange`);
return;
}
e = rawEvent.asV16;

const height = await blockToHeight(ctx, block.height, "IssuePeriodChange");

Expand Down
Loading