-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cardano-graphql-provider): create cardano-graphql-provider package
- Loading branch information
1 parent
369307d
commit 096225f
Showing
17 changed files
with
483 additions
and
17 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
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
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,2 @@ | ||
node_modules | ||
dist |
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 @@ | ||
# Cardano JS SDK | Cardano Graphql Provider |
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,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" | ||
} | ||
} |
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,7 @@ | ||
module.exports = { | ||
"extends": ["../../../.eslintrc.js"], | ||
"parserOptions": { | ||
"project": "./tsconfig.json", | ||
"tsconfigRootDir": __dirname | ||
} | ||
} |
155 changes: 155 additions & 0 deletions
155
packages/cardano-graphql-provider/src/cardanoGraphqlProvider.ts
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,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 | ||
}; | ||
}; |
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 @@ | ||
export * from './cardanoGraphqlProvider'; |
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,8 @@ | ||
{ | ||
"extends": "../../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../dist/", | ||
"rootDir": "." | ||
}, | ||
"references": [{ "path": "../../core/src" }] | ||
} |
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,7 @@ | ||
module.exports = { | ||
"extends": ["../../../test/.eslintrc.js"], | ||
"parserOptions": { | ||
"project": "./tsconfig.json", | ||
"tsconfigRootDir": __dirname | ||
} | ||
} |
Oops, something went wrong.