|
| 1 | +import { |
| 2 | + AppChain, |
| 3 | + BlockStorageNetworkStateModule, |
| 4 | + InMemorySigner, |
| 5 | + InMemoryTransactionSender, |
| 6 | + StateServiceQueryModule, |
| 7 | +} from "@proto-kit/sdk"; |
| 8 | +import { PrivateKey, PublicKey } from "o1js"; |
| 9 | +import { Runtime, runtimeMethod, runtimeModule } from "@proto-kit/module"; |
| 10 | +import { Protocol, State, state } from "@proto-kit/protocol"; |
| 11 | +import { |
| 12 | + Balance, |
| 13 | + Balances, |
| 14 | + BalancesKey, |
| 15 | + TokenId, |
| 16 | + VanillaProtocolModules, |
| 17 | + VanillaRuntimeModules, |
| 18 | + UInt64, |
| 19 | +} from "@proto-kit/library"; |
| 20 | +import { log } from "@proto-kit/common"; |
| 21 | +import { |
| 22 | + BatchProducerModule, |
| 23 | + InMemoryDatabase, |
| 24 | + LocalTaskQueue, |
| 25 | + LocalTaskWorkerModule, |
| 26 | + NoopBaseLayer, |
| 27 | + PrivateMempool, |
| 28 | + Sequencer, |
| 29 | + BlockProducerModule, |
| 30 | + VanillaTaskWorkerModules, |
| 31 | + SequencerStartupModule, |
| 32 | + ManualBlockTrigger, |
| 33 | +} from "@proto-kit/sequencer"; |
| 34 | +import { |
| 35 | + BatchStorageResolver, |
| 36 | + GraphqlSequencerModule, |
| 37 | + GraphqlServer, |
| 38 | + MempoolResolver, |
| 39 | + MerkleWitnessResolver, |
| 40 | + NodeStatusResolver, |
| 41 | + QueryGraphqlModule, |
| 42 | + BlockResolver, |
| 43 | +} from "@proto-kit/api"; |
| 44 | +import { container } from "tsyringe"; |
| 45 | + |
| 46 | +@runtimeModule() |
| 47 | +export class TestBalances extends Balances { |
| 48 | + /** |
| 49 | + * We use `satisfies` here in order to be able to access |
| 50 | + * presets by key in a type safe way. |
| 51 | + */ |
| 52 | + // public static presets = {} satisfies Presets<object>; |
| 53 | + |
| 54 | + @state() public totalSupply = State.from<UInt64>(UInt64); |
| 55 | + |
| 56 | + @runtimeMethod() |
| 57 | + public async getBalanceForUser( |
| 58 | + tokenId: TokenId, |
| 59 | + address: PublicKey |
| 60 | + ): Promise<Balance> { |
| 61 | + return await super.getBalance(tokenId, address); |
| 62 | + } |
| 63 | + |
| 64 | + @runtimeMethod() |
| 65 | + public async addBalance( |
| 66 | + tokenId: TokenId, |
| 67 | + address: PublicKey, |
| 68 | + balance: UInt64 |
| 69 | + ) { |
| 70 | + const totalSupply = await this.totalSupply.get(); |
| 71 | + await this.totalSupply.set(totalSupply.orElse(UInt64.zero).add(balance)); |
| 72 | + |
| 73 | + const previous = await this.balances.get( |
| 74 | + new BalancesKey({ tokenId, address }) |
| 75 | + ); |
| 76 | + await this.balances.set( |
| 77 | + new BalancesKey({ tokenId, address }), |
| 78 | + previous.orElse(UInt64.zero).add(balance) |
| 79 | + ); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +export async function startGraphqlServer() { |
| 84 | + log.setLevel("DEBUG"); |
| 85 | + |
| 86 | + const appChain = AppChain.from({ |
| 87 | + Runtime: Runtime.from({ |
| 88 | + modules: VanillaRuntimeModules.with({ |
| 89 | + Balances: TestBalances, |
| 90 | + }), |
| 91 | + }), |
| 92 | + |
| 93 | + Protocol: Protocol.from({ |
| 94 | + modules: VanillaProtocolModules.with({}), |
| 95 | + }), |
| 96 | + |
| 97 | + Sequencer: Sequencer.from({ |
| 98 | + modules: { |
| 99 | + Database: InMemoryDatabase, |
| 100 | + // Database: PrismaRedisDatabase, |
| 101 | + |
| 102 | + Mempool: PrivateMempool, |
| 103 | + GraphqlServer, |
| 104 | + LocalTaskWorkerModule: LocalTaskWorkerModule.from( |
| 105 | + VanillaTaskWorkerModules.withoutSettlement() |
| 106 | + ), |
| 107 | + |
| 108 | + BaseLayer: NoopBaseLayer, |
| 109 | + BatchProducerModule, |
| 110 | + BlockProducerModule, |
| 111 | + BlockTrigger: ManualBlockTrigger, |
| 112 | + TaskQueue: LocalTaskQueue, |
| 113 | + // SettlementModule: SettlementModule, |
| 114 | + |
| 115 | + Graphql: GraphqlSequencerModule.from({ |
| 116 | + modules: { |
| 117 | + MempoolResolver, |
| 118 | + QueryGraphqlModule, |
| 119 | + BatchStorageResolver, |
| 120 | + BlockResolver, |
| 121 | + NodeStatusResolver, |
| 122 | + MerkleWitnessResolver, |
| 123 | + }, |
| 124 | + |
| 125 | + config: { |
| 126 | + MempoolResolver: {}, |
| 127 | + QueryGraphqlModule: {}, |
| 128 | + BatchStorageResolver: {}, |
| 129 | + NodeStatusResolver: {}, |
| 130 | + MerkleWitnessResolver: {}, |
| 131 | + BlockResolver: {}, |
| 132 | + }, |
| 133 | + }), |
| 134 | + |
| 135 | + SequencerStartupModule, |
| 136 | + }, |
| 137 | + }), |
| 138 | + |
| 139 | + modules: { |
| 140 | + Signer: InMemorySigner, |
| 141 | + TransactionSender: InMemoryTransactionSender, |
| 142 | + QueryTransportModule: StateServiceQueryModule, |
| 143 | + NetworkStateTransportModule: BlockStorageNetworkStateModule, |
| 144 | + }, |
| 145 | + }); |
| 146 | + |
| 147 | + appChain.configure({ |
| 148 | + Runtime: { |
| 149 | + Balances: {}, |
| 150 | + }, |
| 151 | + |
| 152 | + Protocol: { |
| 153 | + BlockProver: {}, |
| 154 | + StateTransitionProver: {}, |
| 155 | + AccountState: {}, |
| 156 | + BlockHeight: {}, |
| 157 | + TransactionFee: { |
| 158 | + tokenId: 0n, |
| 159 | + feeRecipient: PrivateKey.random().toPublicKey().toBase58(), |
| 160 | + baseFee: 0n, |
| 161 | + methods: {}, |
| 162 | + perWeightUnitFee: 0n, |
| 163 | + }, |
| 164 | + LastStateRoot: {}, |
| 165 | + }, |
| 166 | + |
| 167 | + Sequencer: { |
| 168 | + GraphqlServer: { |
| 169 | + port: 8080, |
| 170 | + host: "0.0.0.0", |
| 171 | + graphiql: true, |
| 172 | + }, |
| 173 | + SequencerStartupModule: {}, |
| 174 | + |
| 175 | + // SettlementModule: { |
| 176 | + // address: PrivateKey.random().toPublicKey(), |
| 177 | + // feepayer: PrivateKey.random(), |
| 178 | + // }, |
| 179 | + |
| 180 | + Graphql: { |
| 181 | + QueryGraphqlModule: {}, |
| 182 | + MempoolResolver: {}, |
| 183 | + BatchStorageResolver: {}, |
| 184 | + NodeStatusResolver: {}, |
| 185 | + BlockResolver: {}, |
| 186 | + MerkleWitnessResolver: {}, |
| 187 | + }, |
| 188 | + |
| 189 | + Database: { |
| 190 | + // redis: { |
| 191 | + // host: "localhost", |
| 192 | + // port: 6379, |
| 193 | + // password: "password", |
| 194 | + // }, |
| 195 | + // prisma: { |
| 196 | + // connection: { |
| 197 | + // host: "localhost", |
| 198 | + // password: "password", |
| 199 | + // username: "user", |
| 200 | + // port: 5432, |
| 201 | + // db: { |
| 202 | + // name: "protokit", |
| 203 | + // }, |
| 204 | + // }, |
| 205 | + // }, |
| 206 | + }, |
| 207 | + |
| 208 | + Mempool: {}, |
| 209 | + BatchProducerModule: {}, |
| 210 | + LocalTaskWorkerModule: VanillaTaskWorkerModules.defaultConfig(), |
| 211 | + BaseLayer: {}, |
| 212 | + TaskQueue: {}, |
| 213 | + |
| 214 | + BlockProducerModule: { |
| 215 | + allowEmptyBlock: true, |
| 216 | + }, |
| 217 | + |
| 218 | + BlockTrigger: {}, |
| 219 | + }, |
| 220 | + |
| 221 | + TransactionSender: {}, |
| 222 | + QueryTransportModule: {}, |
| 223 | + NetworkStateTransportModule: {}, |
| 224 | + |
| 225 | + Signer: { |
| 226 | + signer: PrivateKey.random(), |
| 227 | + }, |
| 228 | + }); |
| 229 | + |
| 230 | + await appChain.start(false, container.createChildContainer()); |
| 231 | + // const pk = PublicKey.fromBase58( |
| 232 | + // "B62qmETai5Y8vvrmWSU8F4NX7pTyPqYLMhc1pgX3wD8dGc2wbCWUcqP" |
| 233 | + // ); |
| 234 | + |
| 235 | + const balances = appChain.runtime.resolve("Balances"); |
| 236 | + |
| 237 | + const priv = PrivateKey.fromBase58( |
| 238 | + "EKFEMDTUV2VJwcGmCwNKde3iE1cbu7MHhzBqTmBtGAd6PdsLTifY" |
| 239 | + ); |
| 240 | + |
| 241 | + const tokenId = TokenId.from(0); |
| 242 | + |
| 243 | + const as = await appChain.query.protocol.AccountState.accountState.get( |
| 244 | + priv.toPublicKey() |
| 245 | + ); |
| 246 | + const nonce = Number(as?.nonce.toString() ?? "0"); |
| 247 | + |
| 248 | + const tx = await appChain.transaction( |
| 249 | + priv.toPublicKey(), |
| 250 | + async () => { |
| 251 | + await balances.addBalance(tokenId, priv.toPublicKey(), UInt64.from(1000)); |
| 252 | + }, |
| 253 | + { |
| 254 | + nonce, |
| 255 | + } |
| 256 | + ); |
| 257 | + appChain.resolve("Signer").config.signer = priv; |
| 258 | + await tx.sign(); |
| 259 | + await tx.send(); |
| 260 | + |
| 261 | + const tx2 = await appChain.transaction( |
| 262 | + priv.toPublicKey(), |
| 263 | + async () => { |
| 264 | + await balances.addBalance(tokenId, priv.toPublicKey(), UInt64.from(1000)); |
| 265 | + }, |
| 266 | + { nonce: nonce + 1 } |
| 267 | + ); |
| 268 | + await tx2.sign(); |
| 269 | + await tx2.send(); |
| 270 | + |
| 271 | + return appChain; |
| 272 | +} |
0 commit comments