File tree Expand file tree Collapse file tree 2 files changed +117
-0
lines changed
Expand file tree Collapse file tree 2 files changed +117
-0
lines changed Original file line number Diff line number Diff line change 1+ from dataclasses import dataclass
2+ from typing import Dict , Any
3+
4+ Query = Any
5+ Update = Any
6+ nat64 = Any
7+
8+ __all__ = [
9+ "initialize_supply" ,
10+ "transfer" ,
11+ "balance" ,
12+ "ticker" ,
13+ "name" ,
14+ "total_supply" ,
15+ ]
16+
17+
18+ @dataclass
19+ class Account :
20+ address : str
21+ balance : nat64
22+
23+
24+ @dataclass
25+ class State :
26+ accounts : Dict [str , Account ]
27+ total_supply : nat64
28+ ticker : str
29+ name : str
30+
31+
32+ state = State (
33+ accounts = {},
34+ total_supply = 0 ,
35+ ticker = "" ,
36+ name = "KYBRA" ,
37+ )
38+
39+
40+ def initialize_supply (
41+ ticker : str , name : str , total_supply : nat64 , original_address : str
42+ ) -> Update :
43+ global state
44+ state = State (
45+ accounts = {
46+ original_address : Account (
47+ address = original_address ,
48+ balance = total_supply ,
49+ )
50+ },
51+ ticker = ticker ,
52+ name = name ,
53+ total_supply = total_supply ,
54+ )
55+
56+ return True
57+
58+
59+ def transfer (from_ : str , to : str , amount : nat64 ) -> Update :
60+ global state
61+ if state .accounts [to ] is None :
62+ state .accounts [to ] = Account (address = to , balance = 0 )
63+
64+ state .accounts [from_ ].balance -= amount
65+ state .accounts [to ].balance += amount
66+
67+ return True
68+
69+
70+ def balance (address : str ) -> Query :
71+ account = state .accounts [address ]
72+ if account is None :
73+ return 0
74+ return account .balance
75+
76+
77+ def ticker () -> Query :
78+ return state .ticker
79+
80+
81+ def name () -> Query :
82+ return state .name
83+
84+
85+ def total_supply () -> Query :
86+ return state .total_supply
87+
88+
89+ if __name__ == "__main__" :
90+ print (name ())
Original file line number Diff line number Diff line change 1+ use rustpython_vm as vm;
2+ use vm:: builtins:: PyStrRef ;
3+
4+ fn main ( ) -> vm:: PyResult < ( ) > {
5+ let interp = vm:: Interpreter :: with_init ( Default :: default ( ) , |vm| {
6+ vm. add_native_modules ( rustpython_stdlib:: get_module_inits ( ) ) ;
7+ } ) ;
8+ let result = interp
9+ . run_or_else (
10+ |vm, exc| {
11+ vm. print_exception ( exc. clone ( ) ) ;
12+ Err ( exc)
13+ } ,
14+ |vm| {
15+ vm. insert_sys_path ( vm. new_pyobj ( "examples" ) )
16+ . expect ( "add path" ) ;
17+ let module = vm. import ( "kybra_like" , None , 0 ) ?;
18+ let name_func = module. get_attr ( "name" , vm) ?;
19+ let result = vm. invoke ( & name_func, ( ) ) ?;
20+ let result: PyStrRef = result. try_into_value ( vm) ?;
21+ Ok ( result)
22+ } ,
23+ )
24+ . unwrap ( ) ;
25+ println ! ( "name: {}" , & result) ;
26+ Ok ( ( ) )
27+ }
You can’t perform that action at this time.
0 commit comments