Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions apps/randomness/.env.example
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
RANDOM_CONTRACT_ADDRESS=0x5FbDB2315678afecb367f032d93F642f64180aa3
PRECOMMIT_DELAY=10
# Seconds before the PRECOMMIT_DELAY when we make the commitment
POST_COMMIT_MARGIN=10
TIME_BLOCK=2
BLOCK_TIME=2
CHAIN_ID=31337
RPC_URL=ws://127.0.0.1:8545
EVM_DRAND_GENESIS_TIMESTAMP_SECONDS=1727521075
EVM_DRAND_PERIOD_SECONDS=3
EVM_DRAND_START_ROUND=
EVM_DRAND_URL=https://api.drand.sh/v2/beacons/evmnet
# The example value is the genesis timestamp of the HappyChain testnet
HAPPY_GENESIS_TIMESTAMP_SECONDS=1723165536
RANDOMNESS_DB_PATH=
RANDOMNESS_DB_PATH=
TXM_DB_PATH=
2 changes: 1 addition & 1 deletion apps/randomness/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
- Updated dependencies [fe5b333]
- @happy.tech/common@0.1.0
- @happy.tech/txm@0.1.0
- @happy.tech/contracts@0.1.0
- @happy.tech/contracts@0.1.0
11 changes: 8 additions & 3 deletions apps/randomness/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ include ../../makefiles/formatting.mk
include ../../makefiles/bundling.mk
include ../../makefiles/help.mk

start: ## Starts the randomness service
node --env-file=.env dist/index.es.js
PHONY: start


migrate: ## Runs pending migrations
node --env-file .env dist/migrate.es.js
.PHONY: migrate
tsx --env-file=.env src/migrate.ts
PHONY: migrate

link-anvil:
rm src/ABI/random.ts
Expand All @@ -17,4 +22,4 @@ link-anvil:
link-happy-sepolia:
rm src/ABI/random.ts
ln -s ../../../../contracts/deployments/happy-sepolia/random/abis.ts src/ABI/random.ts
.PHONY: link-happy-sepolia
.PHONY: link-happy-sepolia
1 change: 0 additions & 1 deletion apps/randomness/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { defineConfig } from "@happy.tech/happybuild"

export default defineConfig({
bunConfig: {
entrypoints: ["./src/index.ts", "./src/migrate.ts"],
minify: false,
target: "node",
external: ["better-sqlite3"],
Expand Down
10 changes: 0 additions & 10 deletions apps/randomness/migrations/Migration20241209145000.js

This file was deleted.

14 changes: 14 additions & 0 deletions apps/randomness/migrations/Migration20241210123000.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { Kysely } from "kysely"
import type { Database } from "../src/db/types"

export async function up(db: Kysely<Database>) {
await db.schema
.createTable("randomnesses")
.addColumn("timestamp", "text", (col) => col.notNull())
.addColumn("value", "text", (col) => col.notNull())
.addColumn("hashedValue", "text", (col) => col.notNull())
.addColumn("commitmentTransactionIntentId", "text")
.addColumn("revealTransactionIntentId", "text")
.addColumn("status", "text", (col) => col.notNull())
.execute()
}
5 changes: 3 additions & 2 deletions apps/randomness/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
"main": "./dist/index.es.js",
"module": "./dist/index.es.js",
"dependencies": {
"@happy.tech/contracts": "workspace:0.1.0",
"@happy.tech/common": "workspace:0.1.0",
"@happy.tech/txm": "workspace:0.1.0",
"@happy.tech/contracts": "workspace:0.1.0",
"better-sqlite3": "^11.7.0",
"kysely": "^0.27.5",
"neverthrow": "^8.1.0",
"viem": "^2.21.53",
"zod": "^3.23.8"
},
"devDependencies": {
"@happy.tech/configs": "workspace:",
"@happy.tech/configs": "workspace:0.1.0",
"@happy.tech/happybuild": "workspace:0.1.0",
"typescript": "^5.6.2"
}
}
1 change: 0 additions & 1 deletion apps/randomness/src/ABI/random.ts

This file was deleted.

80 changes: 0 additions & 80 deletions apps/randomness/src/CommitmentManager.ts

This file was deleted.

24 changes: 0 additions & 24 deletions apps/randomness/src/Factories/CommitmentTransactionFactory.ts

This file was deleted.

22 changes: 0 additions & 22 deletions apps/randomness/src/Factories/RevealValueTransactionFactory.ts

This file was deleted.

92 changes: 92 additions & 0 deletions apps/randomness/src/Randomness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import crypto from "node:crypto"
import type { Hex, UUID } from "@happy.tech/common"
import { bytesToHex, encodePacked, keccak256 } from "viem"

export enum RandomnessStatus {
PENDING = "PENDING",
COMMITMENT_SUBMITTED = "COMMITMENT_SUBMITTED",
COMMITMENT_EXECUTED = "COMMITMENT_EXECUTED",
COMMITMENT_FAILED = "COMMITMENT_FAILED",
REVEAL_SUBMITTED = "REVEAL_SUBMITTED",
REVEAL_EXECUTED = "REVEAL_EXECUTED",
REVEAL_FAILED = "REVEAL_FAILED",
REVEAL_NOT_SUBMITTED_ON_TIME = "REVEAL_NOT_SUBMITTED_ON_TIME",
}

export class Randomness {
public timestamp: bigint
public value: bigint
public hashedValue: Hex
public commitmentTransactionIntentId: UUID | undefined
public revealTransactionIntentId: UUID | undefined
public status: RandomnessStatus

constructor(params: {
timestamp: bigint
value: bigint
hashedValue: Hex
commitmentTransactionIntentId?: UUID
revealTransactionIntentId?: UUID
status: RandomnessStatus
}) {
this.timestamp = params.timestamp
this.value = params.value
this.hashedValue = params.hashedValue
this.commitmentTransactionIntentId = params.commitmentTransactionIntentId
this.revealTransactionIntentId = params.revealTransactionIntentId
this.status = params.status
}

public commitmentExecuted(): void {
this.status = RandomnessStatus.COMMITMENT_EXECUTED
}

public revealExecuted(): void {
this.status = RandomnessStatus.REVEAL_EXECUTED
}

public addCommitmentTransactionIntentId(intentId: UUID): void {
this.commitmentTransactionIntentId = intentId
this.status = RandomnessStatus.COMMITMENT_SUBMITTED
}

public addRevealTransactionIntentId(intentId: UUID): void {
this.revealTransactionIntentId = intentId
this.status = RandomnessStatus.REVEAL_SUBMITTED
}

public commitmentFailed(): void {
this.status = RandomnessStatus.COMMITMENT_FAILED
}

public revealFailed(): void {
this.status = RandomnessStatus.REVEAL_FAILED
}

public revealNotSubmittedOnTime(): void {
this.status = RandomnessStatus.REVEAL_NOT_SUBMITTED_ON_TIME
}

static createRandomness(timestamp: bigint): Randomness {
const value = Randomness.generateValue()
const hashedValue = Randomness.hashValue(value)
return new Randomness({
timestamp,
value,
hashedValue,
commitmentTransactionIntentId: undefined,
revealTransactionIntentId: undefined,
status: RandomnessStatus.PENDING,
})
}

private static generateValue(): bigint {
const bytes = crypto.randomBytes(32)

return BigInt(bytesToHex(bytes))
}

private static hashValue(value: bigint): Hex {
return keccak256(encodePacked(["uint256"], [value]))
}
}
Loading