Skip to content

Commit

Permalink
feat: fixes/improvements for contract vars
Browse files Browse the repository at this point in the history
  • Loading branch information
hstove committed Dec 9, 2022
1 parent b82e5d5 commit cc58a0f
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-years-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"clarigen-deno": patch
---

Fixes issues when adding variables / consts to types
32 changes: 31 additions & 1 deletion artifacts/clarigen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,43 @@ export const contracts = {
},
'maps': {},
'variables': {
ERR_TEST: {
name: 'ERR_TEST',
type: {
response: {
ok: 'none',
error: 'uint128',
},
},
access: 'constant',
} as TypedAbiVariable<Response<null, bigint>>,
buffConst: {
name: 'buff-const',
type: {
buffer: {
length: 4,
},
},
access: 'constant',
} as TypedAbiVariable<Uint8Array>,
testBuff: {
name: 'test-buff',
type: 'uint128',
access: 'constant',
} as TypedAbiVariable<bigint>,
counter: {
name: 'counter',
type: 'uint128',
access: 'variable',
} as TypedAbiVariable<bigint>,
},
constants: {
ERR_TEST: {
isOk: false,
value: 123n,
},
buffConst: Uint8Array.from([222, 173, 190, 239]),
testBuff: 3735928559n,
counter: 1n,
},
'non_fungible_tokens': [],
Expand Down Expand Up @@ -317,7 +347,7 @@ export const contracts = {
} as TypedAbiVariable<Response<null, bigint>>,
},
constants: {
eRR_UNAUTHORIZED: {
ERR_UNAUTHORIZED: {
isOk: false,
value: 400n,
},
Expand Down
4 changes: 4 additions & 0 deletions contracts/counter.clar
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
(var-get counter)
)

(define-constant test-buff (buff-to-uint-be 0xdeadbeef))
(define-constant buff-const 0xdeadbeef)
(define-constant ERR_TEST (err u123))

;; Increment the counter.
;;
;; @returns the new value of the counter
Expand Down
6 changes: 6 additions & 0 deletions src/cli/files/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ export function encodeVariables(variables: ClarityAbiVariable[]) {
});
}

(Uint8Array.prototype as any)[Symbol.for('Deno.customInspect')] = function (
this: Uint8Array,
) {
return `Uint8Array.from([${this.join(',')}])`;
};

// deno-lint-ignore no-explicit-any
export function serialize(obj: any) {
return Deno.inspect(obj, {
Expand Down
3 changes: 1 addition & 2 deletions src/cli/files/deno-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ export {
tx,
txErr,
txOk,
// } from "../../deno-clarigen/src/index.ts";
} from "${IMPORT_URL}";
import { factory } from "${IMPORT_URL}";
import { simnet } from "./${typeImportPath}";
export const { test, contracts } = factory(simnet);
export const { test, contracts, accounts } = factory(simnet);
export const {
${contractNames.join(',\n ')}
Expand Down
1 change: 1 addition & 0 deletions src/cli/files/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function getVariables(contract: SessionContract, sessionId: number) {
const fullSrc = contract.source + `\n\n${varFn}`;

const deploy = Tx.deployContract(fakeId, fullSrc, deployer);
(deploy.deployContract as any).clarityVersion = 2;

const { receipts } = chain.mineBlock([deploy]);
const result = receipts[0].result;
Expand Down
3 changes: 2 additions & 1 deletion src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export function factory<T extends AllContracts, A extends Accounts>(
simnet: Simnet<T, A>,
) {
const transformed = contractsFactory(simnet);
const accountMap = new AccountMap(simnet.accounts);

const test = (options: UnitTestOptions<A>) => {
const { fn, ...rest } = options;
Expand All @@ -28,7 +29,6 @@ export function factory<T extends AllContracts, A extends Accounts>(
accounts: Map<keyof A, Account>,
) => {
const chain = new Chain(_chain, accounts);
const accountMap = new AccountMap(simnet.accounts);
fn(chain, accountMap);
};
return Clarinet.test({
Expand All @@ -40,5 +40,6 @@ export function factory<T extends AllContracts, A extends Accounts>(
return {
contracts: transformed,
test,
accounts: accountMap,
};
}
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ export const toCamelCase = (
titleCase?: boolean,
) => {
const inputStr = typeof input === 'string' ? input : String(input);
// Check if the input string only contains uppercase letters and/or underscores
const isUpperCaseAndUnderscore = /^[A-Z_]+$/.test(inputStr);
if (isUpperCaseAndUnderscore) {
return inputStr;
}
const [first, ...parts] = inputStr.replace('!', '').replace('?', '').split(
'-',
);
Expand Down
20 changes: 20 additions & 0 deletions test_pkg/consts_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { simnet } from '../artifacts/clarigen/index.ts';
import { contractsFactory, hexToBytes } from '../mod.ts';
import { assertEquals } from '../src/deps.ts';

const deployed = contractsFactory(simnet);

const counter = deployed.counter.constants;
const tester = deployed.tester.constants;

Deno.test({
name: 'constants and variables are set correctly',
fn() {
assertEquals(counter.counter, 1n);
assertEquals(counter.ERR_TEST.value, 123n);
assertEquals(counter.buffConst, hexToBytes('deadbeef'));
assertEquals(counter.testBuff, 3735928559n);

assertEquals(tester.ERR_UNAUTHORIZED.value, 400n);
},
});
10 changes: 4 additions & 6 deletions tests/counter_test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { assertEquals } from 'https://deno.land/std@0.90.0/testing/asserts.ts';
import { simnet } from '../artifacts/clarigen/index.ts';
import { Chain, factory, txOk } from '../src/index.ts';
import { describe, it } from 'https://deno.land/std@0.159.0/testing/bdd.ts';
import { accounts, Chain, counter, test, txOk } from './helper.ts';

const { test, contracts: { counter } } = factory(simnet);
const alice = accounts.addr('wallet_1');

test({
name: 'Ensure counter works',
fn(chain, accounts) {
const alice = accounts.addr('wallet_1');
fn(chain) {
const receipt = chain.mineOne(
txOk(counter.increment(2), alice),
);
Expand All @@ -29,8 +28,7 @@ test({
// BDD-style testing with `Chain`

describe('BDD-style testing', () => {
const { chain, accounts } = Chain.fromSimnet(simnet);
const alice = accounts.addr('wallet_1');
const { chain } = Chain.fromSimnet(simnet);

it('can increment', () => {
assertEquals(chain.sessionId, 1);
Expand Down
6 changes: 3 additions & 3 deletions tests/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ export {
tx,
txErr,
txOk,
// } from "../../deno-clarigen/src/index.ts";
} from 'https://deno.land/x/clarigen@v0.4.8/mod.ts';
import { factory } from 'https://deno.land/x/clarigen@v0.4.8/mod.ts';
// import { factory } from "https://deno.land/x/clarigen@v0.4.8/mod.ts";
import { factory } from '../mod.ts';
import { simnet } from './../artifacts/clarigen/index.ts';

export const { test, contracts } = factory(simnet);
export const { test, contracts, accounts } = factory(simnet);

export const {
counter,
Expand Down

0 comments on commit cc58a0f

Please sign in to comment.