|
| 1 | +import type { Network } from "../types/network"; |
| 2 | +import { ExampleFactory } from "./factory"; |
| 3 | + |
| 4 | +export class CoretimeExample extends ExampleFactory { |
| 5 | + constructor() { |
| 6 | + super({ |
| 7 | + id: "coretime-operations", |
| 8 | + name: "Coretime Chain Operations", |
| 9 | + description: "Demonstrate Coretime operations: core buying, renewal, and asset management", |
| 10 | + level: "advanced", |
| 11 | + categories: ["coretime", "parachains", "assets", "agile-coretime"], |
| 12 | + }); |
| 13 | + } |
| 14 | + |
| 15 | + generateCode(network: Network): string { |
| 16 | + return `// Coretime Chain Operations Example on ${network.name} |
| 17 | +${this.getImports(network, true)} |
| 18 | +
|
| 19 | +// Connect to ${network.name} Coretime Chain |
| 20 | +const client = createClient( |
| 21 | + withPolkadotSdkCompat( |
| 22 | + getWsProvider("${network.endpoint}") |
| 23 | + ) |
| 24 | +); |
| 25 | +
|
| 26 | +// Get the typed API using the descriptors |
| 27 | +const typedApi = client.getTypedApi(${network.descriptorKey}); |
| 28 | +
|
| 29 | +// Coretime operations demonstration |
| 30 | +const demonstrateCoretimeOperations = async () => { |
| 31 | + try { |
| 32 | + console.log("⏰ Starting Coretime operations demonstration..."); |
| 33 | +
|
| 34 | + // 1. Query current price and configuration |
| 35 | + console.log("\\n💰 Querying Coretime pricing:"); |
| 36 | + try { |
| 37 | + const coretimeConfig = await typedApi.query.Broker.Configuration.getValue(); |
| 38 | + console.log("Coretime configuration:", coretimeConfig); |
| 39 | + } catch (error) { |
| 40 | + console.log("Coretime configuration not available"); |
| 41 | + } |
| 42 | +
|
| 43 | + // 2. Query core sales status |
| 44 | + console.log("\\n🏷️ Querying core sales status:"); |
| 45 | + try { |
| 46 | + const saleInfo = await typedApi.query.Broker.SaleInfo.getValue(); |
| 47 | + console.log("Current sale information:", saleInfo); |
| 48 | + } catch (error) { |
| 49 | + console.log("Sale info not available"); |
| 50 | + } |
| 51 | +
|
| 52 | + // 3. Query available cores |
| 53 | + console.log("\\n🔍 Querying available cores:"); |
| 54 | + try { |
| 55 | + const cores = await typedApi.query.Broker.CoreSchedules.getEntries(); |
| 56 | + console.log("Found", cores.length, "cores with schedules"); |
| 57 | + |
| 58 | + if (cores.length > 0) { |
| 59 | + const firstCore = cores[0]; |
| 60 | + console.log("First core schedule:", { |
| 61 | + coreId: firstCore.key?.args?.[0]?.toString(), |
| 62 | + schedule: firstCore.value |
| 63 | + }); |
| 64 | + } |
| 65 | + } catch (error) { |
| 66 | + console.log("Core schedules not available"); |
| 67 | + } |
| 68 | +
|
| 69 | + // 4. Demonstrate core purchase |
| 70 | + console.log("\\n🛒 Core Purchase Example:"); |
| 71 | + console.log("This would purchase coretime for a specific period:"); |
| 72 | + const purchaseTx = typedApi.tx.Broker.purchase({ |
| 73 | + price_limit: 1000000000000n, // Maximum price willing to pay |
| 74 | + }); |
| 75 | + console.log("Coretime purchase transaction created (not submitted in simulator)"); |
| 76 | +
|
| 77 | + // 5. Demonstrate core renewal |
| 78 | + console.log("\\n🔄 Core Renewal Example:"); |
| 79 | + console.log("This would renew an existing core:"); |
| 80 | + const renewTx = typedApi.tx.Broker.renew({ |
| 81 | + core: 0, // Core ID to renew |
| 82 | + price_limit: 500000000000n, // Maximum price for renewal |
| 83 | + }); |
| 84 | + console.log("Core renewal transaction created for core 0 (not submitted in simulator)"); |
| 85 | +
|
| 86 | + // 6. Query workload information |
| 87 | + console.log("\\n📊 Querying workload information:"); |
| 88 | + try { |
| 89 | + const workload = await typedApi.query.Broker.Workload.getEntries(); |
| 90 | + console.log("Found workload data for", workload.length, "cores"); |
| 91 | + } catch (error) { |
| 92 | + console.log("Workload data not available"); |
| 93 | + } |
| 94 | +
|
| 95 | + // 7. Query reservations |
| 96 | + console.log("\\n📅 Querying reservations:"); |
| 97 | + try { |
| 98 | + const reservations = await typedApi.query.Broker.Reservations.getValue(); |
| 99 | + console.log("Current reservations:", reservations); |
| 100 | + } catch (error) { |
| 101 | + console.log("Reservations not available"); |
| 102 | + } |
| 103 | +
|
| 104 | + // 8. Demonstrate partition core operation |
| 105 | + console.log("\\n✂️ Core Partition Example:"); |
| 106 | + console.log("This would partition a core into smaller time slices:"); |
| 107 | + const partitionTx = typedApi.tx.Broker.partition({ |
| 108 | + core: 0, // Core to partition |
| 109 | + price_limit: 200000000000n, // Price limit for partition |
| 110 | + }); |
| 111 | + console.log("Core partition transaction created (not submitted in simulator)"); |
| 112 | +
|
| 113 | + // 9. Demonstrate core assignment |
| 114 | + console.log("\\n🎯 Core Assignment Example:"); |
| 115 | + console.log("This would assign a core to a parachain:"); |
| 116 | + const assignTx = typedApi.tx.Broker.assign({ |
| 117 | + core: 0, // Core to assign |
| 118 | + begin: 1000, // Starting block |
| 119 | + assignment: [{ |
| 120 | + task: 2000, // Parachain ID |
| 121 | + ratio: [80, 20] // 80% to parachain, 20% to pool |
| 122 | + }], |
| 123 | + end_hint: null |
| 124 | + }); |
| 125 | + console.log("Core assignment transaction created (not submitted in simulator)"); |
| 126 | +
|
| 127 | + // 10. Query core status |
| 128 | + console.log("\\n📋 Querying core status:"); |
| 129 | + try { |
| 130 | + const status = await typedApi.query.Broker.StatusOf.getValue(0); |
| 131 | + console.log("Status of core 0:", status); |
| 132 | + } catch (error) { |
| 133 | + console.log("Core status not available"); |
| 134 | + } |
| 135 | +
|
| 136 | + // 11. Query allowed renewal records |
| 137 | + console.log("\\n🔑 Querying allowed renewal records:"); |
| 138 | + try { |
| 139 | + const aliceAddress = "${this.getTestAccount("alice")}"; |
| 140 | + const allowedRenewal = await typedApi.query.Broker.AllowedRenewalRecords.getValue(aliceAddress); |
| 141 | + console.log("Alice's allowed renewal records:", allowedRenewal); |
| 142 | + } catch (error) { |
| 143 | + console.log("Allowed renewal records not available"); |
| 144 | + } |
| 145 | +
|
| 146 | + // 12. Demonstrate pool operations |
| 147 | + console.log("\\n🏊 Pool Operations Example:"); |
| 148 | + console.log("This would add coretime to the instant pool:"); |
| 149 | + const poolTx = typedApi.tx.Broker.pool({ |
| 150 | + core: 0, // Core to add to pool |
| 151 | + price_limit: 100000000000n, // Price limit |
| 152 | + }); |
| 153 | + console.log("Pool transaction created (not submitted in simulator)"); |
| 154 | +
|
| 155 | + // 13. Query instant pool configuration |
| 156 | + console.log("\\n⚡ Querying instant pool:"); |
| 157 | + try { |
| 158 | + const poolInfo = await typedApi.query.Broker.InstantaneousPoolInfo.getValue(); |
| 159 | + console.log("Instant pool information:", poolInfo); |
| 160 | + } catch (error) { |
| 161 | + console.log("Instant pool info not available"); |
| 162 | + } |
| 163 | +
|
| 164 | + console.log("\\n✅ Coretime operations demonstration completed!"); |
| 165 | + console.log("Note: Coretime operations require:"); |
| 166 | + console.log("- Understanding of core scheduling and availability"); |
| 167 | + console.log("- Proper price limits for purchases and renewals"); |
| 168 | + console.log("- Knowledge of parachain assignments and workload distribution"); |
| 169 | + console.log("- In a real application, you would sign and submit these transactions"); |
| 170 | + console.log("- Coretime is a complex system - study the documentation thoroughly"); |
| 171 | +
|
| 172 | + } catch (error) { |
| 173 | + console.error("❌ Error in Coretime operations:", error); |
| 174 | + } |
| 175 | +}; |
| 176 | +
|
| 177 | +demonstrateCoretimeOperations().catch(console.error); |
| 178 | +`; |
| 179 | + } |
| 180 | +} |
0 commit comments