-
Notifications
You must be signed in to change notification settings - Fork 129
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
feat(tradingapp): TradingApp nitro force move app #455
Draft
nksazonov
wants to merge
14
commits into
master
Choose a base branch
from
feat/nitro-trading-app
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
116ea8d
feat(tradingapp): add suggested TradingStructs, ISettle iface, Tradin…
nksazonov 641612e
feat(trading app): update structs, implement tradingApp
nksazonov 5bc116d
feat(contracts): add Broker Vault contract (#456)
maxpushka 3c071a2
fix(tradingapp): remove compiler error, comment
nksazonov dd9bcd1
fix(vault): remove compiler errors
nksazonov c46d39c
fix(contracts): unlock compiler version
nksazonov f1768f3
feat(tradingapp): reduce proof size for order-response to 1
nksazonov e48cbb0
feat(tradingapp): add postfund in proof to first order
nksazonov b4f3252
test(tradingapp): add success tests
nksazonov 01bd771
feat(brokervault): add msg.sender = broker req for withdrawal
nksazonov e19ee39
fix(brokervault): compiler error
nksazonov 09c0e70
feat(tradingvault): add liqudiation state
nksazonov fe991a3
feat(tradingapp): add outcome, validation and tests
nksazonov a55a6a3
feat(brokervault): change ISettle event, add Deposit, Withdrawal in s…
nksazonov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.22; | ||
|
||
import {ExitFormat as Outcome} from '@statechannels/exit-format/contracts/ExitFormat.sol'; | ||
|
||
import {StrictTurnTaking} from '../nitro/libraries/signature-logic/StrictTurnTaking.sol'; | ||
import {Consensus} from '../nitro/libraries/signature-logic/Consensus.sol'; | ||
import {IForceMoveApp} from '../nitro/interfaces/IForceMoveApp.sol'; | ||
import {NitroUtils} from '../nitro/libraries/NitroUtils.sol'; | ||
import {INitroTypes} from '../nitro/interfaces/INitroTypes.sol'; | ||
|
||
interface ITradingStructs { | ||
struct Order { | ||
bytes32 orderID; // tradeID | ||
} | ||
|
||
enum OrderResponseType { | ||
ACCEPT, | ||
REJECT | ||
} | ||
|
||
struct OrderResponse { | ||
OrderResponseType responseType; | ||
bytes32 orderID; // orderID making the trade | ||
} | ||
|
||
struct AssetAndAmount { | ||
address asset; | ||
uint256 amount; | ||
} | ||
|
||
struct Settlement { | ||
AssetAndAmount[] toTrader; | ||
AssetAndAmount[] toBroker; | ||
} | ||
} | ||
|
||
// FIXME: should Vault support multiple brokers? | ||
interface ISettle { | ||
function settle( | ||
INitroTypes.FixedPart calldata fixedPart, | ||
INitroTypes.RecoveredVariablePart[] calldata proof, | ||
INitroTypes.RecoveredVariablePart calldata candidate | ||
) external; | ||
} | ||
|
||
contract TradingApp is IForceMoveApp { | ||
function stateIsSupported( | ||
FixedPart calldata fixedPart, | ||
RecoveredVariablePart[] calldata proof, | ||
RecoveredVariablePart calldata candidate | ||
) external pure override returns (bool, string memory) { | ||
// FIXME: does the Broker deposit to the Adjudicator? | ||
// turn nums: | ||
// 0 - prefund | ||
// 1 - postfund | ||
// 2 - order | ||
// 2n+1 - order response | ||
// 2n - order or settlement | ||
|
||
uint48 candTurnNum = candidate.variablePart.turnNum; | ||
|
||
// prefund or postfund | ||
if (candTurnNum == 0 || candTurnNum == 1) { | ||
Consensus.requireConsensus(fixedPart, proof, candidate); | ||
return (true, ''); | ||
} | ||
|
||
bytes memory candidateData = candidate.variablePart.appData; | ||
// settlement | ||
// TODO: unsure whether we should check the proof when consensus is reached | ||
if (candTurnNum % 2 == 0 && proof.length == 0) { | ||
Consensus.requireConsensus(fixedPart, proof, candidate); | ||
// NOTE: used just to check the data structure validity | ||
ITradingStructs.Settlement memory _unused = abi.decode( | ||
candidateData, | ||
(ITradingStructs.Settlement) | ||
); | ||
return (true, ''); | ||
} | ||
|
||
// participant 0 signs even turns | ||
// participant 1 signs odd turns | ||
StrictTurnTaking.requireValidTurnTaking(fixedPart, proof, candidate); | ||
require(proof.length == 2, 'proof.length < 2'); | ||
(VariablePart memory proof0, VariablePart memory proof1) = ( | ||
proof[0].variablePart, | ||
proof[1].variablePart | ||
); | ||
require(proof0.turnNum == candTurnNum - 2, 'proof0.turnNum != candTurnNum - 1'); | ||
require(proof1.turnNum == candTurnNum - 1, 'proof1.turnNum != candTurnNum - 1'); | ||
|
||
// order | ||
if (candTurnNum % 2 == 0) { | ||
ITradingStructs.Order memory prevOrder = abi.decode( | ||
proof0.appData, | ||
(ITradingStructs.Order) | ||
); | ||
ITradingStructs.OrderResponse memory prevOrderResponse = abi.decode( | ||
proof1.appData, | ||
(ITradingStructs.OrderResponse) | ||
); | ||
if (prevOrderResponse.responseType == ITradingStructs.OrderResponseType.ACCEPT) { | ||
require( | ||
prevOrderResponse.orderID == prevOrder.orderID, | ||
'orderResponse.orderID != prevOrder.orderID, candidate is order' | ||
); | ||
} | ||
// NOTE: used just to check the data structure validity | ||
ITradingStructs.Order memory _candOrder = abi.decode( | ||
candidateData, | ||
(ITradingStructs.Order) | ||
); | ||
return (true, ''); | ||
} | ||
|
||
// orderResponse | ||
// NOTE: used just to check the data structure validity | ||
ITradingStructs.OrderResponse memory _prevOrderResponse = abi.decode( | ||
proof0.appData, | ||
(ITradingStructs.OrderResponse) | ||
); | ||
|
||
ITradingStructs.Order memory order = abi.decode(proof1.appData, (ITradingStructs.Order)); | ||
ITradingStructs.OrderResponse memory orderResponse = abi.decode( | ||
candidateData, | ||
(ITradingStructs.OrderResponse) | ||
); | ||
if (orderResponse.responseType == ITradingStructs.OrderResponseType.ACCEPT) { | ||
require( | ||
orderResponse.orderID == order.orderID, | ||
'orderResponse.orderID != order.orderID, candidate is orderResponse' | ||
); | ||
} | ||
return (true, ''); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
No, only using multiple vaults