@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,93 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ pragma solidity ^0.8.33;
3
+
4
+ import {CommandContext, CommandBase} from "./Base.sol";
5
+ import {BALANCES, CUSTODIES} from "../utils/Channels.sol";
6
+ import {AssetAmount, HostAmount, BALANCE_KEY, CUSTODY_KEY, Data, DataRef, Blocks, BlockRef, Writers, Writer} from "../Blocks.sol";
7
+
8
+ string constant RDBTB = "redeemFromBalanceToBalances";
9
+ string constant RDCTB = "redeemFromCustodyToBalances";
10
+
11
+ using Blocks for BlockRef;
12
+ using Data for DataRef;
13
+ using Writers for Writer;
14
+
15
+ abstract contract RedeemFromBalanceToBalances is CommandBase {
16
+ uint internal immutable redeemFromBalanceToBalancesId = commandId(RDBTB);
17
+ uint private immutable outScale;
18
+ bool private immutable useRoute;
19
+
20
+ constructor(string memory maybeRoute, uint scaledRatio) {
21
+ outScale = scaledRatio;
22
+ useRoute = bytes(maybeRoute).length > 0;
23
+ emit Command(host, RDBTB, maybeRoute, redeemFromBalanceToBalancesId, BALANCES, BALANCES);
24
+ }
25
+
26
+ // @dev `balance` is the redeemable claim/share/position amount offered for redemption.
27
+ // `rawRoute` is zero-initialized and should be ignored when `maybeRoute` is empty.
28
+ function redeemFromBalanceToBalances(
29
+ bytes32 account,
30
+ AssetAmount memory balance,
31
+ DataRef memory rawRoute,
32
+ Writer memory out
33
+ ) internal virtual;
34
+
35
+ function redeemFromBalanceToBalances(
36
+ CommandContext calldata c
37
+ ) external payable onlyCommand(redeemFromBalanceToBalancesId, c.target) returns (bytes memory) {
38
+ uint i = 0;
39
+ uint q = 0;
40
+ (Writer memory writer, uint end) = Writers.allocScaledBalancesFrom(c.state, i, BALANCE_KEY, outScale);
41
+
42
+ while (i < end) {
43
+ DataRef memory route;
44
+ if (useRoute) (route, q) = Data.routeFrom(c.request, q);
45
+ BlockRef memory ref = Blocks.from(c.state, i);
46
+ AssetAmount memory balance = ref.toBalanceValue(c.state);
47
+ redeemFromBalanceToBalances(c.account, balance, route, writer);
48
+ i = ref.end;
49
+ }
50
+
51
+ return writer.finish();
52
+ }
53
+ }
54
+
55
+ abstract contract RedeemFromCustodyToBalances is CommandBase {
56
+ uint internal immutable redeemFromCustodyToBalancesId = commandId(RDCTB);
57
+ uint private immutable outScale;
58
+ bool private immutable useRoute;
59
+
60
+ constructor(string memory maybeRoute, uint scaledRatio) {
61
+ outScale = scaledRatio;
62
+ useRoute = bytes(maybeRoute).length > 0;
63
+ emit Command(host, RDCTB, maybeRoute, redeemFromCustodyToBalancesId, CUSTODIES, BALANCES);
64
+ }
65
+
66
+ // @dev `custody` is the redeemable claim/share/position amount offered for redemption.
67
+ // `rawRoute` is zero-initialized and should be ignored when `maybeRoute` is empty.
68
+ function redeemFromCustodyToBalances(
69
+ bytes32 account,
70
+ HostAmount memory custody,
71
+ DataRef memory rawRoute,
72
+ Writer memory out
73
+ ) internal virtual;
74
+
75
+ function redeemFromCustodyToBalances(
76
+ CommandContext calldata c
77
+ ) external payable onlyCommand(redeemFromCustodyToBalancesId, c.target) returns (bytes memory) {
78
+ uint i = 0;
79
+ uint q = 0;
80
+ (Writer memory writer, uint end) = Writers.allocScaledBalancesFrom(c.state, i, CUSTODY_KEY, outScale);
81
+
82
+ while (i < end) {
83
+ DataRef memory route;
84
+ if (useRoute) (route, q) = Data.routeFrom(c.request, q);
85
+ BlockRef memory ref = Blocks.from(c.state, i);
86
+ HostAmount memory custody = ref.toCustodyValue(c.state);
87
+ redeemFromCustodyToBalances(c.account, custody, route, writer);
88
+ i = ref.end;
89
+ }
90
+
91
+ return writer.finish();
92
+ }
93
+ }
@@ -0,0 +1,31 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ pragma solidity ^0.8.33;
3
+
4
+ import {CommandBase, CommandContext} from "./Base.sol";
5
+ import {SETUP} from "../utils/Channels.sol";
6
+ import {Data, DataRef, ROUTE_KEY} from "../Blocks.sol";
7
+ using Data for DataRef;
8
+
9
+ string constant NAME = "remove";
10
+
11
+ abstract contract Remove is CommandBase {
12
+ uint internal immutable removeId = commandId(NAME);
13
+
14
+ constructor(string memory route) {
15
+ emit Command(host, NAME, route, removeId, SETUP, SETUP);
16
+ }
17
+
18
+ function remove(bytes32 account, DataRef memory rawRoute) internal virtual;
19
+
20
+ function remove(CommandContext calldata c) external payable onlyCommand(removeId, c.target) returns (bytes memory) {
21
+ uint q = 0;
22
+ while (q < c.request.length) {
23
+ (DataRef memory ref, uint next) = Data.from(c.request, q);
24
+ if (ref.key != ROUTE_KEY) break;
25
+ remove(c.account, ref);
26
+ q = next;
27
+ }
28
+
29
+ return done(0, q);
30
+ }
31
+ }
@@ -0,0 +1,93 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ pragma solidity ^0.8.33;
3
+
4
+ import {CommandContext, CommandBase} from "./Base.sol";
5
+ import {BALANCES, CUSTODIES} from "../utils/Channels.sol";
6
+ import {AssetAmount, HostAmount, BALANCE_KEY, CUSTODY_KEY, ROUTE_EMPTY, Data, DataRef, Blocks, BlockRef, Writers, Writer} from "../Blocks.sol";
7
+
8
+ string constant RFBTB = "repayFromBalanceToBalances";
9
+ string constant RFCTB = "repayFromCustodyToBalances";
10
+
11
+ using Blocks for BlockRef;
12
+ using Data for DataRef;
13
+ using Writers for Writer;
14
+
15
+ abstract contract RepayFromBalanceToBalances is CommandBase {
16
+ uint internal immutable repayFromBalanceToBalancesId = commandId(RFBTB);
17
+ uint private immutable outScale;
18
+ bool private immutable useRoute;
19
+
20
+ constructor(string memory maybeRoute, uint scaledRatio) {
21
+ outScale = scaledRatio;
22
+ useRoute = bytes(maybeRoute).length > 0;
23
+ emit Command(host, RFBTB, maybeRoute, repayFromBalanceToBalancesId, BALANCES, BALANCES);
24
+ }
25
+
26
+ // @dev `balance` is the offered repayment amount and may leave a returned remainder.
27
+ // `rawRoute` is zero-initialized and should be ignored when `maybeRoute` is empty.
28
+ function repayFromBalanceToBalances(
29
+ bytes32 account,
30
+ AssetAmount memory balance,
31
+ DataRef memory rawRoute,
32
+ Writer memory out
33
+ ) internal virtual;
34
+
35
+ function repayFromBalanceToBalances(
36
+ CommandContext calldata c
37
+ ) external payable onlyCommand(repayFromBalanceToBalancesId, c.target) returns (bytes memory) {
38
+ uint i = 0;
39
+ uint q = 0;
40
+ (Writer memory writer, uint end) = Writers.allocScaledBalancesFrom(c.state, i, BALANCE_KEY, outScale);
41
+
42
+ while (i < end) {
43
+ DataRef memory route;
44
+ if (useRoute) (route, q) = Data.routeFrom(c.request, q);
45
+ BlockRef memory ref = Blocks.from(c.state, i);
46
+ AssetAmount memory balance = ref.toBalanceValue(c.state);
47
+ repayFromBalanceToBalances(c.account, balance, route, writer);
48
+ i = ref.end;
49
+ }
50
+
51
+ return writer.finish();
52
+ }
53
+ }
54
+
55
+ abstract contract RepayFromCustodyToBalances is CommandBase {
56
+ uint internal immutable repayFromCustodyToBalancesId = commandId(RFCTB);
57
+ uint private immutable outScale;
58
+ bool private immutable useRoute;
59
+
60
+ constructor(string memory maybeRoute, uint scaledRatio) {
61
+ outScale = scaledRatio;
62
+ useRoute = bytes(maybeRoute).length > 0;
63
+ emit Command(host, RFCTB, maybeRoute, repayFromCustodyToBalancesId, CUSTODIES, BALANCES);
64
+ }
65
+
66
+ // @dev `custody` is the offered repayment amount and may leave a returned remainder.
67
+ // `rawRoute` is zero-initialized and should be ignored when `maybeRoute` is empty.
68
+ function repayFromCustodyToBalances(
69
+ bytes32 account,
70
+ HostAmount memory custody,
71
+ DataRef memory rawRoute,
72
+ Writer memory out
73
+ ) internal virtual;
74
+
75
+ function repayFromCustodyToBalances(
76
+ CommandContext calldata c
77
+ ) external payable onlyCommand(repayFromCustodyToBalancesId, c.target) returns (bytes memory) {
78
+ uint i = 0;
79
+ uint q = 0;
80
+ (Writer memory writer, uint end) = Writers.allocScaledBalancesFrom(c.state, i, CUSTODY_KEY, outScale);
81
+
82
+ while (i < end) {
83
+ DataRef memory route;
84
+ if (useRoute) (route, q) = Data.routeFrom(c.request, q);
85
+ BlockRef memory ref = Blocks.from(c.state, i);
86
+ HostAmount memory custody = ref.toCustodyValue(c.state);
87
+ repayFromCustodyToBalances(c.account, custody, route, writer);
88
+ i = ref.end;
89
+ }
90
+
91
+ return writer.finish();
92
+ }
93
+ }
@@ -0,0 +1,32 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ pragma solidity ^0.8.33;
3
+
4
+ import {CommandContext, CommandBase} from "./Base.sol";
5
+ import {TRANSACTIONS, SETUP} from "../utils/Channels.sol";
6
+ import {BlockRef, TX_KEY, Tx} from "../blocks/Schema.sol";
7
+ import {Blocks} from "../blocks/Readers.sol";
8
+ using Blocks for BlockRef;
9
+
10
+ string constant NAME = "settle";
11
+
12
+ abstract contract Settle is CommandBase {
13
+ uint internal immutable settleId = commandId(NAME);
14
+
15
+ constructor() {
16
+ emit Command(host, NAME, "", settleId, TRANSACTIONS, SETUP);
17
+ }
18
+
19
+ function settle(Tx memory value) internal virtual;
20
+
21
+ function settle(CommandContext calldata c) external payable onlyCommand(settleId, c.target) returns (bytes memory) {
22
+ uint i = 0;
23
+ while (i < c.state.length) {
24
+ BlockRef memory ref = Blocks.from(c.state, i);
25
+ if (ref.key != TX_KEY) break;
26
+ Tx memory value = ref.toTxValue(c.state);
27
+ settle(value);
28
+ i = ref.end;
29
+ }
30
+ return done(0, i);
31
+ }
32
+ }
@@ -0,0 +1,114 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ pragma solidity ^0.8.33;
3
+
4
+ import {CommandContext, CommandBase} from "./Base.sol";
5
+ import {SETUP, BALANCES, CUSTODIES} from "../utils/Channels.sol";
6
+ import {AssetAmount, HostAmount, BALANCE_KEY, CUSTODY_KEY, Blocks, BlockRef, Data, DataRef, Writers, Writer} from "../Blocks.sol";
7
+
8
+ string constant SBTB = "stakeBalanceToBalances";
9
+ string constant SCTB = "stakeCustodyToBalances";
10
+ string constant SCTP = "stakeCustodyToPosition";
11
+
12
+ using Blocks for BlockRef;
13
+ using Data for DataRef;
14
+ using Writers for Writer;
15
+
16
+ abstract contract StakeBalanceToBalances is CommandBase {
17
+ uint internal immutable stakeBalanceToBalancesId = commandId(SBTB);
18
+ uint private immutable outScale;
19
+
20
+ constructor(string memory route, uint scaledRatio) {
21
+ outScale = scaledRatio;
22
+ emit Command(host, SBTB, route, stakeBalanceToBalancesId, BALANCES, BALANCES);
23
+ }
24
+
25
+ function stakeBalanceToBalances(
26
+ bytes32 account,
27
+ AssetAmount memory balance,
28
+ DataRef memory rawRoute,
29
+ Writer memory out
30
+ ) internal virtual;
31
+
32
+ function stakeBalanceToBalances(
33
+ CommandContext calldata c
34
+ ) external payable onlyCommand(stakeBalanceToBalancesId, c.target) returns (bytes memory) {
35
+ uint i = 0;
36
+ uint q = 0;
37
+ (Writer memory writer, uint end) = Writers.allocScaledBalancesFrom(c.state, i, BALANCE_KEY, outScale);
38
+
39
+ while (i < end) {
40
+ DataRef memory route;
41
+ (route, q) = Data.routeFrom(c.request, q);
42
+ BlockRef memory ref = Blocks.from(c.state, i);
43
+ AssetAmount memory balance = ref.toBalanceValue(c.state);
44
+ stakeBalanceToBalances(c.account, balance, route, writer);
45
+ i = ref.end;
46
+ }
47
+
48
+ return writer.finish();
49
+ }
50
+ }
51
+
52
+ abstract contract StakeCustodyToBalances is CommandBase {
53
+ uint internal immutable stakeCustodyToBalancesId = commandId(SCTB);
54
+ uint private immutable outScale;
55
+
56
+ constructor(string memory route, uint scaledRatio) {
57
+ outScale = scaledRatio;
58
+ emit Command(host, SCTB, route, stakeCustodyToBalancesId, CUSTODIES, BALANCES);
59
+ }
60
+
61
+ function stakeCustodyToBalances(
62
+ bytes32 account,
63
+ HostAmount memory custody,
64
+ DataRef memory rawRoute,
65
+ Writer memory out
66
+ ) internal virtual;
67
+
68
+ function stakeCustodyToBalances(
69
+ CommandContext calldata c
70
+ ) external payable onlyCommand(stakeCustodyToBalancesId, c.target) returns (bytes memory) {
71
+ uint i = 0;
72
+ uint q = 0;
73
+ (Writer memory writer, uint end) = Writers.allocScaledBalancesFrom(c.state, i, CUSTODY_KEY, outScale);
74
+
75
+ while (i < end) {
76
+ DataRef memory route;
77
+ (route, q) = Data.routeFrom(c.request, q);
78
+ BlockRef memory ref = Blocks.from(c.state, i);
79
+ HostAmount memory custody = ref.toCustodyValue(c.state);
80
+ stakeCustodyToBalances(c.account, custody, route, writer);
81
+ i = ref.end;
82
+ }
83
+
84
+ return writer.finish();
85
+ }
86
+ }
87
+
88
+ abstract contract StakeCustodyToPosition is CommandBase {
89
+ uint internal immutable stakeCustodyToPositionId = commandId(SCTP);
90
+
91
+ constructor(string memory route) {
92
+ emit Command(host, SCTP, route, stakeCustodyToPositionId, CUSTODIES, SETUP);
93
+ }
94
+
95
+ function stakeCustodyToPosition(bytes32 account, HostAmount memory custody, DataRef memory rawRoute) internal virtual;
96
+
97
+ function stakeCustodyToPosition(
98
+ CommandContext calldata c
99
+ ) external payable onlyCommand(stakeCustodyToPositionId, c.target) returns (bytes memory) {
100
+ uint i = 0;
101
+ uint q = 0;
102
+ while (i < c.state.length) {
103
+ BlockRef memory ref = Blocks.from(c.state, i);
104
+ if (!ref.isCustody()) break;
105
+ HostAmount memory custody = ref.toCustodyValue(c.state);
106
+ DataRef memory route;
107
+ (route, q) = Data.routeFrom(c.request, q);
108
+ stakeCustodyToPosition(c.account, custody, route);
109
+ i = ref.end;
110
+ }
111
+
112
+ return done(0, i);
113
+ }
114
+ }
@@ -0,0 +1,32 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ pragma solidity ^0.8.33;
3
+
4
+ import {CommandBase, CommandContext} from "./Base.sol";
5
+ import {CUSTODIES, SETUP} from "../utils/Channels.sol";
6
+ import {Blocks, BlockRef, HostAmount} from "../Blocks.sol";
7
+ string constant NAME = "supply";
8
+
9
+ using Blocks for BlockRef;
10
+
11
+ abstract contract Supply is CommandBase {
12
+ uint internal immutable supplyId = commandId(NAME);
13
+
14
+ constructor() {
15
+ emit Command(host, NAME, "", supplyId, CUSTODIES, SETUP);
16
+ }
17
+
18
+ function supply(bytes32 account, HostAmount memory value) internal virtual;
19
+
20
+ function supply(CommandContext calldata c) external payable onlyCommand(supplyId, c.target) returns (bytes memory) {
21
+ uint i = 0;
22
+ while (i < c.state.length) {
23
+ BlockRef memory ref = Blocks.from(c.state, i);
24
+ if (!ref.isCustody()) break;
25
+ HostAmount memory value = ref.toCustodyValue(c.state);
26
+ supply(c.account, value);
27
+ i = ref.end;
28
+ }
29
+
30
+ return done(0, i);
31
+ }
32
+ }
@@ -0,0 +1,86 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ pragma solidity ^0.8.33;
3
+
4
+ import {CommandContext, CommandBase} from "./Base.sol";
5
+ import {BALANCES, CUSTODIES} from "../utils/Channels.sol";
6
+ import {AssetAmount, HostAmount, BALANCE_KEY, CUSTODY_KEY, ROUTE_KEY, MINIMUM} from "../blocks/Schema.sol";
7
+ import {Blocks, BlockRef, Data, DataRef, Writers, Writer} from "../Blocks.sol";
8
+ import {routeSchema1} from "../utils/Utils.sol";
9
+ using Blocks for BlockRef;
10
+ using Data for DataRef;
11
+ using Writers for Writer;
12
+
13
+ string constant SEBTB = "swapExactBalanceToBalance";
14
+ string constant SECTB = "swapExactCustodyToBalance";
15
+
16
+ abstract contract SwapExactBalanceToBalance is CommandBase {
17
+ uint internal immutable swapExactBalanceToBalanceId = commandId(SEBTB);
18
+
19
+ constructor(string memory maybeRoute) {
20
+ string memory schema = routeSchema1(maybeRoute, MINIMUM);
21
+ emit Command(host, SEBTB, schema, swapExactBalanceToBalanceId, BALANCES, BALANCES);
22
+ }
23
+
24
+ // @dev implementation extracts the requested minimum from rawRoute.innerMinimum()
25
+ function swapExactBalanceToBalance(
26
+ bytes32 account,
27
+ AssetAmount memory balance,
28
+ DataRef memory rawRoute
29
+ ) internal virtual returns (AssetAmount memory out);
30
+
31
+ function swapExactBalanceToBalance(
32
+ CommandContext calldata c
33
+ ) external payable onlyCommand(swapExactBalanceToBalanceId, c.target) returns (bytes memory) {
34
+ uint i = 0;
35
+ uint q = 0;
36
+ (Writer memory writer, uint end) = Writers.allocBalancesFrom(c.state, i, BALANCE_KEY);
37
+
38
+ while (i < end) {
39
+ DataRef memory route;
40
+ (route, q) = Data.routeFrom(c.request, q);
41
+ BlockRef memory ref = Blocks.from(c.state, i);
42
+ AssetAmount memory balance = ref.toBalanceValue(c.state);
43
+ AssetAmount memory out = swapExactBalanceToBalance(c.account, balance, route);
44
+ writer.appendNonZeroBalance(out);
45
+ i = ref.end;
46
+ }
47
+
48
+ return writer.finish();
49
+ }
50
+ }
51
+
52
+ abstract contract SwapExactCustodyToBalance is CommandBase {
53
+ uint internal immutable swapExactCustodyToBalanceId = commandId(SECTB);
54
+
55
+ constructor(string memory maybeRoute) {
56
+ string memory schema = routeSchema1(maybeRoute, MINIMUM);
57
+ emit Command(host, SECTB, schema, swapExactCustodyToBalanceId, CUSTODIES, BALANCES);
58
+ }
59
+
60
+ // @dev implementation extracts the requested minimum from rawRoute.innerMinimum()
61
+ function swapExactCustodyToBalance(
62
+ bytes32 account,
63
+ HostAmount memory custody,
64
+ DataRef memory rawRoute
65
+ ) internal virtual returns (AssetAmount memory out);
66
+
67
+ function swapExactCustodyToBalance(
68
+ CommandContext calldata c
69
+ ) external payable onlyCommand(swapExactCustodyToBalanceId, c.target) returns (bytes memory) {
70
+ uint i = 0;
71
+ uint q = 0;
72
+ (Writer memory writer, uint end) = Writers.allocBalancesFrom(c.state, i, CUSTODY_KEY);
73
+
74
+ while (i < end) {
75
+ DataRef memory route;
76
+ (route, q) = Data.routeFrom(c.request, q);
77
+ BlockRef memory ref = Blocks.from(c.state, i);
78
+ HostAmount memory custody = ref.toCustodyValue(c.state);
79
+ AssetAmount memory out = swapExactCustodyToBalance(c.account, custody, route);
80
+ writer.appendNonZeroBalance(out);
81
+ i = ref.end;
82
+ }
83
+
84
+ return writer.finish();
85
+ }
86
+ }
@@ -0,0 +1,41 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ pragma solidity ^0.8.33;
3
+
4
+ import {CommandContext, CommandBase} from "./Base.sol";
5
+ import {SETUP} from "../utils/Channels.sol";
6
+ import {AMOUNT, RECIPIENT, AMOUNT_KEY, BlockRef} from "../blocks/Schema.sol";
7
+ import {Blocks} from "../blocks/Readers.sol";
8
+ using Blocks for BlockRef;
9
+
10
+ string constant NAME = "transfer";
11
+ string constant REQUEST = string.concat(AMOUNT, ">", RECIPIENT);
12
+
13
+ abstract contract Transfer is CommandBase {
14
+ uint internal immutable transferId = commandId(NAME);
15
+
16
+ constructor() {
17
+ emit Command(host, NAME, REQUEST, transferId, SETUP, SETUP);
18
+ }
19
+
20
+ function transfer(bytes32 from, bytes32 to, bytes32 asset, bytes32 meta, uint amount) internal virtual;
21
+
22
+ function transfer(bytes32 from, bytes calldata request) internal virtual returns (bytes memory) {
23
+ uint q = 0;
24
+ while (q < request.length) {
25
+ BlockRef memory ref = Blocks.from(request, q);
26
+ if (ref.key != AMOUNT_KEY) break;
27
+ (bytes32 asset, bytes32 meta, uint amount) = ref.unpackAmount(request);
28
+ bytes32 to = ref.innerRecipientAt(request, ref.bound);
29
+ transfer(from, to, asset, meta, amount);
30
+ q = ref.end;
31
+ }
32
+
33
+ return done(0, q);
34
+ }
35
+
36
+ function transfer(
37
+ CommandContext calldata c
38
+ ) external payable onlyCommand(transferId, c.target) returns (bytes memory) {
39
+ return transfer(c.account, c.request);
40
+ }
41
+ }
@@ -0,0 +1,49 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ pragma solidity ^0.8.33;
3
+
4
+ import {CommandContext, CommandBase} from "./Base.sol";
5
+ import {BALANCES} from "../utils/Channels.sol";
6
+ import {AssetAmount, BALANCE_KEY, Blocks, BlockRef, Data, DataRef, Writers, Writer} from "../Blocks.sol";
7
+
8
+ string constant UBTB = "unstakeBalanceToBalances";
9
+
10
+ using Blocks for BlockRef;
11
+ using Data for DataRef;
12
+ using Writers for Writer;
13
+
14
+ abstract contract UnstakeBalanceToBalances is CommandBase {
15
+ uint internal immutable unstakeBalanceToBalancesId = commandId(UBTB);
16
+ uint private immutable outScale;
17
+
18
+ constructor(string memory route, uint scaledRatio) {
19
+ outScale = scaledRatio;
20
+ emit Command(host, UBTB, route, unstakeBalanceToBalancesId, BALANCES, BALANCES);
21
+ }
22
+
23
+ // @dev `balance` is the input stake/share/LP-style position being redeemed.
24
+ function unstakeBalanceToBalances(
25
+ bytes32 account,
26
+ AssetAmount memory balance,
27
+ DataRef memory rawRoute,
28
+ Writer memory out
29
+ ) internal virtual;
30
+
31
+ function unstakeBalanceToBalances(
32
+ CommandContext calldata c
33
+ ) external payable onlyCommand(unstakeBalanceToBalancesId, c.target) returns (bytes memory) {
34
+ uint i = 0;
35
+ uint q = 0;
36
+ (Writer memory writer, uint end) = Writers.allocScaledBalancesFrom(c.state, i, BALANCE_KEY, outScale);
37
+
38
+ while (i < end) {
39
+ DataRef memory route;
40
+ (route, q) = Data.routeFrom(c.request, q);
41
+ BlockRef memory ref = Blocks.from(c.state, i);
42
+ AssetAmount memory balance = ref.toBalanceValue(c.state);
43
+ unstakeBalanceToBalances(c.account, balance, route, writer);
44
+ i = ref.end;
45
+ }
46
+
47
+ return writer.finish();
48
+ }
49
+ }
@@ -0,0 +1,37 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ pragma solidity ^0.8.33;
3
+
4
+ import {CommandContext, CommandBase} from "./Base.sol";
5
+ import {BALANCES, SETUP} from "../utils/Channels.sol";
6
+ import {BlockRef, RECIPIENT} from "../blocks/Schema.sol";
7
+ import {Blocks} from "../blocks/Readers.sol";
8
+ using Blocks for BlockRef;
9
+
10
+ string constant NAME = "withdraw";
11
+
12
+ // @dev Use `withdraw` for externally delivered assets; use `creditBalanceToAccount` for internal balance credits.
13
+ abstract contract Withdraw is CommandBase {
14
+ uint internal immutable withdrawId = commandId(NAME);
15
+
16
+ constructor() {
17
+ emit Command(host, NAME, RECIPIENT, withdrawId, BALANCES, SETUP);
18
+ }
19
+
20
+ function withdraw(bytes32 account, bytes32 asset, bytes32 meta, uint amount) internal virtual;
21
+
22
+ function withdraw(
23
+ CommandContext calldata c
24
+ ) external payable onlyCommand(withdrawId, c.target) returns (bytes memory) {
25
+ bytes32 to = Blocks.resolveRecipient(c.request, 0, c.request.length, c.account);
26
+ uint i = 0;
27
+ while (i < c.state.length) {
28
+ BlockRef memory ref = Blocks.from(c.state, i);
29
+ if (!ref.isBalance()) break;
30
+ (bytes32 asset, bytes32 meta, uint amount) = ref.unpackBalance(c.state);
31
+ withdraw(to, asset, meta, amount);
32
+ i = ref.end;
33
+ }
34
+
35
+ return done(0, i);
36
+ }
37
+ }
@@ -0,0 +1,33 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ pragma solidity ^0.8.33;
3
+
4
+ import {CommandBase, CommandContext} from "../Base.sol";
5
+ import {SETUP} from "../../utils/Channels.sol";
6
+ import {ALLOCATION, ALLOCATION_KEY, BlockRef, HostAmount} from "../../blocks/Schema.sol";
7
+ import {Blocks} from "../../blocks/Readers.sol";
8
+ import {ensureAssetRef} from "../../utils/Assets.sol";
9
+ using Blocks for BlockRef;
10
+
11
+ string constant NAME = "allocate";
12
+
13
+ abstract contract Allocate is CommandBase {
14
+ uint internal immutable allocateId = commandId(NAME);
15
+
16
+ constructor() {
17
+ emit Command(host, NAME, ALLOCATION, allocateId, SETUP, SETUP);
18
+ }
19
+
20
+ function allocate(uint host, bytes32 asset, bytes32 meta, uint amount) internal virtual;
21
+
22
+ function allocate(CommandContext calldata c) external payable onlyAdmin(c.account) onlyCommand(allocateId, c.target) returns (bytes memory) {
23
+ uint i = 0;
24
+ while (i < c.request.length) {
25
+ BlockRef memory ref = Blocks.from(c.request, i);
26
+ if (ref.key != ALLOCATION_KEY) break;
27
+ HostAmount memory v = ref.toAllocationValue(c.request);
28
+ allocate(v.host, v.asset, v.meta, v.amount);
29
+ i = ref.end;
30
+ }
31
+ return done(0, i);
32
+ }
33
+ }
@@ -0,0 +1,34 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ pragma solidity ^0.8.33;
3
+
4
+ import {CommandBase, CommandContext} from "../Base.sol";
5
+ import {SETUP} from "../../utils/Channels.sol";
6
+ import {ASSET, ASSET_KEY, BlockRef} from "../../blocks/Schema.sol";
7
+ import {Blocks} from "../../blocks/Readers.sol";
8
+ using Blocks for BlockRef;
9
+
10
+ string constant NAME = "allowAssets";
11
+
12
+ abstract contract AllowAssets is CommandBase {
13
+ uint internal immutable allowAssetsId = commandId(NAME);
14
+
15
+ constructor() {
16
+ emit Command(host, NAME, ASSET, allowAssetsId, SETUP, SETUP);
17
+ }
18
+
19
+ function allowAsset(bytes32 asset, bytes32 meta) internal virtual returns (bool);
20
+
21
+ function allowAssets(
22
+ CommandContext calldata c
23
+ ) external payable onlyAdmin(c.account) onlyCommand(allowAssetsId, c.target) returns (bytes memory) {
24
+ uint i = 0;
25
+ while (i < c.request.length) {
26
+ BlockRef memory ref = Blocks.from(c.request, i);
27
+ if (ref.key != ASSET_KEY) break;
28
+ (bytes32 asset, bytes32 meta) = ref.unpackAsset(c.request);
29
+ allowAsset(asset, meta);
30
+ i = ref.end;
31
+ }
32
+ return done(0, i);
33
+ }
34
+ }