From 11478ef8ae7948a1836c55bcb113647b5bcfa8a2 Mon Sep 17 00:00:00 2001 From: Jason Schrader Date: Mon, 26 Aug 2024 11:46:16 -0700 Subject: [PATCH] fix: scaffold day 7 exercises --- Clarinet.toml | 10 ++++- contracts/ex7-01.clar | 60 ++++++++++++++++++++++++++++ contracts/ex7-02.clar | 92 +++++++++++++++++++++++++++++++++++++++++++ tests/ex7-01.test.ts | 21 ++++++++++ tests/ex7-02.test.ts | 21 ++++++++++ 5 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 contracts/ex7-01.clar create mode 100644 contracts/ex7-02.clar create mode 100644 tests/ex7-01.test.ts create mode 100644 tests/ex7-02.test.ts diff --git a/Clarinet.toml b/Clarinet.toml index 03d70d3..e2bb24e 100644 --- a/Clarinet.toml +++ b/Clarinet.toml @@ -5,7 +5,6 @@ authors = [] telemetry = false cache_dir = './.cache' requirements = [] - [contracts.ex3-01] path = 'contracts/ex3-01.clar' clarity_version = 2 @@ -46,6 +45,15 @@ path = 'contracts/ex6-02.clar' clarity_version = 2 epoch = 2.5 +[contracts.ex7-01] +path = 'contracts/ex7-01.clar' +clarity_version = 2 +epoch = 2.5 + +[contracts.ex7-02] +path = 'contracts/ex7-02.clar' +clarity_version = 2 +epoch = 2.5 [repl.analysis] passes = ['check_checker'] diff --git a/contracts/ex7-01.clar b/contracts/ex7-01.clar new file mode 100644 index 0000000..1b29fcf --- /dev/null +++ b/contracts/ex7-01.clar @@ -0,0 +1,60 @@ + +;; title: ex7-01 +;; version: +;; summary: +;; description: + +;; traits +;; + +;; BUG: NO TRAIT? + +;; token definitions +;; + +(define-fungible-token SimpleToken u1000000) + +;; constants +;; + +;; data vars +;; + +(define-data-var tokenName (string-utf8 32) u"SimpleToken") + +;; data maps +;; + +;; public functions +;; + +(define-public (mint (amount uint) (recipient principal)) + (ft-mint? SimpleToken amount recipient) +) + + +(define-public (transfer (amount uint) (sender principal) (recipient principal)) + (ft-transfer? SimpleToken amount sender recipient) +) + +(define-public (burn (amount uint) (burner principal)) + (ft-burn? SimpleToken amount burner) +) + +;; read only functions +;; + +(define-read-only (get-balance (account principal)) + (ft-get-balance SimpleToken account) +) + +(define-read-only (get-total-supply) + (ft-get-supply SimpleToken) +) + +;; private functions +;; + +;; Test cases +;; (mint u100 'ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5) ;; This should fail +;; (burn u10 'ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5) ;; This should fail \ No newline at end of file diff --git a/contracts/ex7-02.clar b/contracts/ex7-02.clar new file mode 100644 index 0000000..58aff44 --- /dev/null +++ b/contracts/ex7-02.clar @@ -0,0 +1,92 @@ + +;; title: ex7-02 +;; version: +;; summary: +;; description: + +;; traits +;; + +;; BUG: NO TRAIT? + +;; token definitions +;; + +(define-fungible-token FaucetToken) + +;; constants +;; + +(define-constant CONTRACT_OWNER tx-sender) +(define-constant TOKEN_NAME "The Faucet") +(define-constant TOKEN_SYMBOL "DRIP") +(define-constant TOKEN_DECIMALS u6) +(define-constant CLAIM_AMOUNT u100000000) ;; 100 tokens with 6 decimals +(define-constant BLOCKS_BETWEEN_CLAIMS u144) ;; Approximately 24 hours (assuming 10-minute block times) + +;; data vars +;; + +;; data maps +;; + +(define-map LastClaimedAtBlock principal uint) + +;; public functions +;; + +;; SIP-010 functions: transfer + +(define-public (claim) + ;; Implement the claim function + (ok true) +) + +;; read only functions +;; + +;; SIP-010 functions: get-name, get-symbol, get-decimals, get-balance, get-total-supply, get-token-uri + +(define-read-only (time-until-next-claim (user principal)) + ;; Implement the time check function + u0 +) + +;; private functions +;; + +(define-private (is-claim-allowed (user principal)) + ;; Implement the claim allowance check + true +) + +;; Test cases + +;; Test SIP-010 functions +;; (print (get-name)) +;; (print (get-symbol)) +;; (print (get-decimals)) +;; (print (get-balance tx-sender)) +;; (print (get-total-supply)) +;; (print (get-token-uri)) + +;; Test claim function +;; (print (claim)) +;; (print (get-balance tx-sender)) +;; (print (claim)) ;; Should fail if called twice in a row + +;; Test time-until-next-claim function +;; (print (time-until-next-claim tx-sender)) + +;; Test transfer function +;; (define-constant recipient 'ST1J4G6RR643BCG8G8SR6M2D9Z9KXT2NJDRK3FBTK) +;; (print (transfer u50000000 tx-sender recipient none)) +;; (print (get-balance tx-sender)) +;; (print (get-balance recipient)) + +;; Advanced test: Multiple users +;; (define-constant user2 'ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG) +;; (print (as-contract (transfer CLAIM_AMOUNT tx-sender user2 none))) +;; (print (get-balance user2)) +;; (print (as-contract (claim))) +;; (print (as-contract (time-until-next-claim tx-sender))) \ No newline at end of file diff --git a/tests/ex7-01.test.ts b/tests/ex7-01.test.ts new file mode 100644 index 0000000..4bb9cf3 --- /dev/null +++ b/tests/ex7-01.test.ts @@ -0,0 +1,21 @@ + +import { describe, expect, it } from "vitest"; + +const accounts = simnet.getAccounts(); +const address1 = accounts.get("wallet_1")!; + +/* + The test below is an example. To learn more, read the testing documentation here: + https://docs.hiro.so/stacks/clarinet-js-sdk +*/ + +describe("example tests", () => { + it("ensures simnet is well initalised", () => { + expect(simnet.blockHeight).toBeDefined(); + }); + + // it("shows an example", () => { + // const { result } = simnet.callReadOnlyFn("counter", "get-counter", [], address1); + // expect(result).toBeUint(0); + // }); +}); diff --git a/tests/ex7-02.test.ts b/tests/ex7-02.test.ts new file mode 100644 index 0000000..4bb9cf3 --- /dev/null +++ b/tests/ex7-02.test.ts @@ -0,0 +1,21 @@ + +import { describe, expect, it } from "vitest"; + +const accounts = simnet.getAccounts(); +const address1 = accounts.get("wallet_1")!; + +/* + The test below is an example. To learn more, read the testing documentation here: + https://docs.hiro.so/stacks/clarinet-js-sdk +*/ + +describe("example tests", () => { + it("ensures simnet is well initalised", () => { + expect(simnet.blockHeight).toBeDefined(); + }); + + // it("shows an example", () => { + // const { result } = simnet.callReadOnlyFn("counter", "get-counter", [], address1); + // expect(result).toBeUint(0); + // }); +});