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

feat: support stacks.js v7 #1605

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion components/clarinet-cli/src/generate/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ btc_address = "mvZtbibDAAA3WLpY7zXXFqRa3T4XSknBX7"
"license": "ISC",
"dependencies": {{
"@hirosystems/clarinet-sdk": "^2.3.2",
"@stacks/transactions": "^6.12.0",
"@stacks/transactions": "^7.0.0",
"chokidar-cli": "^3.0.0",
"typescript": "^5.3.3",
"vite": "^5.1.4",
Expand Down
2 changes: 1 addition & 1 deletion components/clarinet-sdk/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
"readme": "./README.md",
"dependencies": {
"@hirosystems/clarinet-sdk-wasm-browser": "2.14.0",
"@stacks/transactions": "^6.13.0"
"@stacks/transactions": "^7.0.0"
}
}
6 changes: 3 additions & 3 deletions components/clarinet-sdk/browser/src/sdkProxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cl } from "@stacks/transactions";
import { Cl, serializeCVBytes } from "@stacks/transactions";
import {
CallFnArgs,
DeployContractArgs,
Expand Down Expand Up @@ -66,7 +66,7 @@ export function getSessionProxy() {
new CallFnArgs(
contract,
method,
args.map((a) => Cl.serialize(a)),
args.map((a) => serializeCVBytes(a)),
sender,
),
);
Expand Down Expand Up @@ -144,7 +144,7 @@ export function getSessionProxy() {

if (prop === "getMapEntry") {
const getMapEntry: GetMapEntry = (contract, mapName, mapKey) => {
const response = session.getMapEntry(contract, mapName, Cl.serialize(mapKey));
const response = session.getMapEntry(contract, mapName, serializeCVBytes(mapKey));
const result = Cl.deserialize(response);
return result;
};
Expand Down
10 changes: 5 additions & 5 deletions components/clarinet-sdk/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@
"readme": "./README.md",
"dependencies": {
"@hirosystems/clarinet-sdk-wasm": "2.14.0",
"@stacks/transactions": "^6.13.0",
"vitest": "^3.0.5",
"@stacks/transactions": "^7.0.0",
"kolorist": "^1.8.0",
"prompts": "^2.4.2",
"vitest": "^3.0.5",
"yargs": "^17.7.2"
},
"devDependencies": {
"@stacks/encryption": "^6.13.0",
"@stacks/network": "^6.13.0",
"@stacks/stacking": "^6.13.0"
"@stacks/encryption": "^7.0.0",
"@stacks/network": "^7.0.0",
"@stacks/stacking": "^7.0.0"
}
}
6 changes: 3 additions & 3 deletions components/clarinet-sdk/node/src/sdkProxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cl } from "@stacks/transactions";
import { Cl, serializeCVBytes } from "@stacks/transactions";
import {
CallFnArgs,
DeployContractArgs,
Expand Down Expand Up @@ -66,7 +66,7 @@ export function getSessionProxy() {
new CallFnArgs(
contract,
method,
args.map((a) => Cl.serialize(a)),
args.map((a) => serializeCVBytes(a)),
sender,
),
);
Expand Down Expand Up @@ -144,7 +144,7 @@ export function getSessionProxy() {

if (prop === "getMapEntry") {
const getMapEntry: GetMapEntry = (contract, mapName, mapKey) => {
const response = session.getMapEntry(contract, mapName, Cl.serialize(mapKey));
const response = session.getMapEntry(contract, mapName, serializeCVBytes(mapKey));
const result = Cl.deserialize(response);
return result;
};
Expand Down
32 changes: 14 additions & 18 deletions components/clarinet-sdk/node/tests/pox-locking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@ import crypto from "crypto";
import { describe, expect, it, beforeEach } from "vitest";

import { Pox4SignatureTopic, StackingClient, poxAddressToTuple } from "@stacks/stacking";
import { StacksDevnet } from "@stacks/network";
import { STACKS_DEVNET } from "@stacks/network";
import { getPublicKeyFromPrivate, publicKeyToBtcAddress } from "@stacks/encryption";
import {
Cl,
ClarityType,
getAddressFromPrivateKey,
TransactionVersion,
createStacksPrivateKey,
} from "@stacks/transactions";
import { Cl, ClarityType, getAddressFromPrivateKey } from "@stacks/transactions";

// test the built package and not the source code
// makes it simpler to handle wasm build
Expand Down Expand Up @@ -158,21 +152,20 @@ describe("test pox-4", () => {
];

const accounts = stackingKeys.map((privKey) => {
const network = new StacksDevnet();
const network = STACKS_DEVNET;

const pubKey = getPublicKeyFromPrivate(privKey);
const stxAddress = getAddressFromPrivateKey(privKey, TransactionVersion.Testnet);
const signerPrivKey = createStacksPrivateKey(privKey);
const signerPubKey = getPublicKeyFromPrivate(signerPrivKey.data);
const stxAddress = getAddressFromPrivateKey(privKey, network);
const signerPubKey = getPublicKeyFromPrivate(privKey);

return {
privKey,
pubKey,
stxAddress,
btcAddr: publicKeyToBtcAddress(pubKey),
signerPrivKey: signerPrivKey,
signerPrivKey: privKey,
signerPubKey: signerPubKey,
client: new StackingClient(stxAddress, network),
client: new StackingClient({ address: stxAddress, network }),
};
});

Expand All @@ -183,12 +176,12 @@ describe("test pox-4", () => {
simnet.setEpoch("3.0");
});

it("can call get-pox-info", async () => {
it("can call get-pox-info", () => {
const poxInfo = simnet.callReadOnlyFn(poxContract, "get-pox-info", [], address1);
expect(poxInfo.result.type).toBe(ClarityType.ResponseOk);
});

it("can call get-pox-info", async () => {
it("can call get-pox-info", () => {
const account = accounts[0];
const rewardCycle = 0;
const burnBlockHeight = 0;
Expand All @@ -198,8 +191,11 @@ describe("test pox-4", () => {

expect(poxInfo.result.type).toBe(ClarityType.ResponseOk);

expect(poxInfo.result).toHaveProperty("value.data.min-amount-ustx", Cl.uint(stackingThreshold));
expect(poxInfo.result).toHaveProperty("value.data.reward-cycle-id", Cl.uint(rewardCycle));
expect(poxInfo.result).toHaveProperty(
"value.value.min-amount-ustx",
Cl.uint(stackingThreshold),
);
expect(poxInfo.result).toHaveProperty("value.value.reward-cycle-id", Cl.uint(rewardCycle));

const sigArgs = {
authId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import {
ListCV,
TupleCV,
BufferCV,
principalToString,
TrueCV,
FalseCV,
BooleanCV,
cvToString,
} from "@stacks/transactions";

import { MatcherState } from "@vitest/expect";
Expand Down Expand Up @@ -69,6 +69,8 @@ type ClarityTypetoValue = {
[ClarityType.Buffer]: BufferCV;
};

const ClarityTypeReversed = Object.fromEntries(Object.entries(ClarityType).map(([k, v]) => [v, k]));

// the "simple clarity values" are CVs that can't be nested and have `value` property
type SimpleCV = BooleanCV | IntCV | UIntCV | StringAsciiCV | StringUtf8CV;
type SimpleCVTypes =
Expand All @@ -79,11 +81,13 @@ type SimpleCVTypes =
| ClarityType.StringASCII
| ClarityType.StringUTF8;

const validClarityTypes = Object.values(ClarityType).filter((t) => typeof t === "number");
const validClarityTypes = Object.values(ClarityType).filter(
(t) => typeof t === "string",
) as string[];

function isClarityValue(input: unknown): input is ClarityValue {
if (!input || typeof input !== "object") return false;
if (!("type" in input) || typeof input.type !== "number") return false;
if (!("type" in input) || typeof input.type !== "string") return false;
if (!validClarityTypes.includes(input.type)) return false;

return true;
Expand All @@ -109,7 +113,7 @@ function checkCVType<T extends ClarityType>(
if (!isCV) {
throw new ClarityTypeError({
message: `actual value must ${notStr(isNot)}be a Clarity "${
ClarityType[expectedType]
ClarityTypeReversed[expectedType]
}", received "${typeof actual}"`,
});
}
Expand All @@ -126,10 +130,10 @@ function checkCVType<T extends ClarityType>(
throw new ClarityTypeError({
// generic and short message
message: `actual value must ${notStr(isNot)}be a Clarity "${
ClarityType[expectedType]
}", received "${ClarityType[actual.type]}"${errorCode}`,
actual: ClarityType[actual.type],
expected: ClarityType[expectedType],
ClarityTypeReversed[expectedType]
}", received "${ClarityTypeReversed[actual.type]}"${errorCode}`,
actual: ClarityTypeReversed[actual.type],
expected: ClarityTypeReversed[expectedType],
});
}

Expand Down Expand Up @@ -219,7 +223,7 @@ expect.extend({
return {
pass: true,
message: () =>
`actual value must ${notStr(this.isNot)}be a Clarity "${ClarityType[expectedType]}"`,
`actual value must ${notStr(this.isNot)}be a Clarity "${ClarityTypeReversed[expectedType]}"`,
};
},

Expand Down Expand Up @@ -286,7 +290,7 @@ expect.extend({
return errorToAssertionResult.call(this, e);
}

const actualString = principalToString(actual);
const actualString = cvToString(actual, "tryAscii");

try {
expected = isStandard
Expand Down Expand Up @@ -341,7 +345,7 @@ expect.extend({
const expected = isListArray ? Cl.prettyPrint(Cl.list(expectedItems), 2) : expectedItems;

return {
pass: this.equals(actual.list, expectedItems, undefined, true),
pass: this.equals(actual.value, expectedItems, undefined, true),
// note: throw a simple message and rely on `actual` and `expected` to display the diff
message: () => `the received List does ${this.isNot ? "" : "not "}match the expected one`,
actual: Cl.prettyPrint(actual, 2),
Expand All @@ -362,7 +366,7 @@ expect.extend({
const expected = isTupleData ? Cl.prettyPrint(Cl.tuple(expectedData), 2) : expectedData;

return {
pass: this.equals(actual.data, expectedData, undefined, true),
pass: this.equals(actual.value, expectedData, undefined, true),
// note: throw a simple message and rely on `actual` and `expected` to display the diff
message: () => `the received Tuple does ${this.isNot ? "" : "not "}match the expected one`,
actual: Cl.prettyPrint(actual, 2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ describe("test nested types", () => {
expect(complexTuple).toEqual(complexTuple);

// toBeTuple with a Cl.tuple
expect(complexTuple).toBeTuple(complexTuple.data);
expect(complexTuple).toBeTuple(complexTuple.value);

// toBeTuple asymmetric matched
expect(complexTuple).toBeTuple({
Expand Down
Loading
Loading