|
| 1 | +/** |
| 2 | + * Copyright (c) 2025-present, Dash Core Group |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | +package dashj.org.platform |
| 8 | + |
| 9 | +import org.bitcoinj.core.Coin |
| 10 | +import org.bitcoinj.core.Sha256Hash |
| 11 | +import org.dashj.platform.dapiclient.model.DocumentQuery |
| 12 | +import org.dashj.platform.dpp.document.Document |
| 13 | +import org.dashj.platform.dpp.identifier.Identifier |
| 14 | +import org.dashj.platform.sdk.Client |
| 15 | +import org.dashj.platform.sdk.client.ClientOptions |
| 16 | +import org.dashj.platform.sdk.platform.Documents |
| 17 | +import org.dashj.platform.sdk.platform.DomainDocument |
| 18 | + |
| 19 | +class RegisteredNamesWithBalance { |
| 20 | + companion object { |
| 21 | + lateinit var sdk: Client |
| 22 | + |
| 23 | + @JvmStatic |
| 24 | + fun main(args: Array<String>) { |
| 25 | + sdk = Client(ClientOptions(network = args[0])) |
| 26 | + sdk.platform.useValidNodes() |
| 27 | + getDocumentsWithIdentityBalance() |
| 28 | + } |
| 29 | + |
| 30 | + private fun getDocumentsWithIdentityBalance() { |
| 31 | + val platform = sdk.platform |
| 32 | + |
| 33 | + var lastItem = Identifier.from(Sha256Hash.ZERO_HASH) |
| 34 | + var documents: List<Document>? = null |
| 35 | + val allDocuments = arrayListOf<Document>() |
| 36 | + var requests = 0 |
| 37 | + val limit = Documents.DOCUMENT_LIMIT |
| 38 | + var queryOpts = DocumentQuery.Builder().limit(limit).orderBy("normalizedLabel").build() |
| 39 | + |
| 40 | + do { |
| 41 | + println(queryOpts.toJSON()) |
| 42 | + |
| 43 | + try { |
| 44 | + documents = platform.documents.get("dpns.domain", queryOpts) |
| 45 | + allDocuments.addAll(documents) |
| 46 | + requests += 1 |
| 47 | + |
| 48 | +// for (doc in documents) { |
| 49 | +// println( |
| 50 | +// "Name: %-20s".format(doc.data["label"]) + |
| 51 | +// " (domain: " + doc.data["normalizedParentDomainName"] + |
| 52 | +// ") Identity: " + doc.ownerId |
| 53 | +// ) |
| 54 | +// } |
| 55 | + |
| 56 | + lastItem = documents.last().id |
| 57 | + if (documents.size == 100) { |
| 58 | + queryOpts = DocumentQuery.Builder() |
| 59 | + .startAt(lastItem) |
| 60 | + .orderBy("normalizedLabel") |
| 61 | + .limit(100) |
| 62 | + .build() |
| 63 | + } |
| 64 | + } catch (e: Exception) { |
| 65 | + println("\nError retrieving results (startAt = $lastItem)") |
| 66 | + println(e.message) |
| 67 | + return |
| 68 | + } |
| 69 | + } while (requests >= 0 && documents!!.size == limit) |
| 70 | + |
| 71 | + val nameBalanceMap = hashMapOf<String, Long>() |
| 72 | + allDocuments.forEachIndexed { i, document -> |
| 73 | + val nameDocument = DomainDocument(document) |
| 74 | + val balance = try { platform.client.getIdentityBalance(nameDocument.dashUniqueIdentityId!!) } catch (e: Exception) { - 2 } |
| 75 | + nameBalanceMap[nameDocument.normalizedLabel] = balance |
| 76 | + } |
| 77 | + println("username count: ${allDocuments.size}") |
| 78 | + allDocuments.forEachIndexed { i, document -> |
| 79 | + val nameDocument = DomainDocument(document) |
| 80 | + val balance = nameBalanceMap[nameDocument.normalizedLabel] |
| 81 | + println("$i. ${nameDocument.label}: ${Coin.valueOf((balance ?: -1) / 1000).toFriendlyString()}") |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments