Skip to content

Commit 096225f

Browse files
committed
feat(cardano-graphql-provider): create cardano-graphql-provider package
1 parent 369307d commit 096225f

17 files changed

+483
-17
lines changed

.eslintrc.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ module.exports = {
1919
"no-useless-constructor": 0,
2020
"quotes": ["error", "single", { "avoidEscape": true }],
2121
"unicorn/filename-case": 0,
22-
"@typescript-eslint/ban-types": 0
22+
"@typescript-eslint/ban-types": 0,
23+
"template-tag-spacing": 0,
24+
"no-magic-numbers": 0
2325
}
2426
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"testnet:down": "docker-compose -p sdk-testnet down"
2323
},
2424
"lint-staged": {
25-
"*(apps/**/*.{js,ts}|packages/!golden-test-generator/*.{js,ts})": [
25+
"*(apps/**/*.{js,ts}|packages/!(*golden-test-generator)/**/*.{js,ts})": [
2626
"yarn lint"
2727
]
2828
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Cardano JS SDK | Cardano Graphql Provider
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "@cardano-sdk/cardano-graphql-provider",
3+
"version": "0.1.0",
4+
"description": "Graphql provider for Cardano",
5+
"engines": {
6+
"node": "^14"
7+
},
8+
"main": "dist/index.js",
9+
"repository": "https://github.com/input-output-hk/cardano-js-sdk/packages/cardano-graphql-provider",
10+
"author": "James Sweetland",
11+
"license": "MPL-2.0",
12+
"scripts": {
13+
"build": "tsc --build ./src",
14+
"cleanup": "shx rm -rf dist node_modules",
15+
"lint": "eslint --ignore-path ../../.eslintignore \"**/*.ts\"",
16+
"test": "jest -c ./test/jest.config.js"
17+
},
18+
"devDependencies": {
19+
"shx": "^0.3.3"
20+
},
21+
"dependencies": {
22+
"@cardano-graphql/client-ts": "^5.1.0-beta.1",
23+
"@cardano-ogmios/client": "^4.0.0-beta.6",
24+
"graphql": "^15.5.1",
25+
"graphql-request": "^3.5.0"
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
"extends": ["../../../.eslintrc.js"],
3+
"parserOptions": {
4+
"project": "./tsconfig.json",
5+
"tsconfigRootDir": __dirname
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import { CardanoProvider } from '@cardano-sdk/core';
2+
import { gql, GraphQLClient } from 'graphql-request';
3+
import { TransactionSubmitResponse } from '@cardano-graphql/client-ts';
4+
import { Schema as Cardano } from '@cardano-ogmios/client';
5+
6+
export const cardanoGraphqlProvider = (uri: string): CardanoProvider => {
7+
const client = new GraphQLClient(uri);
8+
9+
const submitTx: CardanoProvider['submitTx'] = async (signedTransaction) => {
10+
try {
11+
const mutation = gql`
12+
mutation ($transaction: String!) {
13+
submitTransaction(transaction: $transaction) {
14+
hash
15+
}
16+
}
17+
`;
18+
19+
type Response = TransactionSubmitResponse;
20+
type Variables = { transaction: string };
21+
22+
const response = await client.request<Response, Variables>(mutation, { transaction: signedTransaction });
23+
24+
return !!response.hash;
25+
} catch {
26+
return false;
27+
}
28+
};
29+
30+
const utxo: CardanoProvider['utxo'] = async (addresses) => {
31+
const query = gql`
32+
query ($addresses: [String]!) {
33+
utxos(where: { address: { _in: $addresses } }) {
34+
transaction {
35+
hash
36+
}
37+
index
38+
address
39+
value # coins
40+
tokens {
41+
asset {
42+
assetId # asset key
43+
}
44+
quantity
45+
}
46+
}
47+
}
48+
`;
49+
50+
type Utxo = {
51+
transaction: { hash: Cardano.Hash16 };
52+
index: number;
53+
address: Cardano.Address;
54+
value: Cardano.Lovelace;
55+
tokens: {
56+
asset: {
57+
assetId: string;
58+
};
59+
quantity: Cardano.AssetQuantity;
60+
}[];
61+
};
62+
type Response = { utxos: Utxo[] };
63+
type Variables = { addresses: string[] };
64+
65+
const response = await client.request<Response, Variables>(query, { addresses });
66+
67+
return response.utxos.map((uxto) => {
68+
const assets: Cardano.Value['assets'] = {};
69+
70+
for (const t of uxto.tokens) assets[t.asset.assetId] = t.quantity;
71+
72+
return [
73+
{ txId: uxto.transaction.hash, index: uxto.index },
74+
{ address: uxto.address, value: { coins: uxto.value, assets } }
75+
];
76+
});
77+
};
78+
79+
const queryTransactionsByAddresses: CardanoProvider['queryTransactionsByAddresses'] = async (addresses) => {
80+
const query = gql`
81+
query ($addresses: [String]!) {
82+
transactions(
83+
where: { _or: [{ inputs: { address: { _in: $addresses } } }, { outputs: { address: { _in: $addresses } } }] }
84+
) {
85+
hash
86+
inputs {
87+
txHash
88+
sourceTxIndex
89+
}
90+
outputs {
91+
address
92+
value
93+
}
94+
}
95+
}
96+
`;
97+
98+
type Response = {
99+
transactions: {
100+
hash: Cardano.Hash16;
101+
inputs: { txHash: Cardano.Hash16; sourceTxIndex: number }[];
102+
outputs: Cardano.TxOut[];
103+
}[];
104+
};
105+
type Variables = { addresses: string[] };
106+
107+
const response = await client.request<Response, Variables>(query, { addresses });
108+
109+
return response.transactions.map((t) => ({
110+
...t,
111+
inputs: t.inputs.map((index) => ({ txId: index.txHash, index: index.sourceTxIndex }))
112+
}));
113+
};
114+
115+
const queryTransactionsByHashes: CardanoProvider['queryTransactionsByHashes'] = async (hashes) => {
116+
const query = gql`
117+
query ($hashes: [Hash32Hex]!) {
118+
transactions(where: { hash: { _in: $hashes } }) {
119+
hash
120+
inputs {
121+
txHash
122+
sourceTxIndex
123+
}
124+
outputs {
125+
address
126+
value
127+
}
128+
}
129+
}
130+
`;
131+
132+
type Response = {
133+
transactions: {
134+
hash: Cardano.Hash16;
135+
inputs: { txHash: Cardano.Hash16; sourceTxIndex: number }[];
136+
outputs: Cardano.TxOut[];
137+
}[];
138+
};
139+
type Variables = { hashes: string[] };
140+
141+
const response = await client.request<Response, Variables>(query, { hashes });
142+
143+
return response.transactions.map((t) => ({
144+
...t,
145+
inputs: t.inputs.map((index) => ({ txId: index.txHash, index: index.sourceTxIndex }))
146+
}));
147+
};
148+
149+
return {
150+
submitTx,
151+
utxo,
152+
queryTransactionsByAddresses,
153+
queryTransactionsByHashes
154+
};
155+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './cardanoGraphqlProvider';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../../../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "../dist/",
5+
"rootDir": "."
6+
},
7+
"references": [{ "path": "../../core/src" }]
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
"extends": ["../../../test/.eslintrc.js"],
3+
"parserOptions": {
4+
"project": "./tsconfig.json",
5+
"tsconfigRootDir": __dirname
6+
}
7+
}

0 commit comments

Comments
 (0)