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 { Block } from '@stacks/stacks-blockchain-api-types';
import { networkFrom, STACKS_MAINNET, type StacksNetwork, type StacksNetworkName } from '@stacks/network';
import {
AnchorMode,
networkFrom,
type StacksNetworkName,
} from '@stacks/network';
import {
type ClarityValue,
ClarityVersion,
PostConditionMode,
@@ -60,17 +62,24 @@ export async function runSimulation(
view.setBigUint64(0, BigInt(block_height), false); // false for big-endian
// 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>
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
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));
// 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);
const body = new Uint8Array(totalLength);
@@ -284,9 +293,9 @@ To get in touch: contact@stxer.xyz
const nextNonce = async (sender: string) => {
const nonce = nonce_by_address.get(sender);
if (nonce == null) {
const url = `${this.stacksNodeAPI}/v2/accounts/${sender}?proof=${false}&tip=${
block.index_block_hash
}`;
const url = `${
this.stacksNodeAPI
}/v2/accounts/${sender}?proof=${false}&tip=${block.index_block_hash}`;
const account: AccountDataResponse = await richFetch(url).then((r) =>
r.json()
);
@@ -347,8 +356,8 @@ To get in touch: contact@stxer.xyz
}
const id = await runSimulation(
`${this.apiEndpoint}/simulations`,
block.block_hash,
block.block_height,
block.block_hash,
block.block_height,
txs
);
console.log(

View File

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