1- use loam_sdk:: {
2- loamstorage, log,
3- soroban_sdk:: { self , env, Address , LoamKey , PersistentMap , String } ,
4- } ;
1+ use loam_sdk:: soroban_sdk:: { self , env, Address , String } ;
52
63use crate :: {
74 error:: Error ,
8- metadata:: { ContractMetadata , PublishedContract , PublishedWasm } ,
5+ metadata:: { Metadata , PublishedWasm , Wasm } ,
96 version:: { self , Version , INITAL_VERSION } ,
107} ;
118
129use super :: IsPublishable ;
1310
14- #[ loamstorage]
15-
16- pub struct Wasm {
17- registry : PersistentMap < String , PublishedContract > ,
18- }
19-
20- impl Wasm {
21- pub fn find_contract ( & self , name : String ) -> Result < PublishedContract , Error > {
22- self . registry
23- . get ( name)
24- . ok_or ( Error :: NoSuchContractPublished )
25- }
26-
27- pub fn find_version (
28- & self ,
29- name : String ,
30- version : Option < Version > ,
31- ) -> Result < PublishedWasm , Error > {
32- self . find_contract ( name) ?. get ( version)
33- }
34-
35- pub fn set_contract ( & mut self , name : String , contract : & PublishedContract ) {
36- self . registry . set ( name, contract) ;
37- }
38- }
39-
40- impl IsPublishable for Wasm {
41- fn fetch (
42- & self ,
43- contract_name : String ,
44- version : Option < Version > ,
45- ) -> Result < PublishedWasm , Error > {
46- self . find_version ( contract_name, version)
11+ impl IsPublishable for PublishedWasm {
12+ fn fetch ( & self , contract_name : String , version : Option < Version > ) -> Result < Wasm , Error > {
13+ self . get ( & contract_name, version)
4714 }
4815
4916 fn current_version ( & self , contract_name : String ) -> Result < Version , Error > {
50- self . find_contract ( contract_name ) ? . most_recent_version ( )
17+ self . most_recent_version ( & contract_name )
5118 }
5219
5320 fn publish (
@@ -70,34 +37,31 @@ impl IsPublishable for Wasm {
7037 repo : Option < soroban_sdk:: String > ,
7138 kind : Option < version:: Update > ,
7239 ) -> Result < ( ) , Error > {
73- let mut contract = self
74- . find_contract ( wasm_name. clone ( ) )
75- . unwrap_or_else ( |_| PublishedContract :: new ( author. clone ( ) ) ) ;
76-
77- if author != contract. author {
78- return Err ( Error :: AlreadyPublished ) ;
40+ if let Some ( cunnet_author) = self . author ( & wasm_name) {
41+ if author != cunnet_author {
42+ return Err ( Error :: AlreadyPublished ) ;
43+ }
7944 }
80- contract. author . require_auth ( ) ;
8145
82- let keys = contract. versions . keys ( ) ;
83- let last_version = keys. last ( ) . unwrap_or_default ( ) ;
46+ author. require_auth ( ) ;
47+
48+ let last_version: Version = self . most_recent_version ( & wasm_name) . unwrap_or_default ( ) ;
8449 last_version. log ( ) ;
8550 let new_version = last_version. clone ( ) . update ( & kind. unwrap_or_default ( ) ) ;
8651 new_version. log ( ) ;
8752
8853 let metadata = if let Some ( repo) = repo {
89- ContractMetadata { repo }
54+ Metadata { repo }
9055 } else if new_version == INITAL_VERSION {
91- ContractMetadata :: default ( )
56+ Metadata :: default ( )
9257 } else {
93- contract . get ( Some ( last_version) ) ?. metadata
58+ self . get ( & wasm_name , Some ( last_version) ) ?. metadata
9459 } ;
95- let published_binary = PublishedWasm {
60+ let published_binary = Wasm {
9661 hash : wasm_hash,
9762 metadata,
9863 } ;
99- contract. versions . set ( new_version, published_binary) ;
100- self . set_contract ( wasm_name, & contract) ;
101- Ok ( ( ) )
64+ self . set_most_recent_version ( & wasm_name, new_version. clone ( ) ) ;
65+ self . set ( wasm_name, Some ( new_version) , published_binary)
10266 }
10367}
0 commit comments