Skip to content

Commit

Permalink
Online examples in the docs - iteration 1 (#289)
Browse files Browse the repository at this point in the history
* feat: add online block example

* fix: improve blocks example

* fix: update blocks doc

* fix: update docs

* fix: update comments

* fix: add accounts docs

* fix: fix error on await

* fix: add logs docs

* fix: add nodes docs

* fix: add transactions docs

* fix: improve logs comments

* fix: use expect instead of console.log

* fix: improve docs results
  • Loading branch information
fabiorigam authored Nov 24, 2023
1 parent ab6b23b commit 095661f
Show file tree
Hide file tree
Showing 8 changed files with 867 additions and 0 deletions.
35 changes: 35 additions & 0 deletions docs/examples/thorest-client/accounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
HttpClient,
ThorestClient
} from '@vechainfoundation/vechain-sdk-network';
import { expect } from 'expect';

// Url of the testnet network
const _testnetUrl = 'https://testnet.vechain.org/';

// Testnet network instance
const testNetwork = new HttpClient(_testnetUrl);

// Thorest client testnet instance
const thorestTestnetClient = new ThorestClient(testNetwork);

// Get account details
const accountDetails = await thorestTestnetClient.accounts.getAccount(
'0x5034aa590125b64023a0262112b98d72e3c8e40e'
);
expect(accountDetails).toBeDefined();

// Get account code
const accountCode = await thorestTestnetClient.accounts.getBytecode(
'0x5034aa590125b64023a0262112b98d72e3c8e40e'
);
expect(accountCode).toEqual('0x');

// Get account storage
const accountStorage = await thorestTestnetClient.accounts.getStorageAt(
'0x5034aa590125b64023a0262112b98d72e3c8e40e',
'0x0000000000000000000000000000000000000000000000000000000000000001'
);
expect(accountStorage).toEqual(
'0x0000000000000000000000000000000000000000000000000000000000000000'
);
49 changes: 49 additions & 0 deletions docs/examples/thorest-client/blocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
HttpClient,
ThorestClient
} from '@vechainfoundation/vechain-sdk-network';
import { expect } from 'expect';

// Url of the testnet network
const _testnetUrl = 'https://testnet.vechain.org/';

// Testnet network instance
const testNetwork = new HttpClient(_testnetUrl);

// Thorest client testnet instance
const thorestTestnetClient = new ThorestClient(testNetwork);

// Get block details
const blockDetails = await thorestTestnetClient.blocks.getBlock(1);
expect(blockDetails).toEqual({
number: 1,
id: '0x000000019015bbd98fc1c9088d793ba9add53896a29cd9aa3a4dcabd1f561c38',
size: 236,
parentID:
'0x000000000b2bce3c70bc649a02749e8687721b09ed2e15997f466536b20bb127',
timestamp: 1530014410,
gasLimit: 10000000,
beneficiary: '0xb4094c25f86d628fdd571afc4077f0d0196afb48',
gasUsed: 0,
totalScore: 1,
txsRoot:
'0x45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0',
txsFeatures: 0,
stateRoot:
'0x4ec3af0acbad1ae467ad569337d2fe8576fe303928d35b8cdd91de47e9ac84bb',
receiptsRoot:
'0x45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0',
com: false,
signer: '0x25ae0ef84da4a76d5a1dfe80d3789c2c46fee30a',
isTrunk: true,
isFinalized: true,
transactions: []
});

// Get best block details
const bestBlockDetails = await thorestTestnetClient.blocks.getBestBlock();
expect(bestBlockDetails).toBeDefined();

// Get finalizes block details
const finalBlockDetails = await thorestTestnetClient.blocks.getFinalBlock();
expect(finalBlockDetails).toBeDefined();
139 changes: 139 additions & 0 deletions docs/examples/thorest-client/logs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import {
HttpClient,
ThorestClient
} from '@vechainfoundation/vechain-sdk-network';
import { expect } from 'expect';

// Url of the testnet network
const _testnetUrl = 'https://testnet.vechain.org/';

// Testnet network instance
const testNetwork = new HttpClient(_testnetUrl);

// Thorest client testnet instance
const thorestTestnetClient = new ThorestClient(testNetwork);

// Filters event logs based on the provided criteria.
const eventLogs = await thorestTestnetClient.logs.filterEventLogs({
// Specify the range of blocks to search for events
range: {
unit: 'block',
from: 0,
to: 100000
},
// Additional options for the query, such as offset and limit
options: {
offset: 0,
limit: 3
},
// Define criteria for filtering events
criteriaSet: [
{
// Contract address to filter events
address: '0x0000000000000000000000000000456E65726779',
// Topics to further narrow down the search
topic0: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
topic1: '0x0000000000000000000000005034aa590125b64023a0262112b98d72e3c8e40e'
}
],
// Specify the order in which logs should be retrieved (ascending in this case)
order: 'asc'
});
expect(eventLogs).toEqual([
{
address: '0x0000000000000000000000000000456e65726779',
topics: [
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
'0x0000000000000000000000005034aa590125b64023a0262112b98d72e3c8e40e',
'0x0000000000000000000000005034aa590125b64023a0262112b98d72e3c8e40e'
],
data: '0x00000000000000000000000000000000000000000000124bc0ddd92e55fff280',
meta: {
blockID:
'0x000060716a6decc7127d221e8a53cd7b33992db6236490f79d47585f9ae7ca14',
blockNumber: 24689,
blockTimestamp: 1530261290,
txID: '0x0ee8df3a9de6787ec0848ea8951ed8899bb053b6b4af167228dd7c0c012f5346',
txOrigin: '0x5034aa590125b64023a0262112b98d72e3c8e40e',
clauseIndex: 0
}
},
{
address: '0x0000000000000000000000000000456e65726779',
topics: [
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
'0x0000000000000000000000005034aa590125b64023a0262112b98d72e3c8e40e',
'0x0000000000000000000000005034aa590125b64023a0262112b98d72e3c8e40e'
],
data: '0x00000000000000000000000000000000000000000000124bc0ddd92e56000000',
meta: {
blockID:
'0x00006135c993e6cd1ed99aac34679caac80759764ecb01431c9bea0199f3bf4c',
blockNumber: 24885,
blockTimestamp: 1530263250,
txID: '0x86b3364c0faf2df6365b975cf1bd8046264b1eeaa2f266fe15b2df27d7954f65',
txOrigin: '0x5034aa590125b64023a0262112b98d72e3c8e40e',
clauseIndex: 0
}
},
{
address: '0x0000000000000000000000000000456e65726779',
topics: [
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
'0x0000000000000000000000005034aa590125b64023a0262112b98d72e3c8e40e',
'0x000000000000000000000000f881a94423f22ee9a0e3e1442f515f43c966b7ed'
],
data: '0x00000000000000000000000000000000000000000000021e19e0c9bab2400000',
meta: {
blockID:
'0x000069fa97729ea3aaddd0756bb2bf2044fc16cb7d2b391b7982059deb43a86c',
blockNumber: 27130,
blockTimestamp: 1530285700,
txID: '0x9edf26009aa903e2c5e7afbb39a547c9cf324a7f3eedafc33691ce2c9e5c9541',
txOrigin: '0x5034aa590125b64023a0262112b98d72e3c8e40e',
clauseIndex: 0
}
}
]);

// Filters transfer logs based on the provided criteria.
const transferLogs = await thorestTestnetClient.logs.filterTransferLogs({
// Specify the range of blocks to search for transfer events
range: {
unit: 'block',
from: 0,
to: 100000
},
// Additional options for the query, such as offset and limit
options: {
offset: 0,
limit: 3
},
// Define criteria for filtering transfer events
criteriaSet: [
{
// Transaction origin, sender, and recipient addresses to filter transfer events
txOrigin: '0xe59d475abe695c7f67a8a2321f33a856b0b4c71d',
sender: '0xe59d475abe695c7f67a8a2321f33a856b0b4c71d',
recipient: '0x7567d83b7b8d80addcb281a71d54fc7b3364ffed'
}
],
// Specify the order in which transfer logs should be retrieved (ascending in this case)
order: 'asc'
});
expect(transferLogs).toEqual([
{
sender: '0xe59d475abe695c7f67a8a2321f33a856b0b4c71d',
recipient: '0x7567d83b7b8d80addcb281a71d54fc7b3364ffed',
amount: '0x152d02c7e14af6800000',
meta: {
blockID:
'0x00003abbf8435573e0c50fed42647160eabbe140a87efbe0ffab8ef895b7686e',
blockNumber: 15035,
blockTimestamp: 1530164750,
txID: '0x9daa5b584a98976dfca3d70348b44ba5332f966e187ba84510efb810a0f9f851',
txOrigin: '0xe59d475abe695c7f67a8a2321f33a856b0b4c71d',
clauseIndex: 0
}
}
]);
18 changes: 18 additions & 0 deletions docs/examples/thorest-client/nodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
HttpClient,
ThorestClient
} from '@vechainfoundation/vechain-sdk-network';
import { expect } from 'expect';

// Url of the testnet network
const _testnetUrl = 'https://testnet.vechain.org/';

// Testnet network instance
const testNetwork = new HttpClient(_testnetUrl);

// Thorest client testnet instance
const thorestTestnetClient = new ThorestClient(testNetwork);

// Retrieves connected peers of a node.
const peerNodes = await thorestTestnetClient.nodes.getNodes();
expect(peerNodes).toBeDefined();
80 changes: 80 additions & 0 deletions docs/examples/thorest-client/transactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {
HttpClient,
ThorestClient
} from '@vechainfoundation/vechain-sdk-network';
import { expect } from 'expect';

// Url of the testnet network
const _testnetUrl = 'https://testnet.vechain.org/';

// Testnet network instance
const testNetwork = new HttpClient(_testnetUrl);

// Thorest client testnet instance
const thorestTestnetClient = new ThorestClient(testNetwork);

// Retrieves the details of a transaction.
const transactionDetails =
await thorestTestnetClient.transactions.getTransaction(
'0x46d195f69e1ac3922d42c207e4705a3d1642883d97e58f7efc72f179ea326adb'
);
expect(transactionDetails).toEqual({
id: '0x46d195f69e1ac3922d42c207e4705a3d1642883d97e58f7efc72f179ea326adb',
chainTag: 39,
blockRef: '0x010284a0b704e751',
expiration: 2000,
clauses: [
{
to: '0x5d57f07dfeb8c224121433d5b1b401c82bd88f3d',
value: '0x2ea11e32ad50000',
data: '0x'
}
],
gasPriceCoef: 0,
gas: 41192,
origin: '0x2d4ed6b8abd00bc2ef0bdb2258a946c214d9d0af',
delegator: null,
nonce: '0x76eed751cef0e52d',
dependsOn: null,
size: 130,
meta: {
blockID:
'0x010284a1fea0635a2e47dd21f8a1761406df1013e5f4af79e311d8a27373980d',
blockNumber: 16942241,
blockTimestamp: 1699453780
}
});

// Retrieves the receipt of a transaction.
const transactionReceipt =
await thorestTestnetClient.transactions.getTransactionReceipt(
'0x46d195f69e1ac3922d42c207e4705a3d1642883d97e58f7efc72f179ea326adb'
);
expect(transactionReceipt).toEqual({
gasUsed: 21000,
gasPayer: '0x2d4ed6b8abd00bc2ef0bdb2258a946c214d9d0af',
paid: '0x2ea11e32ad50000',
reward: '0xdfd22a8cd98000',
reverted: false,
meta: {
blockID:
'0x010284a1fea0635a2e47dd21f8a1761406df1013e5f4af79e311d8a27373980d',
blockNumber: 16942241,
blockTimestamp: 1699453780,
txID: '0x46d195f69e1ac3922d42c207e4705a3d1642883d97e58f7efc72f179ea326adb',
txOrigin: '0x2d4ed6b8abd00bc2ef0bdb2258a946c214d9d0af'
},
outputs: [
{
contractAddress: null,
events: [],
transfers: [
{
sender: '0x2d4ed6b8abd00bc2ef0bdb2258a946c214d9d0af',
recipient: '0x5d57f07dfeb8c224121433d5b1b401c82bd88f3d',
amount: '0x2ea11e32ad50000'
}
]
}
]
});
Loading

1 comment on commit 095661f

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Coverage

Summary

Lines Statements Branches Functions
Coverage: 100%
100% (1094/1094) 100% (261/261) 100% (218/218)
Title Tests Skipped Failures Errors Time
core 319 0 💤 0 ❌ 0 🔥 1m 29s ⏱️
network 71 0 💤 0 ❌ 0 🔥 46.073s ⏱️
errors 30 0 💤 0 ❌ 0 🔥 10.843s ⏱️

Please sign in to comment.