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

fix: add contract-caller as allowed sender #28

Merged
merged 1 commit into from
Oct 10, 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
5 changes: 5 additions & 0 deletions examples/fungible-token/Clarinet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ path = "contracts/fungible-token.clar"
clarity_version = 2
epoch = 2.4

[contracts.defi]
path = 'contracts/extern/defi.clar'
clarity_version = 2
epoch = 2.4

[repl.analysis]
passes = ["check_checker"]

Expand Down
15 changes: 15 additions & 0 deletions examples/fungible-token/contracts/extern/defi.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(use-trait ft-token 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait)

(define-constant contract-owner tx-sender)

;; contract can hold many different sip-10 tokens
(define-public (get-balance (token <ft-token>))
;; #[filter(token)]
(contract-call? token get-balance (as-contract tx-sender)))

;; any user can release any sip-10 token
;; without worrying about bad token implementations
;; Only `token` can be transferred as we do not use (as-contract (contract-call? token transfer...))
(define-public (release-token (amount uint) (token <ft-token>))
;; #[filter(token)]
(contract-call? token transfer amount (as-contract tx-sender) contract-owner none))
2 changes: 1 addition & 1 deletion examples/fungible-token/contracts/fungible-token.clar
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
)
(begin
;; #[filter(amount, recipient)]
(asserts! (is-eq tx-sender sender) ERR_NOT_TOKEN_OWNER)
(asserts! (or (is-eq tx-sender sender) (is-eq contract-caller sender)) ERR_NOT_TOKEN_OWNER)
(try! (ft-transfer? clarity-coin amount sender recipient))
(match memo to-print (print to-print) 0x)
(ok true)
Expand Down
2 changes: 1 addition & 1 deletion examples/fungible-token/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fungible-token",
"version": "1.0.0",
"version": "2.0.0",
"description": "fungible token smart contract example",
"type": "module",
"scripts": {
Expand Down
45 changes: 45 additions & 0 deletions examples/fungible-token/tests/defi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Cl } from "@stacks/transactions";
import { describe, expect, it } from "vitest";

const accounts = simnet.getAccounts();
const deployer = accounts.get("deployer")!;
const wallet1 = accounts.get("wallet_1")!;

describe("defie tests", () => {
it("ensure user can make defi transaction", () => {
// prepare by minting 100 tokens and transfer to defi contract

let block = simnet.callPublicFn(
"fungible-token",
"mint",
[Cl.uint(100), Cl.standardPrincipal(wallet1)],
deployer
);
// Check the result of the mint transaction
expect(block.result).toBeOk(Cl.bool(true));

block = simnet.callPublicFn(
"fungible-token",
"transfer",
[
Cl.uint(100),
Cl.standardPrincipal(wallet1),
Cl.contractPrincipal(deployer, "defi"),
Cl.none(),
],
wallet1
);

// Check the result of the transfer transaction
expect(block.result).toBeOk(Cl.bool(true));

console.log("check");
block = simnet.callPublicFn(
"defi",
"release-token",
[Cl.uint(42), Cl.contractPrincipal(deployer, "fungible-token")],
wallet1
);
expect(block.result).toBeOk(Cl.bool(true));
});
});
16 changes: 16 additions & 0 deletions examples/fungible-token/tests/fungible-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,20 @@ describe("test fungible token contract", () => {
);
expect(balanceResponse.result).toBeOk(Cl.uint(42));
});

it("transfers more tokens from wallet_2 than wallet_2 owns to wallet_1", () => {
const block = simnet.callPublicFn(
"fungible-token",
"transfer",
[
Cl.uint(42 * 42),
Cl.standardPrincipal(wallet2),
Cl.standardPrincipal(wallet1),
Cl.none(),
],
wallet2
);

expect(block.result).toBeErr(Cl.uint(1));
});
});
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"clarinetVersion": "2.6.0",
"clarinetVersion": "2.9.0",
"examples": [
{
"title": "blank-project",
Expand Down
55 changes: 28 additions & 27 deletions scripts/generate-metadata.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
/*
Script to generate metadata.json file.

This should be run when the examples are modified.

Your locally installed version of Clarinet will be used as the supported Clarinet version.
If this is changing, confirm that each project is compatible.
*/

const fs = require("fs");
const cp = require("child_process");
const path = require("path");
Expand All @@ -28,24 +19,34 @@ cp.execFile("scripts/print-clarinet-version.sh", (error, stdout, stderr) => {
// Put all the example names and descriptions in JSON
fs.readdir(PROJECTS_DIR, (err, projects) => {
if (err) throw err;
const examples = projects.map((projectDir) => {
const manifestPath = path.join(PROJECTS_DIR, projectDir, "Clarinet.toml");

const manifestFile = fs.readFileSync(manifestPath, "utf8");

// *super* basic parsing of the top level data in the manifest file
const parsedManifest = manifestFile.split("\n").reduce((acc, line) => {
const [key, value] = line.split("=");
if (!key || !value) return acc;
return { ...acc, [key.trim()]: value.trim().replaceAll('"', "") };
}, {});

return {
title: parsedManifest.name,
description: parsedManifest.description,
path: path.join(PROJECTS_DIR, projectDir),
};
});

const examples = projects
.filter((projectDir) => {
// Filter out non-directories (like .DS_Store)
return fs.statSync(path.join(PROJECTS_DIR, projectDir)).isDirectory();
})
.map((projectDir) => {
const manifestPath = path.join(
PROJECTS_DIR,
projectDir,
"Clarinet.toml"
);

const manifestFile = fs.readFileSync(manifestPath, "utf8");

// *super* basic parsing of the top level data in the manifest file
const parsedManifest = manifestFile.split("\n").reduce((acc, line) => {
const [key, value] = line.split("=");
if (!key || !value) return acc;
return { ...acc, [key.trim()]: value.trim().replaceAll('"', "") };
}, {});

return {
title: parsedManifest.name,
description: parsedManifest.description,
path: path.join(PROJECTS_DIR, projectDir),
};
});

fs.writeFile(
METADATA_FILE,
Expand Down
Binary file modified zips/blank-project.zip
Binary file not shown.
Binary file modified zips/btc-tx-enabled-nft.zip
Binary file not shown.
Binary file modified zips/clarity-bitcoin.zip
Binary file not shown.
Binary file modified zips/counter.zip
Binary file not shown.
Binary file modified zips/fungible-token.zip
Binary file not shown.
Binary file modified zips/hello-world.zip
Binary file not shown.
Binary file modified zips/lightning-swaps.zip
Binary file not shown.
Binary file modified zips/nft-marketplace.zip
Binary file not shown.
Binary file modified zips/non-fungible-token.zip
Binary file not shown.
Binary file modified zips/ordyswap.zip
Binary file not shown.
Binary file modified zips/semi-fungible-token.zip
Binary file not shown.
Binary file modified zips/stx-defi.zip
Binary file not shown.