mirror of
https://github.com/dap-ps/discover.git
synced 2025-01-31 19:45:53 +00:00
Fetch dapps from back-end and verify them recording to smart contract
This commit is contained in:
parent
dceed08dc5
commit
231ea6d978
@ -32,9 +32,57 @@ class DiscoverService extends BlockchainService {
|
||||
}
|
||||
|
||||
async getDAppsCount() {
|
||||
return DiscoverContract.methods
|
||||
return MetadataClient.getDappsCount();
|
||||
// return DiscoverContract.methods
|
||||
// .getDAppsCount()
|
||||
// .call({ from: this.sharedContext.account })
|
||||
}
|
||||
|
||||
async getAllDappsWithMetadata() {
|
||||
try {
|
||||
const contractDappsCount = await DiscoverContract.methods
|
||||
.getDAppsCount()
|
||||
.call({ from: this.sharedContext.account })
|
||||
|
||||
const dappsCache = JSON.parse(JSON.stringify(await MetadataClient.retrieveMetadataCache()))
|
||||
const dapps = [];
|
||||
|
||||
for (let i = 0; i < contractDappsCount; i++) {
|
||||
const dapp = await DiscoverContract.methods
|
||||
.dapps(index)
|
||||
.call({ from: this.sharedContext.account })
|
||||
|
||||
const dappMetadata = dappsCache[dapp.metadata];
|
||||
delete dappsCache[dapp.metadata];
|
||||
dapp.metadata = dappMetadata.details;
|
||||
dapp.metadata.status = dappMetadata.status
|
||||
|
||||
dapps.push(dapp);
|
||||
}
|
||||
|
||||
Object.keys(dappsCache).forEach(metadataHash => {
|
||||
const dappMetadata = dappsCache[metadataHash];
|
||||
|
||||
dapps.push({
|
||||
developer: "",
|
||||
id: dappMetadata.compressedMetadata,
|
||||
metadata: {
|
||||
...dappMetadata.details,
|
||||
status: dappMetadata.status
|
||||
},
|
||||
balance: 0,
|
||||
rate: 0,
|
||||
available: 0,
|
||||
votesMinted: 0,
|
||||
votesCast: 0,
|
||||
effectiveBalance: 0
|
||||
});
|
||||
});
|
||||
|
||||
return dapps;
|
||||
} catch (error) {
|
||||
throw new Error(`Error fetching dapps. Details: ${error.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
async getDAppByIndexWithMetadata(index) {
|
||||
@ -43,9 +91,10 @@ class DiscoverService extends BlockchainService {
|
||||
.dapps(index)
|
||||
.call({ from: this.sharedContext.account })
|
||||
|
||||
const dappMetadata = await MetadataClient.retrieveMetadataCache(
|
||||
const dappMetadata = await MetadataClient.retrieveDAppFromCache(
|
||||
dapp.metadata,
|
||||
)
|
||||
|
||||
if (dappMetadata === null) return null
|
||||
dapp.metadata = dappMetadata.details
|
||||
dapp.metadata.status = dappMetadata.status
|
||||
@ -128,15 +177,18 @@ class DiscoverService extends BlockchainService {
|
||||
|
||||
const uploadedMetadata = await MetadataClient.upload(dappMetadata)
|
||||
|
||||
let createdTx = null;
|
||||
if (tokenAmount.gt(0)) {
|
||||
const callData = ConnectedDiscoverContract.methods
|
||||
.createDApp(dappId, tokenAmount, uploadedMetadata)
|
||||
.encodeABI()
|
||||
|
||||
const createdTx = await this.sharedContext.SNTService.approveAndCall(
|
||||
createdTx = await this.sharedContext.SNTService.approveAndCall(
|
||||
this.contract,
|
||||
tokenAmount,
|
||||
callData,
|
||||
)
|
||||
}
|
||||
|
||||
await MetadataClient.requestApproval(uploadedMetadata)
|
||||
|
||||
|
@ -27,12 +27,6 @@ class DiscoverValidator {
|
||||
throw new Error('You must submit a unique ID')
|
||||
}
|
||||
|
||||
if (amount.lte(0)) {
|
||||
throw new Error(
|
||||
'You must spend some SNT to submit a ranking in order to avoid spam',
|
||||
)
|
||||
}
|
||||
|
||||
const safeMax = await this.service.safeMax()
|
||||
if (amount.div(this.decimalMultiplier).toNumber() > safeMax) {
|
||||
throw new Error('You cannot stake more SNT than the ceiling dictates')
|
||||
|
@ -76,7 +76,20 @@ class MetadataClient {
|
||||
return formatedDappsMetadata
|
||||
}
|
||||
|
||||
static async retrieveMetadataCache(metadataBytes32) {
|
||||
static async getDappsCount() {
|
||||
if (metadataCache === null)
|
||||
metadataCache = await MetadataClient.retrieveAllDappsMetadata()
|
||||
return Object.keys(metadataCache).length;
|
||||
}
|
||||
|
||||
static async retrieveMetadataCache() {
|
||||
if (metadataCache === null)
|
||||
metadataCache = await MetadataClient.retrieveAllDappsMetadata()
|
||||
|
||||
return metadataCache;
|
||||
}
|
||||
|
||||
static async retrieveDAppFromCache(metadataBytes32) {
|
||||
if (metadataCache === null)
|
||||
metadataCache = await MetadataClient.retrieveAllDappsMetadata()
|
||||
const result = metadataCache[metadataBytes32]
|
||||
|
@ -46,8 +46,10 @@ export const fetchAllDappsAction = () => {
|
||||
Database.creditDapp(dappModel)
|
||||
}
|
||||
}
|
||||
for (let i = N - 1; i >= 1; i -= 1) {
|
||||
dappSource = await discoverService.getDAppByIndexWithMetadata(i)
|
||||
|
||||
const allDapps = await discoverService.getAllDappsWithMetadata();
|
||||
for (let i = 0; i < allDapps.length; i++) {
|
||||
dappSource = allDapps[i];
|
||||
if (dappSource !== null) {
|
||||
const dappModel = DappModel.instanceFromBlockchainWithMetadata(
|
||||
dappSource,
|
||||
@ -62,6 +64,22 @@ export const fetchAllDappsAction = () => {
|
||||
}
|
||||
}
|
||||
}
|
||||
// for (let i = N - 1; i >= 1; i -= 1) {
|
||||
// dappSource = await discoverService.getDAppByIndexWithMetadata(i)
|
||||
// if (dappSource !== null) {
|
||||
// const dappModel = DappModel.instanceFromBlockchainWithMetadata(
|
||||
// dappSource,
|
||||
// )
|
||||
// dappState = dappState.creditDapp(dappModel)
|
||||
// if (
|
||||
// dappModel.id !== transactionStatus.dappId ||
|
||||
// transactionStatus.type !== TYPE_SUBMIT
|
||||
// ) {
|
||||
// dispatch(onUpdateDappsAction(dappState))
|
||||
// Database.creditDapp(dappModel)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
} catch (e) {
|
||||
console.log('error', e)
|
||||
// setTimeout(() => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user