@fastish/contracts 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.
Files changed (105) hide show
  1. package/LICENSE +648 -0
  2. package/README.md +134 -0
  3. package/contracts/Blocks.sol +73 -0
  4. package/contracts/Commands.sol +47 -0
  5. package/contracts/Core.sol +10 -0
  6. package/contracts/Events.sol +18 -0
  7. package/contracts/Schema.sol +57 -0
  8. package/contracts/Utils.sol +105 -0
  9. package/contracts/blocks/Data.sol +646 -0
  10. package/contracts/blocks/Errors.sol +10 -0
  11. package/contracts/blocks/Mem.sol +122 -0
  12. package/contracts/blocks/Readers.sol +938 -0
  13. package/contracts/blocks/Schema.sol +148 -0
  14. package/contracts/blocks/Writers.sol +187 -0
  15. package/contracts/combinators/AmountToBalance.sol +26 -0
  16. package/contracts/combinators/AmountToCustody.sol +37 -0
  17. package/contracts/combinators/CustodyToBalance.sol +27 -0
  18. package/contracts/combinators/EachRoute.sol +19 -0
  19. package/contracts/combinators/MapBalance.sol +26 -0
  20. package/contracts/combinators/MapCustody.sol +26 -0
  21. package/contracts/combinators/RouteToBalance.sol +27 -0
  22. package/contracts/commands/Base.sol +39 -0
  23. package/contracts/commands/Borrow.sol +86 -0
  24. package/contracts/commands/Burn.sol +32 -0
  25. package/contracts/commands/Create.sol +31 -0
  26. package/contracts/commands/CreditTo.sol +36 -0
  27. package/contracts/commands/DebitFrom.sol +44 -0
  28. package/contracts/commands/Deposit.sol +46 -0
  29. package/contracts/commands/Fund.sol +37 -0
  30. package/contracts/commands/Liquidate.sol +93 -0
  31. package/contracts/commands/Liquidity.sol +171 -0
  32. package/contracts/commands/Mint.sol +41 -0
  33. package/contracts/commands/Pipe.sol +54 -0
  34. package/contracts/commands/Provision.sol +48 -0
  35. package/contracts/commands/Reclaim.sol +46 -0
  36. package/contracts/commands/Redeem.sol +93 -0
  37. package/contracts/commands/Remove.sol +31 -0
  38. package/contracts/commands/Repay.sol +93 -0
  39. package/contracts/commands/Settle.sol +32 -0
  40. package/contracts/commands/Stake.sol +114 -0
  41. package/contracts/commands/Supply.sol +32 -0
  42. package/contracts/commands/Swap.sol +86 -0
  43. package/contracts/commands/Transfer.sol +41 -0
  44. package/contracts/commands/Unstake.sol +49 -0
  45. package/contracts/commands/Withdraw.sol +37 -0
  46. package/contracts/commands/admin/Allocate.sol +33 -0
  47. package/contracts/commands/admin/AllowAssets.sol +34 -0
  48. package/contracts/commands/admin/Authorize.sol +32 -0
  49. package/contracts/commands/admin/DenyAssets.sol +34 -0
  50. package/contracts/commands/admin/Destroy.sol +26 -0
  51. package/contracts/commands/admin/Init.sol +26 -0
  52. package/contracts/commands/admin/Relocate.sol +32 -0
  53. package/contracts/commands/admin/Unauthorize.sol +32 -0
  54. package/contracts/core/Access.sol +49 -0
  55. package/contracts/core/Balances.sol +9 -0
  56. package/contracts/core/Host.sol +25 -0
  57. package/contracts/core/Operation.sol +32 -0
  58. package/contracts/core/Validator.sol +31 -0
  59. package/contracts/events/Access.sol +14 -0
  60. package/contracts/events/Asset.sol +14 -0
  61. package/contracts/events/Balance.sol +14 -0
  62. package/contracts/events/Collateral.sol +15 -0
  63. package/contracts/events/Command.sol +14 -0
  64. package/contracts/events/Debt.sol +15 -0
  65. package/contracts/events/Deposit.sol +14 -0
  66. package/contracts/events/Emitter.sol +7 -0
  67. package/contracts/events/Fastish.sol +14 -0
  68. package/contracts/events/Governed.sol +14 -0
  69. package/contracts/events/HostAnnounced.sol +14 -0
  70. package/contracts/events/Listing.sol +14 -0
  71. package/contracts/events/Peer.sol +14 -0
  72. package/contracts/events/Quote.sol +14 -0
  73. package/contracts/events/Withdraw.sol +14 -0
  74. package/contracts/interfaces/IHostDiscovery.sol +6 -0
  75. package/contracts/peer/AllowAssets.sol +31 -0
  76. package/contracts/peer/Base.sol +19 -0
  77. package/contracts/peer/DenyAssets.sol +31 -0
  78. package/contracts/peer/Pull.sol +30 -0
  79. package/contracts/peer/Push.sol +30 -0
  80. package/contracts/test/TestBlockHelper.sol +256 -0
  81. package/contracts/test/TestBorrowHost.sol +46 -0
  82. package/contracts/test/TestBurnHost.sol +28 -0
  83. package/contracts/test/TestCreateHost.sol +26 -0
  84. package/contracts/test/TestDiscovery.sol +6 -0
  85. package/contracts/test/TestECDSA.sol +16 -0
  86. package/contracts/test/TestHost.sol +215 -0
  87. package/contracts/test/TestLiquidityHost.sol +149 -0
  88. package/contracts/test/TestMintHost.sol +40 -0
  89. package/contracts/test/TestPeerHost.sol +34 -0
  90. package/contracts/test/TestReclaimHost.sol +47 -0
  91. package/contracts/test/TestRejectEther.sol +8 -0
  92. package/contracts/test/TestRemoveHost.sol +26 -0
  93. package/contracts/test/TestSwapHost.sol +45 -0
  94. package/contracts/test/TestUtils.sol +180 -0
  95. package/contracts/test/TestValidator.sol +10 -0
  96. package/contracts/utils/Accounts.sol +42 -0
  97. package/contracts/utils/Assets.sol +71 -0
  98. package/contracts/utils/Channels.sol +9 -0
  99. package/contracts/utils/ECDSA.sol +36 -0
  100. package/contracts/utils/Ids.sol +75 -0
  101. package/contracts/utils/Layout.sol +20 -0
  102. package/contracts/utils/Strings.sol +16 -0
  103. package/contracts/utils/Utils.sol +117 -0
  104. package/contracts/utils/Value.sol +18 -0
  105. package/package.json +29 -0
@@ -0,0 +1,122 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ pragma solidity ^0.8.33;
3
+
4
+ import {BALANCE_KEY, CUSTODY_KEY, HostAmount, MemRef, TX_KEY, Tx} from "./Schema.sol";
5
+ import {InvalidBlock, MalformedBlocks} from "./Errors.sol";
6
+
7
+ library Mem {
8
+ function from(bytes memory source, uint i) internal pure returns (MemRef memory ref) {
9
+ uint eod = source.length;
10
+ if (i == eod) return MemRef(bytes4(0), 0, 0, i);
11
+ if (i > eod) revert MalformedBlocks();
12
+
13
+ unchecked {
14
+ ref.i = i + 12;
15
+ }
16
+ if (ref.i > eod) revert MalformedBlocks();
17
+
18
+ bytes32 w;
19
+ assembly ("memory-safe") {
20
+ w := mload(add(add(source, 0x20), i))
21
+ }
22
+
23
+ ref.key = bytes4(w);
24
+ ref.bound = ref.i + uint32(bytes4(w << 32));
25
+ ref.end = ref.i + uint32(bytes4(w << 64));
26
+
27
+ if (ref.bound > ref.end || ref.end > eod) revert MalformedBlocks();
28
+ }
29
+
30
+ function slice(bytes memory source, uint start, uint end) internal pure returns (bytes memory out) {
31
+ if (end < start || end > source.length) revert MalformedBlocks();
32
+ uint len = end - start;
33
+ out = new bytes(len);
34
+ if (len == 0) return out;
35
+
36
+ assembly ("memory-safe") {
37
+ mcopy(add(out, 0x20), add(add(source, 0x20), start), len)
38
+ }
39
+ }
40
+
41
+ function count(bytes memory source, uint i, bytes4 key) internal pure returns (uint count_, uint next) {
42
+ next = i;
43
+ while (next < source.length) {
44
+ MemRef memory ref = from(source, next);
45
+ if (ref.key != key) break;
46
+ unchecked {
47
+ ++count_;
48
+ }
49
+ next = ref.end;
50
+ }
51
+ }
52
+
53
+ function find(bytes memory source, uint i, uint limit, bytes4 key) internal pure returns (MemRef memory ref) {
54
+ if (limit > source.length) revert MalformedBlocks();
55
+ while (i < limit) {
56
+ ref = from(source, i);
57
+ if (ref.end > limit) revert MalformedBlocks();
58
+ if (ref.key == key) return ref;
59
+ i = ref.end;
60
+ }
61
+
62
+ return MemRef(bytes4(0), limit, limit, limit);
63
+ }
64
+
65
+ function ensure(MemRef memory ref, bytes4 key) internal pure {
66
+ if (key == 0 || key != ref.key) revert InvalidBlock();
67
+ }
68
+
69
+ function ensure(MemRef memory ref, bytes4 key, uint len) internal pure {
70
+ if (key == 0 || key != ref.key || len != (ref.bound - ref.i)) revert InvalidBlock();
71
+ }
72
+
73
+ function ensure(MemRef memory ref, bytes4 key, uint min, uint max) internal pure {
74
+ uint len = ref.bound - ref.i;
75
+ if (key == 0 || key != ref.key || len < min || (max != 0 && len > max)) revert InvalidBlock();
76
+ }
77
+
78
+ function unpackBalance(
79
+ MemRef memory ref,
80
+ bytes memory source
81
+ ) internal pure returns (bytes32 asset, bytes32 meta, uint amount) {
82
+ ensure(ref, BALANCE_KEY, 96);
83
+ uint i = ref.i;
84
+
85
+ assembly ("memory-safe") {
86
+ let p := add(add(source, 0x20), i)
87
+ asset := mload(p)
88
+ meta := mload(add(p, 0x20))
89
+ amount := mload(add(p, 0x40))
90
+ }
91
+ }
92
+
93
+ function toCustodyValue(
94
+ MemRef memory ref,
95
+ bytes memory source
96
+ ) internal pure returns (HostAmount memory value) {
97
+ ensure(ref, CUSTODY_KEY, 128);
98
+ uint i = ref.i;
99
+
100
+ assembly ("memory-safe") {
101
+ let p := add(add(source, 0x20), i)
102
+ mstore(value, mload(p))
103
+ mstore(add(value, 0x20), mload(add(p, 0x20)))
104
+ mstore(add(value, 0x40), mload(add(p, 0x40)))
105
+ mstore(add(value, 0x60), mload(add(p, 0x60)))
106
+ }
107
+ }
108
+
109
+ function toTxValue(MemRef memory ref, bytes memory source) internal pure returns (Tx memory value) {
110
+ ensure(ref, TX_KEY, 160);
111
+ uint i = ref.i;
112
+
113
+ assembly ("memory-safe") {
114
+ let p := add(add(source, 0x20), i)
115
+ mstore(value, mload(p))
116
+ mstore(add(value, 0x20), mload(add(p, 0x20)))
117
+ mstore(add(value, 0x40), mload(add(p, 0x40)))
118
+ mstore(add(value, 0x60), mload(add(p, 0x60)))
119
+ mstore(add(value, 0x80), mload(add(p, 0x80)))
120
+ }
121
+ }
122
+ }