Skip to content

Commit

Permalink
feat(ts): add Starknet data types (#1523)
Browse files Browse the repository at this point in the history
  • Loading branch information
xJonathanLEI committed Dec 6, 2023
1 parent 3571a57 commit 167696e
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/ninety-comics-joke.md
@@ -0,0 +1,5 @@
---
'@graphprotocol/graph-ts': minor
---

add starknet data types
39 changes: 39 additions & 0 deletions 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<Bytes>,
public data: Array<Bytes>,
public block: Block,
public transaction: Transaction,
) {}
}
}
10 changes: 10 additions & 0 deletions packages/ts/common/numbers.ts
Expand Up @@ -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<ByteArray>(this.reverse()));
}
}
32 changes: 31 additions & 1 deletion packages/ts/global/global.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -563,6 +580,19 @@ export function id_of_type(typeId: TypeId): usize {
return idof<Array<arweave.Transaction>>();
case TypeId.ArweaveTransactionWithBlockPtr:
return idof<arweave.TransactionWithBlockPtr>();
/**
* Starknet type ids
*/
case TypeId.StarknetBlock:
return idof<starknet.Block>();
case TypeId.StarknetTransaction:
return idof<starknet.Transaction>();
case TypeId.StarknetTransactionTypeEnum:
return idof<Array<starknet.TransactionType>>();
case TypeId.StarknetEvent:
return idof<starknet.Event>();
case TypeId.StarknetArrayBytes:
return idof<Array<Bytes>>();
default:
return 0;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/ts/index.ts
Expand Up @@ -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';
Expand Down
2 changes: 2 additions & 0 deletions packages/ts/test/test.js
Expand Up @@ -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 {
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 167696e

Please sign in to comment.