create-ponder 0.6.24 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +11 -17
  2. package/dist/index.js +1 -1
  3. package/package.json +1 -1
  4. package/templates/empty/ponder.schema.ts +4 -6
  5. package/templates/etherscan/ponder.schema.ts +4 -6
  6. package/templates/feature-api-functions/package.json +2 -0
  7. package/templates/feature-api-functions/ponder-env.d.ts +2 -3
  8. package/templates/feature-api-functions/ponder.schema.ts +35 -44
  9. package/templates/feature-api-functions/src/api/index.ts +10 -15
  10. package/templates/feature-api-functions/src/index.ts +46 -53
  11. package/templates/feature-blocks/ponder-env.d.ts +2 -3
  12. package/templates/feature-blocks/ponder.config.ts +1 -1
  13. package/templates/feature-blocks/ponder.schema.ts +4 -6
  14. package/templates/feature-blocks/src/index.ts +4 -5
  15. package/templates/feature-call-traces/ponder-env.d.ts +2 -3
  16. package/templates/feature-call-traces/ponder.schema.ts +7 -9
  17. package/templates/feature-call-traces/src/index.ts +12 -11
  18. package/templates/feature-factory/ponder-env.d.ts +2 -3
  19. package/templates/feature-factory/ponder.schema.ts +3 -5
  20. package/templates/feature-filter/ponder-env.d.ts +2 -3
  21. package/templates/feature-filter/ponder.schema.ts +5 -7
  22. package/templates/feature-filter/src/index.ts +4 -7
  23. package/templates/feature-multichain/ponder.schema.ts +4 -6
  24. package/templates/feature-multichain/src/index.ts +5 -11
  25. package/templates/feature-proxy/ponder-env.d.ts +2 -3
  26. package/templates/feature-proxy/ponder.schema.ts +12 -10
  27. package/templates/feature-proxy/src/index.ts +7 -16
  28. package/templates/feature-read-contract/ponder-env.d.ts +2 -3
  29. package/templates/feature-read-contract/ponder.schema.ts +9 -12
  30. package/templates/feature-read-contract/src/FileStore.ts +16 -20
  31. package/templates/project-friendtech/ponder-env.d.ts +2 -3
  32. package/templates/project-friendtech/ponder.schema.ts +43 -55
  33. package/templates/project-friendtech/src/FriendtechSharesV1.ts +56 -70
  34. package/templates/project-uniswap-v3-flash/ponder-env.d.ts +2 -3
  35. package/templates/project-uniswap-v3-flash/ponder.schema.ts +9 -10
  36. package/templates/project-uniswap-v3-flash/src/index.ts +25 -34
  37. package/templates/reference-erc1155/ponder-env.d.ts +2 -3
  38. package/templates/reference-erc1155/ponder.schema.ts +19 -23
  39. package/templates/reference-erc1155/src/index.ts +60 -76
  40. package/templates/reference-erc20/package.json +1 -0
  41. package/templates/reference-erc20/ponder-env.d.ts +2 -3
  42. package/templates/reference-erc20/ponder.schema.ts +35 -44
  43. package/templates/reference-erc20/src/index.ts +40 -57
  44. package/templates/reference-erc4626/ponder-env.d.ts +2 -3
  45. package/templates/reference-erc4626/ponder.schema.ts +43 -54
  46. package/templates/reference-erc4626/src/index.ts +41 -65
  47. package/templates/reference-erc721/ponder-env.d.ts +2 -3
  48. package/templates/reference-erc721/ponder.schema.ts +14 -25
  49. package/templates/reference-erc721/src/index.ts +21 -25
  50. package/templates/subgraph/ponder.schema.ts +4 -6
@@ -1,29 +1,25 @@
1
- import { createSchema } from "@ponder/core";
1
+ import { onchainTable, primaryKey } from "@ponder/core";
2
2
 
3
- export default createSchema((p) => ({
4
- Account: p.createTable({
5
- id: p.hex(),
6
- tokens: p.many("TokenBalance.ownerId"),
3
+ export const account = onchainTable("account", (t) => ({
4
+ address: t.hex().primaryKey(),
5
+ }));
7
6
 
8
- transferFromEvents: p.many("TransferEvent.fromId"),
9
- transferToEvents: p.many("TransferEvent.toId"),
7
+ export const tokenBalance = onchainTable(
8
+ "token_balance",
9
+ (t) => ({
10
+ tokenId: t.bigint().notNull(),
11
+ owner: t.hex().notNull(),
12
+ balance: t.bigint().notNull(),
10
13
  }),
11
- TokenBalance: p.createTable({
12
- id: p.string(),
13
- tokenId: p.bigint(),
14
- balance: p.bigint(),
15
-
16
- ownerId: p.hex().references("Account.id"),
17
- owner: p.one("ownerId"),
14
+ (table) => ({
15
+ pk: primaryKey({ columns: [table.owner, table.tokenId] }),
18
16
  }),
19
- TransferEvent: p.createTable({
20
- id: p.string(),
21
- timestamp: p.int(),
22
- fromId: p.hex().references("Account.id"),
23
- toId: p.hex().references("Account.id"),
24
- tokenId: p.bigint(),
17
+ );
25
18
 
26
- from: p.one("fromId"),
27
- to: p.one("toId"),
28
- }),
19
+ export const transferEvent = onchainTable("transfer_event", (t) => ({
20
+ id: t.text().primaryKey(),
21
+ timestamp: t.integer().notNull(),
22
+ from: t.hex().notNull(),
23
+ to: t.hex().notNull(),
24
+ token: t.bigint().notNull(),
29
25
  }));
@@ -1,109 +1,93 @@
1
1
  import { ponder } from "@/generated";
2
+ import * as schema from "../ponder.schema";
2
3
 
3
4
  ponder.on("ERC1155:TransferSingle", async ({ event, context }) => {
4
- const { Account, TokenBalance, TransferEvent } = context.db;
5
-
6
5
  // Create an Account for the sender, or update the balance if it already exists.
7
- await Account.upsert({
8
- id: event.args.from,
9
- });
6
+ await context.db
7
+ .insert(schema.account)
8
+ .values({ address: event.args.from })
9
+ .onConflictDoNothing();
10
10
 
11
- await TokenBalance.upsert({
12
- id: `${event.args.id}-${event.args.from}`,
13
- create: {
11
+ await context.db
12
+ .insert(schema.tokenBalance)
13
+ .values({
14
+ owner: event.args.from,
14
15
  tokenId: event.args.id,
15
- ownerId: event.args.from,
16
16
  balance: -event.args.amount,
17
- },
18
- update: ({ current }) => ({
19
- tokenId: event.args.id,
20
- ownerId: event.args.from,
21
- balance: current.balance - event.args.amount,
22
- }),
23
- });
17
+ })
18
+ .onConflictDoUpdate((row) => ({
19
+ balance: row.balance - event.args.amount,
20
+ }));
24
21
 
25
22
  // Create an Account for the recipient, or update the balance if it already exists.
26
- await Account.upsert({
27
- id: event.args.to,
28
- });
23
+ await context.db
24
+ .insert(schema.account)
25
+ .values({ address: event.args.to })
26
+ .onConflictDoNothing();
29
27
 
30
- await TokenBalance.upsert({
31
- id: `${event.args.id}-${event.args.to}`,
32
- create: {
28
+ await context.db
29
+ .insert(schema.tokenBalance)
30
+ .values({
31
+ owner: event.args.to,
33
32
  tokenId: event.args.id,
34
- ownerId: event.args.to,
35
33
  balance: event.args.amount,
36
- },
37
- update: ({ current }) => ({
38
- tokenId: event.args.id,
39
- ownerId: event.args.to,
40
- balance: current.balance + event.args.amount,
41
- }),
42
- });
34
+ })
35
+ .onConflictDoUpdate((row) => ({
36
+ balance: row.balance + event.args.amount,
37
+ }));
43
38
 
44
39
  // Create a TransferEvent.
45
- await TransferEvent.create({
40
+ await context.db.insert(schema.transferEvent).values({
46
41
  id: event.log.id,
47
- data: {
48
- fromId: event.args.from,
49
- toId: event.args.to,
50
- tokenId: event.args.id,
51
- timestamp: Number(event.block.timestamp),
52
- },
42
+ from: event.args.from,
43
+ to: event.args.to,
44
+ token: event.args.id,
45
+ timestamp: Number(event.block.timestamp),
53
46
  });
54
47
  });
55
48
 
56
49
  ponder.on("ERC1155:TransferBatch", async ({ event, context }) => {
57
- const { Account, TokenBalance, TransferEvent } = context.db;
58
-
59
- await Account.upsert({
60
- id: event.args.from,
61
- });
62
-
63
- await Account.upsert({
64
- id: event.args.to,
65
- });
50
+ await context.db
51
+ .insert(schema.account)
52
+ .values({ address: event.args.from })
53
+ .onConflictDoNothing();
54
+ await context.db
55
+ .insert(schema.account)
56
+ .values({ address: event.args.to })
57
+ .onConflictDoNothing();
66
58
 
67
59
  for (let i = 0; i < event.args.ids.length; i++) {
68
60
  const id = event.args.ids[i]!;
69
61
  const amount = event.args.amounts[i]!;
70
62
 
71
- await TokenBalance.upsert({
72
- id: `${id}-${event.args.from}`,
73
- create: {
63
+ await context.db
64
+ .insert(schema.tokenBalance)
65
+ .values({
66
+ owner: event.args.from,
74
67
  tokenId: id,
75
- ownerId: event.args.from,
76
68
  balance: -amount,
77
- },
78
- update: ({ current }) => ({
79
- tokenId: id,
80
- ownerId: event.args.from,
81
- balance: current.balance - amount,
82
- }),
83
- });
69
+ })
70
+ .onConflictDoUpdate((row) => ({
71
+ balance: row.balance - amount,
72
+ }));
84
73
 
85
- await TokenBalance.upsert({
86
- id: `${id}-${event.args.to}`,
87
- create: {
74
+ await context.db
75
+ .insert(schema.tokenBalance)
76
+ .values({
77
+ owner: event.args.to,
88
78
  tokenId: id,
89
- ownerId: event.args.to,
90
79
  balance: amount,
91
- },
92
- update: ({ current }) => ({
93
- tokenId: id,
94
- ownerId: event.args.to,
95
- balance: current.balance + amount,
96
- }),
97
- });
80
+ })
81
+ .onConflictDoUpdate((row) => ({
82
+ balance: row.balance + amount,
83
+ }));
98
84
 
99
- await TransferEvent.create({
100
- id: `${event.log.id}-${i}`,
101
- data: {
102
- fromId: event.args.from,
103
- toId: event.args.to,
104
- tokenId: id,
105
- timestamp: Number(event.block.timestamp),
106
- },
85
+ await context.db.insert(schema.transferEvent).values({
86
+ id: event.log.id,
87
+ from: event.args.from,
88
+ to: event.args.to,
89
+ token: id,
90
+ timestamp: Number(event.block.timestamp),
107
91
  });
108
92
  }
109
93
  });
@@ -12,6 +12,7 @@
12
12
  },
13
13
  "dependencies": {
14
14
  "@ponder/core": "workspace:*",
15
+ "drizzle-kit": "0.22.8",
15
16
  "hono": "^4.5.0",
16
17
  "viem": "^2.21.3"
17
18
  },
@@ -7,7 +7,7 @@ declare module "@/generated" {
7
7
  import type { Virtual } from "@ponder/core";
8
8
 
9
9
  type config = typeof import("./ponder.config.ts").default;
10
- type schema = typeof import("./ponder.schema.ts").default;
10
+ type schema = typeof import("./ponder.schema.ts");
11
11
 
12
12
  export const ponder: Virtual.Registry<config, schema>;
13
13
 
@@ -21,8 +21,7 @@ declare module "@/generated" {
21
21
  schema,
22
22
  name
23
23
  >;
24
- export type ApiContext = Virtual.Drizzle<schema>;
24
+ export type ApiContext = Virtual.ApiContext<schema>;
25
25
  export type IndexingFunctionArgs<name extends EventNames = EventNames> =
26
26
  Virtual.IndexingFunctionArgs<config, schema, name>;
27
- export type Schema = Virtual.Schema<schema>;
28
27
  }
@@ -1,50 +1,41 @@
1
- import { createSchema } from "@ponder/core";
1
+ import { index, onchainTable, primaryKey } from "@ponder/core";
2
2
 
3
- export default createSchema((p) => ({
4
- Account: p.createTable({
5
- id: p.hex(),
6
- balance: p.bigint(),
7
- isOwner: p.boolean(),
3
+ export const account = onchainTable("account", (t) => ({
4
+ address: t.hex().primaryKey(),
5
+ balance: t.bigint().notNull(),
6
+ isOwner: t.boolean().notNull(),
7
+ }));
8
8
 
9
- allowances: p.many("Allowance.ownerId"),
10
- approvalOwnerEvents: p.many("ApprovalEvent.ownerId"),
11
- approvalSpenderEvents: p.many("ApprovalEvent.spenderId"),
12
- transferFromEvents: p.many("TransferEvent.fromId"),
13
- transferToEvents: p.many("TransferEvent.toId"),
9
+ export const allowance = onchainTable(
10
+ "allowance",
11
+ (t) => ({
12
+ owner: t.hex(),
13
+ spender: t.hex(),
14
+ amount: t.bigint().notNull(),
14
15
  }),
15
- Allowance: p.createTable({
16
- id: p.string(),
17
- amount: p.bigint(),
18
-
19
- ownerId: p.hex().references("Account.id"),
20
- spenderId: p.hex().references("Account.id"),
21
-
22
- owner: p.one("ownerId"),
23
- spender: p.one("spenderId"),
16
+ (table) => ({
17
+ pk: primaryKey({ columns: [table.owner, table.spender] }),
24
18
  }),
25
- TransferEvent: p.createTable(
26
- {
27
- id: p.string(),
28
- amount: p.bigint(),
29
- timestamp: p.int(),
30
-
31
- fromId: p.hex().references("Account.id"),
32
- toId: p.hex().references("Account.id"),
33
-
34
- from: p.one("fromId"),
35
- to: p.one("toId"),
36
- },
37
- { fromIdIndex: p.index("fromId") },
38
- ),
39
- ApprovalEvent: p.createTable({
40
- id: p.string(),
41
- amount: p.bigint(),
42
- timestamp: p.int(),
43
-
44
- ownerId: p.hex().references("Account.id"),
45
- spenderId: p.hex().references("Account.id"),
46
-
47
- owner: p.one("ownerId"),
48
- spender: p.one("spenderId"),
19
+ );
20
+
21
+ export const transferEvent = onchainTable(
22
+ "transfer_event",
23
+ (t) => ({
24
+ id: t.text().primaryKey(),
25
+ amount: t.bigint().notNull(),
26
+ timestamp: t.integer().notNull(),
27
+ from: t.hex().notNull(),
28
+ to: t.hex().notNull(),
29
+ }),
30
+ (table) => ({
31
+ fromIdx: index("from_index").on(table.from),
49
32
  }),
33
+ );
34
+
35
+ export const approvalEvent = onchainTable("approval_event", (t) => ({
36
+ id: t.text().primaryKey(),
37
+ amount: t.bigint().notNull(),
38
+ timestamp: t.integer().notNull(),
39
+ owner: t.hex().notNull(),
40
+ spender: t.hex().notNull(),
50
41
  }));
@@ -1,70 +1,53 @@
1
1
  import { ponder } from "@/generated";
2
+ import {
3
+ account,
4
+ allowance,
5
+ approvalEvent,
6
+ transferEvent,
7
+ } from "../ponder.schema";
2
8
 
3
9
  ponder.on("ERC20:Transfer", async ({ event, context }) => {
4
- const { Account, TransferEvent } = context.db;
5
-
6
- // Create an Account for the sender, or update the balance if it already exists.
7
- await Account.upsert({
8
- id: event.args.from,
9
- create: {
10
- balance: BigInt(0),
11
- isOwner: false,
12
- },
13
- update: ({ current }) => ({
14
- balance: current.balance - event.args.amount,
15
- }),
16
- });
17
-
18
- // Create an Account for the recipient, or update the balance if it already exists.
19
- await Account.upsert({
20
- id: event.args.to,
21
- create: {
22
- balance: event.args.amount,
23
- isOwner: false,
24
- },
25
- update: ({ current }) => ({
26
- balance: current.balance + event.args.amount,
27
- }),
28
- });
29
-
30
- // Create a TransferEvent.
31
- await TransferEvent.create({
10
+ await context.db
11
+ .insert(account)
12
+ .values({ address: event.args.from, balance: 0n, isOwner: false })
13
+ .onConflictDoUpdate((row) => ({
14
+ balance: row.balance - event.args.amount,
15
+ }));
16
+
17
+ await context.db
18
+ .insert(account)
19
+ .values({ address: event.args.to, balance: 0n, isOwner: false })
20
+ .onConflictDoUpdate((row) => ({
21
+ balance: row.balance + event.args.amount,
22
+ }));
23
+
24
+ // add row to "transfer_event".
25
+ await context.db.insert(transferEvent).values({
32
26
  id: event.log.id,
33
- data: {
34
- fromId: event.args.from,
35
- toId: event.args.to,
36
- amount: event.args.amount,
37
- timestamp: Number(event.block.timestamp),
38
- },
27
+ amount: event.args.amount,
28
+ timestamp: Number(event.block.timestamp),
29
+ from: event.args.from,
30
+ to: event.args.to,
39
31
  });
40
32
  });
41
33
 
42
34
  ponder.on("ERC20:Approval", async ({ event, context }) => {
43
- const { Allowance, ApprovalEvent } = context.db;
44
-
45
- const allowanceId = `${event.args.owner}-${event.args.spender}`;
46
-
47
- // Create or update the Allowance.
48
- await Allowance.upsert({
49
- id: allowanceId,
50
- create: {
51
- ownerId: event.args.owner,
52
- spenderId: event.args.spender,
53
- amount: event.args.amount,
54
- },
55
- update: {
35
+ // upsert "allowance".
36
+ await context.db
37
+ .insert(allowance)
38
+ .values({
39
+ spender: event.args.spender,
40
+ owner: event.args.owner,
56
41
  amount: event.args.amount,
57
- },
58
- });
42
+ })
43
+ .onConflictDoUpdate({ amount: event.args.amount });
59
44
 
60
- // Create an ApprovalEvent.
61
- await ApprovalEvent.create({
45
+ // add row to "approval_event".
46
+ await context.db.insert(approvalEvent).values({
62
47
  id: event.log.id,
63
- data: {
64
- ownerId: event.args.owner,
65
- spenderId: event.args.spender,
66
- amount: event.args.amount,
67
- timestamp: Number(event.block.timestamp),
68
- },
48
+ amount: event.args.amount,
49
+ timestamp: Number(event.block.timestamp),
50
+ owner: event.args.owner,
51
+ spender: event.args.spender,
69
52
  });
70
53
  });
@@ -7,7 +7,7 @@ declare module "@/generated" {
7
7
  import type { Virtual } from "@ponder/core";
8
8
 
9
9
  type config = typeof import("./ponder.config.ts").default;
10
- type schema = typeof import("./ponder.schema.ts").default;
10
+ type schema = typeof import("./ponder.schema.ts");
11
11
 
12
12
  export const ponder: Virtual.Registry<config, schema>;
13
13
 
@@ -21,8 +21,7 @@ declare module "@/generated" {
21
21
  schema,
22
22
  name
23
23
  >;
24
- export type ApiContext = Virtual.Drizzle<schema>;
24
+ export type ApiContext = Virtual.ApiContext<schema>;
25
25
  export type IndexingFunctionArgs<name extends EventNames = EventNames> =
26
26
  Virtual.IndexingFunctionArgs<config, schema, name>;
27
- export type Schema = Virtual.Schema<schema>;
28
27
  }
@@ -1,62 +1,51 @@
1
- import { createSchema } from "@ponder/core";
1
+ import { onchainTable, primaryKey } from "@ponder/core";
2
2
 
3
- export default createSchema((p) => ({
4
- Account: p.createTable({
5
- id: p.hex(),
6
- balance: p.bigint(),
7
- isOwner: p.boolean(),
3
+ export const account = onchainTable("account", (t) => ({
4
+ address: t.hex().primaryKey(),
5
+ balance: t.bigint().notNull(),
6
+ }));
8
7
 
9
- allowances: p.many("Allowance.ownerId"),
10
- approvalOwnerEvents: p.many("ApprovalEvent.ownerId"),
11
- approvalSpenderEvents: p.many("ApprovalEvent.spenderId"),
12
- transferFromEvents: p.many("TransferEvent.fromId"),
13
- transferToEvents: p.many("TransferEvent.toId"),
8
+ export const allowance = onchainTable(
9
+ "allowance",
10
+ (t) => ({
11
+ owner: t.hex(),
12
+ spender: t.hex(),
13
+ amount: t.bigint().notNull(),
14
14
  }),
15
- Allowance: p.createTable({
16
- id: p.string(),
17
- amount: p.bigint(),
18
-
19
- ownerId: p.hex().references("Account.id"),
20
- spenderId: p.hex().references("Account.id"),
21
-
22
- owner: p.one("ownerId"),
23
- spender: p.one("spenderId"),
15
+ (table) => ({
16
+ pk: primaryKey({ columns: [table.owner, table.spender] }),
24
17
  }),
25
- TransferEvent: p.createTable({
26
- id: p.string(),
27
- amount: p.bigint(),
28
- timestamp: p.int(),
29
-
30
- fromId: p.hex().references("Account.id"),
31
- toId: p.hex().references("Account.id"),
18
+ );
19
+
20
+ export const transferEvent = onchainTable("transfer_event", (t) => ({
21
+ id: t.text().primaryKey(),
22
+ amount: t.bigint().notNull(),
23
+ timestamp: t.integer().notNull(),
24
+ from: t.hex().notNull(),
25
+ to: t.hex().notNull(),
26
+ }));
32
27
 
33
- from: p.one("fromId"),
34
- to: p.one("toId"),
35
- }),
36
- ApprovalEvent: p.createTable({
37
- id: p.string(),
38
- amount: p.bigint(),
39
- timestamp: p.int(),
28
+ export const approvalEvent = onchainTable("approval_event", (t) => ({
29
+ id: t.text().primaryKey(),
30
+ amount: t.bigint().notNull(),
31
+ timestamp: t.integer().notNull(),
32
+ owner: t.hex().notNull(),
33
+ spender: t.hex().notNull(),
34
+ }));
40
35
 
41
- ownerId: p.hex().references("Account.id"),
42
- spenderId: p.hex().references("Account.id"),
36
+ export const depositEvent = onchainTable("deposit_event", (t) => ({
37
+ id: t.text().primaryKey(),
38
+ sender: t.hex().notNull(),
39
+ receiver: t.hex().notNull(),
40
+ assets: t.bigint().notNull(),
41
+ shares: t.bigint().notNull(),
42
+ }));
43
43
 
44
- owner: p.one("ownerId"),
45
- spender: p.one("spenderId"),
46
- }),
47
- DepositEvent: p.createTable({
48
- id: p.string(),
49
- sender: p.hex().references("Account.id"),
50
- receiver: p.hex().references("Account.id"),
51
- assets: p.bigint(),
52
- shares: p.bigint(),
53
- }),
54
- WithdrawEvent: p.createTable({
55
- id: p.string(),
56
- sender: p.hex().references("Account.id"),
57
- receiver: p.hex().references("Account.id"),
58
- owner: p.hex().references("Account.id"),
59
- assets: p.bigint(),
60
- shares: p.bigint(),
61
- }),
44
+ export const withdrawalEvent = onchainTable("withdrawal_event", (t) => ({
45
+ id: t.text().primaryKey(),
46
+ sender: t.hex().notNull(),
47
+ receiver: t.hex().notNull(),
48
+ owner: t.hex().notNull(),
49
+ assets: t.bigint().notNull(),
50
+ shares: t.bigint().notNull(),
62
51
  }));