@prajwalghate/sourcetruth 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,52 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^0.8.20;
3
+
4
+ interface IERC20 {
5
+ function transfer(address to, uint256 amount) external returns (bool);
6
+ function transferFrom(address from, address to, uint256 amount) external returns (bool);
7
+ function balanceOf(address who) external view returns (uint256);
8
+ }
9
+
10
+ interface IRouter {
11
+ function swap(address tokenIn, address tokenOut, uint256 amountIn) external returns (uint256);
12
+ }
13
+
14
+ interface IStrategy {
15
+ function invest(uint256 amount) external;
16
+ function withdraw(uint256 amount) external;
17
+ function harvest() external returns (uint256);
18
+ }
19
+
20
+ contract Strategy is IStrategy {
21
+ address public immutable vault;
22
+ IERC20 public immutable asset;
23
+ IERC20 public immutable reward;
24
+ IRouter public router;
25
+
26
+ constructor(address _vault, IERC20 _asset, IERC20 _reward, IRouter _router) {
27
+ vault = _vault; asset = _asset; reward = _reward; router = _router;
28
+ }
29
+
30
+ function invest(uint256) external { _onlyVault(); }
31
+
32
+ function withdraw(uint256 amount) external {
33
+ _onlyVault();
34
+ asset.transfer(vault, amount);
35
+ }
36
+
37
+ function harvest() external returns (uint256 gained) {
38
+ _onlyVault();
39
+ uint256 earned = reward.balanceOf(address(this));
40
+ gained = router.swap(address(reward), address(asset), earned);
41
+ asset.transfer(vault, gained);
42
+ }
43
+
44
+ function emergencyExit() external {
45
+ if (msg.sender != vault) revert();
46
+ asset.transfer(vault, asset.balanceOf(address(this)));
47
+ }
48
+
49
+ function _onlyVault() internal view {
50
+ require(msg.sender == vault, "not vault");
51
+ }
52
+ }
@@ -0,0 +1,67 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // A small yield vault, written to show what sourcetruth draws. Not production code.
3
+ pragma solidity ^0.8.20;
4
+
5
+ import "./Strategy.sol";
6
+
7
+ abstract contract Owned {
8
+ address public owner;
9
+ modifier onlyOwner() { require(msg.sender == owner, "not owner"); _; }
10
+ function transferOwnership(address to) external onlyOwner { owner = to; }
11
+ }
12
+
13
+ contract Vault is Owned {
14
+ IERC20 public immutable asset;
15
+ IStrategy public strategy;
16
+ mapping(address => uint256) public shares;
17
+ mapping(address => bool) public keepers;
18
+ uint256 public feeBps = 100;
19
+
20
+ constructor(IERC20 _asset) { asset = _asset; owner = msg.sender; }
21
+
22
+ function deposit(uint256 amount) external {
23
+ asset.transferFrom(msg.sender, address(this), amount);
24
+ shares[msg.sender] += amount;
25
+ _invest();
26
+ }
27
+
28
+ function withdraw(uint256 amount) external {
29
+ shares[msg.sender] -= amount;
30
+ strategy.withdraw(amount);
31
+ asset.transfer(msg.sender, amount);
32
+ }
33
+
34
+ function harvest() external {
35
+ _onlyKeeper();
36
+ uint256 profit = strategy.harvest();
37
+ _takeFee(profit);
38
+ _invest();
39
+ }
40
+
41
+ function takeFee(uint256 amount) external { _takeFee(amount); }
42
+
43
+ function setStrategy(IStrategy next) external onlyOwner { strategy = next; }
44
+ function setKeeper(address keeper, bool allowed) external onlyOwner { keepers[keeper] = allowed; }
45
+
46
+ function execute(address target, bytes calldata data) external onlyOwner returns (bytes memory) {
47
+ (bool ok, bytes memory out) = target.delegatecall(data);
48
+ require(ok, "call failed");
49
+ return out;
50
+ }
51
+
52
+ function _onlyKeeper() internal view {
53
+ require(keepers[msg.sender], "not keeper");
54
+ }
55
+
56
+ function _invest() internal {
57
+ uint256 idle = asset.balanceOf(address(this));
58
+ if (idle > 0) {
59
+ asset.transfer(address(strategy), idle);
60
+ strategy.invest(idle);
61
+ }
62
+ }
63
+
64
+ function _takeFee(uint256 amount) internal {
65
+ asset.transfer(owner, amount * feeBps / 10_000);
66
+ }
67
+ }
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@prajwalghate/sourcetruth",
3
+ "version": "0.1.0",
4
+ "description": "See what Daml and Solidity contract code does — who can act, what each action changes — from source, and what could not be traced.",
5
+ "type": "module",
6
+ "bin": { "sourcetruth": "./bin/sourcetruth.mjs" },
7
+ "files": ["bin/", "src/", "examples/", "README.md", "GUIDE.md", "CHANGELOG.md", "LICENSE"],
8
+ "scripts": {
9
+ "test": "node --test test/*.test.mjs",
10
+ "demo": "node bin/sourcetruth.mjs --demo -o demo.html"
11
+ },
12
+ "keywords": ["audit", "security", "smart-contracts", "daml", "canton", "solidity", "evm", "static-analysis", "access-control"],
13
+ "homepage": "https://prajwalghate.github.io/sourcetruth/",
14
+ "repository": { "type": "git", "url": "git+https://github.com/prajwalghate/sourcetruth.git" },
15
+ "bugs": { "url": "https://github.com/prajwalghate/sourcetruth/issues" },
16
+ "author": "Prajwal Ghate",
17
+ "license": "MIT",
18
+ "engines": { "node": ">=22" },
19
+ "publishConfig": { "access": "public" }
20
+ }