forked from momentum-xyz/ubercontroller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmint.js
More file actions
195 lines (146 loc) · 5.21 KB
/
mint.js
File metadata and controls
195 lines (146 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const out = {
data: {},
logs: [],
error: null
}
let ApiModule
let UUIDModule
try {
ApiModule = require('/usr/local/lib/node_modules/@polkadot/api');
UUIDModule = require('/usr/local/lib/node_modules/uuid');
} catch (e) {
exitWithError(e.message)
}
const {ApiPromise, WsProvider, Keyring} = ApiModule
const {v4} = UUIDModule
const collectionId = 3
async function hasPayment(block_hash, from_wallet, amount, api, admin_pair) {
const signedBlock = await api.rpc.chain.getBlock(block_hash);
// console.log(signedBlock)
// console.log(JSON.stringify(signedBlock))
// the information for each of the contained extrinsics
for (const ex of signedBlock.block.extrinsics) {
const info = ex.toHuman()
if (info.signer &&
info.signer.Id &&
info.signer.Id.toString &&
info.signer.Id.toString() === from_wallet) {
if (!info.method) {
continue
}
const {args, method, section} = info.method
if (section && section === "balances") {
if (method && method.includes("transfer")) {
if (args && args.dest) {
if (args.dest.Id === admin_pair.address) {
return true
}
}
}
}
}
}
return false
}
async function mint(api, owner_wallet, admin_pair) {
const item = getRandom(1, 1_000_000_000)
log("itemID=" + item)
return new Promise((resolve, reject) => {
api.tx.uniques
.mint(collectionId, item, owner_wallet)
.signAndSend(admin_pair, (result) => {
log(`Current status is ${result.status}`);
if (result.status.isInBlock) {
log(`Transaction included at blockHash ${result.status.asInBlock}`);
} else if (result.status.isFinalized) {
log(`Transaction finalized at blockHash ${result.status.asFinalized}`);
// unsub();
resolve(item)
}
});
})
}
async function setMeta(api, item_id, name, image, admin_pair) {
const isFrozen = false
const uuid = v4()
// ["d83670c7-a120-47a4-892d-f9ec75604f74","Mitia",0,"https://picsum.photos/102"]
const meta = [uuid, name, 0, image]
return new Promise((resolve, reject) => {
api.tx.uniques
.setMetadata(collectionId, item_id, JSON.stringify(meta), isFrozen)
.signAndSend(admin_pair, (result) => {
log(`Current status is ${result.status}`);
if (result.status.isInBlock) {
log(`Transaction included at blockHash ${result.status.asInBlock}`);
} else if (result.status.isFinalized) {
log(`Transaction finalized at blockHash ${result.status.asFinalized}`);
// unsub();
resolve(uuid)
}
});
})
}
async function main() {
if (process.argv.length !== 6) {
const m = `Provide target wallet as first cli argument, admin mnemonic phrase as second, meta as third, block_hash as forth`
exitWithError(m)
}
const TARGET_WALLET = process.argv[2]
log(`target wallet: ${TARGET_WALLET}`)
const PHRASE = process.argv[3]
log(`mnemonic phrase: ${PHRASE.substring(0, 3)} ... ${PHRASE.substring(PHRASE.length - 3)}; length: ${PHRASE.length}`)
const META = process.argv[4]
log(`NFT metadata: ${META}`)
const BLOCK_HASH = process.argv[5]
log(`block hash: ${BLOCK_HASH}`)
let m
try {
m = JSON.parse(META)
} catch (e) {
exitWithError(e.message)
}
const name = m.name
const image = m.image
if (!name) {
exitWithError(`META must contain name`)
}
if (!image && image !== "") {
exitWithError(`META must contain image (can be empty string)`)
}
const url = "wss://drive.antst.net:19947"
// const url = "wss://rpc.polkadot.io"
log(`Using url: ${url}`)
const wsProvider = new WsProvider(url);
const api = await ApiPromise.create({provider: wsProvider});
const keyring = new Keyring({type: 'sr25519'});
const newPair = keyring.addFromUri(PHRASE);
log(newPair.address)
const amount = 1
const flag = await hasPayment(BLOCK_HASH, TARGET_WALLET, amount, api, newPair)
if (!flag) {
exitWithError(`Payment to admin wallet from wallet=${TARGET_WALLET} not found in given block blockHash=${BLOCK_HASH}`)
}
const itemID = await mint(api, TARGET_WALLET, newPair)
log(` Item minted: itemID=${itemID}`)
const userID = await setMeta(api, itemID, name, image, newPair)
log("disconnect")
out.data = {userID: userID, name, image}
console.log(JSON.stringify(out))
await api.disconnect()
}
function log(m) {
// console.log(m)
out.logs.push(m)
}
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
process.on('unhandledRejection', error => {
exitWithError(`unhandledRejection: ${error.message}`)
});
function exitWithError(message) {
out.error = message
console.log(JSON.stringify(out))
process.exit(0)
}
main()