-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding /utils/tcrs to interact with tcrs and map3 kleros listener APIs
- Loading branch information
1 parent
5399ed6
commit caf4a01
Showing
3 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface TcrCheckResult { | ||
inTcr: boolean, | ||
ipfsUri: string, | ||
resolutionTxHash: string | ||
} |