create-0g-app 1.0.7 → 1.0.8
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.
package/README.md
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
# create-0g-app
|
|
2
|
+
|
|
3
|
+
The fastest way to build on [0G](https://0g.ai). One command scaffolds a full-stack app pre-wired with working code for 0G Storage, Compute (AI inference), smart contracts, and INFTs.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npx create-0g-app@latest
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## What gets generated
|
|
12
|
+
|
|
13
|
+
A monorepo with two workspaces:
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
my-app/
|
|
17
|
+
├── packages/
|
|
18
|
+
│ ├── web/ # Next.js 15 frontend
|
|
19
|
+
│ │ ├── app/
|
|
20
|
+
│ │ │ ├── page.tsx # Main page (edit this)
|
|
21
|
+
│ │ │ ├── layout.tsx
|
|
22
|
+
│ │ │ └── api/ # Server-side API routes (storage, compute, INFT)
|
|
23
|
+
│ │ ├── components/ # Feature UI components
|
|
24
|
+
│ │ ├── lib/ # Client-side SDK helpers
|
|
25
|
+
│ │ └── .env.local # Your private keys + config (never committed)
|
|
26
|
+
│ └── contracts/ # Hardhat project (if contracts or INFT selected)
|
|
27
|
+
│ ├── contracts/ # Solidity source files
|
|
28
|
+
│ └── scripts/ # Deploy scripts
|
|
29
|
+
├── scripts/
|
|
30
|
+
│ └── patch-0g-sdk.js # Patches @0glabs/0g-ts-sdk ABI for current testnet
|
|
31
|
+
└── package.json # Root workspace scripts
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Quick Start
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npx create-0g-app@latest my-app
|
|
40
|
+
cd my-app
|
|
41
|
+
|
|
42
|
+
# Add your private key to packages/web/.env.local
|
|
43
|
+
# Get testnet OG tokens at https://faucet.0g.ai
|
|
44
|
+
|
|
45
|
+
npm run dev # Start dev server at localhost:3000
|
|
46
|
+
npm run deploy # Deploy contracts to 0G Galileo testnet
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## Features
|
|
52
|
+
|
|
53
|
+
### Contracts (Hardhat + 0G Galileo)
|
|
54
|
+
|
|
55
|
+
Scaffolds a Hardhat project targeting the 0G Galileo testnet (chain ID `16602`).
|
|
56
|
+
|
|
57
|
+
**Key files:**
|
|
58
|
+
- `packages/contracts/contracts/MyContract.sol` — your Solidity contract
|
|
59
|
+
- `packages/contracts/scripts/deploy.ts` — deploys and writes address to `.env.local`
|
|
60
|
+
- `packages/contracts/hardhat.config.ts` — pre-configured for 0G Galileo
|
|
61
|
+
|
|
62
|
+
**Commands:**
|
|
63
|
+
```bash
|
|
64
|
+
npm run deploy # compile + deploy to Galileo testnet
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
**RPC:** `https://evmrpc-testnet.0g.ai`
|
|
68
|
+
|
|
69
|
+
**Explorer:** `https://chainscan-galileo.0g.ai`
|
|
70
|
+
|
|
71
|
+
**Docs:** https://docs.0g.ai/developer-hub/building-on-0g/contracts
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
### Storage (`@0glabs/0g-ts-sdk`)
|
|
76
|
+
|
|
77
|
+
Uploads and retrieves files from 0G decentralized storage. Files are split into chunks, Merkle-tree verified, and dispersed across storage nodes. The Merkle root (`rootHash`) is recorded on-chain as a permanent content address.
|
|
78
|
+
|
|
79
|
+
**Key files:**
|
|
80
|
+
- `packages/web/app/api/upload/route.ts` — server-side upload handler
|
|
81
|
+
- `packages/web/app/api/download/[rootHash]/route.ts` — server-side download handler
|
|
82
|
+
- `packages/web/lib/0g-storage.ts` — client helpers (`uploadFile`, `downloadFile`)
|
|
83
|
+
- `packages/web/components/StorageSection.tsx` — upload/download UI
|
|
84
|
+
|
|
85
|
+
**Key SDK calls:**
|
|
86
|
+
```ts
|
|
87
|
+
import { ZgFile, Indexer } from "@0glabs/0g-ts-sdk";
|
|
88
|
+
|
|
89
|
+
const zgFile = await ZgFile.fromFilePath(tmpPath);
|
|
90
|
+
const [tree, err] = await zgFile.merkleTree(); // build Merkle tree
|
|
91
|
+
const [tx, uploadErr] = await indexer.upload(zgFile, RPC_URL, signer as any);
|
|
92
|
+
// tx.rootHash = permanent content address
|
|
93
|
+
// tx.txHash = on-chain submission transaction
|
|
94
|
+
|
|
95
|
+
const dlErr = await indexer.download(rootHash, outPath, false); // retrieve by root hash
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
> **Note:** `scripts/patch-0g-sdk.js` patches the SDK's ABI to match the current testnet `FixedPriceFlow` contract. It runs automatically on `npm install` via `postinstall`.
|
|
99
|
+
|
|
100
|
+
**Docs:** https://docs.0g.ai/developer-hub/building-on-0g/storage/sdk
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
### Compute (`@0glabs/0g-serving-broker`)
|
|
105
|
+
|
|
106
|
+
Routes AI inference through the 0G compute network. Every query is cryptographically verified and paid for on-chain using OG tokens via a ledger/escrow system. The API is OpenAI-compatible.
|
|
107
|
+
|
|
108
|
+
**Key files:**
|
|
109
|
+
- `packages/web/app/api/compute/broker.ts` — broker singleton, ledger management
|
|
110
|
+
- `packages/web/app/api/compute/route.ts` — inference endpoint
|
|
111
|
+
- `packages/web/app/api/compute/setup/route.ts` — one-time provider setup
|
|
112
|
+
- `packages/web/components/ComputeSection.tsx` — chat UI
|
|
113
|
+
|
|
114
|
+
**Payment flow:**
|
|
115
|
+
1. `broker.ledger.addLedger(3)` — deposit 3 OG into on-chain escrow (one-time per wallet)
|
|
116
|
+
2. `broker.inference.acknowledgeProviderSigner(providerAddress)` — whitelist the provider
|
|
117
|
+
3. `broker.ledger.transferFund(providerAddress, "inference", 1_OG)` — fund provider sub-account
|
|
118
|
+
4. `broker.inference.getRequestHeaders(providerAddress, query)` — sign each request off-chain
|
|
119
|
+
5. HTTP call to provider's OpenAI-compatible endpoint
|
|
120
|
+
6. `broker.inference.processResponse(providerAddress, chatId, content)` — settle micropayment
|
|
121
|
+
|
|
122
|
+
**Key SDK calls:**
|
|
123
|
+
```ts
|
|
124
|
+
import { createZGComputeNetworkBroker } from "@0glabs/0g-serving-broker";
|
|
125
|
+
|
|
126
|
+
const broker = await createZGComputeNetworkBroker(signer);
|
|
127
|
+
const { endpoint, model } = await broker.inference.getServiceMetadata(providerAddress);
|
|
128
|
+
const headers = await broker.inference.getRequestHeaders(providerAddress, query);
|
|
129
|
+
// use with openai client: new OpenAI({ baseURL: endpoint }).chat.completions.create(...)
|
|
130
|
+
await broker.inference.processResponse(providerAddress, chatId, content);
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
**Env vars required:**
|
|
134
|
+
```
|
|
135
|
+
PRIVATE_KEY= # pays for inference (server-side only)
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
> **Important:** Run provider setup once per wallet. Each setup call transfers 1 OG to the provider sub-account.
|
|
139
|
+
|
|
140
|
+
**Docs:** https://docs.0g.ai/developer-hub/building-on-0g/compute-network/inference
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
### INFT — Intelligent NFTs (`@0glabs/0g-ts-sdk` + ERC-721)
|
|
145
|
+
|
|
146
|
+
Mints ERC-721 tokens whose metadata lives on 0G decentralized storage. The `storageRoot` (Merkle root of the metadata file) and `metadataHash` (keccak256 of the JSON) are recorded on-chain, making metadata verifiable and tamper-proof.
|
|
147
|
+
|
|
148
|
+
**Key files:**
|
|
149
|
+
- `packages/contracts/contracts/INFT.sol` — ERC-721 contract with `mint(to, storageRoot, metadataHash)`
|
|
150
|
+
- `packages/contracts/scripts/deployINFT.ts` — deploys and writes address to `.env.local`
|
|
151
|
+
- `packages/web/app/api/inft/mint/route.ts` — uploads metadata to 0G Storage then mints on-chain
|
|
152
|
+
- `packages/web/app/api/inft/token/route.ts` — fetches token owner + metadata from chain + storage
|
|
153
|
+
- `packages/web/lib/0g-inft.ts` — client helpers (`mintINFT`, `getToken`)
|
|
154
|
+
- `packages/web/components/INFTSection.tsx` — mint + lookup UI
|
|
155
|
+
|
|
156
|
+
**Mint flow:**
|
|
157
|
+
```ts
|
|
158
|
+
// 1. Build metadata JSON
|
|
159
|
+
const metadata = { name, description, createdAt: Date.now() };
|
|
160
|
+
|
|
161
|
+
// 2. Upload to 0G Storage → get storageRoot
|
|
162
|
+
const [tx] = await indexer.upload(zgFile, RPC_URL, signer as any);
|
|
163
|
+
const storageRoot = tx.rootHash;
|
|
164
|
+
|
|
165
|
+
// 3. Compute metadata hash for tamper detection
|
|
166
|
+
const metadataHash = ethers.keccak256(ethers.toUtf8Bytes(JSON.stringify(metadata)));
|
|
167
|
+
|
|
168
|
+
// 4. Mint on-chain — records ownership + both hashes permanently
|
|
169
|
+
await contract.mint(recipient, storageRoot, metadataHash);
|
|
170
|
+
|
|
171
|
+
// 5. Parse INFTMinted event for tokenId
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
**Env vars required:**
|
|
175
|
+
```
|
|
176
|
+
PRIVATE_KEY= # deploy + sign storage + pay gas
|
|
177
|
+
NEXT_PUBLIC_INFT_ADDRESS= # auto-populated after `npm run deploy`
|
|
178
|
+
STORAGE_INDEXER_RPC=https://indexer-storage-testnet-turbo.0g.ai
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
**Docs:** https://docs.0g.ai/developer-hub/building-on-0g/inft/integration
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
## Environment Variables
|
|
186
|
+
|
|
187
|
+
All env vars live in `packages/web/.env.local` (copied from `.env.example` on scaffold).
|
|
188
|
+
|
|
189
|
+
| Variable | Required for | Description |
|
|
190
|
+
|---|---|---|
|
|
191
|
+
| `NEXT_PUBLIC_RPC_URL` | All | 0G Galileo EVM RPC |
|
|
192
|
+
| `NEXT_PUBLIC_CHAIN_ID` | All | `16602` (Galileo testnet) |
|
|
193
|
+
| `PRIVATE_KEY` | Contracts, Storage, Compute, INFT | Server-side signing key. Never exposed to browser. |
|
|
194
|
+
| `STORAGE_INDEXER_RPC` | Storage, INFT | 0G Storage indexer endpoint |
|
|
195
|
+
| `NEXT_PUBLIC_CONTRACT_ADDRESS` | Contracts | Auto-written by deploy script |
|
|
196
|
+
| `NEXT_PUBLIC_INFT_ADDRESS` | INFT | Auto-written by deploy script |
|
|
197
|
+
|
|
198
|
+
---
|
|
199
|
+
|
|
200
|
+
## Testnet Resources
|
|
201
|
+
|
|
202
|
+
| Resource | URL |
|
|
203
|
+
|---|---|
|
|
204
|
+
| Faucet (get OG tokens) | https://faucet.0g.ai |
|
|
205
|
+
| Explorer | https://chainscan-galileo.0g.ai |
|
|
206
|
+
| RPC | https://evmrpc-testnet.0g.ai |
|
|
207
|
+
| Storage Indexer | https://indexer-storage-testnet-turbo.0g.ai |
|
|
208
|
+
| 0G Docs | https://docs.0g.ai |
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
## Stack
|
|
213
|
+
|
|
214
|
+
- **Frontend:** Next.js 15, React 19, TypeScript
|
|
215
|
+
- **Wallet:** wagmi 2 + viem (0G Galileo chain pre-configured)
|
|
216
|
+
- **Contracts:** Hardhat, Solidity, ethers v6
|
|
217
|
+
- **Storage:** `@0glabs/0g-ts-sdk`
|
|
218
|
+
- **Compute:** `@0glabs/0g-serving-broker`
|
|
219
|
+
- **INFT:** `@0glabs/0g-ts-sdk` + OpenZeppelin ERC-721
|
package/package.json
CHANGED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2020",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"moduleResolution": "node",
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"strict": true,
|
|
8
|
+
"skipLibCheck": true,
|
|
9
|
+
"resolveJsonModule": true,
|
|
10
|
+
"outDir": "dist"
|
|
11
|
+
},
|
|
12
|
+
"include": ["hardhat.config.ts", "scripts/**/*.ts", "test/**/*.ts"]
|
|
13
|
+
}
|