-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathqueryChainGraph.ts
72 lines (69 loc) · 2.01 KB
/
queryChainGraph.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { ChaingraphClient, graphql } from "chaingraph-ts";
const chaingraphUrl = "https://gql.chaingraph.pat.mn/v1/graphql";
const chaingraph = new ChaingraphClient(chaingraphUrl);
export async function getTapSwapOrigin(txidParent:string) {
const queryParentTxId = graphql(`query parentTxId(
$txidParent: bytea
){
transaction(
where: {
hash: {
_eq: $txidParent
}
}
) {
inputs {
outpoint_transaction_hash,
outpoint_index
}
}
}`);
const result = await chaingraph.query(queryParentTxId, {txidParent: `\\x${txidParent}`});
if (!result.data) throw new Error('Error in ChainGraph query parentTxId');
const outpoint = result.data.transaction[0].inputs[0]
const txid = outpoint.outpoint_transaction_hash.slice(2);
const vout = outpoint.outpoint_index
const getLockingBytecode = graphql(`query lockingBytecode(
$txid: bytea
) {
transaction(
where: {
hash: {
_eq: $txid
}
}
) {
outputs {
locking_bytecode
}
}
}`);
const result2 = await chaingraph.query(getLockingBytecode, {txid: `\\x${txid}`});
if (!result2.data) throw new Error('Error in ChainGraph query lockingBytecode');
return result2.data.transaction[0].outputs[+vout].locking_bytecode;
}
export async function queryNftAddresses(tokenId:string, offset = 0) {
const queryNftsOnAddress = graphql(`query nftsOnAddress(
$offset: Int!
$tokenId: String
) {
output(
offset: $offset
where: {
token_category: {
_eq: $tokenId
}
_and: [
{ nonfungible_token_capability: { _eq: "none" } }
]
_not: { spent_by: {} }
}
) {
locking_bytecode,
transaction_hash
}
}`);
const queryResult = await chaingraph.query(queryNftsOnAddress, {tokenId: `\\x${tokenId}`, offset})
if (!queryResult.data) throw new Error('Error in ChainGraph query nftsOnAddress');
return queryResult.data;
}