Skip to content

Commit

Permalink
Website: Update ERC-6123 - Streamlined Reference Implementation, adde…
Browse files Browse the repository at this point in the history
…d Unit Tests

Merged by EIP-Bot.
  • Loading branch information
pekola authored Jul 30, 2024
1 parent 5ab0970 commit c05baa7
Show file tree
Hide file tree
Showing 8 changed files with 500 additions and 306 deletions.
17 changes: 9 additions & 8 deletions ERCS/erc-6123.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,23 @@ The following methods specify a Smart Derivative Contract's trade initiation and
A party can initiate a trade by providing the party address to trade with, trade data, trade position, payment amount for the trade and initial settlement data. Only registered counterparties are allowed to use that function.

```solidity
function inceptTrade(address _withParty, string memory _tradeData, int _position, int256 _paymentAmount, string memory _initialSettlementData) external;
function inceptTrade(address withParty, string memory tradeData, int position, int256 paymentAmount, string memory initialSettlementData) external;
```

#### Trade Initiation Phase: `confirmTrade`

A counterparty can confirm a trade by providing its trade specification data, which then gets matched against the data stored from `inceptTrade` call.

```solidity
function confirmTrade(address _withParty, string memory _tradeData, int _position, int256 _paymentAmount, string memory _initialSettlementData) external;
function confirmTrade(address withParty, string memory tradeData, int position, int256 paymentAmount, string memory initialSettlementData) external;
```

#### Trade Initiation Phase: `cancelTrade`

The counterparty that called `inceptTrade` has the option to cancel the trade, e.g., in the case where the trade is not confirmed in a timely manner.

```solidity
function cancelTrade(address _withParty, string memory _tradeData, int _position, int256 _paymentAmount, string memory _initialSettlementData) external;
function cancelTrade(address withParty, string memory tradeData, int position, int256 paymentAmount, string memory initialSettlementData) external;
```

#### Trade Settlement Phase: `initiateSettlement`
Expand All @@ -106,34 +106,35 @@ function performSettlement(int256 settlementAmount, string memory settlementData

This method - either called back from the provided settlement token directly or from an eligible address - completes the settlement transfer.
This might result in a termination or start of the next settlement phase, depending on the provided success flag.
The transactionData is emitted as part of the corresponding event: `TradeSettled` or `TradeTerminated`

```solidity
function afterTransfer(uint256 transactionHash, bool success) external;
function afterTransfer(bool success, uint256 transactionData) external;
```


#### Trade Termination: `requestTermination`

Allows an eligible party to request a mutual termination with a termination amount she is willing to pay and provide further termination terms (e.g. an XML)
Allows an eligible party to request a mutual termination of the trade with the correspondig `tradeData` with a termination amount she is willing to pay and provide further termination terms (e.g. an XML)

```solidity
function requestTradeTermination(string memory tradeId, int256 _terminationPayment, string memory terminationTerms) external;
function requestTradeTermination(string memory tradeData, int256 terminationPayment, string memory terminationTerms) external;
```

#### Trade Termination: `confirmTradeTermination`

Allows an eligible party to confirm a previously requested (mutual) trade termination, including termination payment value and termination terms

```solidity
function confirmTradeTermination(string memory tradeId, int256 _terminationPayment, string memory terminationTerms) external;
function confirmTradeTermination(string memory tradeData, int256 terminationPayment, string memory terminationTerms) external;
```

#### Trade Termination: `cancelTradeTermination`

The party that initiated `requestTradeTermination` has the option to withdraw the request, e.g., in the case where the termination is not confirmed in a timely manner.

```solidity
function cancelTradeTermination(string memory tradeId, int256 _terminationPayment, string memory terminationTerms) external;
function cancelTradeTermination(string memory tradeData, int256 terminationPayment, string memory terminationTerms) external;
```

### Trade Events
Expand Down
37 changes: 13 additions & 24 deletions assets/erc-6123/contracts/ERC20Settlement.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,14 @@ contract ERC20Settlement is ERC20, IERC20Settlement{
}

function checkedTransfer(address to, uint256 value, uint256 transactionID) public onlySDC{
try this.transfer(to,value) returns (bool transferSuccessFlag) {
ISDC(sdcAddress).afterTransfer(transactionID, transferSuccessFlag);
}
catch{
ISDC(sdcAddress).afterTransfer(transactionID, false);
}
if ( balanceOf(sdcAddress) < value)
ISDC(sdcAddress).afterTransfer(false, transactionID);
else
ISDC(sdcAddress).afterTransfer(true, transactionID);
}

function checkedTransferFrom(address from, address to, uint256 value, uint256 transactionID) external onlySDC {
// TODO: Bug - reason="Error: Transaction reverted: contract call run out of gas and made the transaction revert", method="estimateGas",
if (this.balanceOf(from)< value || this.allowance(from,address(msg.sender)) < value )
ISDC(sdcAddress).afterTransfer(transactionID, false);
try this.transfer(to,value) returns (bool transferSuccessFlag) {
ISDC(sdcAddress).afterTransfer(transactionID, transferSuccessFlag);
}
catch{
ISDC(sdcAddress).afterTransfer(transactionID, false);
}
// address owner = _msgSender(); // currently not used
function checkedTransferFrom(address from, address to, uint256 value, uint256 transactionID) external view onlySDC {
revert("not implemented");
}

function checkedBatchTransfer(address[] memory to, uint256[] memory values, uint256 transactionID ) public onlySDC{
Expand All @@ -65,14 +54,14 @@ contract ERC20Settlement is ERC20, IERC20Settlement{
for(uint256 i = 0; i < values.length; i++)
requiredBalance += values[i];
if (balanceOf(msg.sender) < requiredBalance){
ISDC(sdcAddress).afterTransfer(transactionID, false);
ISDC(sdcAddress).afterTransfer(false, transactionID);
return;
}
else{
for(uint256 i = 0; i < to.length; i++){
transfer(to[i],values[i]);
_transfer(sdcAddress,to[i],values[i]);
}
ISDC(sdcAddress).afterTransfer(transactionID, true);
ISDC(sdcAddress).afterTransfer(true, transactionID);
}
}

Expand All @@ -88,15 +77,15 @@ contract ERC20Settlement is ERC20, IERC20Settlement{
totalRequiredBalance += values[j];
}
if (balanceOf(fromAddress) < totalRequiredBalance){
ISDC(sdcAddress).afterTransfer(transactionID, false);
break;
ISDC(sdcAddress).afterTransfer(false, transactionID);
return;
}

}
for(uint256 i = 0; i < to.length; i++){
transferFrom(from[i],to[i],values[i]);
_transfer(from[i],to[i],values[i]);
}
ISDC(sdcAddress).afterTransfer(transactionID, true);
ISDC(sdcAddress).afterTransfer(true, transactionID);
}

}
61 changes: 32 additions & 29 deletions assets/erc-6123/contracts/ISDC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ interface ISDC {

/**
* @dev Emitted when an active trade is terminated
* @param cause string holding the cause of the termination
* @param cause string holding data associated with the termination, e.g. transactionData upon a failed transaction
*/
event TradeTerminated(string cause);

Expand All @@ -105,7 +105,7 @@ interface ISDC {
/**
* @dev Emitted when settlement process has been finished
*/
event TradeSettled();
event TradeSettled(string transactionData);

/**
* @dev Emitted when a settlement gets requested
Expand Down Expand Up @@ -152,35 +152,35 @@ interface ISDC {
/**
* @notice Incepts a trade, stores trade data
* @dev emits a {TradeIncepted} event
* @param _withParty is the party the inceptor wants to trade with
* @param _tradeData a description of the trade specification e.g. in xml format, suggested structure - see assets/eip-6123/doc/sample-tradedata-filestructure.xml
* @param _position is the position the inceptor has in that trade
* @param _paymentAmount is the payment amount which can be positive or negative (viewed from the inceptor)
* @param _initialSettlementData the initial settlement data (e.g. initial market data at which trade was incepted)
* @param withParty is the party the inceptor wants to trade with
* @param tradeData a description of the trade specification e.g. in xml format, suggested structure - see assets/eip-6123/doc/sample-tradedata-filestructure.xml
* @param position is the position the inceptor has in that trade
* @param paymentAmount is the payment amount which can be positive or negative (viewed from the inceptor)
* @param initialSettlementData the initial settlement data (e.g. initial market data at which trade was incepted)
*/
function inceptTrade(address _withParty, string memory _tradeData, int _position, int256 _paymentAmount, string memory _initialSettlementData) external;
function inceptTrade(address withParty, string memory tradeData, int position, int256 paymentAmount, string memory initialSettlementData) external;

/**
* @notice Performs a matching of provided trade data and settlement data of a previous trade inception
* @dev emits a {TradeConfirmed} event if trade data match
* @param _withParty is the party the confirmer wants to trade with
* @param _tradeData a description of the trade specification e.g. in xml format, suggested structure - see assets/eip-6123/doc/sample-tradedata-filestructure.xml
* @param _position is the position the confirmer has in that trade (negative of the position the inceptor has in the trade)
* @param _paymentAmount is the payment amount which can be positive or negative (viewed from the confirmer, negative of the inceptor's view)
* @param _initialSettlementData the initial settlement data (e.g. initial market data at which trade was incepted)
* @param withParty is the party the confirmer wants to trade with
* @param tradeData a description of the trade specification e.g. in xml format, suggested structure - see assets/eip-6123/doc/sample-tradedata-filestructure.xml
* @param position is the position the confirmer has in that trade (negative of the position the inceptor has in the trade)
* @param paymentAmount is the payment amount which can be positive or negative (viewed from the confirmer, negative of the inceptor's view)
* @param initialSettlementData the initial settlement data (e.g. initial market data at which trade was incepted)
*/
function confirmTrade(address _withParty, string memory _tradeData, int _position, int256 _paymentAmount, string memory _initialSettlementData) external;
function confirmTrade(address withParty, string memory tradeData, int position, int256 paymentAmount, string memory initialSettlementData) external;

/**
* @notice Performs a matching of provided trade data and settlement data of a previous trade inception. Required to be called by inceptor.
* @dev emits a {TradeCanceled} event if trade data match and msg.sender agrees with the party that incepted the trade.
* @param _withParty is the party the inceptor wants to trade with
* @param _tradeData a description of the trade specification e.g. in xml format, suggested structure - see assets/eip-6123/doc/sample-tradedata-filestructure.xml
* @param _position is the position the inceptor has in that trade
* @param _paymentAmount is the payment amount which can be positive or negative (viewed from the inceptor)
* @param _initialSettlementData the initial settlement data (e.g. initial market data at which trade was incepted)
* @param withParty is the party the inceptor wants to trade with
* @param tradeData a description of the trade specification e.g. in xml format, suggested structure - see assets/eip-6123/doc/sample-tradedata-filestructure.xml
* @param position is the position the inceptor has in that trade
* @param paymentAmount is the payment amount which can be positive or negative (viewed from the inceptor)
* @param initialSettlementData the initial settlement data (e.g. initial market data at which trade was incepted)
*/
function cancelTrade(address _withParty, string memory _tradeData, int _position, int256 _paymentAmount, string memory _initialSettlementData) external;
function cancelTrade(address withParty, string memory tradeData, int position, int256 paymentAmount, string memory initialSettlementData) external;

/// Settlement Cycle: Settlement

Expand All @@ -202,35 +202,38 @@ interface ISDC {
/**
* @notice May get called from outside to to finish a transfer (callback). The trade decides on how to proceed based on success flag
* @param success tells the protocol whether transfer was successful
* @param transactionData data associtated with the transfer, will be emitted via the events.
* @dev may emit a {TradeSettled} event or a {TradeTerminated} event
*/
function afterTransfer(uint256 transactionHash, bool success) external;
function afterTransfer(bool success, uint256 transactionData) external;


/// Trade termination

/**
* @notice Called from a counterparty to request a mutual termination
* @dev emits a {TradeTerminationRequest}
* @param tradeId the trade identifier which is supposed to be terminated
* @param terminationTerms the termination terms
* @param tradeData a description of the trade specification e.g. in xml format, suggested structure - see assets/eip-6123/doc/sample-tradedata-filestructure.xml
* @param terminationPayment an agreed termination amount (viewed from the requester)
* @param terminationTerms the termination terms to be stored on chain.
*/
function requestTradeTermination(string memory tradeId, int256 _terminationPayment, string memory terminationTerms) external;
function requestTradeTermination(string memory tradeData, int256 terminationPayment, string memory terminationTerms) external;

/**
* @notice Called from a party to confirm an incepted termination, which might trigger a final settlement before trade gets closed
* @dev emits a {TradeTerminationConfirmed}
* @param tradeId the trade identifier of the trade which is supposed to be terminated
* @param terminationTerms the termination terms
* @param tradeData a description of the trade specification e.g. in xml format, suggested structure - see assets/eip-6123/doc/sample-tradedata-filestructure.xml
* @param terminationPayment an agreed termination amount (viewed from the confirmer, negative of the value provided by the requester)
* @param terminationTerms the termination terms to be stored on chain.
*/
function confirmTradeTermination(string memory tradeId, int256 _terminationPayment, string memory terminationTerms) external;
function confirmTradeTermination(string memory tradeData, int256 terminationPayment, string memory terminationTerms) external;

/**
* @notice Called from a party to confirm an incepted termination, which might trigger a final settlement before trade gets closed
* @dev emits a {TradeTerminationConfirmed}
* @param tradeId the trade identifier of the trade which is supposed to be terminated
* @param tradeData a description of the trade specification e.g. in xml format, suggested structure - see assets/eip-6123/doc/sample-tradedata-filestructure.xml
* @param terminationTerms the termination terms
*/
function cancelTradeTermination(string memory tradeId, int256 _terminationPayment, string memory terminationTerms) external;
function cancelTradeTermination(string memory tradeData, int256 terminationPayment, string memory terminationTerms) external;

}
Loading

0 comments on commit c05baa7

Please sign in to comment.