Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🎸 Country Code Mapping Table Functionality #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
},
"dependencies": {
"fastify": "^4.26.2",
"i18n-iso-countries": "^7.11.0",
"ts-node": "^10.9.2"
}
}
16 changes: 10 additions & 6 deletions src/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ function datasetCommand(yargs: yargs.Argv<{}>): {
},
}
)
.command("getAllCountriesCallingCode", "Get all countries calling code")
.command("isWinner", "The account is the dataset auditor winner", {
datasetId: {
description: "Dataset Id",
Expand Down Expand Up @@ -553,6 +554,15 @@ async function dataset(
path: String(argv.path),
})
break
case "getDatasetState":
await new DatasetMetadatas().getDatasetState({
context,
datasetId: Number(argv.datasetId),
})
break
case "getAllCountriesCallingCode":
await new DatasetMetadatas().getAllCountriesCallingCode()
break
case "submitDatasetProof":
await new DatasetProofs().submitDatasetProof({
context,
Expand Down Expand Up @@ -601,12 +611,6 @@ async function dataset(
path: String(argv.path),
})
break
case "getDatasetState":
await new DatasetMetadatas().getDatasetState({
context,
datasetId: Number(argv.datasetId),
})
break
default:
console.log("Unknown command.")
}
Expand Down
53 changes: 43 additions & 10 deletions src/dataset/metadata/repo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
********************************************************************************/

import fs from "fs"
import * as countries from "i18n-iso-countries"
import { DatasetState } from "@dataswapjs/dataswapjs"
import { handleEvmError, logMethodCall } from "../../../shared/utils/utils"
import { chainSuccessInterval, blockPeriod } from "../../../shared/constant"
Expand Down Expand Up @@ -192,14 +193,13 @@ export class DatasetMetadatas {
fs.readFileSync(options.path).toString()
) as DatasetReplicaRequirements

const state = await handleEvmError(
options.context.evm.datasetMetadata.getDatasetState(
datasetReplicaRequirements.datasetId
)
)
if (state != DatasetState.MetadataSubmitted) {
console.log("Dataset state is not MetadataSubmitted, do nothing~")
return true
if (
!(await this.checkSubmissionRequirementsCriteria({
context: options.context,
datasetReplicaRequirements,
}))
) {
return false
}

options.context.evm.datasetRequirement
Expand All @@ -211,8 +211,8 @@ export class DatasetMetadatas {
datasetReplicaRequirements.dataPreparers,
datasetReplicaRequirements.storageProviders,
datasetReplicaRequirements.regions,
datasetReplicaRequirements.countrys,
datasetReplicaRequirements.citys,
datasetReplicaRequirements.countries,
datasetReplicaRequirements.cities,
datasetReplicaRequirements.amount
)
)
Expand All @@ -236,4 +236,37 @@ export class DatasetMetadatas {
)
)
}

@logMethodCall(["context"])
async getAllCountriesCallingCode(): Promise<{
[numericKey: string]: string
}> {
return countries.getNumericCodes()
}

private async checkSubmissionRequirementsCriteria(options: {
context: Context
datasetReplicaRequirements: DatasetReplicaRequirements
}): Promise<boolean> {
const state = await handleEvmError(
options.context.evm.datasetMetadata.getDatasetState(
options.datasetReplicaRequirements.datasetId
)
)
if (state != DatasetState.MetadataSubmitted) {
console.log("Dataset state is not MetadataSubmitted, do nothing~")
return false
}
for (const country of options.datasetReplicaRequirements.countries) {
if (!countries.isValid(Number(country))) {
console.log(
"Dataset replic requirements countries invalid:",
country
)
return false
}
}

return true
}
}
4 changes: 2 additions & 2 deletions src/dataset/metadata/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export interface DatasetReplicaRequirements {
dataPreparers: string[][]
storageProviders: string[][]
regions: bigint[]
countrys: bigint[]
citys: bigint[][]
countries: bigint[]
cities: bigint[][]
amount: bigint
}
16 changes: 8 additions & 8 deletions testdata/datasetReplicaRequirements.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@
3474,
4211
],
"countrys": [
517,
1300,
2040,
3063,
4687
"countries": [
840,
528,
508,
504,
533
],
"citys": [
"cities": [
[
202186,
1302254,
Expand Down Expand Up @@ -95,5 +95,5 @@
]
],
"amount": 0,
"datasetId": 16
"datasetId": 17
}
Loading