From 12349b3e69da169f3363a92deef795cc1f5747d5 Mon Sep 17 00:00:00 2001 From: Christian Fries Date: Sun, 10 Nov 2024 20:04:27 +0100 Subject: [PATCH] Update ERC-6123: Fixes to reference implementation of Settlement Token Merged by EIP-Bot. --- ERCS/erc-6123.md | 2 +- assets/erc-6123/contracts/ERC20Settlement.sol | 16 +++++++++------- assets/erc-6123/contracts/ISDC.sol | 10 +++++++--- .../contracts/SDCSingleTradePledgedBalance.sol | 5 +++-- assets/erc-6123/package.json | 2 +- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/ERCS/erc-6123.md b/ERCS/erc-6123.md index e9054db0a7..dc6fb80aa3 100644 --- a/ERCS/erc-6123.md +++ b/ERCS/erc-6123.md @@ -138,7 +138,7 @@ The transactionData is emitted as part of the corresponding event: `SettlementTr This might result in a termination or start of the next settlement phase, depending on the provided success flag. ```solidity -function afterTransfer(bool success, string memory transactionData) external; +function afterTransfer(bool success, uint256 transactionID, string memory transactionData) external; ``` diff --git a/assets/erc-6123/contracts/ERC20Settlement.sol b/assets/erc-6123/contracts/ERC20Settlement.sol index c04be0b302..0fa805e952 100644 --- a/assets/erc-6123/contracts/ERC20Settlement.sol +++ b/assets/erc-6123/contracts/ERC20Settlement.sol @@ -7,8 +7,6 @@ import "@openzeppelin/contracts/utils/introspection/ERC165Checker.sol"; import "@openzeppelin/contracts/utils/Strings.sol"; import "./IERC20Settlement.sol"; -contract ERC20Settlement is ERC20, IERC20Settlement{ - /*------------------------------------------- DESCRIPTION --------------------------------------------------------------------------------------- * @title Reference (example) Implementation for Settlement Token Interface * @dev This token performs transfers on-chain. @@ -16,7 +14,7 @@ contract ERC20Settlement is ERC20, IERC20Settlement{ * Only SDC can call checkedTransfers * Settlement Token calls back the referenced SDC by calling "afterTransfer" with a success flag. Depending on this SDC perfoms next state change */ - +contract ERC20Settlement is ERC20, IERC20Settlement{ modifier onlySDC() { require(msg.sender == sdcAddress, "Only allowed to be called from SDC Address"); _; @@ -39,10 +37,13 @@ contract ERC20Settlement is ERC20, IERC20Settlement{ } function transferAndCallback(address to, uint256 value, uint256 transactionID, address callbackContract) public onlySDC{ - if ( balanceOf(sdcAddress) < value) + if ( balanceOf(msg.sender) < value) { ISDC(callbackContract).afterTransfer(false, transactionID, Strings.toString(transactionID)); - else + } + else { + _transfer(msg.sender,to,value); ISDC(callbackContract).afterTransfer(true, transactionID, Strings.toString(transactionID)); + } } function transferFromAndCallback(address from, address to, uint256 value, uint256 transactionID, address callbackContract) external view onlySDC { @@ -52,15 +53,16 @@ contract ERC20Settlement is ERC20, IERC20Settlement{ function transferBatchAndCallback(address[] memory to, uint256[] memory values, uint256 transactionID, address callbackContract) public onlySDC{ require (to.length == values.length, "Array Length mismatch"); uint256 requiredBalance = 0; - for(uint256 i = 0; i < values.length; i++) + for(uint256 i = 0; i < values.length; i++) { requiredBalance += values[i]; + } if (balanceOf(msg.sender) < requiredBalance){ ISDC(callbackContract).afterTransfer(false, transactionID, Strings.toString(transactionID)); return; } else{ for(uint256 i = 0; i < to.length; i++){ - _transfer(sdcAddress,to[i],values[i]); + _transfer(msg.sender,to[i],values[i]); } ISDC(callbackContract).afterTransfer(true, transactionID, Strings.toString(transactionID)); } diff --git a/assets/erc-6123/contracts/ISDC.sol b/assets/erc-6123/contracts/ISDC.sol index a34b7d1ba2..7b1088b131 100644 --- a/assets/erc-6123/contracts/ISDC.sol +++ b/assets/erc-6123/contracts/ISDC.sol @@ -128,13 +128,17 @@ interface ISDC { /** * @dev Emitted when settlement process has been finished + * @param transactionID a transaction id + * @param transactionData data associtated with the transfer, will be emitted via the events. */ - event SettlementTransferred(string transactionData); + event SettlementTransferred(uint256 transactionID, string transactionData); /** * @dev Emitted when settlement process has been finished + * @param transactionID a transaction id + * @param transactionData data associtated with the transfer, will be emitted via the events. */ - event SettlementFailed(string transactionData); + event SettlementFailed(uint256 transactionID, string transactionData); /* Events related to trade termination */ @@ -222,7 +226,7 @@ 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 transactionID a transaction + * @param transactionID a transaction id * @param transactionData data associtated with the transfer, will be emitted via the events. * @dev emit a {SettlementTransferred} or a {SettlementFailed} event. May emit a {TradeTerminated} event. */ diff --git a/assets/erc-6123/contracts/SDCSingleTradePledgedBalance.sol b/assets/erc-6123/contracts/SDCSingleTradePledgedBalance.sol index 5d2c610f1a..25586d214c 100644 --- a/assets/erc-6123/contracts/SDCSingleTradePledgedBalance.sol +++ b/assets/erc-6123/contracts/SDCSingleTradePledgedBalance.sol @@ -109,11 +109,12 @@ contract SDCSingleTradePledgedBalance is SDCSingleTrade { else if ( inStateTransfer() ){ if (success){ setTradeState(TradeState.Settled); - emit SettlementTransferred("Settlement Settled - Pledge Transfer"); + emit SettlementTransferred(transactionID, "Settlement Settled - Pledge Transfer"); } else{ // Settlement & Pledge Case: transferAmount is transferred from SDC balance (i.e. pledged balance). - int256 settlementAmount = settlementAmounts[settlementAmounts.length-1]; setTradeState(TradeState.InTermination); + emit SettlementFailed(transactionID, "Settlement Failed - Pledge Transfer"); + int256 settlementAmount = settlementAmounts[settlementAmounts.length-1]; processTerminationWithPledge(settlementAmount); emit TradeTerminated(tradeID, "Settlement Failed - Pledge Transfer"); } diff --git a/assets/erc-6123/package.json b/assets/erc-6123/package.json index b72cb2e7be..c8857b4772 100644 --- a/assets/erc-6123/package.json +++ b/assets/erc-6123/package.json @@ -1,6 +1,6 @@ { "name": "@finmath.net/sdc", - "version": "0.4.3", + "version": "0.4.4", "description": "Solidity Smart Derivative Contracts", "author": "Christian Fries, Peter Kohl-Landgraf, Alexandros Korpis", "license": "ISC",