-
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.
feat: thor solo script used for seeding accounts (#278)
* feat: thor solo script used for seeding accounts * refactor: using hex decimal string instead of number for vet value clauses * docs: thor-solo custom data snapshot in readme * chore: test origin head * chore: restore change * docs: added example file reference --------- Co-authored-by: Fabio Rigamonti <[email protected]>
- Loading branch information
1 parent
7745922
commit 771d3eb
Showing
2 changed files
with
139 additions
and
2 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
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,102 @@ | ||
import { | ||
Transaction, | ||
TransactionHandler, | ||
contract, | ||
unitsUtils | ||
} from '@vechainfoundation/vechain-sdk-core'; | ||
import { ALL_ACCOUNTS, soloNetwork } from './fixture'; | ||
import { BUILT_IN_CONTRACTS } from './built-in-fixture'; | ||
import { ThorestClient } from '../src'; | ||
|
||
/** | ||
* Constructs clauses for transferring VTHO tokens. | ||
* | ||
* @remarks | ||
* This constant creates an array of transaction clauses for transferring VTHO tokens | ||
* from the built-in energy contract to the first 10 accounts in the `ALL_ACCOUNTS` array. | ||
*/ | ||
const CLAUSES_VTHO = ALL_ACCOUNTS.slice(0, 10).map((account) => ({ | ||
to: BUILT_IN_CONTRACTS.ENERGY_ADDRESS, | ||
value: 0, | ||
data: contract.encodeFunctionInput( | ||
BUILT_IN_CONTRACTS.ENERGY_ABI, | ||
'transfer', | ||
[account.address, unitsUtils.parseVET('500000000')] | ||
) | ||
})); | ||
|
||
/** | ||
* Constructs clauses for transferring VET tokens. | ||
* | ||
* @remarks | ||
* This constant creates an array of transaction clauses for transferring VET tokens | ||
* to the first 10 accounts in the `ALL_ACCOUNTS` array. | ||
*/ | ||
const CLAUSES_VET = ALL_ACCOUNTS.slice(0, 10).map((account) => ({ | ||
to: account.address, | ||
value: `0x${unitsUtils.parseVET('500000000').toString(16)}`, | ||
data: '0x' | ||
})); | ||
|
||
/** | ||
* Constructs a transaction body. | ||
* | ||
* @remarks blockRef is set to the genesis block ref. Expiration is set to 100000 blocks as an example but can be set to any value. | ||
* gas is set to 100000 as an example but can be set to a estimated gas value when the functionality is implemented. | ||
*/ | ||
const txBody = { | ||
chainTag: 246, | ||
blockRef: '0x0000000000000000', | ||
expiration: 100000, | ||
gasPriceCoef: 0, | ||
gas: 100000, | ||
dependsOn: null, | ||
nonce: '0x851fd66f' | ||
}; | ||
|
||
/** | ||
* Array of transaction bodies with clauses for VTHO and VET transfers. | ||
*/ | ||
const txBodies = CLAUSES_VTHO.map((clause, index) => ({ | ||
...txBody, | ||
clauses: [clause, CLAUSES_VET[index]] | ||
})); | ||
|
||
/** | ||
* Array of unsigned transactions. | ||
*/ | ||
const unsignedTxs = txBodies.map((txBody) => new Transaction(txBody)); | ||
|
||
/** | ||
* Array of signed transactions where each transaction is being signed and performed by each of the 10 thor-solo pre-seeded accounts. | ||
*/ | ||
const txs = unsignedTxs.map((unsignedTx, index) => | ||
TransactionHandler.sign( | ||
unsignedTx, | ||
Buffer.from(ALL_ACCOUNTS[10 + index].privateKey, 'hex') // 10 is the index of the first thor-solo genesis account | ||
) | ||
); | ||
|
||
/** | ||
* Distributes balances of VTHO and VET to the first 10 accounts. | ||
* | ||
* @remarks | ||
* This function signs and sends transactions to distribute VTHO and VET tokens | ||
* to the first 10 accounts in the `ALL_ACCOUNTS` array. It uses the ThorestClient | ||
* to interact with the VechainThor blockchain. | ||
* | ||
* @returns A Promise that resolves when all transactions have been processed. | ||
*/ | ||
const distributeBalances = async (): Promise<void> => { | ||
const thorestSoloClient = new ThorestClient(soloNetwork); | ||
|
||
for (const tx of txs) { | ||
const resp = await thorestSoloClient.transactions.sendTransaction( | ||
`0x${tx.encoded.toString('hex')}` | ||
); | ||
|
||
console.log(resp.id); // Print the transaction id | ||
} | ||
}; | ||
|
||
export { distributeBalances }; |
771d3eb
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