-
Notifications
You must be signed in to change notification settings - Fork 2
Implement settlement net off with off-chain hints #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: marcello-pv01 <marcello@pv0.one>
Signed-off-by: marcello-pv01 <marcello@pv0.one>
Signed-off-by: marcello-pv01 <marcello@pv0.one>
Signed-off-by: marcello-pv01 <marcello@pv0.one>
Signed-off-by: marcello-pv01 <marcello@pv0.one>
Signed-off-by: marcello-pv01 <marcello@pv0.one>
Signed-off-by: marcello-pv01 <marcello@pv0.one>
| } | ||
|
|
||
| // Additional Step: For fungible assets, enforce debtor→creditor-only transfers with gross == net per party | ||
| // TODO: evaluate whether this is too restrictive, this would possibly prevent partial netting arrangements, write tests for this in case we decide to keep it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's review this together
| uint256 go = grossOut[base]; | ||
| uint256 gi = grossIn[base]; | ||
| if (d < 0) { | ||
| require(gi == 0, "Fungible: debtor must not receive"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to write tests and possibly convert to exceptions for these guys
Signed-off-by: marcello-pv01 <marcello@pv0.one>
ddd2c42 to
0150525
Compare
| uint256 cutoffDate, | ||
| bool isAutoSettled | ||
| ) external returns (uint256 id) { | ||
| return _createSettlement(flows, nettedFlows,settlementReference, cutoffDate, isAutoSettled, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| return _createSettlement(flows, nettedFlows,settlementReference, cutoffDate, isAutoSettled, true); | |
| return _createSettlement(flows, nettedFlows, settlementReference, cutoffDate, isAutoSettled, true); |
| * @param nettedFlows The flows that will be used to process the settlement, used to provide optimised settlement solutions. | ||
| * @param settlementReference A free text reference for the settlement. | ||
| * @param cutoffDate The deadline for approvals and execution. | ||
| * @param isAutoSettled If true, the settlement will be executed automatically after all approvals are in place. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing useNettingOff parameter
| if (block.timestamp > cutoffDate) revert CutoffDatePassed(); | ||
| uint256 lengthFlows = flows.length; | ||
| if (lengthFlows == 0) revert NoFlowsProvided(); | ||
| // no need to check for nettedFlows being not empty if useNettingOff is false at runtime |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also empty nettedFlows is a valid optimisation.
Signed-off-by: marcello-pv01 <marcello@pv0.one>
Signed-off-by: marcello-pv01 <marcello@pv0.one>
Signed-off-by: marcello-pv01 <marcello@pv0.one>
Signed-off-by: marcello-pv01 <marcello@pv0.one>
33ae549 to
306c4c0
Compare
| uint256 idxA = k * partyCount + pFrom; | ||
| uint256 idxB = k * partyCount + pTo; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| uint256 idxA = k * partyCount + pFrom; | |
| uint256 idxB = k * partyCount + pTo; | |
| uint256 idxFrom = k * partyCount + pFrom; | |
| uint256 idxTo = k * partyCount + pTo; |
Signed-off-by: marcello-pv01 <marcello@pv0.one>
| * @param party Address of the party to compute requirements for. | ||
| * @return result NetRequirement data struct with ETH and ERC20 requirements. | ||
| */ | ||
| function _computeNetRequirementsForParty(IDeliveryVersusPaymentV1.Flow[] memory flows, address party) internal pure returns ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move this function where "private" ones live
| if (f.token == address(0)) { | ||
| // ETH leg | ||
| result.ethRequiredNet += f.amountOrId; | ||
| } else if (!f.isNFT) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should probably add nft as well for completeness
Signed-off-by: marcello-pv01 <marcello@pv0.one>
Signed-off-by: marcello-pv01 <marcello@pv0.one>
Signed-off-by: marcello-pv01 <marcello@pv0.one>
|
Agreed with Kevin to release the version without netting off to the public as that's the one audited by ShadowyCreators. |
Description
We want to allow executing a settlement in the most efficient way as possible while keeping the same trace of the settlement on the smart contract.
This means that we want to allow the user to define a settlement as the explicit set of cashflows, but we want to let them settle it in the most capital/gas efficient way possible, i.e. netting off transfers where applicable.
This PR adds an additional functionality upon settlement creation, where the caller could send an additional array of netted flows which will be used for the settlement when it gets executed.
The idea is that those netted flows are computed off-chain, where the computation is cheaper and easier to run and implement, but on-chain we still verify that the netted flows provided are equivalent to the explicit flows.
In order to simplify the life for devs that want to achieve trivial optimisations, I added an helper function to the helper contract which greedily computes those optimisations.
This is not as efficient as using MILP approaches but for simple scenarios they are equivalent, and it's still better than nothing.
I implemented tests for both functionalities and they are passing.
Considerations
This solution will cost extra gas to store the netted off flows and the
useNettingOffflag.If no netted flows are provided useNettingOff will be set to false automatically, if they are, even if it's an empty array, then useNettingOff will be set to true. The reason for this is that no flows is an optimisation, think of
A -> B: 10 USDC, B -> A: 10 USDC.Changes
Checklist