@@ -5,9 +5,10 @@ use near_sdk::{
55 env:: { self , block_timestamp} ,
66 ext_contract, log, near, require,
77 store:: { IterableMap , IterableSet , Vector } ,
8- AccountId , NearToken , PanicOnDefault , Promise , PublicKey ,
8+ AccountId , Gas , NearToken , PanicOnDefault , Promise , PromiseError , PublicKey ,
99} ;
1010
11+ use crate :: events:: * ;
1112use crate :: pool:: * ;
1213use crate :: types:: * ;
1314
@@ -20,6 +21,8 @@ mod types;
2021mod upgrade;
2122mod view;
2223
24+ const GAS_REGISTER_WORKER_CALLBACK : Gas = Gas :: from_tgas ( 10 ) ;
25+
2326#[ near( serializers = [ json, borsh] ) ]
2427#[ derive( Clone ) ]
2528pub struct Worker {
@@ -84,26 +87,48 @@ impl Contract {
8487
8588 // TODO: verify predecessor implicit account is derived from this public key
8689 let public_key = env:: signer_account_pk ( ) ;
87-
88- let predecessor = env:: predecessor_account_id ( ) ;
89- self . worker_by_account_id . insert (
90- predecessor,
91- Worker {
92- pool_id,
93- checksum,
94- codehash,
95- } ,
96- ) ;
90+ let worker_id = env:: predecessor_account_id ( ) ;
9791
9892 // add the public key to the intents vault
9993 ext_intents_vault:: ext ( self . get_pool_account_id ( pool_id) )
10094 . with_attached_deposit ( NearToken :: from_yoctonear ( 1 ) )
101- . add_public_key ( self . intents_contract_id . clone ( ) , public_key)
95+ . add_public_key ( self . intents_contract_id . clone ( ) , public_key. clone ( ) )
96+ . then (
97+ Self :: ext ( env:: current_account_id ( ) )
98+ . with_static_gas ( GAS_REGISTER_WORKER_CALLBACK )
99+ . on_worker_key_added ( worker_id, pool_id, public_key, codehash, checksum) ,
100+ )
102101 }
103102
104- pub fn require_approved_codehash ( & self ) {
105- let worker = self . get_worker ( env:: predecessor_account_id ( ) ) ;
106- self . assert_approved_codehash ( & worker. codehash ) ;
103+ #[ private]
104+ pub fn on_worker_key_added (
105+ & mut self ,
106+ worker_id : AccountId ,
107+ pool_id : u32 ,
108+ public_key : PublicKey ,
109+ codehash : String ,
110+ checksum : String ,
111+ #[ callback_result] call_result : Result < ( ) , PromiseError > ,
112+ ) {
113+ if call_result. is_ok ( ) {
114+ self . worker_by_account_id . insert (
115+ worker_id. clone ( ) ,
116+ Worker {
117+ pool_id,
118+ checksum : checksum. clone ( ) ,
119+ codehash : codehash. clone ( ) ,
120+ } ,
121+ ) ;
122+
123+ Event :: WorkerRegistered {
124+ worker_id : & worker_id,
125+ pool_id : & pool_id,
126+ public_key : & public_key,
127+ codehash : & codehash,
128+ checksum : & checksum,
129+ }
130+ . emit ( ) ;
131+ }
107132 }
108133}
109134
@@ -114,4 +139,9 @@ impl Contract {
114139 "Invalid code hash"
115140 ) ;
116141 }
142+
143+ // fn require_approved_codehash(&self) {
144+ // let worker = self.get_worker(env::predecessor_account_id());
145+ // self.assert_approved_codehash(&worker.codehash);
146+ // }
117147}
0 commit comments