1
1
import { getFrequencyAPI , getSignerAccountKeys } from "./services/connect.js" ;
2
- import dsnp , { SchemaName as DsnpSchemaName } from "../dsnp/index.js" ;
2
+ import dsnp , { SchemaName as DsnpSchemaName , SchemaMapping , GENESIS_HASH_MAINNET } from "../dsnp/index.js" ;
3
3
import { EventRecord } from "@polkadot/types/interfaces" ;
4
4
5
5
export const deploy = async ( ) => {
@@ -42,7 +42,8 @@ export const deploy = async () => {
42
42
43
43
console . log ( "Deploy of Schemas Starting..." ) ;
44
44
45
- return await createSchemas ( schemaNames ) ;
45
+ const mapping = await createSchemas ( schemaNames ) ;
46
+ console . log ( "Generated schema mapping:\n" , JSON . stringify ( mapping , null , 2 ) ) ;
46
47
} ;
47
48
48
49
// Given a list of events, a section and a method,
@@ -54,17 +55,13 @@ const eventWithSectionAndMethod = (events: EventRecord[], section: string, metho
54
55
55
56
// Given a list of schema names, attempt to create them with the chain.
56
57
const createSchemas = async ( schemaNames : string [ ] ) => {
57
- type SchemaInfo = {
58
- schemaName : string ;
59
- id ?: number ;
60
- } ;
58
+ type SchemaInfo = [ schemaName : DsnpSchemaName , { [ version : string ] : number } ] ;
61
59
62
60
const promises : Promise < SchemaInfo > [ ] = [ ] ;
63
61
const api = await getFrequencyAPI ( ) ;
64
62
const signerAccountKeys = getSignerAccountKeys ( ) ;
65
63
// Mainnet genesis hash means we should propose instead of create
66
- const shouldPropose =
67
- api . genesisHash . toHex ( ) === "0x4a587bf17a404e3572747add7aab7bbe56e805a5479c6c436f07f36fcc8d3ae1" ;
64
+ const shouldPropose = api . genesisHash . toHex ( ) === GENESIS_HASH_MAINNET ;
68
65
69
66
if ( shouldPropose && schemaNames . length > 1 ) {
70
67
console . error ( "Proposing to create schemas can only occur one at a time. Please try again with only one schema." ) ;
@@ -92,55 +89,70 @@ const createSchemas = async (schemaNames: string[]) => {
92
89
// Propose to create
93
90
const promise = new Promise < SchemaInfo > ( ( resolve , reject ) => {
94
91
api . tx . schemas
95
- . proposeToCreateSchema (
92
+ . proposeToCreateSchemaV2 (
96
93
json_no_ws ,
97
94
schemaDeploy . modelType ,
98
95
schemaDeploy . payloadLocation ,
99
96
schemaDeploy . settings ,
97
+ "dsnp." + schemaName ,
100
98
)
101
99
. signAndSend ( signerAccountKeys , { nonce } , ( { status, events, dispatchError } ) => {
102
100
if ( dispatchError ) {
103
101
console . error ( "ERROR: " , dispatchError . toHuman ( ) ) ;
104
102
console . log ( "Might already have a proposal with the same hash?" ) ;
105
- reject ( ) ;
103
+ reject ( dispatchError . toHuman ( ) ) ;
106
104
} else if ( status . isInBlock || status . isFinalized ) {
107
105
const evt = eventWithSectionAndMethod ( events , "council" , "Proposed" ) ;
108
106
if ( evt ) {
109
107
const id = evt ?. data [ 1 ] ;
110
108
const hash = evt ?. data [ 2 ] . toHex ( ) ;
111
109
console . log ( "SUCCESS: " + schemaName + " schema proposed with id of " + id + " and hash of " + hash ) ;
112
- resolve ( { schemaName, id : Number ( id . toHuman ( ) ) } ) ;
113
- } else resolve ( { schemaName } ) ;
110
+ const v2n = Object . fromEntries ( [ [ schemaDeploy . dsnpVersion , Number ( id . toHuman ( ) ) ] ] ) ;
111
+ resolve ( [ schemaName as DsnpSchemaName , v2n ] ) ;
112
+ } else {
113
+ const err = "Proposed event not found" ;
114
+ console . error ( `ERROR: ${ err } ` ) ;
115
+ reject ( err ) ;
116
+ }
114
117
}
115
118
} ) ;
116
119
} ) ;
117
120
promises [ idx ] = promise ;
118
121
} else {
119
122
// Create directly via sudo
120
- const tx = api . tx . schemas . createSchemaViaGovernance (
123
+ const tx = api . tx . schemas . createSchemaViaGovernanceV2 (
121
124
signerAccountKeys . address ,
122
125
json_no_ws ,
123
126
schemaDeploy . modelType ,
124
127
schemaDeploy . payloadLocation ,
125
128
schemaDeploy . settings ,
129
+ "dsnp." + schemaName ,
126
130
) ;
127
131
const promise = new Promise < SchemaInfo > ( ( resolve , reject ) => {
128
132
api . tx . sudo . sudo ( tx ) . signAndSend ( signerAccountKeys , { nonce } , ( { status, events, dispatchError } ) => {
129
133
if ( dispatchError ) {
130
134
console . error ( "ERROR: " , dispatchError . toHuman ( ) ) ;
131
- reject ( ) ;
135
+ reject ( dispatchError . toHuman ( ) ) ;
132
136
} else if ( status . isInBlock || status . isFinalized ) {
133
137
const evt = eventWithSectionAndMethod ( events , "schemas" , "SchemaCreated" ) ;
134
138
if ( evt ) {
135
- const val = evt ?. data [ 1 ] ;
136
- console . log ( "SUCCESS: " + schemaName + " schema created with id of " + val ) ;
137
- resolve ( { schemaName, id : Number ( val . toHuman ( ) ) } ) ;
138
- } else resolve ( { schemaName } ) ;
139
+ const id = evt ?. data [ 1 ] ;
140
+ console . log ( "SUCCESS: " + schemaName + " schema created with id of " + id ) ;
141
+ const v2n = Object . fromEntries ( [ [ schemaDeploy . dsnpVersion , Number ( id . toHuman ( ) ) ] ] ) ;
142
+ resolve ( [ schemaName as DsnpSchemaName , v2n ] ) ;
143
+ } else {
144
+ const err = "SchemaCreated event not found" ;
145
+ console . error ( `ERROR: ${ err } ` ) ;
146
+ reject ( err ) ;
147
+ }
139
148
}
140
149
} ) ;
141
150
} ) ;
142
151
promises [ idx ] = promise ;
143
152
}
144
153
}
145
- return Promise . all ( promises ) ;
154
+ const output = await Promise . all ( promises ) ;
155
+ const mapping : { [ genesisHash : string ] : SchemaMapping } = { } ;
156
+ mapping [ api . genesisHash . toString ( ) ] = Object . fromEntries ( output ) ;
157
+ return mapping ;
146
158
} ;
0 commit comments