-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Online examples in docs - iteration 2 (#297)
* feat: add docker commands to docs * fix: add sendTransaction to the docs * fix: add tx delegation * fix: improve comments * fix: update node version * fix: revert node version * fix: fix typo * fix: improve examples
- Loading branch information
1 parent
a64f364
commit 9085373
Showing
8 changed files
with
330 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { | ||
Transaction, | ||
TransactionUtils, | ||
TransactionHandler, | ||
dataUtils, | ||
unitsUtils | ||
} from '@vechainfoundation/vechain-sdk-core'; | ||
import { | ||
HttpClient, | ||
ThorestClient | ||
} from '@vechainfoundation/vechain-sdk-network'; | ||
import { expect } from 'expect'; | ||
|
||
// Url of the solo network | ||
const _soloUrl = 'http://localhost:8669'; | ||
|
||
// Solo network instance | ||
const soloNetwork = new HttpClient(_soloUrl); | ||
|
||
// Thorest client solo instance | ||
const thorestSoloClient = new ThorestClient(soloNetwork); | ||
|
||
// Get latest block | ||
const latestBlock = await thorestSoloClient.blocks.getBestBlock(); | ||
|
||
// Create clauses | ||
const clauses = [ | ||
{ | ||
to: '0x9e7911de289c3c856ce7f421034f66b6cde49c39', | ||
value: unitsUtils.parseVET('10000').toString(), // VET transfer transaction | ||
data: '0x' | ||
} | ||
]; | ||
|
||
// Get gas @NOTE this is an approximation | ||
const gas = 5000 + TransactionUtils.intrinsicGas(clauses) * 5; | ||
|
||
// Create delegated transaction | ||
const delegatedTransaction = new Transaction({ | ||
chainTag: 0xf6, | ||
blockRef: latestBlock !== null ? latestBlock.id.slice(0, 18) : '0x0', | ||
expiration: 32, | ||
clauses, | ||
gasPriceCoef: 128, | ||
gas, | ||
dependsOn: null, | ||
nonce: 12345678, | ||
reserved: { | ||
features: 1 | ||
} | ||
}); | ||
|
||
// Private keys of sender | ||
const pkSender = | ||
'ea5383ac1f9e625220039a4afac6a7f868bf1ad4f48ce3a1dd78bd214ee4ace5'; | ||
|
||
/** Private key of delegate | ||
* @NOTE The delegate account must have enough VET and VTHO to pay for the gas | ||
*/ | ||
const pkDelegate = | ||
'432f38bcf338c374523e83fdb2ebe1030aba63c7f1e81f7d76c5f53f4d42e766'; | ||
|
||
// Normal signature and delegation signature | ||
const rawDelegatedSigned = TransactionHandler.signWithDelegator( | ||
delegatedTransaction, | ||
Buffer.from(pkSender, 'hex'), | ||
Buffer.from(pkDelegate, 'hex') | ||
).encoded; | ||
|
||
// Send transaction | ||
const send = await thorestSoloClient.transactions.sendTransaction( | ||
`0x${rawDelegatedSigned.toString('hex')}` | ||
); | ||
expect(send).toBeDefined(); | ||
expect(send).toHaveProperty('id'); | ||
expect(dataUtils.isHexString(send.id)).toBe(true); | ||
|
||
// Get transaction details and receipt | ||
const transactionDetails = await thorestSoloClient.transactions.getTransaction( | ||
send.id | ||
); | ||
const transactionReceipt = | ||
await thorestSoloClient.transactions.getTransactionReceipt(send.id); | ||
|
||
expect(transactionDetails).toBeDefined(); | ||
expect(transactionReceipt).toBeDefined(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,73 @@ | ||
import { | ||
Transaction, | ||
TransactionUtils, | ||
TransactionHandler, | ||
dataUtils, | ||
unitsUtils | ||
} from '@vechainfoundation/vechain-sdk-core'; | ||
import { | ||
HttpClient, | ||
ThorestClient | ||
} from '@vechainfoundation/vechain-sdk-network'; | ||
import { expect } from 'expect'; | ||
|
||
// Url of the testnet network | ||
const _testnetUrl = 'https://testnet.vechain.org/'; | ||
// Url of the solo network | ||
const _soloUrl = 'http://localhost:8669'; | ||
|
||
// Testnet network instance | ||
const testNetwork = new HttpClient(_testnetUrl); | ||
// Solo network instance | ||
const soloNetwork = new HttpClient(_soloUrl); | ||
|
||
// Thorest client testnet instance | ||
const thorestTestnetClient = new ThorestClient(testNetwork); | ||
// Thorest client solo instance | ||
const thorestSoloClient = new ThorestClient(soloNetwork); | ||
|
||
// 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 | ||
// Get latest block | ||
const latestBlock = await thorestSoloClient.blocks.getBestBlock(); | ||
|
||
// Create clauses | ||
const clauses = [ | ||
{ | ||
to: '0x9e7911de289c3c856ce7f421034f66b6cde49c39', | ||
value: unitsUtils.parseVET('10000').toString(), // VET transfer transaction | ||
data: '0x' | ||
} | ||
]; | ||
|
||
// Create transaction | ||
const transaction = new Transaction({ | ||
chainTag: 0xf6, | ||
blockRef: latestBlock !== null ? latestBlock.id.slice(0, 18) : '0x0', | ||
expiration: 32, | ||
clauses, | ||
gasPriceCoef: 128, | ||
gas: 5000 + TransactionUtils.intrinsicGas(clauses) * 5, | ||
dependsOn: null, | ||
nonce: 12345678 | ||
}); | ||
|
||
// Retrieves the receipt of a transaction. | ||
// Private keys of sender | ||
const pkSender = | ||
'ea5383ac1f9e625220039a4afac6a7f868bf1ad4f48ce3a1dd78bd214ee4ace5'; | ||
|
||
// Normal signature and delegation signature | ||
const rawNormalSigned = TransactionHandler.sign( | ||
transaction, | ||
Buffer.from(pkSender, 'hex') | ||
).encoded; | ||
|
||
// Send transaction | ||
const send = await thorestSoloClient.transactions.sendTransaction( | ||
`0x${rawNormalSigned.toString('hex')}` | ||
); | ||
expect(send).toBeDefined(); | ||
expect(send).toHaveProperty('id'); | ||
expect(dataUtils.isHexString(send.id)).toBe(true); | ||
|
||
// Get transaction details and receipt | ||
const transactionDetails = await thorestSoloClient.transactions.getTransaction( | ||
send.id | ||
); | ||
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' | ||
} | ||
] | ||
} | ||
] | ||
}); | ||
await thorestSoloClient.transactions.getTransactionReceipt(send.id); | ||
|
||
expect(transactionDetails).toBeDefined(); | ||
expect(transactionReceipt).toBeDefined(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
9085373
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test Coverage
Summary