@superfluid-finance/ethereum-contracts 1.3.2-dev.bf84335.0 → 1.3.2-dev.ccb1898.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 (122) hide show
  1. package/build/contracts/AccessControl.json +1 -1
  2. package/build/contracts/AccessControlEnumerable.json +1 -1
  3. package/build/contracts/Address.json +1 -1
  4. package/build/contracts/AgreementBase.json +18 -18
  5. package/build/contracts/AgreementLibrary.json +1272 -1183
  6. package/build/contracts/BaseRelayRecipient.json +55 -55
  7. package/build/contracts/BatchLiquidator.json +211 -211
  8. package/build/contracts/BatchOperation.json +599 -599
  9. package/build/contracts/CFAv1Library.json +2282 -2282
  10. package/build/contracts/CallUtils.json +198 -198
  11. package/build/contracts/ClosingOnUpdateFlowTestApp.json +2472 -2472
  12. package/build/contracts/ConstantFlowAgreementV1.json +12931 -13308
  13. package/build/contracts/Context.json +1 -1
  14. package/build/contracts/ContextDefinitions.json +599 -599
  15. package/build/contracts/Create2.json +1 -1
  16. package/build/contracts/CustomSuperTokenBase.json +20 -20
  17. package/build/contracts/ERC165.json +1 -1
  18. package/build/contracts/ERC1820Implementer.json +1 -1
  19. package/build/contracts/ERC1820RegistryCompiled.json +22 -22
  20. package/build/contracts/ERC20.json +1 -1
  21. package/build/contracts/ERC20Burnable.json +1 -1
  22. package/build/contracts/ERC20Pausable.json +1 -1
  23. package/build/contracts/ERC20PresetMinterPauser.json +1 -1
  24. package/build/contracts/ERC20WithTokenInfo.json +23 -23
  25. package/build/contracts/ERC777Helper.json +380 -380
  26. package/build/contracts/ERC777RecipientDrainingGas.json +858 -858
  27. package/build/contracts/ERC777RecipientReverting.json +858 -858
  28. package/build/contracts/EnumerableSet.json +1 -1
  29. package/build/contracts/EventsEmitter.json +43 -43
  30. package/build/contracts/ExclusiveInflowTestApp.json +2473 -2473
  31. package/build/contracts/FixedSizeData.json +200 -200
  32. package/build/contracts/FlowExchangeTestApp.json +2473 -2473
  33. package/build/contracts/FlowOperatorDefinitions.json +599 -599
  34. package/build/contracts/FullUpgradableSuperTokenProxy.json +115 -115
  35. package/build/contracts/IAccessControl.json +1 -1
  36. package/build/contracts/IAccessControlEnumerable.json +1 -1
  37. package/build/contracts/IConstantFlowAgreementV1.json +567 -567
  38. package/build/contracts/IDAv1Library.json +2614 -2614
  39. package/build/contracts/IERC165.json +1 -1
  40. package/build/contracts/IERC1820Implementer.json +1 -1
  41. package/build/contracts/IERC1820Registry.json +1 -1
  42. package/build/contracts/IERC20.json +1 -1
  43. package/build/contracts/IERC20Metadata.json +1 -1
  44. package/build/contracts/IERC20Permit.json +1 -1
  45. package/build/contracts/IERC777.json +1 -1
  46. package/build/contracts/IERC777Recipient.json +1 -1
  47. package/build/contracts/IERC777Sender.json +1 -1
  48. package/build/contracts/IInstantDistributionAgreementV1.json +640 -640
  49. package/build/contracts/IMaticBridgedNativeSuperToken.json +63 -63
  50. package/build/contracts/IMaticBridgedNativeSuperTokenCustom.json +63 -63
  51. package/build/contracts/IMultiSigWallet.json +24 -24
  52. package/build/contracts/IPureSuperToken.json +43 -43
  53. package/build/contracts/IPureSuperTokenCustom.json +43 -43
  54. package/build/contracts/IRelayRecipient.json +27 -27
  55. package/build/contracts/IResolver.json +38 -38
  56. package/build/contracts/ISETH.json +48 -48
  57. package/build/contracts/ISETHCustom.json +48 -48
  58. package/build/contracts/ISuperAgreement.json +50 -50
  59. package/build/contracts/ISuperApp.json +192 -192
  60. package/build/contracts/ISuperToken.json +524 -524
  61. package/build/contracts/ISuperTokenFactory.json +161 -161
  62. package/build/contracts/ISuperfluid.json +1145 -1178
  63. package/build/contracts/ISuperfluidGovernance.json +243 -243
  64. package/build/contracts/ISuperfluidToken.json +389 -389
  65. package/build/contracts/ITOGAv1.json +1442 -1442
  66. package/build/contracts/ITOGAv2.json +1442 -1442
  67. package/build/contracts/Initializable.json +1 -1
  68. package/build/contracts/InstantDistributionAgreementV1.json +5820 -5820
  69. package/build/contracts/MaticBridgedNativeSuperTokenProxy.json +231 -231
  70. package/build/contracts/NonClosableOutflowTestApp.json +2473 -2473
  71. package/build/contracts/Ownable.json +1 -1
  72. package/build/contracts/Pausable.json +1 -1
  73. package/build/contracts/Proxy.json +1 -1
  74. package/build/contracts/PureSuperToken.json +109 -109
  75. package/build/contracts/Resolver.json +98 -98
  76. package/build/contracts/SETHProxy.json +227 -227
  77. package/build/contracts/SafeCast.json +1 -1
  78. package/build/contracts/SafeERC20.json +1 -1
  79. package/build/contracts/SafeMath.json +1 -1
  80. package/build/contracts/SelfDeletingFlowTestApp.json +2472 -2472
  81. package/build/contracts/SlotsBitmapLibrary.json +457 -457
  82. package/build/contracts/StreamRedirector.json +748 -748
  83. package/build/contracts/Strings.json +1 -1
  84. package/build/contracts/SuperAppBase.json +240 -240
  85. package/build/contracts/SuperAppDefinitions.json +599 -599
  86. package/build/contracts/SuperToken.json +4732 -4732
  87. package/build/contracts/SuperTokenFactory.json +750 -750
  88. package/build/contracts/SuperTokenFactoryBase.json +738 -738
  89. package/build/contracts/SuperTokenFactoryHelper.json +743 -743
  90. package/build/contracts/SuperUpgrader.json +440 -440
  91. package/build/contracts/Superfluid.json +15366 -16050
  92. package/build/contracts/SuperfluidFrameworkDeployer.json +765 -765
  93. package/build/contracts/SuperfluidGovernanceBase.json +2233 -2233
  94. package/build/contracts/SuperfluidGovernanceConfigs.json +599 -599
  95. package/build/contracts/SuperfluidGovernanceII.json +149 -149
  96. package/build/contracts/SuperfluidGovernanceIIProxy.json +131 -131
  97. package/build/contracts/SuperfluidLoader.json +210 -210
  98. package/build/contracts/SuperfluidToken.json +2420 -2420
  99. package/build/contracts/TOGA.json +1461 -1461
  100. package/build/contracts/TestGovernance.json +248 -248
  101. package/build/contracts/TestToken.json +93 -93
  102. package/build/contracts/TokenCustodian.json +214 -214
  103. package/build/contracts/TokenInfo.json +32 -32
  104. package/build/contracts/UUPSProxiable.json +139 -139
  105. package/build/contracts/UUPSProxy.json +81 -81
  106. package/build/contracts/UUPSUtils.json +37 -37
  107. package/build/contracts-sizes.txt +6 -6
  108. package/contracts/agreements/AgreementLibrary.sol +39 -15
  109. package/contracts/agreements/ConstantFlowAgreementV1.sol +65 -53
  110. package/contracts/interfaces/superfluid/ISuperfluid.sol +29 -23
  111. package/contracts/mocks/AgreementMock.sol +2 -2
  112. package/contracts/mocks/CFAAppMocks.sol +1 -1
  113. package/contracts/mocks/MultiFlowTesterApp.sol +13 -12
  114. package/contracts/mocks/SuperTokenMock.sol +2 -2
  115. package/contracts/mocks/SuperfluidMock.sol +9 -9
  116. package/contracts/superfluid/Superfluid.sol +34 -38
  117. package/contracts/superfluid/SuperfluidToken.sol +14 -14
  118. package/package.json +2 -2
  119. package/scripts/deploy-mfa.ts +27 -0
  120. package/scripts/libs/getConfig.js +14 -0
  121. package/utils/README.md +18 -0
  122. package/utils/mfa-tester.html +687 -0
@@ -15,15 +15,15 @@
15
15
  "absolutePath": "project:/contracts/upgradability/UUPSUtils.sol",
16
16
  "exportedSymbols": {
17
17
  "UUPSUtils": [
18
- 37106
18
+ 37098
19
19
  ]
20
20
  },
21
- "id": 37107,
21
+ "id": 37099,
22
22
  "license": "AGPLv3",
23
23
  "nodeType": "SourceUnit",
24
24
  "nodes": [
25
25
  {
26
- "id": 37084,
26
+ "id": 37076,
27
27
  "literals": [
28
28
  "solidity",
29
29
  "0.8",
@@ -39,15 +39,15 @@
39
39
  "contractDependencies": [],
40
40
  "contractKind": "library",
41
41
  "documentation": {
42
- "id": 37085,
42
+ "id": 37077,
43
43
  "nodeType": "StructuredDocumentation",
44
44
  "src": "60:76:97",
45
45
  "text": " @title UUPS (Universal Upgradeable Proxy Standard) Shared Library"
46
46
  },
47
47
  "fullyImplemented": true,
48
- "id": 37106,
48
+ "id": 37098,
49
49
  "linearizedBaseContracts": [
50
- 37106
50
+ 37098
51
51
  ],
52
52
  "name": "UUPSUtils",
53
53
  "nameLocation": "145:9:97",
@@ -56,17 +56,17 @@
56
56
  {
57
57
  "constant": true,
58
58
  "documentation": {
59
- "id": 37086,
59
+ "id": 37078,
60
60
  "nodeType": "StructuredDocumentation",
61
61
  "src": "162:288:97",
62
62
  "text": " @dev Implementation slot constant.\n Using https://eips.ethereum.org/EIPS/eip-1967 standard\n Storage slot 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc\n (obtained as bytes32(uint256(keccak256('eip1967.proxy.implementation')) - 1))."
63
63
  },
64
- "id": 37089,
64
+ "id": 37081,
65
65
  "mutability": "constant",
66
66
  "name": "_IMPLEMENTATION_SLOT",
67
67
  "nameLocation": "481:20:97",
68
68
  "nodeType": "VariableDeclaration",
69
- "scope": 37106,
69
+ "scope": 37098,
70
70
  "src": "455:115:97",
71
71
  "stateVariable": true,
72
72
  "storageLocation": "default",
@@ -75,7 +75,7 @@
75
75
  "typeString": "bytes32"
76
76
  },
77
77
  "typeName": {
78
- "id": 37087,
78
+ "id": 37079,
79
79
  "name": "bytes32",
80
80
  "nodeType": "ElementaryTypeName",
81
81
  "src": "455:7:97",
@@ -86,7 +86,7 @@
86
86
  },
87
87
  "value": {
88
88
  "hexValue": "307833363038393461313362613161333231303636376338323834393264623938646361336532303736636333373335613932306133636135303564333832626263",
89
- "id": 37088,
89
+ "id": 37080,
90
90
  "isConstant": false,
91
91
  "isLValue": false,
92
92
  "isPure": true,
@@ -104,7 +104,7 @@
104
104
  },
105
105
  {
106
106
  "body": {
107
- "id": 37096,
107
+ "id": 37088,
108
108
  "nodeType": "Block",
109
109
  "src": "681:107:97",
110
110
  "statements": [
@@ -145,33 +145,33 @@
145
145
  "evmVersion": "london",
146
146
  "externalReferences": [
147
147
  {
148
- "declaration": 37089,
148
+ "declaration": 37081,
149
149
  "isOffset": false,
150
150
  "isSlot": false,
151
151
  "src": "751:20:97",
152
152
  "valueSize": 1
153
153
  },
154
154
  {
155
- "declaration": 37093,
155
+ "declaration": 37085,
156
156
  "isOffset": false,
157
157
  "isSlot": false,
158
158
  "src": "737:4:97",
159
159
  "valueSize": 1
160
160
  }
161
161
  ],
162
- "id": 37095,
162
+ "id": 37087,
163
163
  "nodeType": "InlineAssembly",
164
164
  "src": "691:91:97"
165
165
  }
166
166
  ]
167
167
  },
168
168
  "documentation": {
169
- "id": 37090,
169
+ "id": 37082,
170
170
  "nodeType": "StructuredDocumentation",
171
171
  "src": "577:36:97",
172
172
  "text": "@dev Get implementation address."
173
173
  },
174
- "id": 37097,
174
+ "id": 37089,
175
175
  "implemented": true,
176
176
  "kind": "function",
177
177
  "modifiers": [],
@@ -179,23 +179,23 @@
179
179
  "nameLocation": "627:14:97",
180
180
  "nodeType": "FunctionDefinition",
181
181
  "parameters": {
182
- "id": 37091,
182
+ "id": 37083,
183
183
  "nodeType": "ParameterList",
184
184
  "parameters": [],
185
185
  "src": "641:2:97"
186
186
  },
187
187
  "returnParameters": {
188
- "id": 37094,
188
+ "id": 37086,
189
189
  "nodeType": "ParameterList",
190
190
  "parameters": [
191
191
  {
192
192
  "constant": false,
193
- "id": 37093,
193
+ "id": 37085,
194
194
  "mutability": "mutable",
195
195
  "name": "impl",
196
196
  "nameLocation": "675:4:97",
197
197
  "nodeType": "VariableDeclaration",
198
- "scope": 37097,
198
+ "scope": 37089,
199
199
  "src": "667:12:97",
200
200
  "stateVariable": false,
201
201
  "storageLocation": "default",
@@ -204,7 +204,7 @@
204
204
  "typeString": "address"
205
205
  },
206
206
  "typeName": {
207
- "id": 37092,
207
+ "id": 37084,
208
208
  "name": "address",
209
209
  "nodeType": "ElementaryTypeName",
210
210
  "src": "667:7:97",
@@ -219,7 +219,7 @@
219
219
  ],
220
220
  "src": "666:14:97"
221
221
  },
222
- "scope": 37106,
222
+ "scope": 37098,
223
223
  "src": "618:170:97",
224
224
  "stateMutability": "view",
225
225
  "virtual": false,
@@ -227,7 +227,7 @@
227
227
  },
228
228
  {
229
229
  "body": {
230
- "id": 37104,
230
+ "id": 37096,
231
231
  "nodeType": "Block",
232
232
  "src": "896:171:97",
233
233
  "statements": [
@@ -266,33 +266,33 @@
266
266
  "evmVersion": "london",
267
267
  "externalReferences": [
268
268
  {
269
- "declaration": 37089,
269
+ "declaration": 37081,
270
270
  "isOffset": false,
271
271
  "isSlot": false,
272
272
  "src": "988:20:97",
273
273
  "valueSize": 1
274
274
  },
275
275
  {
276
- "declaration": 37100,
276
+ "declaration": 37092,
277
277
  "isOffset": false,
278
278
  "isSlot": false,
279
279
  "src": "1026:11:97",
280
280
  "valueSize": 1
281
281
  }
282
282
  ],
283
- "id": 37103,
283
+ "id": 37095,
284
284
  "nodeType": "InlineAssembly",
285
285
  "src": "906:155:97"
286
286
  }
287
287
  ]
288
288
  },
289
289
  "documentation": {
290
- "id": 37098,
290
+ "id": 37090,
291
291
  "nodeType": "StructuredDocumentation",
292
292
  "src": "794:40:97",
293
293
  "text": "@dev Set new implementation address."
294
294
  },
295
- "id": 37105,
295
+ "id": 37097,
296
296
  "implemented": true,
297
297
  "kind": "function",
298
298
  "modifiers": [],
@@ -300,17 +300,17 @@
300
300
  "nameLocation": "848:17:97",
301
301
  "nodeType": "FunctionDefinition",
302
302
  "parameters": {
303
- "id": 37101,
303
+ "id": 37093,
304
304
  "nodeType": "ParameterList",
305
305
  "parameters": [
306
306
  {
307
307
  "constant": false,
308
- "id": 37100,
308
+ "id": 37092,
309
309
  "mutability": "mutable",
310
310
  "name": "codeAddress",
311
311
  "nameLocation": "874:11:97",
312
312
  "nodeType": "VariableDeclaration",
313
- "scope": 37105,
313
+ "scope": 37097,
314
314
  "src": "866:19:97",
315
315
  "stateVariable": false,
316
316
  "storageLocation": "default",
@@ -319,7 +319,7 @@
319
319
  "typeString": "address"
320
320
  },
321
321
  "typeName": {
322
- "id": 37099,
322
+ "id": 37091,
323
323
  "name": "address",
324
324
  "nodeType": "ElementaryTypeName",
325
325
  "src": "866:7:97",
@@ -335,19 +335,19 @@
335
335
  "src": "865:21:97"
336
336
  },
337
337
  "returnParameters": {
338
- "id": 37102,
338
+ "id": 37094,
339
339
  "nodeType": "ParameterList",
340
340
  "parameters": [],
341
341
  "src": "896:0:97"
342
342
  },
343
- "scope": 37106,
343
+ "scope": 37098,
344
344
  "src": "839:228:97",
345
345
  "stateMutability": "nonpayable",
346
346
  "virtual": false,
347
347
  "visibility": "internal"
348
348
  }
349
349
  ],
350
- "scope": 37107,
350
+ "scope": 37099,
351
351
  "src": "137:933:97",
352
352
  "usedErrors": []
353
353
  }
@@ -360,7 +360,7 @@
360
360
  },
361
361
  "networks": {},
362
362
  "schemaVersion": "3.4.8",
363
- "updatedAt": "2022-08-11T12:58:52.103Z",
363
+ "updatedAt": "2022-08-23T08:53:53.802Z",
364
364
  "devdoc": {
365
365
  "kind": "dev",
366
366
  "methods": {},
@@ -1,11 +1,11 @@
1
- Superfluid.sol : 22388 (2188)
1
+ SuperTokenFactory.sol : 5370 (19206)
2
2
  FullUpgradableSuperTokenProxy.sol : 606 (23970)
3
+ Superfluid.sol : 22285 (2291)
4
+ SuperToken.sol : 19203 (5373)
3
5
  SuperfluidToken.sol : 2 (24574)
4
- SuperToken.sol : 19204 (5372)
5
- SuperTokenFactory.sol : 5370 (19206)
6
+ InstantDistributionAgreementV1.sol : 21835 (2741)
6
7
  AgreementLibrary.sol : 143 (24433)
7
- ConstantFlowAgreementV1.sol : 21683 (2893)
8
+ ConstantFlowAgreementV1.sol : 21805 (2771)
8
9
  AgreementBase.sol : 2 (24574)
9
- InstantDistributionAgreementV1.sol : 21844 (2732)
10
- SuperfluidGovernanceBase.sol : 2 (24574)
11
10
  SuperfluidGovernanceII.sol : 7347 (17229)
11
+ SuperfluidGovernanceBase.sol : 2 (24574)
@@ -53,8 +53,8 @@ library AgreementLibrary {
53
53
  address account;
54
54
  bytes32 agreementId;
55
55
  bytes agreementData;
56
- uint256 appAllowanceGranted;
57
- int256 appAllowanceUsed;
56
+ uint256 appCreditGranted;
57
+ int256 appCreditUsed;
58
58
  uint256 noopBit;
59
59
  }
60
60
 
@@ -140,17 +140,41 @@ library AgreementLibrary {
140
140
 
141
141
  appContext = ISuperfluid(msg.sender).decodeCtx(newCtx);
142
142
 
143
- // adjust allowance used to the range [appAllowanceWanted..appAllowanceGranted]
144
- appContext.appAllowanceUsed = max(0, min(
145
- inputs.appAllowanceGranted.toInt256(),
146
- max(appContext.appAllowanceWanted.toInt256(), appContext.appAllowanceUsed)));
147
-
143
+ // adjust credit used to the range [appCreditUsed..appCreditGranted]
144
+ appContext.appCreditUsed = _adjustNewAppCreditUsed(
145
+ inputs.appCreditGranted,
146
+ appContext.appCreditUsed
147
+ );
148
148
  }
149
149
  // [SECURITY] NOTE: ctx should be const, do not modify it ever to ensure callback stack correctness
150
- newCtx = _popCallbackStack(ctx, appContext.appAllowanceUsed);
150
+ newCtx = _popCallbackStack(ctx, appContext.appCreditUsed);
151
151
  }
152
152
  }
153
153
 
154
+ /**
155
+ * @dev Determines how much app credit the app will use.
156
+ * @param appCreditGranted set prior to callback based on input flow
157
+ * @param appCallbackDepositDelta set in callback - sum of deposit deltas of callback agreements and
158
+ * current flow owed deposit amount
159
+ */
160
+ function _adjustNewAppCreditUsed(
161
+ uint256 appCreditGranted,
162
+ int256 appCallbackDepositDelta
163
+ ) internal pure returns (int256) {
164
+ // NOTE: we use max(0, ...) because appCallbackDepositDelta can be negative and appCallbackDepositDelta
165
+ // should never go below 0, otherwise the SuperApp can return more money than borrowed
166
+ return max(
167
+ 0,
168
+
169
+ // NOTE: we use min(appCreditGranted, appCallbackDepositDelta) to ensure that the SuperApp borrows
170
+ // appCreditGranted at most and appCallbackDepositDelta at least (if smaller than appCreditGranted)
171
+ min(
172
+ appCreditGranted.toInt256(),
173
+ appCallbackDepositDelta
174
+ )
175
+ );
176
+ }
177
+
154
178
  function _selectorFromNoopBit(uint256 noopBit)
155
179
  private pure
156
180
  returns (bytes4 selector)
@@ -177,25 +201,25 @@ library AgreementLibrary {
177
201
  private
178
202
  returns (bytes memory appCtx)
179
203
  {
180
- // app allowance params stack PUSH
181
- // pass app allowance and current allowance used to the app,
204
+ // app credit params stack PUSH
205
+ // pass app credit and current credit used to the app,
182
206
  appCtx = ISuperfluid(msg.sender).appCallbackPush(
183
207
  ctx,
184
208
  ISuperApp(inputs.account),
185
- inputs.appAllowanceGranted,
186
- inputs.appAllowanceUsed,
209
+ inputs.appCreditGranted,
210
+ inputs.appCreditUsed,
187
211
  inputs.token);
188
212
  }
189
213
 
190
214
  function _popCallbackStack(
191
215
  bytes memory ctx,
192
- int256 appAllowanceUsedDelta
216
+ int256 appCreditUsedDelta
193
217
  )
194
218
  private
195
219
  returns (bytes memory newCtx)
196
220
  {
197
- // app allowance params stack POP
198
- return ISuperfluid(msg.sender).appCallbackPop(ctx, appAllowanceUsedDelta);
221
+ // app credit params stack POP
222
+ return ISuperfluid(msg.sender).appCallbackPop(ctx, appCreditUsedDelta);
199
223
  }
200
224
 
201
225
  /**************************************************************************
@@ -19,7 +19,6 @@ import { AgreementBase } from "./AgreementBase.sol";
19
19
  import { SafeCast } from "@openzeppelin/contracts/utils/math/SafeCast.sol";
20
20
  import { AgreementLibrary } from "./AgreementLibrary.sol";
21
21
 
22
-
23
22
  /**
24
23
  * @title ConstantFlowAgreementV1 contract
25
24
  * @author Superfluid
@@ -898,15 +897,14 @@ contract ConstantFlowAgreementV1 is
898
897
  FlowData memory newFlowData;
899
898
  (depositDelta,,newFlowData) = _changeFlow(
900
899
  currentContext.timestamp,
901
- currentContext.appAllowanceToken,
900
+ currentContext.appCreditToken,
902
901
  token, flowParams, oldFlowData);
903
902
 
904
- // STEP 2: update app allowance used
905
- if (currentContext.appAllowanceToken == token) {
906
- newCtx = ISuperfluid(msg.sender).ctxUseAllowance(
903
+ // STEP 2: update app credit used
904
+ if (currentContext.appCreditToken == token) {
905
+ newCtx = ISuperfluid(msg.sender).ctxUseCredit(
907
906
  ctx,
908
- newFlowData.deposit, // allowanceWantedMore
909
- depositDelta // allowanceUsedDelta
907
+ depositDelta
910
908
  );
911
909
  } else {
912
910
  newCtx = ctx;
@@ -959,14 +957,34 @@ contract ConstantFlowAgreementV1 is
959
957
  }
960
958
  vars.cbdata = AgreementLibrary.callAppBeforeCallback(cbStates, ctx);
961
959
 
962
- (,cbStates.appAllowanceGranted,) = _changeFlow(
960
+ ISuperfluidGovernance gov = ISuperfluidGovernance(ISuperfluid(msg.sender).getGovernance());
961
+
962
+ (,cbStates.appCreditGranted,) = _changeFlow(
963
963
  currentContext.timestamp,
964
- currentContext.appAllowanceToken,
964
+ currentContext.appCreditToken,
965
965
  token, flowParams, oldFlowData);
966
- cbStates.appAllowanceGranted = cbStates.appAllowanceGranted * uint256(currentContext.appCallbackLevel + 1);
967
- cbStates.appAllowanceUsed = oldFlowData.owedDeposit.toInt256();
966
+
967
+
968
+ // Rule CFA-2
969
+ // https://github.com/superfluid-finance/protocol-monorepo/wiki/About-App-Credit
970
+ // Allow apps to take an additional amount of app credit (minimum deposit)
971
+ uint256 minimumDeposit = gov.getConfigAsUint256(
972
+ ISuperfluid(msg.sender), token, SUPERTOKEN_MINIMUM_DEPOSIT_KEY);
973
+
974
+ // NOTE: we do not provide additionalAppCreditAmount when cbStates.appCreditGranted is 0
975
+ // (closing streams)
976
+ uint256 additionalAppCreditAmount = cbStates.appCreditGranted == 0
977
+ ? 0
978
+ : AgreementLibrary.max(
979
+ DEFAULT_MINIMUM_DEPOSIT,
980
+ minimumDeposit
981
+ );
982
+ cbStates.appCreditGranted = cbStates.appCreditGranted + additionalAppCreditAmount;
983
+
984
+ cbStates.appCreditUsed = oldFlowData.owedDeposit.toInt256();
985
+
968
986
  // - each app level can at least "relay" the same amount of input flow rate to others
969
- // - each app level get a same amount of allowance
987
+ // - each app level gets the same amount of credit
970
988
 
971
989
  if (optype == FlowChangeType.CREATE_FLOW) {
972
990
  cbStates.noopBit = SuperAppDefinitions.AFTER_AGREEMENT_CREATED_NOOP;
@@ -983,38 +1001,31 @@ contract ConstantFlowAgreementV1 is
983
1001
  } else {
984
1002
  (,,vars.newFlowData) = _changeFlow(
985
1003
  currentContext.timestamp,
986
- currentContext.appAllowanceToken,
1004
+ currentContext.appCreditToken,
987
1005
  token, flowParams, oldFlowData);
988
1006
  }
989
1007
 
990
1008
  // REVIEW the re-entrace assumptions from this point on
991
1009
 
992
- // NOTE: vars.appContext.appAllowanceUsed will be adjusted by callAppAfterCallback
993
- // and its range will be [0, currentContext.appAllowance]
1010
+ // NOTE: vars.appContext.appCreditUsed will be adjusted by callAppAfterCallback
1011
+ // and its range will be [0, currentContext.appCreditGranted]
994
1012
  {
995
- // clipping the allowance used amount before storing
996
- if (vars.appContext.appAllowanceUsed > 0) {
997
- // give more to the app
998
- vars.appContext.appAllowanceUsed =
999
- _clipDepositNumber(vars.appContext.appAllowanceUsed.toUint256()).toInt256();
1000
- }
1001
-
1002
- int256 appAllowanceDelta = vars.appContext.appAllowanceUsed
1013
+ int256 appCreditDelta = vars.appContext.appCreditUsed
1003
1014
  - oldFlowData.owedDeposit.toInt256();
1004
1015
 
1005
- // update flow data and account state with the allowance delta
1016
+ // update flow data and account state with the credit delta
1006
1017
  {
1007
1018
  vars.newFlowData.deposit = (vars.newFlowData.deposit.toInt256()
1008
- + appAllowanceDelta).toUint256();
1019
+ + appCreditDelta).toUint256();
1009
1020
  vars.newFlowData.owedDeposit = (vars.newFlowData.owedDeposit.toInt256()
1010
- + appAllowanceDelta).toUint256();
1021
+ + appCreditDelta).toUint256();
1011
1022
  token.updateAgreementData(flowParams.flowId, _encodeFlowData(vars.newFlowData));
1012
1023
  // update sender and receiver deposit (for sender) and owed deposit (for receiver)
1013
1024
  _updateAccountFlowState(
1014
1025
  token,
1015
1026
  flowParams.sender,
1016
1027
  0, // flow rate delta
1017
- appAllowanceDelta, // deposit delta
1028
+ appCreditDelta, // deposit delta
1018
1029
  0, // owed deposit delta
1019
1030
  currentContext.timestamp
1020
1031
  );
@@ -1023,22 +1034,21 @@ contract ConstantFlowAgreementV1 is
1023
1034
  flowParams.receiver,
1024
1035
  0, // flow rate delta
1025
1036
  0, // deposit delta
1026
- appAllowanceDelta, // owed deposit delta
1037
+ appCreditDelta, // owed deposit delta
1027
1038
  currentContext.timestamp
1028
1039
  );
1029
1040
  }
1030
1041
 
1031
- if (address(currentContext.appAllowanceToken) == address(0) ||
1032
- currentContext.appAllowanceToken == token)
1042
+ if (address(currentContext.appCreditToken) == address(0) ||
1043
+ currentContext.appCreditToken == token)
1033
1044
  {
1034
- newCtx = ISuperfluid(msg.sender).ctxUseAllowance(
1045
+ newCtx = ISuperfluid(msg.sender).ctxUseCredit(
1035
1046
  newCtx,
1036
- vars.newFlowData.deposit, // allowanceWantedMore
1037
- appAllowanceDelta // allowanceUsedDelta
1047
+ appCreditDelta
1038
1048
  );
1039
1049
  }
1040
1050
 
1041
- // if receiver super app doesn't have enough available balance to give back app allowance
1051
+ // if receiver super app doesn't have enough available balance to give back app credit
1042
1052
  // revert (non termination callbacks),
1043
1053
  // or take it from the sender and jail the app
1044
1054
  if (ISuperfluid(msg.sender).isApp(ISuperApp(flowParams.receiver))) {
@@ -1055,8 +1065,10 @@ contract ConstantFlowAgreementV1 is
1055
1065
  int256 userDamageAmount = AgreementLibrary.min(
1056
1066
  // user will take the damage if the app is broke,
1057
1067
  -availableBalance,
1058
- // but user's damage is limited to the amount of app allowance it gives to the app
1059
- AgreementLibrary.max(0, -appAllowanceDelta));
1068
+ // but user's damage is limited to the amount of app credit it gives to the app
1069
+ // appCreditDelta should ALWAYS be negative because we are closing an agreement
1070
+ // therefore the value will be positive due to the '-' sign in front of it
1071
+ -appCreditDelta);
1060
1072
  token.settleBalance(
1061
1073
  flowParams.sender,
1062
1074
  -userDamageAmount
@@ -1085,11 +1097,11 @@ contract ConstantFlowAgreementV1 is
1085
1097
  *
1086
1098
  * NOTE:
1087
1099
  * - leaving owed deposit unchanged for later adjustment
1088
- * - depositDelta output is always clipped (see _clipDepositNumber)
1100
+ * - depositDelta output is always clipped (see _clipDepositNumberRoundingUp)
1089
1101
  */
1090
1102
  function _changeFlow(
1091
1103
  uint256 currentTimestamp,
1092
- ISuperfluidToken appAllowanceToken,
1104
+ ISuperfluidToken appCreditToken,
1093
1105
  ISuperfluidToken token,
1094
1106
  FlowParams memory flowParams,
1095
1107
  FlowData memory oldFlowData
@@ -1097,7 +1109,7 @@ contract ConstantFlowAgreementV1 is
1097
1109
  private
1098
1110
  returns (
1099
1111
  int256 depositDelta,
1100
- uint256 appAllowanceBase,
1112
+ uint256 appCreditBase,
1101
1113
  FlowData memory newFlowData
1102
1114
  )
1103
1115
  {
@@ -1110,25 +1122,25 @@ contract ConstantFlowAgreementV1 is
1110
1122
  ISuperfluidGovernance gov = ISuperfluidGovernance(ISuperfluid(msg.sender).getGovernance());
1111
1123
  minimumDeposit = gov.getConfigAsUint256(
1112
1124
  ISuperfluid(msg.sender), token, SUPERTOKEN_MINIMUM_DEPOSIT_KEY);
1113
- // rounding up the number for app allowance too
1125
+ // rounding up the number for app credit too
1114
1126
  // CAVEAT:
1115
1127
  // - Now app could create a flow rate that is slightly higher than the incoming flow rate.
1116
1128
  // - The app may be jailed due to negative balance if it does this without its own balance.
1117
1129
  // Rule of thumbs:
1118
- // - App can use app allowance to create a flow that has the same incoming flow rate
1130
+ // - App can use app credit to create a flow that has the same incoming flow rate
1119
1131
  // - But due to deposit clipping, there is no guarantee that the sum of the out going flow
1120
- // deposit can be covered by the allowance always.
1121
- // - It is advisable for the app to check the allowance usages carefully, and if possible
1132
+ // deposit can be covered by the credit always.
1133
+ // - It is advisable for the app to check the credit usages carefully, and if possible
1122
1134
  // Always have some its own balances to cover the deposits.
1123
1135
 
1124
1136
  // preliminary calc of new deposit required, may be changed later in step 2.
1125
1137
  // used as a variable holding the new deposit amount in the meantime
1126
- appAllowanceBase = _calculateDeposit(flowParams.flowRate, liquidationPeriod);
1138
+ appCreditBase = _calculateDeposit(flowParams.flowRate, liquidationPeriod);
1127
1139
  }
1128
1140
 
1129
1141
  // STEP 2: apply minimum deposit rule and calculate deposit delta
1130
1142
  // preliminary calc depositDelta (minimum deposit rule not yet applied)
1131
- depositDelta = appAllowanceBase.toInt256()
1143
+ depositDelta = appCreditBase.toInt256()
1132
1144
  - oldFlowData.deposit.toInt256()
1133
1145
  + oldFlowData.owedDeposit.toInt256();
1134
1146
 
@@ -1143,11 +1155,11 @@ contract ConstantFlowAgreementV1 is
1143
1155
  newDeposit = minimumDeposit;
1144
1156
  }
1145
1157
 
1146
- // allowance should be of the same token
1147
- if (address(appAllowanceToken) != address(0) &&
1148
- appAllowanceToken != token)
1158
+ // credit should be of the same token
1159
+ if (address(appCreditToken) != address(0) &&
1160
+ appCreditToken != token)
1149
1161
  {
1150
- appAllowanceBase = 0;
1162
+ appCreditBase = 0;
1151
1163
  }
1152
1164
 
1153
1165
  // STEP 3: update current flow info
@@ -1204,9 +1216,9 @@ contract ConstantFlowAgreementV1 is
1204
1216
  )
1205
1217
  private view
1206
1218
  {
1207
- // do not enforce balance checks during callbacks for the appAllowanceToken
1219
+ // do not enforce balance checks during callbacks for the appCreditToken
1208
1220
  if (currentContext.callType != ContextDefinitions.CALL_INFO_CALL_TYPE_APP_CALLBACK ||
1209
- currentContext.appAllowanceToken != token) {
1221
+ currentContext.appCreditToken != token) {
1210
1222
  (int256 availableBalance,,) = token.realtimeBalanceOf(currentContext.msgSender, currentContext.timestamp);
1211
1223
  require(availableBalance >= 0, "CFA: not enough available balance");
1212
1224
  }
@@ -1296,7 +1308,7 @@ contract ConstantFlowAgreementV1 is
1296
1308
  return ((deposit >> 32)) << 32;
1297
1309
  }
1298
1310
 
1299
- function _clipDepositNumber(uint256 deposit)
1311
+ function _clipDepositNumberRoundingUp(uint256 deposit)
1300
1312
  internal pure
1301
1313
  returns(uint256)
1302
1314
  {
@@ -1317,7 +1329,7 @@ contract ConstantFlowAgreementV1 is
1317
1329
  // NOTE: safecast for int96 with extra assertion
1318
1330
  assert(liquidationPeriod <= uint256(int256(type(int96).max)));
1319
1331
  deposit = uint256(int256(flowRate * int96(uint96(liquidationPeriod))));
1320
- return _clipDepositNumber(deposit);
1332
+ return _clipDepositNumberRoundingUp(deposit);
1321
1333
  }
1322
1334
 
1323
1335
  /**************************************************************************