@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
package/README.md ADDED
@@ -0,0 +1,134 @@
1
+ # fastish
2
+
3
+ `fastish` is the Solidity library used to build hosts and commands for the Fastish protocol.
4
+
5
+ It contains the reusable contracts, utilities, and encoding helpers that Fastish applications compose on top of. If you are building a Fastish host, a command contract, or a small protocol extension that needs to speak Fastish's id, asset, and block formats, this repo is the shared foundation.
6
+
7
+ ## What You Build With It
8
+
9
+ - `Host` contracts that register with Fastish discovery and expose trusted command endpoints
10
+ - `Command` contracts that execute protocol actions such as transfer, deposit, withdraw, settlement, and admin flows
11
+ - Shared request/response block parsing and writing logic
12
+ - Shared id, asset, account, and event encoding used across the protocol
13
+
14
+ ## Main Entry Points
15
+
16
+ Most consumers should start from the barrel files in `contracts/`:
17
+
18
+ - `contracts/Core.sol`: core host and validation building blocks
19
+ - `contracts/Commands.sol`: base command contract plus standard command mixins
20
+ - `contracts/Blocks.sol`: block schema, readers, and writers
21
+ - `contracts/Utils.sol`: ids, assets, accounts, layout, strings, and value helpers
22
+ - `contracts/Events.sol`: reusable event emitters and event contracts
23
+
24
+ ## Start Here
25
+
26
+ If you are new to Fastish, read [`docs/GETTING_STARTED.md`](docs/GETTING_STARTED.md) first.
27
+
28
+ It walks through:
29
+
30
+ - the host and command mental model
31
+ - which built-in commands expect `request` vs `state`
32
+ - a minimal host example
33
+ - a built-in command example
34
+ - a custom command example
35
+ - simple TypeScript request encoding
36
+
37
+ ## Typical Usage
38
+
39
+ ### Build a Host
40
+
41
+ Extend `Host` when you want a Fastish host contract with admin command support and optional discovery registration.
42
+
43
+ ```solidity
44
+ // SPDX-License-Identifier: GPL-3.0-only
45
+ pragma solidity ^0.8.33;
46
+
47
+ import {Host} from "fastish/contracts/Core.sol";
48
+
49
+ contract ExampleHost is Host {
50
+ constructor(address fastish)
51
+ Host(fastish, 1, "example")
52
+ {}
53
+ }
54
+ ```
55
+
56
+ `fastish` is the trusted Fastish runtime. If it is a contract, the host also announces itself there during deployment. Use `address(0)` for a self-managed host that does not auto-register.
57
+
58
+ `Host` already layers in the standard admin command flows used by Fastish hosts:
59
+
60
+ - `Authorize`
61
+ - `Unauthorize`
62
+ - `Relocate`
63
+
64
+ ### Build a Command
65
+
66
+ Extend `CommandBase` when you want a Fastish command mixin that runs inside the protocol's trusted call model. Commands are abstract contracts mixed into a host or composed as a standalone module.
67
+
68
+ ```solidity
69
+ // SPDX-License-Identifier: GPL-3.0-only
70
+ pragma solidity ^0.8.33;
71
+
72
+ import {CommandBase, CommandContext} from "fastish/contracts/Commands.sol";
73
+
74
+ string constant NAME = "myCommand";
75
+ string constant ROUTE = "route(uint foo, uint bar)";
76
+
77
+ abstract contract ExampleCommand is CommandBase {
78
+ uint internal immutable myCommandId = commandId(NAME);
79
+
80
+ constructor() {
81
+ emit Command(host, NAME, ROUTE, myCommandId, 0, 0);
82
+ }
83
+
84
+ function myCommand(
85
+ CommandContext calldata c
86
+ ) external payable onlyCommand(myCommandId, c.target) returns (bytes memory) {
87
+ return "";
88
+ }
89
+ }
90
+ ```
91
+
92
+ `CommandBase` gives you the common Fastish command context:
93
+
94
+ - trusted caller enforcement
95
+ - admin checks
96
+ - expiry checks
97
+ - command-to-command or command-to-host calls through encoded Fastish node ids
98
+ - shared command events
99
+
100
+ ## Repo Layout
101
+
102
+ - `contracts/core`: host, access control, balances, and validation primitives
103
+ - `contracts/commands`: standard command building blocks and admin commands
104
+ - `contracts/peer`: peer protocol surfaces for inter-host asset flows
105
+ - `contracts/blocks`: request/response block encoding and decoding
106
+ - `contracts/utils`: shared protocol encoding helpers
107
+ - `contracts/events`: protocol event contracts and emitters
108
+ - `contracts/interfaces`: discovery interfaces and shared external protocol surfaces
109
+
110
+ ## Install And Compile
111
+
112
+ ```bash
113
+ npm install @fastish/contracts
114
+ npm run compile
115
+ ```
116
+
117
+ The stable import surface for consumers is:
118
+
119
+ - `fastish/contracts/Core.sol`
120
+ - `fastish/contracts/Commands.sol`
121
+ - `fastish/contracts/Blocks.sol`
122
+ - `fastish/contracts/Utils.sol`
123
+ - `fastish/contracts/Events.sol`
124
+
125
+ ## When To Use This Repo
126
+
127
+ Use `fastish` if you want to:
128
+
129
+ - create a new Fastish host
130
+ - implement a new Fastish command
131
+ - reuse Fastish's block format and wire encoding
132
+ - share protocol-level Solidity code across multiple Fastish applications
133
+
134
+ If you are looking for a full end-user app or deployment repo, this library is the lower-level protocol package rather than the full product surface.
@@ -0,0 +1,73 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ pragma solidity ^0.8.33;
3
+
4
+ import {
5
+ ALLOCATION,
6
+ ALLOCATION_KEY,
7
+ AMOUNT,
8
+ AMOUNT_KEY,
9
+ ASSET,
10
+ ASSET_KEY,
11
+ AUTH,
12
+ AUTH_KEY,
13
+ BALANCE,
14
+ BALANCE_KEY,
15
+ BOUNTY,
16
+ BOUNTY_KEY,
17
+ BlockRef,
18
+ CUSTODY,
19
+ CUSTODY_KEY,
20
+ DataRef,
21
+ DataPairRef,
22
+ FUNDING,
23
+ FUNDING_KEY,
24
+ HostAmount,
25
+ LISTING,
26
+ LISTING_KEY,
27
+ Listing,
28
+ MAXIMUM,
29
+ MAXIMUM_KEY,
30
+ MINIMUM,
31
+ MINIMUM_KEY,
32
+ MemRef,
33
+ NODE,
34
+ NODE_KEY,
35
+ QUANTITY,
36
+ QUANTITY_KEY,
37
+ PARTY,
38
+ PARTY_KEY,
39
+ RATE,
40
+ RATE_KEY,
41
+ RECIPIENT,
42
+ RECIPIENT_KEY,
43
+ ROUTE,
44
+ ROUTE_EMPTY,
45
+ ROUTE_KEY,
46
+ STEP,
47
+ STEP_KEY,
48
+ TX,
49
+ TX_KEY,
50
+ Tx,
51
+ Writer,
52
+ AssetAmount
53
+ } from "./blocks/Schema.sol";
54
+ import {Blocks} from "./blocks/Readers.sol";
55
+ import {Data} from "./blocks/Data.sol";
56
+ import {Mem} from "./blocks/Mem.sol";
57
+ import {
58
+ InvalidBlock,
59
+ MalformedBlocks,
60
+ UnexpectedAsset,
61
+ UnexpectedHost,
62
+ UnexpectedMeta,
63
+ ZeroNode,
64
+ ZeroRecipient
65
+ } from "./blocks/Errors.sol";
66
+ import {
67
+ BALANCE_BLOCK_LEN,
68
+ CUSTODY_BLOCK_LEN,
69
+ IncompleteWriter,
70
+ TX_BLOCK_LEN,
71
+ Writers,
72
+ WriterOverflow
73
+ } from "./blocks/Writers.sol";
@@ -0,0 +1,47 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ pragma solidity ^0.8.33;
3
+
4
+ import {CommandBase, CommandContext} from "./commands/Base.sol";
5
+ import {NoOperation} from "./core/Operation.sol";
6
+ import {BALANCES, CLAIMS, CUSTODIES, PIPE, SETUP, TRANSACTIONS} from "./utils/Channels.sol";
7
+ import {BorrowAgainstBalanceToBalance, BorrowAgainstCustodyToBalance} from "./commands/Borrow.sol";
8
+ import {Burn} from "./commands/Burn.sol";
9
+ import {Create} from "./commands/Create.sol";
10
+ import {CreditBalanceToAccount} from "./commands/CreditTo.sol";
11
+ import {DebitAccountToBalance} from "./commands/DebitFrom.sol";
12
+ import {Deposit} from "./commands/Deposit.sol";
13
+ import {Remove} from "./commands/Remove.sol";
14
+ import {Fund} from "./commands/Fund.sol";
15
+ import {
16
+ AddLiquidityFromBalancesToBalances,
17
+ AddLiquidityFromCustodiesToBalances,
18
+ RemoveLiquidityFromBalanceToBalances,
19
+ RemoveLiquidityFromCustodyToBalances
20
+ } from "./commands/Liquidity.sol";
21
+ import {LiquidateFromBalanceToBalances, LiquidateFromCustodyToBalances} from "./commands/Liquidate.sol";
22
+ import {MintToBalances} from "./commands/Mint.sol";
23
+ import {Pipe} from "./commands/Pipe.sol";
24
+ import {Provision} from "./commands/Provision.sol";
25
+ import {ReclaimToBalances} from "./commands/Reclaim.sol";
26
+ import {RedeemFromBalanceToBalances, RedeemFromCustodyToBalances} from "./commands/Redeem.sol";
27
+ import {RepayFromBalanceToBalances, RepayFromCustodyToBalances} from "./commands/Repay.sol";
28
+ import {Settle} from "./commands/Settle.sol";
29
+ import {StakeBalanceToBalances, StakeCustodyToBalances, StakeCustodyToPosition} from "./commands/Stake.sol";
30
+ import {Supply} from "./commands/Supply.sol";
31
+ import {SwapExactBalanceToBalance, SwapExactCustodyToBalance} from "./commands/Swap.sol";
32
+ import {Transfer} from "./commands/Transfer.sol";
33
+ import {UnstakeBalanceToBalances} from "./commands/Unstake.sol";
34
+ import {Withdraw} from "./commands/Withdraw.sol";
35
+ import {AllowAssets} from "./commands/admin/AllowAssets.sol";
36
+ import {Destroy} from "./commands/admin/Destroy.sol";
37
+ import {Authorize} from "./commands/admin/Authorize.sol";
38
+ import {DenyAssets} from "./commands/admin/DenyAssets.sol";
39
+ import {Init} from "./commands/admin/Init.sol";
40
+ import {Relocate} from "./commands/admin/Relocate.sol";
41
+ import {Allocate} from "./commands/admin/Allocate.sol";
42
+ import {Unauthorize} from "./commands/admin/Unauthorize.sol";
43
+ import {PeerBase} from "./peer/Base.sol";
44
+ import {PeerAllowAssets} from "./peer/AllowAssets.sol";
45
+ import {PeerDenyAssets} from "./peer/DenyAssets.sol";
46
+ import {PeerPull} from "./peer/Pull.sol";
47
+ import {PeerPush} from "./peer/Push.sol";
@@ -0,0 +1,10 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ pragma solidity ^0.8.33;
3
+
4
+ import {AccessControl} from "./core/Access.sol";
5
+ import {Balances} from "./core/Balances.sol";
6
+ import {Host} from "./core/Host.sol";
7
+ import {FailedCall, NoOperation, OperationBase} from "./core/Operation.sol";
8
+ import {Validator} from "./core/Validator.sol";
9
+ import {HostDiscovery} from "./core/Host.sol";
10
+ import {IHostDiscovery} from "./interfaces/IHostDiscovery.sol";
@@ -0,0 +1,18 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ pragma solidity ^0.8.33;
3
+
4
+ import {AccessEvent} from "./events/Access.sol";
5
+ import {AssetEvent} from "./events/Asset.sol";
6
+ import {BalanceEvent} from "./events/Balance.sol";
7
+ import {CollateralEvent} from "./events/Collateral.sol";
8
+ import {CommandEvent} from "./events/Command.sol";
9
+ import {DebtEvent} from "./events/Debt.sol";
10
+ import {DepositEvent} from "./events/Deposit.sol";
11
+ import {EventEmitter} from "./events/Emitter.sol";
12
+ import {GovernedEvent} from "./events/Governed.sol";
13
+ import {HostAnnouncedEvent} from "./events/HostAnnounced.sol";
14
+ import {ListingEvent} from "./events/Listing.sol";
15
+ import {PeerEvent} from "./events/Peer.sol";
16
+ import {QuoteEvent} from "./events/Quote.sol";
17
+ import {FastishEvent} from "./events/Fastish.sol";
18
+ import {WithdrawalEvent} from "./events/Withdraw.sol";
@@ -0,0 +1,57 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ pragma solidity ^0.8.33;
3
+
4
+ import {
5
+ ALLOCATION,
6
+ ALLOCATION_KEY,
7
+ AMOUNT,
8
+ AMOUNT_KEY,
9
+ ASSET,
10
+ ASSET_KEY,
11
+ AUTH,
12
+ AUTH_KEY,
13
+ AUTH_PROOF_LEN,
14
+ AUTH_TOTAL_LEN,
15
+ BALANCE,
16
+ BALANCE_KEY,
17
+ BOUNTY,
18
+ BOUNTY_KEY,
19
+ BlockPairRef,
20
+ BlockRef,
21
+ CUSTODY,
22
+ CUSTODY_KEY,
23
+ DataPairRef,
24
+ DataRef,
25
+ FUNDING,
26
+ FUNDING_KEY,
27
+ HostAmount,
28
+ UserAmount,
29
+ LISTING,
30
+ LISTING_KEY,
31
+ Listing,
32
+ MAXIMUM,
33
+ MAXIMUM_KEY,
34
+ MINIMUM,
35
+ MINIMUM_KEY,
36
+ MemRef,
37
+ NODE,
38
+ NODE_KEY,
39
+ QUANTITY,
40
+ QUANTITY_KEY,
41
+ PARTY,
42
+ PARTY_KEY,
43
+ RATE,
44
+ RATE_KEY,
45
+ RECIPIENT,
46
+ RECIPIENT_KEY,
47
+ ROUTE,
48
+ ROUTE_EMPTY,
49
+ ROUTE_KEY,
50
+ STEP,
51
+ STEP_KEY,
52
+ TX,
53
+ TX_KEY,
54
+ Tx,
55
+ Writer,
56
+ AssetAmount
57
+ } from "./blocks/Schema.sol";
@@ -0,0 +1,105 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ pragma solidity ^0.8.33;
3
+
4
+ import {
5
+ CLAIMS,
6
+ BALANCES,
7
+ CUSTODIES,
8
+ PIPE,
9
+ SETUP,
10
+ TRANSACTIONS
11
+ } from "./utils/Channels.sol";
12
+ import {
13
+ ACCOUNT_FAMILY,
14
+ accountEvmAddr,
15
+ accountPrefix,
16
+ addrOr,
17
+ ADMIN_PREFIX,
18
+ ensureEvmAccount,
19
+ InvalidAccount,
20
+ isAdminAccount,
21
+ toAdminAccount,
22
+ toUserAccount,
23
+ USER_PREFIX
24
+ } from "./utils/Accounts.sol";
25
+ import {
26
+ BadAmount,
27
+ ensureAmount,
28
+ ensureAssetRef,
29
+ ensureBalanceRef,
30
+ ERC20_PREFIX,
31
+ ERC721_PREFIX,
32
+ InvalidAsset,
33
+ isAsset32,
34
+ localErc20Addr,
35
+ localErc721Issuer,
36
+ resolveAmount,
37
+ toErc20Asset,
38
+ toErc721Asset,
39
+ toValueAsset,
40
+ VALUE_PREFIX,
41
+ ZeroAmount
42
+ } from "./utils/Assets.sol";
43
+ import {ECDSA} from "./utils/ECDSA.sol";
44
+ import {
45
+ COMMAND_ARGS,
46
+ COMMAND_PREFIX,
47
+ ensureCommand,
48
+ ensureHost,
49
+ HOST_PREFIX,
50
+ InvalidId,
51
+ isCommand,
52
+ isHost,
53
+ isPeer,
54
+ localHostAddr,
55
+ localNodeAddr,
56
+ NODE_FAMILY,
57
+ PEER_ARGS,
58
+ PEER_PREFIX,
59
+ toCommandId,
60
+ toCommandSelector,
61
+ toHostId,
62
+ toPeerId,
63
+ toPeerSelector
64
+ } from "./utils/Ids.sol";
65
+ import {
66
+ ACCOUNT,
67
+ ADMIN,
68
+ ASSET,
69
+ COMMAND,
70
+ ERC20,
71
+ ERC721,
72
+ EVM32,
73
+ EVM64,
74
+ HOST,
75
+ NODE,
76
+ PEER,
77
+ USER,
78
+ VALUE
79
+ } from "./utils/Layout.sol";
80
+ import {bytes32ToString} from "./utils/Strings.sol";
81
+ import {
82
+ applyBps,
83
+ beforeBps,
84
+ isFamily,
85
+ isLocal,
86
+ isLocalFamily,
87
+ matchesBase,
88
+ MAX_BPS,
89
+ max8,
90
+ max16,
91
+ max24,
92
+ max32,
93
+ max40,
94
+ max64,
95
+ max96,
96
+ max128,
97
+ max160,
98
+ routeSchema1,
99
+ routeSchema2,
100
+ toLocalBase,
101
+ toLocalFamily,
102
+ toUnspecifiedBase,
103
+ ValueOverflow
104
+ } from "./utils/Utils.sol";
105
+ import {InsufficientValue, msgValue, useValue, ValueBudget} from "./utils/Value.sol";