Skip to content

Commit

Permalink
add types for watr testnet
Browse files Browse the repository at this point in the history
  • Loading branch information
abhijeetbhagat committed Jan 12, 2024
1 parent 1a72635 commit e25660c
Show file tree
Hide file tree
Showing 12 changed files with 1,112 additions and 4 deletions.
2 changes: 1 addition & 1 deletion explorer-ui/src/contexts/chainNames.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ test("Shibuya chain info", () => {
})

test("watr testnet chain info", () => {
expect(resolveInfoFromName("watr network testnet")).toEqual("rococoWatr")
expect(resolveInfoFromName("watr testnet")).toEqual("rococoWatr")
})
2 changes: 1 addition & 1 deletion explorer-ui/src/contexts/chainNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const names = [
endpoint: "watr"
},
{
chain: "watr network testnet",
chain: "watr testnet",
endpoint: "rococoWatr"
},
{
Expand Down
4 changes: 2 additions & 2 deletions squid-ink/src/chain-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ const chainConfig: Record<string, ChainConfig> = {
ss58Format: 19,
token: {
tokenDecimals: 18,
tokenSymbol: "WATRD",
tokenSymbol: "WATR",
},
},
watr_test: {
name: "Watr Network Testnet",
name: "Watr Testnet",
ss58Format: 19,
token: {
tokenDecimals: 18,
Expand Down
27 changes: 27 additions & 0 deletions squid-ink/src/chains/watrtestnet/normalised-types/calls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as ss58 from "@subsquid/ss58";
import { ResolvedContractsCallCall } from "chains/normalised-return-types";
import { ss58Format } from "../../../chain-config";
import { ContractsCallCall } from "../types/calls";

export class NormalisedContractsCallCall extends ContractsCallCall {
resolve(): ResolvedContractsCallCall {
if (this.isWatrNodeV1000) {
const { dest, value, gasLimit, storageDepositLimit, data } =
this.asWatrNodeV1000;
// TODO: Ensure proper support of MultiAddress
if (dest.__kind === "Index") {
throw new Error("Multi-address of type Index is not supported!");
}
return {
contractAddress: ss58.codec(ss58Format).encode(dest.value),
value,
gasLimit: {
refTime: gasLimit,
},
storageDepositLimit,
data,
};
}
throw new Error("No Runtime version found");
}
}
189 changes: 189 additions & 0 deletions squid-ink/src/chains/watrtestnet/normalised-types/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable max-classes-per-file */
import * as ss58 from "@subsquid/ss58";
import { toHex } from "@subsquid/util-internal-hex";
import {
ResolvedBalancesEndowedEvent,
ResolvedBalancesReservedEvent,
ResolvedBalancesTransferEvent,
ResolvedBalancesWithdrawEvent,
ResolvedContractEmittedEvent,
ResolvedContractCodeRemovedEvent,
ResolvedContractsCodeStoredEvent,
ResolvedContractsCodeUpdatedEvent,
ResolvedContractsInstantiatedEvent,
ResolvedContractTerminatedEvent,
ResolvedNewAccountEvent,
} from "chains/normalised-return-types";
import { ss58Format } from "../../../chain-config";
import {
BalancesEndowedEvent,
BalancesReservedEvent,
BalancesTransferEvent,
BalancesWithdrawEvent,
ContractsCodeRemovedEvent,
ContractsCodeStoredEvent,
ContractsContractCodeUpdatedEvent,
ContractsContractEmittedEvent,
ContractsInstantiatedEvent,
ContractsTerminatedEvent,
SystemNewAccountEvent,
} from "../types/events";

export class NormalisedBalancesTransferEvent extends BalancesTransferEvent {
resolve(): ResolvedBalancesTransferEvent {
if (this.isWatrNodeV1000) {
const { from, to, amount } = this.asWatrNodeV1000;
return {
from: ss58.codec(ss58Format).encode(from),
to: ss58.codec(ss58Format).encode(to),
amount,
};
}
throw new Error(
"No runtime version found while decoding [BalancesTransferEvent]"
);
}
}

export class NormalisedBalancesEndowedEvent extends BalancesEndowedEvent {
resolve(): ResolvedBalancesEndowedEvent {
if (this.isWatrNodeV1000) {
const { account, freeBalance } = this.asWatrNodeV1000;
return {
account: ss58.codec(ss58Format).encode(account),
freeBalance,
};
}
throw new Error(
"No runtime version found while decoding [BalancesEndowedEvent]"
);
}
}

export class NormalisedBalancesWithdrawEvent extends BalancesWithdrawEvent {
resolve(): ResolvedBalancesWithdrawEvent {
if (this.isWatrNodeV1000) {
const { who, amount } = this.asWatrNodeV1000;
return {
account: ss58.codec(ss58Format).encode(who),
amount,
};
}
throw new Error(
"No runtime version found while decoding [BalancesWithdrawEvent]"
);
}
}

export class NormalisedBalancesReservedEvent extends BalancesReservedEvent {
resolve(): ResolvedBalancesReservedEvent {
if (this.isWatrNodeV1000) {
const { who, amount } = this.asWatrNodeV1000;
return {
account: ss58.codec(ss58Format).encode(who),
amount,
};
}
throw new Error(
"No runtime version found while decoding [BalancesReservedEvent]"
);
}
}

export class NormalisedContractsCodeRemovedEvent extends ContractsCodeRemovedEvent {
resolve(): ResolvedContractCodeRemovedEvent {
if (this.isWatrNodeV1000) {
const { codeHash } = this.asWatrNodeV1000;
return { codeHash: toHex(codeHash) };
}
throw new Error(
"No runtime version found while decoding [ContractsCodeRemovedEvent]"
);
}
}

export class NormalisedContractsInstantiatedEvent extends ContractsInstantiatedEvent {
resolve(): ResolvedContractsInstantiatedEvent {
if (this.isWatrNodeV1000) {
const { deployer, contract } = this.asWatrNodeV1000;
return {
deployer: ss58.codec(ss58Format).encode(deployer),
contract: ss58.codec(ss58Format).encode(contract),
};
}
throw new Error(
"No runtime version found while decoding [ContractsInstantiatedEvent]"
);
}
}

export class NormalisedContractsCodeStoredEvent extends ContractsCodeStoredEvent {
resolve(): ResolvedContractsCodeStoredEvent {
if (this.isWatrNodeV1000) {
const { codeHash } = this.asWatrNodeV1000;
return { codeHash: toHex(codeHash) };
}
throw new Error(
"No runtime version found while decoding [ContractsCodeStoredEvent]"
);
}
}

export class NormalisedContractsCodeUpdatedEvent extends ContractsContractCodeUpdatedEvent {
resolve(): ResolvedContractsCodeUpdatedEvent {
if (this.isWatrNodeV1000) {
const { contract, newCodeHash, oldCodeHash } = this.asWatrNodeV1000;
return {
contract: ss58.codec(ss58Format).encode(contract),
newCodeHash: toHex(newCodeHash),
oldCodeHash: toHex(oldCodeHash),
};
}
throw new Error(
"No runtime version found while decoding [ContractsContractCodeUpdatedEvent]"
);
}
}

export class NormalisedContractEmittedEvent extends ContractsContractEmittedEvent {
resolve(): ResolvedContractEmittedEvent {
if (this.isWatrNodeV1000) {
const { contract, data } = this.asWatrNodeV1000;
return { contract: ss58.codec(ss58Format).encode(contract), data };
}
throw new Error(
"No runtime version found while decoding [ContractsContractEmittedEvent]"
);
}
}

export class NormalisedContractTerminatedEvent extends ContractsTerminatedEvent {
resolve(): ResolvedContractTerminatedEvent {
if (this.isWatrNodeV1000) {
const { contract, beneficiary } = this.asWatrNodeV1000 as {
contract: Uint8Array;
beneficiary: Uint8Array;
};
return {
contract: ss58.codec(ss58Format).encode(contract),
beneficiary: ss58.codec(ss58Format).encode(beneficiary),
};
}
throw new Error(
"No runtime version found while decoding [ContractsTerminatedEvent]"
);
}
}

export class NormalisedSystemNewAccountEvent extends SystemNewAccountEvent {
resolve(): ResolvedNewAccountEvent {
if (this.isWatrNodeV1000) {
const { account } = this.asWatrNodeV1000;
return { account: ss58.codec(ss58Format).encode(account) };
}
throw new Error(
"No runtime version found while decoding [SystemNewAccountEvent]"
);
}
}
3 changes: 3 additions & 0 deletions squid-ink/src/chains/watrtestnet/normalised-types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./calls";
export * from "./events";
export * from "./storage";
90 changes: 90 additions & 0 deletions squid-ink/src/chains/watrtestnet/normalised-types/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import assert from "assert";
import * as ss58 from "@subsquid/ss58";
import { decodeHex } from "@subsquid/util-internal-hex";
import { ResolvedContractInfoOfStorage } from "chains/normalised-return-types";
import {
BalancesAccountStorage,
ContractsCodeStorageStorage,
ContractsContractInfoOfStorage,
ContractsOwnerInfoOfStorage,
SystemAccountStorage,
} from "../types/storage";
import {
AccountData,
AccountInfo,
OwnerInfo,
PrefabWasmModule,
} from "../types/watrNodeV1000";
import { ss58Format } from "../../../chain-config";

export class NormalisedSystemAccountStorage extends SystemAccountStorage {
async get(accountId: string): Promise<AccountInfo> {
assert(this.isExists);
if (this.isWatrNodeV1000) {
return this.getAsWatrNodeV1000(ss58.codec(ss58Format).decode(accountId));
}
throw new Error("No Runtime version found");
}
}

export class NormalisedBalancesAccountStorage extends BalancesAccountStorage {
async get(accountId: string): Promise<AccountData> {
assert(this.isExists);
if (this.isWatrNodeV1000) {
return this.getAsWatrNodeV1000(ss58.codec(ss58Format).decode(accountId));
}
throw new Error("No Runtime version found");
}
}

export class NormalisedContractInfoOfStorage extends ContractsContractInfoOfStorage {
async get(accountId: string): Promise<ResolvedContractInfoOfStorage> {
assert(this.isExists);
let info: ResolvedContractInfoOfStorage | undefined;
if (this.isWatrNodeV1000) {
info = await this.getAsWatrNodeV1000(
ss58.codec(ss58Format).decode(accountId)
);
} else {
throw new Error("No Runtime version found");
}
if (info) {
return info;
}
throw new Error(
`ContractInfoOf not found in storage for accountId [${accountId}]`
);
}
}

export class NormalisedCodeStorageStorage extends ContractsCodeStorageStorage {
async get(key: string): Promise<PrefabWasmModule> {
assert(this.isExists);
let info: PrefabWasmModule | undefined;
if (this.isWatrNodeV1000) {
info = await this.getAsWatrNodeV1000(decodeHex(key));
} else {
throw new Error("No Runtime version found");
}
if (info) {
return info;
}
throw new Error(`CodeStorage not found in storage for key [${key}]`);
}
}

export class NormalisedOwnerInfoOfStorage extends ContractsOwnerInfoOfStorage {
async get(key: string): Promise<OwnerInfo> {
assert(this.isExists);
let info: OwnerInfo | undefined;
if (this.isWatrNodeV1000) {
info = await this.getAsWatrNodeV1000(decodeHex(key));
} else {
throw new Error("No Runtime version found");
}
if (info) {
return info;
}
throw new Error(`CodeStorage not found in storage for key [${key}]`);
}
}
Loading

0 comments on commit e25660c

Please sign in to comment.