Skip to content

Commit f403c1e

Browse files
committed
feat: Add ChaingraphNetworkProvider
closes CashScript#249
1 parent 78100b2 commit f403c1e

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

packages/cashscript/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"@bitauth/libauth": "^3.0.0",
4747
"@cashscript/utils": "^0.10.5",
4848
"@mr-zwets/bchn-api-wrapper": "^1.0.1",
49+
"chaingraph-ts": "^0.2.3",
4950
"delay": "^5.0.0",
5051
"electrum-cash": "^2.0.10",
5152
"fast-deep-equal": "^3.1.3",
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { ChaingraphClient } from 'chaingraph-ts';
2+
import { Utxo, Network } from '../interfaces.js';
3+
import NetworkProvider from './NetworkProvider.js';
4+
5+
export default class ChaingraphNetworkProvider implements NetworkProvider {
6+
private client: ChaingraphClient;
7+
8+
constructor(
9+
public network: Network,
10+
chaingraphOrUrl: ChaingraphClient | string
11+
) {
12+
if (chaingraphOrUrl instanceof ChaingraphClient) {
13+
this.client = chaingraphOrUrl;
14+
} else if (typeof chaingraphOrUrl === 'string') {
15+
this.client = new ChaingraphClient(chaingraphOrUrl);
16+
} else {
17+
throw new Error(
18+
'Invalid parameter. Must be an instance of ChaingraphClient or a chaingraph url'
19+
);
20+
}
21+
}
22+
23+
async getUtxos(address: string): Promise<Utxo[]> {
24+
const result = await this.client.getUtxosForAddress(address);
25+
26+
const utxos: Utxo[] = result.map((utxo) => ({
27+
txid: utxo.transaction_hash,
28+
vout: Number(utxo.output_index),
29+
satoshis: BigInt(utxo.value_satoshis),
30+
token: utxo.token_category
31+
? {
32+
category: utxo.token_category,
33+
amount: BigInt(utxo.fungible_token_amount!),
34+
nft: utxo.nonfungible_token_commitment
35+
? {
36+
capability: utxo.nonfungible_token_capability!,
37+
commitment: utxo.nonfungible_token_commitment,
38+
}
39+
: undefined,
40+
}
41+
: undefined,
42+
}));
43+
44+
return utxos;
45+
}
46+
47+
async getBlockHeight(): Promise<number> {
48+
return this.client.getBlockHeight();
49+
}
50+
51+
async getRawTransaction(txid: string): Promise<string> {
52+
const response = await this.client.getRawTransaction(txid);
53+
return response!;
54+
}
55+
56+
async sendRawTransaction(txHex: string): Promise<string> {
57+
const response = await this.client.sendRawTransaction(txHex);
58+
return response.send_transaction.transaction_hash;
59+
}
60+
}

packages/cashscript/src/network/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export { default as BitcoinRpcNetworkProvider } from './BitcoinRpcNetworkProvide
33
export { default as ElectrumNetworkProvider } from './ElectrumNetworkProvider.js';
44
export { default as FullStackNetworkProvider } from './FullStackNetworkProvider.js';
55
export { default as MockNetworkProvider } from './MockNetworkProvider.js';
6+
export { default as ChaingraphNetworkProvider } from './ChaingraphNetworkProvider.js';

yarn.lock

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
# yarn lockfile v1
33

44

5+
"@0no-co/graphql.web@^1.0.5":
6+
version "1.0.13"
7+
resolved "https://registry.yarnpkg.com/@0no-co/graphql.web/-/graphql.web-1.0.13.tgz#978f4d3a869240f2d487fa1c1009028b34bc33b5"
8+
integrity sha512-jqYxOevheVTU1S36ZdzAkJIdvRp2m3OYIG5SEoKDw5NI8eVwkoI0D/Q3DYNGmXCxkA6CQuoa7zvMiDPTLqUNuw==
9+
10+
"@0no-co/graphqlsp@^1.12.13":
11+
version "1.12.16"
12+
resolved "https://registry.yarnpkg.com/@0no-co/graphqlsp/-/graphqlsp-1.12.16.tgz#58fe7bad53b3ad9fdf2d5f41ddeb9b418d289a03"
13+
integrity sha512-B5pyYVH93Etv7xjT6IfB7QtMBdaaC07yjbhN6v8H7KgFStMkPvi+oWYBTibMFRMY89qwc9H8YixXg8SXDVgYWw==
14+
dependencies:
15+
"@gql.tada/internal" "^1.0.0"
16+
graphql "^15.5.0 || ^16.0.0 || ^17.0.0"
17+
518
"@ampproject/remapping@^2.1.0":
619
version "2.2.0"
720
resolved "https://registry.yarnpkg.com/@ampproject/remapping/-/remapping-2.2.0.tgz#56c133824780de3174aed5ab6834f3026790154d"
@@ -957,6 +970,22 @@
957970
unique-filename "^1.1.1"
958971
which "^1.3.1"
959972

973+
"@gql.tada/[email protected]":
974+
version "1.6.3"
975+
resolved "https://registry.yarnpkg.com/@gql.tada/cli-utils/-/cli-utils-1.6.3.tgz#b893cec74908da4df0602691e2e0b1497fda8cda"
976+
integrity sha512-jFFSY8OxYeBxdKi58UzeMXG1tdm4FVjXa8WHIi66Gzu9JWtCE6mqom3a8xkmSw+mVaybFW5EN2WXf1WztJVNyQ==
977+
dependencies:
978+
"@0no-co/graphqlsp" "^1.12.13"
979+
"@gql.tada/internal" "1.0.8"
980+
graphql "^15.5.0 || ^16.0.0 || ^17.0.0"
981+
982+
"@gql.tada/[email protected]", "@gql.tada/internal@^1.0.0":
983+
version "1.0.8"
984+
resolved "https://registry.yarnpkg.com/@gql.tada/internal/-/internal-1.0.8.tgz#3ba9fa6534809788bbbe103492f70b8e9d754027"
985+
integrity sha512-XYdxJhtHC5WtZfdDqtKjcQ4d7R1s0d1rnlSs3OcBEUbYiPoJJfZU7tWsVXuv047Z6msvmr4ompJ7eLSK5Km57g==
986+
dependencies:
987+
"@0no-co/graphql.web" "^1.0.5"
988+
960989
"@humanwhocodes/config-array@^0.11.13", "@humanwhocodes/config-array@^0.11.14":
961990
version "0.11.14"
962991
resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.11.14.tgz#d78e481a039f7566ecc9660b4ea7fe6b1fec442b"
@@ -2579,6 +2608,14 @@
25792608
resolved "https://registry.yarnpkg.com/@ungap/structured-clone/-/structured-clone-1.2.0.tgz#756641adb587851b5ccb3e095daf27ae581c8406"
25802609
integrity sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==
25812610

2611+
"@urql/core@^5.0.8":
2612+
version "5.1.0"
2613+
resolved "https://registry.yarnpkg.com/@urql/core/-/core-5.1.0.tgz#7f4b81f1aba1ca34ae6354763abeb87ff9af84ff"
2614+
integrity sha512-yC3sw8yqjbX45GbXxfiBY8GLYCiyW/hLBbQF9l3TJrv4ro00Y0ChkKaD9I2KntRxAVm9IYBqh0awX8fwWAe/Yw==
2615+
dependencies:
2616+
"@0no-co/graphql.web" "^1.0.5"
2617+
wonka "^6.3.2"
2618+
25822619
"@zkochan/cmd-shim@^3.1.0":
25832620
version "3.1.0"
25842621
resolved "https://registry.yarnpkg.com/@zkochan/cmd-shim/-/cmd-shim-3.1.0.tgz#2ab8ed81f5bb5452a85f25758eb9b8681982fd2e"
@@ -3509,6 +3546,17 @@ cashaddrjs-slp@^0.2.11:
35093546
dependencies:
35103547
big-integer "^1.6.34"
35113548

3549+
chaingraph-ts@^0.2.3:
3550+
version "0.2.3"
3551+
resolved "https://registry.yarnpkg.com/chaingraph-ts/-/chaingraph-ts-0.2.3.tgz#0d6a029fe8a0d99934431f4eda18771297825e7e"
3552+
integrity sha512-21lJoTaXMrp0pBZX1Cl51TdIUY5OIUZ7dXYRO9jY+Tpgjkc882cQpWMZw+LPqE3sHbgXkMfQEepEwbI7Hay3CA==
3553+
dependencies:
3554+
"@bitauth/libauth" "^3.0.0"
3555+
"@urql/core" "^5.0.8"
3556+
gql.tada "^1.8.10"
3557+
graphql-ws "^5.16.0"
3558+
ws "^8.18.0"
3559+
35123560
chalk@^2.0.0, chalk@^2.3.1, chalk@^2.4.2:
35133561
version "2.4.2"
35143562
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424"
@@ -5654,6 +5702,16 @@ gopd@^1.0.1:
56545702
dependencies:
56555703
get-intrinsic "^1.1.3"
56565704

5705+
gql.tada@^1.8.10:
5706+
version "1.8.10"
5707+
resolved "https://registry.yarnpkg.com/gql.tada/-/gql.tada-1.8.10.tgz#096a1b30d3c6fc74212fe07d507f01a4095f7f67"
5708+
integrity sha512-FrvSxgz838FYVPgZHGOSgbpOjhR+yq44rCzww3oOPJYi0OvBJjAgCiP6LEokZIYND2fUTXzQAyLgcvgw1yNP5A==
5709+
dependencies:
5710+
"@0no-co/graphql.web" "^1.0.5"
5711+
"@0no-co/graphqlsp" "^1.12.13"
5712+
"@gql.tada/cli-utils" "1.6.3"
5713+
"@gql.tada/internal" "1.0.8"
5714+
56575715
graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.2.0, graceful-fs@^4.2.2:
56585716
version "4.2.4"
56595717
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.4.tgz#2256bde14d3632958c465ebc96dc467ca07a29fb"
@@ -5674,6 +5732,16 @@ graphemer@^1.4.0:
56745732
resolved "https://registry.yarnpkg.com/graphemer/-/graphemer-1.4.0.tgz#fb2f1d55e0e3a1849aeffc90c4fa0dd53a0e66c6"
56755733
integrity sha512-EtKwoO6kxCL9WO5xipiHTZlSzBm7WLT627TqC/uVRd0HKmq8NXyebnNYxDoBi7wt8eTWrUrKXCOVaFq9x1kgag==
56765734

5735+
graphql-ws@^5.16.0:
5736+
version "5.16.2"
5737+
resolved "https://registry.yarnpkg.com/graphql-ws/-/graphql-ws-5.16.2.tgz#7b0306c1bdb0e97a05e800ccd523f46fb212e37c"
5738+
integrity sha512-E1uccsZxt/96jH/OwmLPuXMACILs76pKF2i3W861LpKBCYtGIyPQGtWLuBLkND4ox1KHns70e83PS4te50nvPQ==
5739+
5740+
"graphql@^15.5.0 || ^16.0.0 || ^17.0.0":
5741+
version "16.10.0"
5742+
resolved "https://registry.yarnpkg.com/graphql/-/graphql-16.10.0.tgz#24c01ae0af6b11ea87bf55694429198aaa8e220c"
5743+
integrity sha512-AjqGKbDGUFRKIRCP9tCKiIGHyriz2oHEbPIbEtcSLSs4YjReZOIPQQWek4+6hjw62H9QShXHyaGivGiYVLeYFQ==
5744+
56775745
growly@^1.3.0:
56785746
version "1.3.0"
56795747
resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081"
@@ -10702,6 +10770,11 @@ windows-release@^3.1.0:
1070210770
dependencies:
1070310771
execa "^1.0.0"
1070410772

10773+
wonka@^6.3.2:
10774+
version "6.3.4"
10775+
resolved "https://registry.yarnpkg.com/wonka/-/wonka-6.3.4.tgz#76eb9316e3d67d7febf4945202b5bdb2db534594"
10776+
integrity sha512-CjpbqNtBGNAeyNS/9W6q3kSkKE52+FjIj7AkFlLr11s/VWGUu6a2CdYSdGxocIhIVjaW/zchesBQUKPVU69Cqg==
10777+
1070510778
word-wrap@^1.2.5:
1070610779
version "1.2.5"
1070710780
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.5.tgz#d2c45c6dd4fbce621a66f136cbe328afd0410b34"
@@ -10799,6 +10872,11 @@ ws@^7.5.2:
1079910872
resolved "https://registry.yarnpkg.com/ws/-/ws-7.5.9.tgz#54fa7db29f4c7cec68b1ddd3a89de099942bb591"
1080010873
integrity sha512-F+P9Jil7UiSKSkppIiD94dN07AwvFixvLIj1Og1Rl9GGMuNipJnV9JzjD6XuqmAeiswGvUmNLjr5cFuXwNS77Q==
1080110874

10875+
ws@^8.18.0:
10876+
version "8.18.0"
10877+
resolved "https://registry.yarnpkg.com/ws/-/ws-8.18.0.tgz#0d7505a6eafe2b0e712d232b42279f53bc289bbc"
10878+
integrity sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==
10879+
1080210880
xdg-basedir@^4.0.0:
1080310881
version "4.0.0"
1080410882
resolved "https://registry.yarnpkg.com/xdg-basedir/-/xdg-basedir-4.0.0.tgz#4bc8d9984403696225ef83a1573cbbcb4e79db13"

0 commit comments

Comments
 (0)