Skip to content

Commit

Permalink
adding /utils/tcrs to interact with tcrs and map3 kleros listener APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
pellicceama committed Oct 20, 2022
1 parent 5399ed6 commit caf4a01
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/utils/tcrs/kleros-tcr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { TokenList } from "@uniswap/token-lists";
import axios from "axios";
import { getAddressFromAssetId } from "../addresses";
import { getEnsIpfsContentHash, getIpfsContent } from "../ipfs";
import { TcrCheckResult } from "./types";

interface KlerosTcrCache {
tokenlist: TokenList,
lastUpdated: number,
ipfsHash: string
}
let cache: KlerosTcrCache = {
tokenlist: null,
lastUpdated: 0,
ipfsHash: null
}

async function needBeUpdateCache() {
if (Date.now() - cache.lastUpdated > 1000 * 60 * 60) {
const KLEROS_ENS_ENS_DOMAIN = 't2crtokens.eth';
const ipfsHash = await getEnsIpfsContentHash(KLEROS_ENS_ENS_DOMAIN);

cache.tokenlist = await getIpfsContent(ipfsHash);
cache.lastUpdated = Date.now();
}
}

export async function checkIfAssetInKlerosTCR(assetId: string): Promise<TcrCheckResult> {

try {
await needBeUpdateCache();
const address = getAddressFromAssetId(assetId);

const tokenInTcr = cache.tokenlist.tokens.some((token: any) => token.address === address);

if(!tokenInTcr) {
return {
inTcr: false,
ipfsUri: null,
resolutionTxHash: null
}
}

const query = {
query: `
query GetKlerosTcrTokenByAddress {
tokens(where: {address: ${address}}) {
address
status
ticker
name
requests(orderBy: resolutionTime, orderDirection: desc, first: 1) {
resolutionTx
}
}
}
`
};

const url = 'https://api.thegraph.com/subgraphs/name/kleros/t2cr';
const response = await axios.post(url, { query });
const token = response.data.data.tokens[0];

if(!token) {
return {
inTcr: false,
ipfsUri: null,
resolutionTxHash: null
}
}

return {
inTcr: token.status === 'Registered',
ipfsUri: cache.ipfsHash,
resolutionTxHash: token.requests[0].resolutionTx
}
} catch (e) {
throw e;
}
}
53 changes: 53 additions & 0 deletions src/utils/tcrs/map-tcrs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import axios from "axios";
import { AssetMap } from "../../model/AssetMap";
import { TcrCheckResult } from "./types";

export async function checkIfMapInMapsTcr(map: AssetMap): Promise<TcrCheckResult> {
try {
// TODO;
const url = 'https://console.map3.xyz/api/kleros/map'

const response = await axios.get(url + '/kleros-map3-map?from=' + map.from + '&to=' + map.to);

return {
inTcr: response.data.status === 'Registered',
ipfsUri: response.data.ipfs_uri,
resolutionTxHash: response.data.resolution_tx
}

} catch (err) {
// console.error('checkIfMapInMapsTcr error', err);

return {
inTcr: false,
ipfsUri: null,
resolutionTxHash: null
}
}
}


export async function checkIfAssetInMap3TCR(assetId: string): Promise<TcrCheckResult> {

try {
// TODO;
const url = 'https://console.map3.xyz/api/kleros/map'

const response = await axios.get(url + '/kleros-map3-asset?id=' + assetId);

return {
inTcr: response.data.status === 'Registered',
ipfsUri: response.data.ipfs_uri,
resolutionTxHash: response.data.resolution_tx
}

} catch (err) {
// console.error('checkIfAssetInMap3TCR error', err);

return {
inTcr: false,
ipfsUri: null,
resolutionTxHash: null
}
}
}
5 changes: 5 additions & 0 deletions src/utils/tcrs/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface TcrCheckResult {
inTcr: boolean,
ipfsUri: string,
resolutionTxHash: string
}

0 comments on commit caf4a01

Please sign in to comment.