Skip to content

Commit 167696e

Browse files
authored
feat(ts): add Starknet data types (#1523)
1 parent 3571a57 commit 167696e

File tree

6 files changed

+89
-1
lines changed

6 files changed

+89
-1
lines changed

.changeset/ninety-comics-joke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/graph-ts': minor
3+
---
4+
5+
add starknet data types

packages/ts/chain/starknet.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import '../common/eager_offset';
2+
import { Bytes } from '../common/collections';
3+
import { BigInt } from '../common/numbers';
4+
5+
export namespace starknet {
6+
export class Block {
7+
constructor(
8+
public number: BigInt,
9+
public hash: Bytes,
10+
public prevHash: Bytes,
11+
public timestamp: BigInt,
12+
) {}
13+
}
14+
15+
export class Transaction {
16+
constructor(
17+
public type: TransactionType,
18+
public hash: Bytes,
19+
) {}
20+
}
21+
22+
export enum TransactionType {
23+
DEPLOY = 0,
24+
INVOKE_FUNCTION = 1,
25+
DECLARE = 2,
26+
L1_HANDLER = 3,
27+
DEPLOY_ACCOUNT = 4,
28+
}
29+
30+
export class Event {
31+
constructor(
32+
public fromAddr: Bytes,
33+
public keys: Array<Bytes>,
34+
public data: Array<Bytes>,
35+
public block: Block,
36+
public transaction: Transaction,
37+
) {}
38+
}
39+
}

packages/ts/common/numbers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,3 +438,13 @@ export class BigDecimal {
438438
return diff.digits > BigInt.fromI32(0) ? 1 : -1;
439439
}
440440
}
441+
442+
/** A type representing Starknet's field element type. */
443+
export class Felt extends Bytes {
444+
/**
445+
* Modifies and transforms the object IN-PLACE into `BigInt`.
446+
*/
447+
intoBigInt(): BigInt {
448+
return BigInt.fromUnsignedBytes(changetype<ByteArray>(this.reverse()));
449+
}
450+
}

packages/ts/global/global.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { arweave } from '../chain/arweave';
22
import { cosmos } from '../chain/cosmos';
33
import { ethereum } from '../chain/ethereum';
44
import { near } from '../chain/near';
5+
import { starknet } from '../chain/starknet';
56
import { Bytes, Entity, Result, TypedMap, TypedMapEntry, Wrapped } from '../common/collections';
67
import { BigDecimal } from '../common/numbers';
78
import { JSONValue, Value } from '../common/value';
@@ -230,7 +231,23 @@ export enum TypeId {
230231
```
231232
*/
232233

233-
// Reserved discriminant space for a future blockchain type IDs: [3,500, 4,499]
234+
// Reserved discriminant space for Starknet type IDs: [3,500, 4,499]
235+
StarknetBlock = 3500,
236+
StarknetTransaction = 3501,
237+
StarknetTransactionTypeEnum = 3502,
238+
StarknetEvent = 3503,
239+
StarknetArrayBytes = 3504,
240+
/*
241+
Continue to add more Starknet type IDs here. e.g.:
242+
```
243+
NextStarknetType = 3505,
244+
AnotherStarknetType = 3506,
245+
...
246+
LastStarknetType = 4499,
247+
```
248+
*/
249+
250+
// Reserved discriminant space for a future blockchain type IDs: [4,500, 5,499]
234251
}
235252

236253
export function id_of_type(typeId: TypeId): usize {
@@ -563,6 +580,19 @@ export function id_of_type(typeId: TypeId): usize {
563580
return idof<Array<arweave.Transaction>>();
564581
case TypeId.ArweaveTransactionWithBlockPtr:
565582
return idof<arweave.TransactionWithBlockPtr>();
583+
/**
584+
* Starknet type ids
585+
*/
586+
case TypeId.StarknetBlock:
587+
return idof<starknet.Block>();
588+
case TypeId.StarknetTransaction:
589+
return idof<starknet.Transaction>();
590+
case TypeId.StarknetTransactionTypeEnum:
591+
return idof<Array<starknet.TransactionType>>();
592+
case TypeId.StarknetEvent:
593+
return idof<starknet.Event>();
594+
case TypeId.StarknetArrayBytes:
595+
return idof<Array<Bytes>>();
566596
default:
567597
return 0;
568598
}

packages/ts/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export * from './chain/ethereum';
1111
export * from './chain/near';
1212
// Cosmos support
1313
export * from './chain/cosmos';
14+
// Starknet support
15+
export * from './chain/starknet';
1416
// Regular re-exports
1517
export * from './common/collections';
1618
export * from './common/conversion';

packages/ts/test/test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ async function main() {
3636
fs.copyFileSync('chain/ethereum.ts', 'test/temp_lib/chain/ethereum.ts');
3737
fs.copyFileSync('chain/near.ts', 'test/temp_lib/chain/near.ts');
3838
fs.copyFileSync('chain/cosmos.ts', 'test/temp_lib/chain/cosmos.ts');
39+
fs.copyFileSync('chain/starknet.ts', 'test/temp_lib/chain/starknet.ts');
3940
fs.copyFileSync('index.ts', 'test/temp_lib/index.ts');
4041

4142
try {
@@ -70,6 +71,7 @@ async function main() {
7071
fs.unlinkSync('test/temp_lib/chain/ethereum.ts');
7172
fs.unlinkSync('test/temp_lib/chain/near.ts');
7273
fs.unlinkSync('test/temp_lib/chain/cosmos.ts');
74+
fs.unlinkSync('test/temp_lib/chain/starknet.ts');
7375
fs.rmdirSync('test/temp_lib/chain');
7476
fs.unlinkSync('test/temp_lib/index.ts');
7577
fs.rmdirSync('test/temp_lib');

0 commit comments

Comments
 (0)