feat: add clarity version support

This commit is contained in:
Kyle Fang
2024-11-20 04:58:45 +00:00
parent 5061c5be2b
commit 4531699f88
2 changed files with 54 additions and 33 deletions

View File

@@ -1,8 +1,10 @@
import { type AccountDataResponse, getNodeInfo, richFetch } from 'ts-clarity'; import { type AccountDataResponse, getNodeInfo, richFetch } from 'ts-clarity';
import type { Block } from '@stacks/stacks-blockchain-api-types'; import type { Block } from '@stacks/stacks-blockchain-api-types';
import { networkFrom, STACKS_MAINNET, type StacksNetwork, type StacksNetworkName } from '@stacks/network';
import { import {
AnchorMode, networkFrom,
type StacksNetworkName,
} from '@stacks/network';
import {
type ClarityValue, type ClarityValue,
ClarityVersion, ClarityVersion,
PostConditionMode, PostConditionMode,
@@ -60,17 +62,24 @@ export async function runSimulation(
view.setBigUint64(0, BigInt(block_height), false); // false for big-endian view.setBigUint64(0, BigInt(block_height), false); // false for big-endian
// Convert block hash to bytes // Convert block hash to bytes
const hashHex = block_hash.startsWith('0x') ? block_hash.substring(2) : block_hash; const hashHex = block_hash.startsWith('0x')
? block_hash.substring(2)
: block_hash;
// biome-ignore lint/style/noNonNullAssertion: <explanation> // biome-ignore lint/style/noNonNullAssertion: <explanation>
const hashBytes = new Uint8Array(hashHex.match(/.{1,2}/g)!.map(byte => Number.parseInt(byte, 16))); const hashBytes = new Uint8Array(
hashHex.match(/.{1,2}/g)!.map((byte) => Number.parseInt(byte, 16))
);
// Convert transactions to bytes // Convert transactions to bytes
const txBytes = txs const txBytes = txs
.map((t) => 'contract_id' in t && 'code' in t ? runEval(t) : runTx(t)) .map((t) => ('contract_id' in t && 'code' in t ? runEval(t) : runTx(t)))
.map((t) => serializeCVBytes(t)); .map((t) => serializeCVBytes(t));
// Combine all byte arrays // Combine all byte arrays
const totalLength = header.length + heightBytes.length + hashBytes.length + const totalLength =
header.length +
heightBytes.length +
hashBytes.length +
txBytes.reduce((acc, curr) => acc + curr.length, 0); txBytes.reduce((acc, curr) => acc + curr.length, 0);
const body = new Uint8Array(totalLength); const body = new Uint8Array(totalLength);
@@ -284,9 +293,9 @@ To get in touch: contact@stxer.xyz
const nextNonce = async (sender: string) => { const nextNonce = async (sender: string) => {
const nonce = nonce_by_address.get(sender); const nonce = nonce_by_address.get(sender);
if (nonce == null) { if (nonce == null) {
const url = `${this.stacksNodeAPI}/v2/accounts/${sender}?proof=${false}&tip=${ const url = `${
block.index_block_hash this.stacksNodeAPI
}`; }/v2/accounts/${sender}?proof=${false}&tip=${block.index_block_hash}`;
const account: AccountDataResponse = await richFetch(url).then((r) => const account: AccountDataResponse = await richFetch(url).then((r) =>
r.json() r.json()
); );

View File

@@ -1,5 +1,5 @@
import { uintCV } from "@stacks/transactions"; import { uintCV } from '@stacks/transactions';
import { SimulationBuilder } from ".."; import { SimulationBuilder } from '..';
SimulationBuilder.new() SimulationBuilder.new()
.withSender('SP212Y5JKN59YP3GYG07K3S8W5SSGE4KH6B5STXER') .withSender('SP212Y5JKN59YP3GYG07K3S8W5SSGE4KH6B5STXER')
@@ -23,25 +23,31 @@ SimulationBuilder.new()
(ok (var-get counter))) (ok (var-get counter)))
`, `,
}) })
.addEvalCode('SP212Y5JKN59YP3GYG07K3S8W5SSGE4KH6B5STXER.test-simulation', '(get-counter)') .addEvalCode(
'SP212Y5JKN59YP3GYG07K3S8W5SSGE4KH6B5STXER.test-simulation',
'(get-counter)'
)
.addContractCall({ .addContractCall({
contract_id: 'SP212Y5JKN59YP3GYG07K3S8W5SSGE4KH6B5STXER.test-simulation', contract_id: 'SP212Y5JKN59YP3GYG07K3S8W5SSGE4KH6B5STXER.test-simulation',
function_name: 'increment', function_name: 'increment',
function_args: [uintCV(10)], function_args: [uintCV(10)],
}) })
.addEvalCode('SP212Y5JKN59YP3GYG07K3S8W5SSGE4KH6B5STXER.test-simulation', '(get-counter)') .addEvalCode(
'SP212Y5JKN59YP3GYG07K3S8W5SSGE4KH6B5STXER.test-simulation',
'(get-counter)'
)
.run() .run()
.catch(console.error); .catch(console.error);
SimulationBuilder.new({ SimulationBuilder.new({
apiEndpoint: 'https://testnet-api.stxer.xyz', apiEndpoint: 'https://testnet-api.stxer.xyz',
stacksNodeAPI: 'https://api.testnet.hiro.so', stacksNodeAPI: 'https://api.testnet.hiro.so',
network: 'testnet' network: 'testnet',
}) })
.withSender('ST3MZM9WJ34Y4311XBJDBKQ41SXX5DY68406J26WJ') .withSender('ST3MZM9WJ34Y4311XBJDBKQ41SXX5DY68406J26WJ')
.addContractDeploy({ .addContractDeploy({
contract_name: 'test-simulation', contract_name: 'test-simulation',
source_code: ` source_code: `
;; counter example ;; counter example
(define-data-var counter uint u0) (define-data-var counter uint u0)
@@ -58,13 +64,19 @@ SimulationBuilder.new({
(define-read-only (get-counter) (define-read-only (get-counter)
(ok (var-get counter))) (ok (var-get counter)))
`, `,
}) })
.addEvalCode('ST3MZM9WJ34Y4311XBJDBKQ41SXX5DY68406J26WJ.test-simulation', '(get-counter)') .addEvalCode(
.addContractCall({ 'ST3MZM9WJ34Y4311XBJDBKQ41SXX5DY68406J26WJ.test-simulation',
contract_id: 'ST3MZM9WJ34Y4311XBJDBKQ41SXX5DY68406J26WJ.test-simulation', '(get-counter)'
function_name: 'increment', )
function_args: [uintCV(10)], .addContractCall({
}) contract_id: 'ST3MZM9WJ34Y4311XBJDBKQ41SXX5DY68406J26WJ.test-simulation',
.addEvalCode('ST3MZM9WJ34Y4311XBJDBKQ41SXX5DY68406J26WJ.test-simulation', '(get-counter)') function_name: 'increment',
.run() function_args: [uintCV(10)],
.catch(console.error); })
.addEvalCode(
'ST3MZM9WJ34Y4311XBJDBKQ41SXX5DY68406J26WJ.test-simulation',
'(get-counter)'
)
.run()
.catch(console.error);