Skip to content

Commit

Permalink
Merge pull request #1 from ThierryM1212/node-client
Browse files Browse the repository at this point in the history
Node client
  • Loading branch information
ThierryM1212 authored Nov 4, 2023
2 parents 6c15dc3 + 47445df commit 19ffa6c
Show file tree
Hide file tree
Showing 12 changed files with 2,668 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/_test-vectors/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./mockedBoxes";
export * from "./mockedTransactions";
export * from "./nodeMocks";
1,648 changes: 1,648 additions & 0 deletions packages/_test-vectors/nodeMocks.ts

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions packages/node-client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# @fleet-sdk/node-client

## 0.1.0
- initial version
21 changes: 21 additions & 0 deletions packages/node-client/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 ThierryM1212

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/node-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# @fleet-sdk/node-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/node-client)](https://www.npmjs.com/package/@fleet-sdk/node-client)

Blockchain data client for Ergo node with additional indexes enabled (ergo.node.extraIndex = true).

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

## Test
```sh
pnpm test:unit node-client
```
45 changes: 45 additions & 0 deletions packages/node-client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "@fleet-sdk/node-client",
"version": "0.1.0",
"description": "Node client for Fleet SDK.",
"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": false,
"repository": "fleet-sdk/fleet",
"license": "MIT",
"publishConfig": {
"access": "public",
"provenance": true
},
"keywords": [
"ergo",
"blockchain",
"node client"
],
"dependencies": {
"@fleet-sdk/common": "workspace:^",
"@fleet-sdk/core": "workspace:^",
"@fleet-sdk/serializer": "workspace:^"
},
"scripts": {
"build": "tsup --config ../../tsup.config.ts"
},
"engines": {
"node": ">=14"
},
"files": [
"src",
"dist",
"!**/*.spec.*",
"!**/*.json",
"!tests",
"CHANGELOG.md",
"LICENSE",
"README.md"
]
}
1 change: 1 addition & 0 deletions packages/node-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./nodeClient";
275 changes: 275 additions & 0 deletions packages/node-client/src/nodeClient.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
import { BlockHeader, SignedTransaction } from "@fleet-sdk/common";
import { ErgoBox } from "@fleet-sdk/core";
import {
mockCompileError,
mockCompileScript,
mockGetBalance,
mockGetBoxById,
mockIndexedHeight,
mockLastHeaders,
mockNodeError,
mockNodeInfo,
mockPostTxSuccess,
mockTokenInfo,
mockTransactionList,
mockUTXOByAddress
} from "_test-vectors";
import { afterEach, describe, expect, it, vi } from "vitest";
import { getNodeClient } from "./nodeClient";

import * as rest from "./utils/rest";

describe("Test node client", async () => {
afterEach(() => {
vi.restoreAllMocks();
});

const nodeClient = getNodeClient("https://test0.com:9053/");

const testOptions = {
url: "https://test0.com:9053/"
};
it("nodeOptions", async () => {
nodeClient.nodeOptions = testOptions;
expect(nodeClient.nodeOptions).toEqual(testOptions);
});

it("getHeight", async () => {
vi.spyOn(rest, "get").mockImplementation(() => Promise.resolve(mockIndexedHeight));
const height = await nodeClient.getCurrentHeight();
expect(height).toBe(1125453);
const indexedHeight = await nodeClient.getIndexedHeight();
expect(indexedHeight).toBe(1125453);
});

it("getLastHeaders", async () => {
vi.spyOn(rest, "get").mockImplementation(() => Promise.resolve(mockLastHeaders));
const headerArray = await nodeClient.getLastHeaders();
expect(headerArray).toBeInstanceOf(Array<BlockHeader>);
expect(headerArray.length).toBe(10);
});

it("addressHasTransactions - true", async () => {
vi.spyOn(rest, "post").mockImplementation(() => Promise.resolve(mockTransactionList));
const addressWithTx = await nodeClient.addressHasTransactions(
"9g16ZMPo22b3qaRL7HezyQt2HSW2ZBF6YR3WW9cYQjgQwYKxxoT"
);
expect(addressWithTx).toBe(true);
});

it("getTransactionsByAddress", async () => {
vi.spyOn(rest, "post").mockImplementation(() => Promise.resolve(mockTransactionList));
const tx = await nodeClient.getTransactionsByAddress(
"9g16ZMPo22b3qaRL7HezyQt2HSW2ZBF6YR3WW9cYQjgQwYKxxoT",
5
);
expect(tx).toBeInstanceOf(Array<SignedTransaction>);
expect(tx.length).toBe(5);
});

it("addressHasTransactions - false", async () => {
vi.spyOn(rest, "post").mockImplementation(() => Promise.resolve([]));
const addressWithoutTx = await nodeClient.addressHasTransactions(
"9g16ZMPo22b3qaRL7HezyQt2HSW2ZBF6YR3WW9cYQjgQwYKxxoT"
);
expect(addressWithoutTx).toBe(false);
});

it("getBoxByBoxId", async () => {
vi.spyOn(rest, "get").mockImplementation(() => Promise.resolve(mockGetBoxById));
const box = await nodeClient.getBoxByBoxId(
"45ce2cd800136a44f2cbf8b48472c7585cd37530de842823684b38a0ffa317a6"
);
expect(box).toBeInstanceOf(ErgoBox);
});

it("getBoxByIndex", async () => {
vi.spyOn(rest, "get").mockImplementation(() => Promise.resolve(mockGetBoxById));
const box = await nodeClient.getBoxByIndex(33263731);
expect(box).toBeInstanceOf(ErgoBox);
});

it("getTransactionByTransactionId", async () => {
vi.spyOn(rest, "get").mockImplementation(() => Promise.resolve(mockTransactionList.items[0]));
const tx = await nodeClient.getTransactionByTransactionId(
"fa467416eca865a46b686059ad7c293afd08b1d429a393137ea99600d475ae9a"
);
expect(tx.id).toBeDefined();
});

it("getTransactionByTransactionId", async () => {
vi.spyOn(rest, "get").mockImplementation(() => Promise.resolve(mockTransactionList.items[0]));
const tx = await nodeClient.getTransactionByIndex(5631288);
expect(tx.id).toBeDefined();
});

it("getBalanceByAddress - confirmed", async () => {
vi.spyOn(rest, "post").mockImplementation(() => Promise.resolve(mockGetBalance));
const bal = await nodeClient.getBalanceByAddress(
"9g16ZMPo22b3qaRL7HezyQt2HSW2ZBF6YR3WW9cYQjgQwYKxxoT"
);
expect(bal.nanoERG).toBeDefined();
});

it("getBalanceByAddress - unconfirmed", async () => {
vi.spyOn(rest, "post").mockImplementation(() => Promise.resolve(mockGetBalance));
const bal2 = await nodeClient.getBalanceByAddress(
"9g16ZMPo22b3qaRL7HezyQt2HSW2ZBF6YR3WW9cYQjgQwYKxxoT",
false
);
expect(bal2.nanoERG).toBeDefined();
});

it("getTokenInfo", async () => {
vi.spyOn(rest, "get").mockImplementation(() => Promise.resolve(mockTokenInfo));
const tokenInfo = await nodeClient.getTokenInfo(
"fbbaac7337d051c10fc3da0ccb864f4d32d40027551e1c3ea3ce361f39b91e40"
);
expect(tokenInfo.name).toBe("kushti");
});

it("getNodeInfo", async () => {
vi.spyOn(rest, "get").mockImplementation(() => Promise.resolve(mockNodeInfo));
const nodeInfo = await nodeClient.getNodeInfo();
expect(nodeInfo.appVersion).toBeDefined();
});

it("compileErgoscript - success", async () => {
vi.spyOn(rest, "post").mockImplementation(() => Promise.resolve(mockCompileScript));
const compiled = await nodeClient.compileErgoscript("HEIGHT > 100");
if (compiled.address) {
expect(compiled.address).toBe("5yE8zxMTsrGEPw5WM5ET");
} else {
expect(true).toBe(false);
}
});

it("compileErgoscript - error", async () => {
vi.spyOn(rest, "post").mockImplementation(() => Promise.resolve(mockCompileError));
const compileError = await nodeClient.compileErgoscript("HEIHT > 100");
if (compileError.error) {
expect(compileError.error).toBe(400);
} else {
expect(true).toBe(false);
}
});

it("getUnspentBoxesByAddress", async () => {
vi.spyOn(rest, "post").mockImplementation(() => Promise.resolve(mockUTXOByAddress));
const utxos = await nodeClient.getUnspentBoxesByAddress(
"9g16ZMPo22b3qaRL7HezyQt2HSW2ZBF6YR3WW9cYQjgQwYKxxoT"
);
expect(utxos).toBeInstanceOf(Array<ErgoBox>);
});
it("getUnspentBoxesByErgotree", async () => {
vi.spyOn(rest, "post").mockImplementation(() => Promise.resolve(mockUTXOByAddress));
const utxos = await nodeClient.getUnspentBoxesByErgotree(
"0008cd03b4cf5eb18d1f45f73472bc96578a87f6d967015c59c636c7a0b139348ce826b0"
);
expect(utxos).toBeInstanceOf(Array<ErgoBox>);
});
it("getBoxesByAddress", async () => {
vi.spyOn(rest, "post").mockImplementation(() => Promise.resolve({ items: mockUTXOByAddress }));
const utxos = await nodeClient.getBoxesByAddress(
"9g16ZMPo22b3qaRL7HezyQt2HSW2ZBF6YR3WW9cYQjgQwYKxxoT"
);
expect(utxos).toBeInstanceOf(Array<ErgoBox>);
});
it("getBoxesByErgotree", async () => {
vi.spyOn(rest, "post").mockImplementation(() => Promise.resolve({ items: mockUTXOByAddress }));
const utxos = await nodeClient.getBoxesByErgotree(
"0008cd03b4cf5eb18d1f45f73472bc96578a87f6d967015c59c636c7a0b139348ce826b0"
);
expect(utxos).toBeInstanceOf(Array<ErgoBox>);
});

it("sendTransaction - success", async () => {
vi.spyOn(rest, "post").mockImplementation(() => Promise.resolve(mockPostTxSuccess));
const txId = await nodeClient.sendTransaction({
inputs: [],
outputs: [],
dataInputs: [],
id: ""
});
expect(txId).toEqual({
transactionId: "18b11ee7adbb1e2b837052d7f28df3c50ffb257c31447b051eac21b74780d842"
});
const txId2 = await nodeClient.checkTransaction({
inputs: [],
outputs: [],
dataInputs: [],
id: ""
});
expect(txId2).toEqual({
transactionId: "18b11ee7adbb1e2b837052d7f28df3c50ffb257c31447b051eac21b74780d842"
});
});

it("sendTransaction - error", async () => {
vi.spyOn(rest, "post").mockImplementation(() => Promise.resolve(mockNodeError));
const txError = await nodeClient.sendTransaction({
inputs: [],
outputs: [],
dataInputs: [],
id: ""
});
expect(txError.error).toBe(400);
const txError2 = await nodeClient.checkTransaction({
inputs: [],
outputs: [],
dataInputs: [],
id: ""
});
expect(txError2.error).toBe(400);
});

it("getUnconfirmedTransactions", async () => {
vi.spyOn(rest, "get").mockImplementation(() => Promise.resolve(mockTransactionList.items));
const unconfirmedTx = await nodeClient.getUnconfirmedTransactions();
expect(unconfirmedTx).toBeInstanceOf(Array<SignedTransaction>);
});

it("getUnconfirmedTransactionsByTransactionId - success", async () => {
vi.spyOn(rest, "get").mockImplementation(() => Promise.resolve(mockTransactionList.items[0]));
const tx = await nodeClient.getUnconfirmedTransactionsByTransactionId(
"18b11ee7adbb1e2b837052d7f28df3c50ffb257c31447b051eac21b74780d842"
);
if (tx) {
expect(tx.id).toBeDefined();
} else {
expect(true).toBe(false);
}
});

it("getUnconfirmedTransactionsByTransactionId - error", async () => {
vi.spyOn(rest, "get").mockImplementation(() => Promise.resolve(mockNodeError));
const tx = await nodeClient.getUnconfirmedTransactionsByTransactionId(
"18b11ee7adbb1e2b837052d7f28df3c50ffb257c31447b051eac21b74780d842"
);
expect(tx).toBeUndefined();
});

it("getUnconfirmedTransactionsByErgoTree", async () => {
vi.spyOn(rest, "post").mockImplementation(() => Promise.resolve(mockTransactionList.items));
const unconfirmedTx = await nodeClient.getUnconfirmedTransactionsByErgoTree(
"108a010400040004000e20b10b0001e...1273860193721b738701937227738801738901"
);
expect(unconfirmedTx).toBeInstanceOf(Array<SignedTransaction>);
});

it("getUnspentBoxesByTokenId", async () => {
vi.spyOn(rest, "get").mockImplementation(() => Promise.resolve(mockUTXOByAddress));
const utxos = await nodeClient.getUnspentBoxesByTokenId(
"fbbaac7337d051c10fc3da0ccb864f4d32d40027551e1c3ea3ce361f39b91e40"
);
expect(utxos).toBeInstanceOf(Array<ErgoBox>);
});

it("getBoxesByTokenId", async () => {
vi.spyOn(rest, "get").mockImplementation(() => Promise.resolve({ items: mockUTXOByAddress }));
const utxos = await nodeClient.getBoxesByTokenId(
"fbbaac7337d051c10fc3da0ccb864f4d32d40027551e1c3ea3ce361f39b91e40"
);
expect(utxos).toBeInstanceOf(Array<ErgoBox>);
});
});
Loading

0 comments on commit 19ffa6c

Please sign in to comment.