Skip to content

Commit

Permalink
feat(cardano-graphql-provider): create cardano-graphql-provider package
Browse files Browse the repository at this point in the history
  • Loading branch information
sweeetland committed Aug 20, 2021
1 parent 369307d commit 096225f
Show file tree
Hide file tree
Showing 17 changed files with 483 additions and 17 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ module.exports = {
"no-useless-constructor": 0,
"quotes": ["error", "single", { "avoidEscape": true }],
"unicorn/filename-case": 0,
"@typescript-eslint/ban-types": 0
"@typescript-eslint/ban-types": 0,
"template-tag-spacing": 0,
"no-magic-numbers": 0
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"testnet:down": "docker-compose -p sdk-testnet down"
},
"lint-staged": {
"*(apps/**/*.{js,ts}|packages/!golden-test-generator/*.{js,ts})": [
"*(apps/**/*.{js,ts}|packages/!(*golden-test-generator)/**/*.{js,ts})": [
"yarn lint"
]
},
Expand Down
2 changes: 2 additions & 0 deletions packages/cardano-graphql-provider/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
1 change: 1 addition & 0 deletions packages/cardano-graphql-provider/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Cardano JS SDK | Cardano Graphql Provider
27 changes: 27 additions & 0 deletions packages/cardano-graphql-provider/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@cardano-sdk/cardano-graphql-provider",
"version": "0.1.0",
"description": "Graphql provider for Cardano",
"engines": {
"node": "^14"
},
"main": "dist/index.js",
"repository": "https://github.com/input-output-hk/cardano-js-sdk/packages/cardano-graphql-provider",
"author": "James Sweetland",
"license": "MPL-2.0",
"scripts": {
"build": "tsc --build ./src",
"cleanup": "shx rm -rf dist node_modules",
"lint": "eslint --ignore-path ../../.eslintignore \"**/*.ts\"",
"test": "jest -c ./test/jest.config.js"
},
"devDependencies": {
"shx": "^0.3.3"
},
"dependencies": {
"@cardano-graphql/client-ts": "^5.1.0-beta.1",
"@cardano-ogmios/client": "^4.0.0-beta.6",
"graphql": "^15.5.1",
"graphql-request": "^3.5.0"
}
}
7 changes: 7 additions & 0 deletions packages/cardano-graphql-provider/src/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
"extends": ["../../../.eslintrc.js"],
"parserOptions": {
"project": "./tsconfig.json",
"tsconfigRootDir": __dirname
}
}
155 changes: 155 additions & 0 deletions packages/cardano-graphql-provider/src/cardanoGraphqlProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { CardanoProvider } from '@cardano-sdk/core';
import { gql, GraphQLClient } from 'graphql-request';
import { TransactionSubmitResponse } from '@cardano-graphql/client-ts';
import { Schema as Cardano } from '@cardano-ogmios/client';

export const cardanoGraphqlProvider = (uri: string): CardanoProvider => {
const client = new GraphQLClient(uri);

const submitTx: CardanoProvider['submitTx'] = async (signedTransaction) => {
try {
const mutation = gql`
mutation ($transaction: String!) {
submitTransaction(transaction: $transaction) {
hash
}
}
`;

type Response = TransactionSubmitResponse;
type Variables = { transaction: string };

const response = await client.request<Response, Variables>(mutation, { transaction: signedTransaction });

return !!response.hash;
} catch {
return false;
}
};

const utxo: CardanoProvider['utxo'] = async (addresses) => {
const query = gql`
query ($addresses: [String]!) {
utxos(where: { address: { _in: $addresses } }) {
transaction {
hash
}
index
address
value # coins
tokens {
asset {
assetId # asset key
}
quantity
}
}
}
`;

type Utxo = {
transaction: { hash: Cardano.Hash16 };
index: number;
address: Cardano.Address;
value: Cardano.Lovelace;
tokens: {
asset: {
assetId: string;
};
quantity: Cardano.AssetQuantity;
}[];
};
type Response = { utxos: Utxo[] };
type Variables = { addresses: string[] };

const response = await client.request<Response, Variables>(query, { addresses });

return response.utxos.map((uxto) => {
const assets: Cardano.Value['assets'] = {};

for (const t of uxto.tokens) assets[t.asset.assetId] = t.quantity;

return [
{ txId: uxto.transaction.hash, index: uxto.index },
{ address: uxto.address, value: { coins: uxto.value, assets } }
];
});
};

const queryTransactionsByAddresses: CardanoProvider['queryTransactionsByAddresses'] = async (addresses) => {
const query = gql`
query ($addresses: [String]!) {
transactions(
where: { _or: [{ inputs: { address: { _in: $addresses } } }, { outputs: { address: { _in: $addresses } } }] }
) {
hash
inputs {
txHash
sourceTxIndex
}
outputs {
address
value
}
}
}
`;

type Response = {
transactions: {
hash: Cardano.Hash16;
inputs: { txHash: Cardano.Hash16; sourceTxIndex: number }[];
outputs: Cardano.TxOut[];
}[];
};
type Variables = { addresses: string[] };

const response = await client.request<Response, Variables>(query, { addresses });

return response.transactions.map((t) => ({
...t,
inputs: t.inputs.map((index) => ({ txId: index.txHash, index: index.sourceTxIndex }))
}));
};

const queryTransactionsByHashes: CardanoProvider['queryTransactionsByHashes'] = async (hashes) => {
const query = gql`
query ($hashes: [Hash32Hex]!) {
transactions(where: { hash: { _in: $hashes } }) {
hash
inputs {
txHash
sourceTxIndex
}
outputs {
address
value
}
}
}
`;

type Response = {
transactions: {
hash: Cardano.Hash16;
inputs: { txHash: Cardano.Hash16; sourceTxIndex: number }[];
outputs: Cardano.TxOut[];
}[];
};
type Variables = { hashes: string[] };

const response = await client.request<Response, Variables>(query, { hashes });

return response.transactions.map((t) => ({
...t,
inputs: t.inputs.map((index) => ({ txId: index.txHash, index: index.sourceTxIndex }))
}));
};

return {
submitTx,
utxo,
queryTransactionsByAddresses,
queryTransactionsByHashes
};
};
1 change: 1 addition & 0 deletions packages/cardano-graphql-provider/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './cardanoGraphqlProvider';
8 changes: 8 additions & 0 deletions packages/cardano-graphql-provider/src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"outDir": "../dist/",
"rootDir": "."
},
"references": [{ "path": "../../core/src" }]
}
7 changes: 7 additions & 0 deletions packages/cardano-graphql-provider/test/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
"extends": ["../../../test/.eslintrc.js"],
"parserOptions": {
"project": "./tsconfig.json",
"tsconfigRootDir": __dirname
}
}
Loading

0 comments on commit 096225f

Please sign in to comment.