Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new simulation for CCIP-019 execution #77

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
85 changes: 85 additions & 0 deletions simulations/stxer-ccip019-execution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { StacksMainnet } from "@stacks/network";
import { AnchorMode, PostConditionMode, SignedTokenTransferOptions, StacksTransaction, boolCV, bufferCV, contractPrincipalCV, listCV, makeSTXTokenTransfer, makeUnsignedContractCall, makeUnsignedContractDeploy, makeUnsignedSTXTokenTransfer, principalCV, serializeCV, stringAsciiCV, tupleCV, uintCV } from "@stacks/transactions";
import { c32addressDecode } from "c32check";
import fs from "fs";

// current beta api endpoint
const SIMULATION_API_ENDPOINT = "https://api.stxer.xyz/simulations";

function runTx(tx: StacksTransaction) {
// type 0: run transaction
return tupleCV({ type: uintCV(0), data: bufferCV(tx.serialize()) });
}

const common_params = {
network: new StacksMainnet(),
publicKey: "",
postConditionMode: PostConditionMode.Allow,
anchorMode: AnchorMode.Any,
fee: 100,
};

function runEval(address: string, contractName: string, code: string) {
// type 1: eval arbitrary code inside a contract
return tupleCV({
type: uintCV(1),
data: bufferCV(
serializeCV(
tupleCV({
contract: contractPrincipalCV(address, contractName),
code: stringAsciiCV(code),
})
)
),
});
}

async function directExecute(address: string, nonce: number) {
const [, addressHash] = c32addressDecode(address);
const directExecuteTx = await makeUnsignedContractCall({
contractAddress: "SP8A9HZ3PKST0S42VM9523Z9NV42SZ026V4K39WH",
contractName: "ccd001-direct-execute",
functionName: "direct-execute",
functionArgs: [principalCV("SP8A9HZ3PKST0S42VM9523Z9NV42SZ026V4K39WH.ccip019-pox-4-stacking")],
nonce: nonce++,
...common_params,
});
directExecuteTx.auth.spendingCondition.signer = addressHash;
return directExecuteTx;
}

async function main() {
// set current block height and hash
const block_height = 161649;
const block_hash = "654a1b5a9f3a8345b62d8cf854e57a742c84c7330991b86f4585949ca6fa2033";

// execute the proposal with 3rd tx
const executeTx = await directExecute("SP7DGES13508FHRWS1FB0J3SZA326FP6QRMB6JDE", 122);

// lock stx to the pool
const address = "SPN4Y5QPGQA8882ZXW90ADC2DHYXMSTN8VAR8C3X";
const [, addressHash] = c32addressDecode(address);
const lockStxTx = await makeUnsignedContractCall({
contractAddress: "SP21YTSM60CAY6D011EZVEVNKXVW8FVZE198XEFFP",
contractName: "pox4-fast-pool-v3",
functionName: "delegate-stack-stx-many",
functionArgs: [listCV([principalCV("SP8A9HZ3PKST0S42VM9523Z9NV42SZ026V4K39WH.ccd002-treasury-mia-mining-v3"), principalCV("SP8A9HZ3PKST0S42VM9523Z9NV42SZ026V4K39WH.ccd002-treasury-mia-rewards-v3")])],
nonce: 816, // matches Hiro Explorer
...common_params,
});
lockStxTx.auth.spendingCondition.signer = addressHash;

const req = tupleCV({
block_height: uintCV(block_height),
block_hash: bufferCV(Buffer.from(block_hash, "hex")),
steps: listCV([executeTx, lockStxTx].map((t) => runTx(t))),
});
const body = serializeCV(req);
const rs: any = await fetch(SIMULATION_API_ENDPOINT, {
method: "POST",
body,
}).then((rs) => rs.json());
console.log("Simulation will be available at: https://stxer.xyz/simulations/" + rs.id);
}

main().catch(console.error);