genlayer 0.12.4 → 0.13.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.
- package/.env.example +4 -0
- package/CHANGELOG.md +8 -0
- package/dist/index.js +17970 -17628
- package/esbuild.config.dev.js +1 -2
- package/esbuild.config.prod.js +1 -1
- package/eslint.config.js +2 -1
- package/package.json +5 -3
- package/src/commands/contracts/call.ts +23 -33
- package/src/commands/contracts/deploy.ts +109 -33
- package/src/commands/contracts/index.ts +14 -3
- package/src/commands/general/init.ts +0 -1
- package/src/commands/keygen/create.ts +5 -25
- package/src/commands/scaffold/index.ts +16 -0
- package/src/commands/scaffold/new.ts +34 -0
- package/src/index.ts +2 -0
- package/src/lib/accounts/KeypairManager.ts +43 -0
- package/src/lib/actions/BaseAction.ts +34 -7
- package/src/lib/config/simulator.ts +2 -2
- package/templates/default/LICENSE +21 -0
- package/templates/default/README.md +101 -0
- package/templates/default/__init__.py +0 -0
- package/templates/default/app/.env.example +2 -0
- package/templates/default/app/.vscode/extensions.json +3 -0
- package/templates/default/app/README.md +5 -0
- package/templates/default/app/index.html +17 -0
- package/templates/default/app/package-lock.json +4920 -0
- package/templates/default/app/package.json +23 -0
- package/templates/default/app/postcss.config.js +6 -0
- package/templates/default/app/public/favicon.png +0 -0
- package/templates/default/app/src/App.vue +16 -0
- package/templates/default/app/src/components/Address.vue +38 -0
- package/templates/default/app/src/components/BetsScreen.vue +329 -0
- package/templates/default/app/src/logic/FootballBets.js +100 -0
- package/templates/default/app/src/main.js +5 -0
- package/templates/default/app/src/services/genlayer.js +19 -0
- package/templates/default/app/src/style.css +3 -0
- package/templates/default/app/tailwind.config.js +8 -0
- package/templates/default/app/vite.config.js +7 -0
- package/templates/default/config/__init__.py +0 -0
- package/templates/default/config/genlayer_config.py +14 -0
- package/templates/default/contracts/__init__.py +0 -0
- package/templates/default/contracts/football_bets.py +119 -0
- package/templates/default/deploy/deployScript.ts +31 -0
- package/templates/default/package-lock.json +3231 -0
- package/templates/default/package.json +7 -0
- package/templates/default/requirements.txt +6 -0
- package/templates/default/test/__init__.py +0 -0
- package/templates/default/test/football_bets_get_contract_schema_for_code.py +124 -0
- package/templates/default/test/test_football_bet_success_draw.py +108 -0
- package/templates/default/test/test_football_bet_success_win.py +106 -0
- package/templates/default/test/test_football_bet_unsuccess.py +107 -0
- package/templates/default/tools/__init__.py +0 -0
- package/templates/default/tools/accounts.py +5 -0
- package/templates/default/tools/calldata.py +224 -0
- package/templates/default/tools/request.py +134 -0
- package/templates/default/tools/response.py +52 -0
- package/templates/default/tools/structure.py +39 -0
- package/templates/default/tools/transactions.py +28 -0
- package/templates/default/tools/types.py +214 -0
- package/templates/default/tsconfig.json +7 -0
- package/tests/actions/call.test.ts +39 -79
- package/tests/actions/create.test.ts +11 -72
- package/tests/actions/deploy.test.ts +201 -33
- package/tests/actions/new.test.ts +80 -0
- package/tests/commands/call.test.ts +6 -1
- package/tests/commands/deploy.test.ts +12 -1
- package/tests/commands/new.test.ts +68 -0
- package/tests/index.test.ts +4 -0
- package/tests/libs/accounts/KeypairManager.test.ts +110 -0
- package/tests/libs/baseAction.test.ts +40 -0
- package/vitest.config.ts +1 -1
- package/src/lib/accounts/getPrivateKey.ts +0 -21
- package/tests/libs/getPrivateKey.test.ts +0 -96
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# { "Depends": "py-genlayer:test" }
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from genlayer import *
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass
|
|
9
|
+
class Bet:
|
|
10
|
+
id: str
|
|
11
|
+
has_resolved: bool
|
|
12
|
+
game_date: str
|
|
13
|
+
resolution_url: str
|
|
14
|
+
team1: str
|
|
15
|
+
team2: str
|
|
16
|
+
predicted_winner: str
|
|
17
|
+
real_winner: str
|
|
18
|
+
real_score: str
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@gl.contract
|
|
22
|
+
class FootballBets:
|
|
23
|
+
bets: TreeMap[Address, TreeMap[str, Bet]]
|
|
24
|
+
points: TreeMap[Address, u256]
|
|
25
|
+
|
|
26
|
+
def __init__(self):
|
|
27
|
+
pass
|
|
28
|
+
|
|
29
|
+
def _check_match(self, resolution_url: str, team1: str, team2: str) -> dict:
|
|
30
|
+
def get_match_result() -> str:
|
|
31
|
+
web_data = gl.get_webpage(resolution_url, mode="text")
|
|
32
|
+
|
|
33
|
+
task = f"""
|
|
34
|
+
Extract the match result for:
|
|
35
|
+
Team 1: {team1}
|
|
36
|
+
Team 2: {team2}
|
|
37
|
+
|
|
38
|
+
Web content:
|
|
39
|
+
{web_data}
|
|
40
|
+
|
|
41
|
+
Respond in JSON:
|
|
42
|
+
{{
|
|
43
|
+
"score": str, // e.g., "1:2" or "-" if unresolved
|
|
44
|
+
"winner": int // 0 for draw, -1 if unresolved
|
|
45
|
+
}}
|
|
46
|
+
It is mandatory that you respond only using the JSON format above,
|
|
47
|
+
nothing else. Don't include any other words or characters,
|
|
48
|
+
your output must be only JSON without any formatting prefix or suffix.
|
|
49
|
+
This result should be perfectly parsable by a JSON parser without errors.
|
|
50
|
+
"""
|
|
51
|
+
result = gl.exec_prompt(task).replace("```json", "").replace("```", "")
|
|
52
|
+
return json.dumps(json.loads(result), sort_keys=True)
|
|
53
|
+
|
|
54
|
+
result_json = json.loads(gl.eq_principle_strict_eq(get_match_result))
|
|
55
|
+
return result_json
|
|
56
|
+
|
|
57
|
+
@gl.public.write
|
|
58
|
+
def create_bet(
|
|
59
|
+
self, game_date: str, team1: str, team2: str, predicted_winner: str
|
|
60
|
+
) -> None:
|
|
61
|
+
match_resolution_url = (
|
|
62
|
+
"https://www.bbc.com/sport/football/scores-fixtures/" + game_date
|
|
63
|
+
)
|
|
64
|
+
# commented to allow to test matches in the past.
|
|
65
|
+
# match_status = await self._check_match(match_resolution_url, team1, team2)
|
|
66
|
+
|
|
67
|
+
# if int(match_status["winner"]) > -1:
|
|
68
|
+
# raise Exception("Game already finished")
|
|
69
|
+
|
|
70
|
+
sender_address = gl.message.sender_account
|
|
71
|
+
|
|
72
|
+
bet_id = f"{game_date}_{team1}_{team2}".lower()
|
|
73
|
+
if sender_address in self.bets and bet_id in self.bets[sender_address]:
|
|
74
|
+
raise Exception("Bet already created")
|
|
75
|
+
|
|
76
|
+
bet = Bet(
|
|
77
|
+
id=bet_id,
|
|
78
|
+
has_resolved=False,
|
|
79
|
+
game_date=game_date,
|
|
80
|
+
resolution_url=match_resolution_url,
|
|
81
|
+
team1=team1,
|
|
82
|
+
team2=team2,
|
|
83
|
+
predicted_winner=predicted_winner,
|
|
84
|
+
real_winner="",
|
|
85
|
+
real_score="",
|
|
86
|
+
)
|
|
87
|
+
self.bets.get_or_insert_default(sender_address)[bet_id] = bet
|
|
88
|
+
|
|
89
|
+
@gl.public.write
|
|
90
|
+
def resolve_bet(self, bet_id: str) -> None:
|
|
91
|
+
if self.bets[gl.message.sender_account][bet_id].has_resolved:
|
|
92
|
+
raise Exception("Bet already resolved")
|
|
93
|
+
|
|
94
|
+
bet = self.bets[gl.message.sender_account][bet_id]
|
|
95
|
+
bet_status = self._check_match(bet.resolution_url, bet.team1, bet.team2)
|
|
96
|
+
|
|
97
|
+
if int(bet_status["winner"]) < 0:
|
|
98
|
+
raise Exception("Game not finished")
|
|
99
|
+
|
|
100
|
+
bet.has_resolved = True
|
|
101
|
+
bet.real_winner = str(bet_status["winner"])
|
|
102
|
+
bet.real_score = bet_status["score"]
|
|
103
|
+
|
|
104
|
+
if bet.real_winner == bet.predicted_winner:
|
|
105
|
+
if gl.message.sender_account not in self.points:
|
|
106
|
+
self.points[gl.message.sender_account] = 0
|
|
107
|
+
self.points[gl.message.sender_account] += 1
|
|
108
|
+
|
|
109
|
+
@gl.public.view
|
|
110
|
+
def get_bets(self) -> dict:
|
|
111
|
+
return {k.as_hex: v for k, v in self.bets.items()}
|
|
112
|
+
|
|
113
|
+
@gl.public.view
|
|
114
|
+
def get_points(self) -> dict:
|
|
115
|
+
return {k.as_hex: v for k, v in self.points.items()}
|
|
116
|
+
|
|
117
|
+
@gl.public.view
|
|
118
|
+
def get_player_points(self, player_address: str) -> int:
|
|
119
|
+
return self.points.get(Address(player_address), 0)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { readFileSync } from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { TransactionHash, TransactionStatus, GenLayerClient } from "genlayer-js/types";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export default async function main(client: GenLayerClient<any>) {
|
|
7
|
+
const filePath = path.resolve(process.cwd(), "contracts/football_bets.py");
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
const contractCode = new Uint8Array(readFileSync(filePath));
|
|
11
|
+
|
|
12
|
+
await client.initializeConsensusSmartContract();
|
|
13
|
+
|
|
14
|
+
const deployTransaction = await client.deployContract({
|
|
15
|
+
code: contractCode,
|
|
16
|
+
args: [],
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const receipt = await client.waitForTransactionReceipt({
|
|
20
|
+
hash: deployTransaction as TransactionHash,
|
|
21
|
+
status: TransactionStatus.ACCEPTED,
|
|
22
|
+
retries: 200,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
if (receipt.consensus_data?.leader_receipt?.execution_result !== "SUCCESS") {
|
|
26
|
+
throw new Error(`Deployment failed. Receipt: ${JSON.stringify(receipt)}`);
|
|
27
|
+
}
|
|
28
|
+
} catch (error) {
|
|
29
|
+
throw new Error((`Error during deployment:, ${error}`));
|
|
30
|
+
}
|
|
31
|
+
}
|