Skip to content

Commit

Permalink
v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ndatg committed Feb 7, 2024
0 parents commit 387ce9e
Show file tree
Hide file tree
Showing 35 changed files with 9,912 additions and 0 deletions.
50 changes: 50 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"env": {
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"overrides": [
{
"env": {
"node": true
},
"files": [
".eslintrc.{js,cjs}"
],
"parserOptions": {
"sourceType": "script"
}
}
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
"indent": [
"error",
4
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"double"
],
"semi": [
"error",
"always"
],
"@typescript-eslint/no-explicit-any": 0
}
}
47 changes: 47 additions & 0 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Simple workflow for deploying static content to GitHub Pages
name: Deploy static content to Pages

on:
# Runs on pushes targeting the default branch
push:
branches: ["main"]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: true

jobs:
# Single deploy job since we're just deploying
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Node.js
uses: actions/setup-node@v3
- run: npm install
- run: npm run docs
- name: Setup Pages
uses: actions/configure-pages@v3
- name: Upload artifact
uses: actions/upload-pages-artifact@v2
# Upload entire repository
with:
path: './docs'
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v2
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/.idea/
/node_modules/
/dist/
/docs/
.DS_Store
*.iml
.env
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea/
/.github/
/examples/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 ndatg

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.
254 changes: 254 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
# ton-http-api

[![MIT License](https://img.shields.io/badge/License-MIT-green.svg)](https://choosealicense.com/licenses/mit/)

The lightweight TON typescript library includes api, subscribers, and clients for interacting with smart contracts.

## Documentation

Go to the [documentation](https://ndatg.github.io/ton-http-api/) for detailed information.

## Components

| Component | Status |
| ----------------- | ------------------------------------------------------------------ |
| [Toncenter API V2](https://toncenter.com/api/v2/) ||
| [Toncenter API V3](https://toncenter.com/api/v3/) ||
| Client for toncenter API V2 ||
| Client for toncenter API V3 ||
| Block subscriber for toncenter API V2 ||
| Block subscriber for toncenter API V3 ||


## Installation


```bash
npm install ton-http-api --save
```

## Usage/Examples

Some of the examples can be found in the [examples](https://github.com/ndatg/ton-http-api/tree/main/examples) directory.

Register your API key in the [@tonapibot](https://t.me/tonapibot) to get access with higher limits.

### Api V3

In most cases, I recommend using toncenter `TonHttpApiV3`, this is the new and improved version.

```javascript
import { TonHttpApiV3 } from "ton-http-api";

const api = new TonHttpApiV3({
endpoint: "https://toncenter.com/",
apiKey: "" // optional
});

const masterchainInfo = await api.getMasterchainInfo();
console.log(masterchainInfo);

const nftCollections = await api.getNftCollections({
collection_address: "EQCA14o1-VWhS2efqoh_9M1b_A9DtKTuoqfmkn83AbJzwnPi"
});
console.log(nftCollections);

const nftItems = await api.getNftItems({
collection_address: "EQCA14o1-VWhS2efqoh_9M1b_A9DtKTuoqfmkn83AbJzwnPi"
});
console.log(nftItems);

const jettonWallets = await api.getJettonWallets({
jetton_address: "EQBynBO23ywHy_CgarY9NK9FTz0yDsG82PtcbSTQgGoXwiuA"
});
console.log(jettonWallets);
```

### Api V2

You can also use the old toncenter `TonHttpApiV2`.

```javascript
import { TonHttpApiV2 } from "ton-http-api";

const api = new TonHttpApiV2({
endpoint: "https://toncenter.com/",
apiKey: "" // optional
});

const data = await api.getMasterchainInfo();
console.log(data);

const data = await api.getTransactions("EQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqB2N");
console.log(data);
```

### Client V2 / V3

`TonClientV2` and `TonClientV3` provide compatibility with other libraries (e.g. deploy and test smart contracts).

`TonClientV2` works in the same way as `TonClientV3`, but uses `TonHttpApiV2`.

For example, let's do a highload wallet deploy!

```bash
npm install ton-highload-wallet-contract @ton/crypto @ton/core --save
```

```javascript
import { TonClientV3 } from "ton-http-api";
import { HighloadWalletContractV2 } from "ton-highload-wallet-contract";
import { mnemonicToPrivateKey } from "@ton/crypto";
import { internal } from "@ton/core";

const client = new TonClientV3({
endpoint: "https://testnet.toncenter.com/",
apiKey: "", // optional
});

const mnemonic = [ /* ... */ ];

// create contract
const key = await mnemonicToPrivateKey(mnemonic);
const contract = client.open(HighloadWalletContractV2.create({ publicKey: key.publicKey, workchain: 0 }));
console.log(`send test coins to the address: ${contract.address}`);

// send transfer
await contract.sendTransfer({
secretKey: key.secretKey,
messages: [
internal({
to: "EQBYivdc0GAk-nnczaMnYNuSjpeXu2nJS3DZ4KqLjosX5sVC",
value: "0.2",
body: "test 1",
bounce: false,
}),
internal({
to: "EQBYivdc0GAk-nnczaMnYNuSjpeXu2nJS3DZ4KqLjosX5sVC",
value: "0.2",
body: "test 2",
bounce: false,
})
],
});

```

### Subscriber V3

You can use `TonSubscriberV3` to listen blocks in the blockchain, it uses the `TonHttpApiV3`.

The `getTransactionsByMasterchainBlock` method gets transactions from both masterchain and shardchains.

```javascript
import {
TonHttpApiV3, TonSubscriberV3,
TonMemoryBlockStorageV3, SchemaV3
} from "ton-http-api";

const api = new TonHttpApiV3({
endpoint: "https://toncenter.com/",
apiKey: "" // optional
});

const storage = new TonMemoryBlockStorageV3();

const subscriber = new TonSubscriberV3({
api: api,
storage: storage,
// logger: logger,
// masterchainTickSleepTime: 3000,
// startSeqno: 35744539
});
await subscriber.start();

subscriber.on("block", async (args: { block: SchemaV3.Block }) => {
try {

let offset = 0;
let stopped = false;
let transactions: any = [];

do {
try {
const data = await api.getTransactionsByMasterchainBlock(args.block.seqno, {
limit: 256,
offset
});
transactions = [...transactions, ...data];

if (data.length < 256) {
stopped = true;
break;
}

offset += 256;
} catch (error) {
console.log(error);
}
} while(!stopped);

console.log(`seqno: ${args.block.seqno} / transactions: ${transactions.length}`);

} catch (error) {
console.log(error);
}
});
```

### Subscriber V2

You can use `TonSubscriberV2` to listen blocks in the blockchain, it uses the `TonHttpApiV2`.

```javascript
import {
TonHttpApiV2, TonSubscriberV2,
TonMemoryBlockStorageV2, SchemaV2
} from "ton-http-api";

const api = new TonHttpApiV2({
endpoint: "https://toncenter.com/",
apiKey: "" // optional
});

const storage = new TonMemoryBlockStorageV2();

const subscriber = new TonSubscriberV2({
api: api,
storage: storage,
// logger: logger,
// masterchainTickSleepTime: 3000,
// shardchainTickSleepTime: 100,
// startSeqno: 35744539
});

subscriber.on("block", async (args: { block: SchemaV2.BlockHeader, shards?: SchemaV2.BlockShards }) => {
try {

const { workchain, shard, seqno } = args.block.id;

let stopped = false;
let transactions: any = [];
do {
try {
const data = await api.getBlockTransactions(workchain, shard, seqno, {
count: 1024,
});
transactions = [...transactions, ...data.transactions];
stopped = true;
} catch (error) {
console.log(error);
}
} while(!stopped);

console.log(`workchain: ${workchain} / seqno: ${seqno} / transactions: ${transactions.length}`);

} catch (error) {
console.log(error);
}
});
```
## Feedback

If you have any feedback, please reach out to me at telegram [@ndatg](https://t.me/ndatg).

Loading

0 comments on commit 387ce9e

Please sign in to comment.