diff --git a/.changeset/ninety-comics-joke.md b/.changeset/ninety-comics-joke.md new file mode 100644 index 000000000..9350b0110 --- /dev/null +++ b/.changeset/ninety-comics-joke.md @@ -0,0 +1,5 @@ +--- +'@graphprotocol/graph-ts': minor +--- + +add starknet data types diff --git a/packages/ts/chain/starknet.ts b/packages/ts/chain/starknet.ts new file mode 100644 index 000000000..97f32d6ab --- /dev/null +++ b/packages/ts/chain/starknet.ts @@ -0,0 +1,39 @@ +import '../common/eager_offset'; +import { Bytes } from '../common/collections'; +import { BigInt } from '../common/numbers'; + +export namespace starknet { + export class Block { + constructor( + public number: BigInt, + public hash: Bytes, + public prevHash: Bytes, + public timestamp: BigInt, + ) {} + } + + export class Transaction { + constructor( + public type: TransactionType, + public hash: Bytes, + ) {} + } + + export enum TransactionType { + DEPLOY = 0, + INVOKE_FUNCTION = 1, + DECLARE = 2, + L1_HANDLER = 3, + DEPLOY_ACCOUNT = 4, + } + + export class Event { + constructor( + public fromAddr: Bytes, + public keys: Array, + public data: Array, + public block: Block, + public transaction: Transaction, + ) {} + } +} diff --git a/packages/ts/common/numbers.ts b/packages/ts/common/numbers.ts index 3f12a9378..4e19b9638 100644 --- a/packages/ts/common/numbers.ts +++ b/packages/ts/common/numbers.ts @@ -438,3 +438,13 @@ export class BigDecimal { return diff.digits > BigInt.fromI32(0) ? 1 : -1; } } + +/** A type representing Starknet's field element type. */ +export class Felt extends Bytes { + /** + * Modifies and transforms the object IN-PLACE into `BigInt`. + */ + intoBigInt(): BigInt { + return BigInt.fromUnsignedBytes(changetype(this.reverse())); + } +} diff --git a/packages/ts/global/global.ts b/packages/ts/global/global.ts index 2b2b0079b..ec7e4bad3 100644 --- a/packages/ts/global/global.ts +++ b/packages/ts/global/global.ts @@ -2,6 +2,7 @@ import { arweave } from '../chain/arweave'; import { cosmos } from '../chain/cosmos'; import { ethereum } from '../chain/ethereum'; import { near } from '../chain/near'; +import { starknet } from '../chain/starknet'; import { Bytes, Entity, Result, TypedMap, TypedMapEntry, Wrapped } from '../common/collections'; import { BigDecimal } from '../common/numbers'; import { JSONValue, Value } from '../common/value'; @@ -230,7 +231,23 @@ export enum TypeId { ``` */ - // Reserved discriminant space for a future blockchain type IDs: [3,500, 4,499] + // Reserved discriminant space for Starknet type IDs: [3,500, 4,499] + StarknetBlock = 3500, + StarknetTransaction = 3501, + StarknetTransactionTypeEnum = 3502, + StarknetEvent = 3503, + StarknetArrayBytes = 3504, + /* + Continue to add more Starknet type IDs here. e.g.: + ``` + NextStarknetType = 3505, + AnotherStarknetType = 3506, + ... + LastStarknetType = 4499, + ``` + */ + + // Reserved discriminant space for a future blockchain type IDs: [4,500, 5,499] } export function id_of_type(typeId: TypeId): usize { @@ -563,6 +580,19 @@ export function id_of_type(typeId: TypeId): usize { return idof>(); case TypeId.ArweaveTransactionWithBlockPtr: return idof(); + /** + * Starknet type ids + */ + case TypeId.StarknetBlock: + return idof(); + case TypeId.StarknetTransaction: + return idof(); + case TypeId.StarknetTransactionTypeEnum: + return idof>(); + case TypeId.StarknetEvent: + return idof(); + case TypeId.StarknetArrayBytes: + return idof>(); default: return 0; } diff --git a/packages/ts/index.ts b/packages/ts/index.ts index c1a5dd5cd..c496e440e 100644 --- a/packages/ts/index.ts +++ b/packages/ts/index.ts @@ -11,6 +11,8 @@ export * from './chain/ethereum'; export * from './chain/near'; // Cosmos support export * from './chain/cosmos'; +// Starknet support +export * from './chain/starknet'; // Regular re-exports export * from './common/collections'; export * from './common/conversion'; diff --git a/packages/ts/test/test.js b/packages/ts/test/test.js index eb22402b8..a4cb12573 100644 --- a/packages/ts/test/test.js +++ b/packages/ts/test/test.js @@ -36,6 +36,7 @@ async function main() { fs.copyFileSync('chain/ethereum.ts', 'test/temp_lib/chain/ethereum.ts'); fs.copyFileSync('chain/near.ts', 'test/temp_lib/chain/near.ts'); fs.copyFileSync('chain/cosmos.ts', 'test/temp_lib/chain/cosmos.ts'); + fs.copyFileSync('chain/starknet.ts', 'test/temp_lib/chain/starknet.ts'); fs.copyFileSync('index.ts', 'test/temp_lib/index.ts'); try { @@ -70,6 +71,7 @@ async function main() { fs.unlinkSync('test/temp_lib/chain/ethereum.ts'); fs.unlinkSync('test/temp_lib/chain/near.ts'); fs.unlinkSync('test/temp_lib/chain/cosmos.ts'); + fs.unlinkSync('test/temp_lib/chain/starknet.ts'); fs.rmdirSync('test/temp_lib/chain'); fs.unlinkSync('test/temp_lib/index.ts'); fs.rmdirSync('test/temp_lib');