Skip to content

Commit

Permalink
Add GraphQL client (#56)
Browse files Browse the repository at this point in the history
* [ergo-graphql client] add initial package structure (#39)

* add initial package structure

* fix jsdoc grammar

* fix lockfile

* add gql client

* basic implementation of gql-client

* add pnpm-lock file

* fix comments

* add mutation implementation

* Update packages/graphql-client/src/graphqlClient.ts

Co-authored-by: Alison Robson <87387688+arobsn@users.noreply.github.com>

* fix comments

* add internal graphql client

* remove urql

* complete tests

* add more tests

* lint code

* fix coverage

---------

Co-authored-by: Alison Robson <87387688+arobsn@users.noreply.github.com>
  • Loading branch information
SepehrGanji and arobsn authored Oct 18, 2023
1 parent 80daecc commit 03b8764
Show file tree
Hide file tree
Showing 20 changed files with 4,051 additions and 1,297 deletions.
19 changes: 19 additions & 0 deletions packages/common/src/error.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, expect, it } from "vitest";
import { FleetError, NotSupportedError } from "./error";

describe("Errors", () => {
it("Should construct errors", () => {
const errorMsg = "error smoke test";

expect(new FleetError().name).to.be.equal("FleetError");
expect(new NotSupportedError().name).to.be.equal("NotSupportedError");

expect(() => {
throw new FleetError(errorMsg);
}).to.throw(FleetError, errorMsg);

expect(() => {
throw new NotSupportedError(errorMsg);
}).to.throw(NotSupportedError, errorMsg);
});
});
14 changes: 14 additions & 0 deletions packages/common/src/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export class FleetError extends Error {
constructor(message?: string) {
super(message);

Object.setPrototypeOf(this, new.target.prototype);
this.name = new.target.name;
}
}

export class NotSupportedError extends FleetError {
constructor(message?: string) {
super(message);
}
}
1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./utils";
export * from "./types";
export * from "./models";
export * from "./error";
8 changes: 4 additions & 4 deletions packages/common/src/types/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ export type BlockHeader = {
difficulty: string;
parentId: BlockHeaderId;
votes: string;
size: number;
extensionId: HexString;
transactionsId: HexString;
adProofsId: HexString;
size?: number;
extensionId?: HexString;
transactionsId?: HexString;
adProofsId?: HexString;
};

export type Block = {
Expand Down
65 changes: 65 additions & 0 deletions packages/common/src/types/chainClients.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { RequireAtLeastOne } from "type-fest";
import { BlockHeader } from "./block";
import { Box } from "./boxes";
import { HexString } from "./common";
import { SignedTransaction, TransactionId, UnsignedTransaction } from "./transactions";

export type QueryBase = {
take?: number;
skip?: number;
};

export type BoxQuery<W extends BoxWhere> = {
where: RequireAtLeastOne<W>;

/**
* Determines if it should include unspent boxes from the mempool.
* @default true
*/
includeUnconfirmed?: boolean;
} & QueryBase;

export type BoxWhere = {
/** Base16-encoded BoxId */
boxId?: HexString;

/** Base16-encoded ErgoTree or Base58-encoded address */
contract?: HexString;

/** Base16-encoded contract template */
template?: HexString;

/** Base16-encoded TokenId */
tokenId?: HexString;
};

export type ChainClientBox = Box<bigint> & {
confirmed: boolean;
};

export interface IChainDataClient<B extends BoxWhere> {
/**
* Get unspent boxes from the blockchain.
*/
getUnspentBoxes(query: BoxQuery<B>): Promise<ChainClientBox[]>;

/**
* Get the last `n` block headers from the blockchain.
*/
getLastHeaders(count: number): Promise<BlockHeader[]>;

/**
* Check for transaction validity without broadcasting it to the network.
*/
checkTransaction(transaction: SignedTransaction): Promise<boolean>;

/**
* Broadcast a transaction to the network.
*/
submitTransaction(transaction: SignedTransaction): Promise<TransactionId>;

/**
* Evaluate a transaction and return Base16-encoded evaluation result.
*/
reduceTransaction(transaction: UnsignedTransaction): Promise<HexString>;
}
1 change: 1 addition & 0 deletions packages/common/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from "./registers";
export * from "./token";
export * from "./transactions";
export * from "./block";
export * from "./chainClients";
21 changes: 21 additions & 0 deletions packages/graphql-client/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Nautilus Team

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
13 changes: 13 additions & 0 deletions packages/graphql-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# @fleet-sdk/graphql-client [![License](https://badgen.net/github/license/fleet-sdk/fleet/)](https://github.com/fleet-sdk/fleet/blob/master/LICENSE) [![npm](https://badgen.net/npm/v/@fleet-sdk/graphql-client)](https://www.npmjs.com/package/@fleet-sdk/graphql-client)

Blockchain data client for ergo-graphql.

## Build
```sh
pnpm --filter graphql-client build
```

## Test
```sh
pnpm test:unit graphql-client
```
47 changes: 47 additions & 0 deletions packages/graphql-client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@fleet-sdk/graphql-client",
"version": "0.0.0",
"description": "Blockchain data client for ergo-graphql",
"main": "./dist/index.cjs.js",
"module": "./dist/index.esm.js",
"types": "./dist/index.d.ts",
"exports": {
"require": "./dist/index.cjs.js",
"import": "./dist/index.esm.js"
},
"sideEffects": true,
"repository": "fleet-sdk/fleet",
"license": "MIT",
"publishConfig": {
"access": "public",
"provenance": true
},
"keywords": [
"ergo",
"blockchain",
"crypto"
],
"scripts": {
"build": "tsup --config ../../tsup.config.ts"
},
"engines": {
"node": ">=14"
},
"dependencies": {
"@fleet-sdk/common": "workspace:^",
"@fleet-sdk/core": "workspace:^"
},
"files": [
"src",
"dist",
"!**/*.spec.*",
"!**/*.json",
"!tests",
"CHANGELOG.md",
"LICENSE",
"README.md"
],
"devDependencies": {
"@ergo-graphql/types": "^0.5.0"
}
}
Loading

0 comments on commit 03b8764

Please sign in to comment.