Skip to content

Commit

Permalink
Merge pull request #27 from dataswap/feat/matching-cars-to-get-corres…
Browse files Browse the repository at this point in the history
…ponding-IDs

feat: 🎸 Add matching cars to get corresponding IDs
  • Loading branch information
siriusyim authored Mar 27, 2024
2 parents 033750f + d2ade91 commit 20e6563
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 3 deletions.
30 changes: 30 additions & 0 deletions src/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,21 @@ function matchingCommand(yargs: yargs.Argv<{}>): {
type: "string",
},
})
.command("getCarsIdsWithState", "Get cars Ids and matching state", {
replicaIndex: {
description: "Replica index",
alias: "r",
demandOption: true,
type: "number",
},

path: {
description: "cars Ids file path",
alias: "p",
demandOption: true,
type: "string",
},
})
.command("bidding", "Bidding matching", {
matchingId: {
description: "Matching Id",
Expand Down Expand Up @@ -552,6 +567,8 @@ async function dataset(
datasetId: Number(argv.datasetId),
})
break
default:
console.log("Unknown command.")
}
}

Expand Down Expand Up @@ -593,6 +610,13 @@ async function matching(
path: String(argv.path),
})
break
case "getCarsIdsWithState":
await new Matching().getCarsIdsWithState({
context,
replicaIndex: Number(argv.replicaIndex),
path: String(argv.path),
})
break
case "bidding":
await new Matching().bidding({
context,
Expand All @@ -606,6 +630,8 @@ async function matching(
matchingId: Number(argv.matchingId),
})
break
default:
console.log("Unknown command.")
}
}

Expand Down Expand Up @@ -665,6 +691,8 @@ async function storage(
matchingId: Number(argv.matchingId),
})
break
default:
console.log("Unknown command.")
}
}

Expand Down Expand Up @@ -704,5 +732,7 @@ async function finance(
})
)
break
default:
console.log("Unknown command.")
}
}
85 changes: 82 additions & 3 deletions src/matching/repo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
********************************************************************************/

import fs from "fs"
import { MatchingState } from "@dataswapjs/dataswapjs"
import { MatchingState, Car } from "@dataswapjs/dataswapjs"
import { handleEvmError, logMethodCall } from "../../shared/utils/utils"
import {
MatchingMetadataSubmitInfo,
Expand Down Expand Up @@ -143,13 +143,50 @@ export class Matching {
context: Context
path: string
}): Promise<bigint[]> {
const ids = JSON.parse(fs.readFileSync(options.path).toString())
const cars = JSON.parse(fs.readFileSync(options.path).toString())

return await handleEvmError(
options.context.evm.carstore.getCarsIds(ids.carsHash)
options.context.evm.carstore.getCarsIds(cars.carsHash)
)
}

/**
* Retrieves the IDs of cars based on the provided JSON file path.
*
* @param options An object containing the context and the path to the JSON file.
* @returns A Promise resolving to an array of BigInt values representing the car IDs.
*/
@logMethodCall(["context"])
async getCarsIdsWithState(options: {
context: Context
replicaIndex: number
path: string
}): Promise<{ matchinged: string[]; unmatching: string[] }> {
const cars = JSON.parse(fs.readFileSync(options.path).toString())

const ids: bigint[] = await handleEvmError(
options.context.evm.carstore.getCarsIds(cars.carsHash)
)
const unmatching: bigint[] = []
const matchinged: bigint[] = []

for (const id of ids) {
const car = (await handleEvmError(
options.context.evm.carstore.getCar(id)
)) as Car

if (
car.matchingIds &&
car.matchingIds[options.replicaIndex] != (undefined || 0)
) {
matchinged.push(id)
} else {
unmatching.push(id)
}
}
return await this.formatIdsWithState({ matchinged, unmatching })
}

/**
* Places a bid on a matching.
* @param options - The options object containing the context, matching ID, and bid amount.
Expand Down Expand Up @@ -189,4 +226,46 @@ export class Matching {
)
)
}

/**
* Formats the given IDs with their state (matchinged/unmatching).
* @param options The options object containing unmatching and matchinged IDs.
* @returns A Promise resolving to an object with formatted unmatching and matchinged IDs.
*/
private async formatIdsWithState(options: {
matchinged: bigint[]
unmatching: bigint[]
}): Promise<{ matchinged: string[]; unmatching: string[] }> {
const unmatchingIds: string[] = this.formatRange(options.unmatching)
const matchingedIds: string[] = this.formatRange(options.matchinged)

return { matchinged: matchingedIds, unmatching: unmatchingIds }
}

/**
* Formats the given array of IDs into ranges (e.g., [1, 2, 3, 5] => ["1-3", "5"]).
* @param ids The array of IDs to be formatted.
* @returns An array of strings representing formatted ranges of IDs.
*/
private formatRange(ids: bigint[]): string[] {
// Sort the IDs and remove duplicates
const sortedIds = Array.from(new Set(ids)).sort(
(a, b) => Number(a) - Number(b)
)
const ranges: string[] = []

let start = sortedIds[0]
let end = sortedIds[0]
for (let i = 1; i < sortedIds.length; i++) {
if (sortedIds[i] - end === BigInt(1)) {
end = sortedIds[i]
} else {
ranges.push(start === end ? `${start}` : `${start}-${end}`)
start = end = sortedIds[i]
}
}
ranges.push(start === end ? `${start}` : `${start}-${end}`)

return ranges
}
}

0 comments on commit 20e6563

Please sign in to comment.