Storage with/without annotations This section shows how to write storage when :
it has annotations it has no annotation it has a mix of annotated and not-annotated properties To do so, let's look at three examples of contract origination showing initial values in the storage.
//storage representation in Michelson
(pair
(pair
(pair (address %theAddress) (bool %theBool))
(pair (nat %theNat) (int %theNumber)))
(mutez %theTez))
Copy We need to write the storage as a Javascript object and include the annotated names in it.
Tezos . contract
. originate ( {
code : contractStorageAnnot ,
storage : {
theAddress : 'tz1PgQt52JMirBUhhkq1eanX8hVd1Fsg71Lr' ,
theBool : true ,
theNat : '3' ,
theNumber : '5' ,
theTez : '10' ,
} ,
} )
. then ( ( originationOp ) => {
console . log ( ` Waiting for confirmation of origination for ${ originationOp . contractAddress } ... ` ) ;
return originationOp . contract ( ) ;
} )
. then ( ( ) => {
console . log ( ` Origination completed. ` ) ;
} )
. catch ( ( error ) => console . log ( ` Error: ${ JSON . stringify ( error , null , 2 ) } ` ) ) ;
Copy
Tezos . wallet
. originate ( {
code : contractStorageAnnot ,
storage : {
theAddress : 'tz1PgQt52JMirBUhhkq1eanX8hVd1Fsg71Lr' ,
theBool : true ,
theNat : '3' ,
theNumber : '5' ,
theTez : '10' ,
} ,
} )
. send ( )
. then ( ( originationOp ) => {
console . log ( ` Waiting for confirmation of origination... ` ) ;
return originationOp . contract ( ) ;
} )
. then ( ( ) => {
console . log ( ` Origination completed. ` ) ;
} )
. catch ( ( error ) => console . log ( ` Error: ${ JSON . stringify ( error , null , 2 ) } ` ) ) ;
Copy //storage representation in Michelson
(pair
(pair
(pair (address) (bool))
(pair (nat) (int)))
(mutez))
Copy All properties in storage are accessible by the index corresponding to the order that the storage is defined.
Tezos . contract
. originate ( {
code : contractStorageWithoutAnnot ,
storage : {
0 : 'tz1PgQt52JMirBUhhkq1eanX8hVd1Fsg71Lr' ,
1 : true ,
2 : '3' ,
3 : '5' ,
4 : '10' ,
} ,
} )
. then ( ( originationOp ) => {
console . log ( ` Waiting for confirmation of origination for ${ originationOp . contractAddress } ... ` ) ;
return originationOp . contract ( ) ;
} )
. then ( ( ) => {
console . log ( ` Origination completed. ` ) ;
} )
. catch ( ( error ) => console . log ( ` Error: ${ JSON . stringify ( error , null , 2 ) } ` ) ) ;
Copy
Tezos . wallet
. originate ( {
code : contractStorageWithoutAnnot ,
storage : {
0 : 'tz1PgQt52JMirBUhhkq1eanX8hVd1Fsg71Lr' ,
1 : true ,
2 : '3' ,
3 : '5' ,
4 : '10' ,
} ,
} )
. send ( )
. then ( ( originationOp ) => {
console . log ( ` Waiting for confirmation of origination... ` ) ;
return originationOp . contract ( ) ;
} )
. then ( ( ) => {
console . log ( ` Origination completed. ` ) ;
} )
. catch ( ( error ) => console . log ( ` Error: ${ JSON . stringify ( error , null , 2 ) } ` ) ) ;
Copy //storage representation in Michelson
(pair
(pair
(pair (address) (bool))
(pair (nat %theNat) (int %theNumber)))
(mutez))
Copy In the following example, only the elements in positions 2 and 3 have an annotation. We need to access these elements with their annotated name and the others with corresponding indexes.
Note that when proprieties have annotations, we cannot access them by index. For example, if you replace "theNat" by 2 and "theNumber" by 3 in this code example, it will fail.
Tezos . contract
. originate ( {
code : contractStorageWithAndWithoutAnnot ,
storage : {
0 : 'tz1PgQt52JMirBUhhkq1eanX8hVd1Fsg71Lr' ,
1 : true ,
theNat : '3' ,
theNumber : '5' ,
4 : '10' ,
} ,
} )
. then ( ( originationOp ) => {
console . log ( ` Waiting for confirmation of origination for ${ originationOp . contractAddress } ... ` ) ;
return originationOp . contract ( ) ;
} )
. then ( ( ) => {
console . log ( ` Origination completed. ` ) ;
} )
. catch ( ( error ) => console . log ( ` Error: ${ JSON . stringify ( error , null , 2 ) } ` ) ) ;
Copy
Tezos . wallet
. originate ( {
code : contractStorageWithAndWithoutAnnot ,
storage : {
0 : 'tz1PgQt52JMirBUhhkq1eanX8hVd1Fsg71Lr' ,
1 : true ,
theNat : '3' ,
theNumber : '5' ,
4 : '10' ,
} ,
} )
. send ( )
. then ( ( originationOp ) => {
console . log ( ` Waiting for confirmation of origination... ` ) ;
return originationOp . contract ( ) ;
} )
. then ( ( ) => {
console . log ( ` Origination completed. ` ) ;
} )
. catch ( ( error ) => console . log ( ` Error: ${ JSON . stringify ( error , null , 2 ) } ` ) ) ;
Copy