Skip to content

Commit

Permalink
ERC-6123: Move to Draft: Enlarged interface, streamlined reference im…
Browse files Browse the repository at this point in the history
…plementation. (#25)

* Revised version of ERC-6123 including reference implementation for OTC-Derivatives and according Unit Tests

* Revised Doc, renamed SDC in SDCAbstract

* Revised Readme

* Changed Status to "DRAFT" - here we go again.

* Fixed some typos.

* Fixed some typos.

* Fixed typos. Fixed typo in file name.

* Removed unused variables.

* Reverted named change. Refactor rename SDCAbstract to SDC

* Changed the instruction to run out-of-the-box (clean checkout) wit

* Used view, where possible. Used local variable.

* Commenting our unused parameter.

* Fixed reference to image. Fixed blank lines after headlines.

* Fixed blank lines before code.

* Fixed headline (duplicate/wrong).

* Added note on NPM version numbers.

* Used ERC-20 in place of EIP-20

* Added abstract. Used Specification as headline.

* Fixed link to eip-20.md

* Minor fixed to README.md

* Minor fixes to README.md

* Simplified instruction to run unit tests, added package.json and hardhat.config.js

* Simplified instruction to run unit tests, added package.json and hardhat.config.js

* Removed duplicated resulted from manual merge with new ERC repo.

* Fixed link: eip-20.md -> erc-20.md

* Fixed Bug in SDCPledgedBalance.sol (afterTransfer), minor rewriting in markdown doc.

* Minor fixes (typos).

* Linking to eip instead of erc (see remark 7 ii on migrating pull requests to ERC repo).

* Linking to eip instead of erc (see remark 7 ii on migrating pull requests to ERC repo).

* Removed external link upon request.

* Update ERCS/erc-6123.md

Co-authored-by: Sam Wilson <[email protected]>

* Update ERCS/erc-6123.md

Co-authored-by: Sam Wilson <[email protected]>

* Update ERCS/erc-6123.md

Co-authored-by: Sam Wilson <[email protected]>

---------

Co-authored-by: Peter KL <[email protected]>
Co-authored-by: Sam Wilson <[email protected]>
  • Loading branch information
3 people authored Dec 11, 2023
1 parent 56251b5 commit d52e58e
Show file tree
Hide file tree
Showing 13 changed files with 806 additions and 606 deletions.
178 changes: 91 additions & 87 deletions ERCS/erc-6123.md

Large diffs are not rendered by default.

66 changes: 33 additions & 33 deletions assets/erc-6123/README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
# SDC Solidity implementation

## Description
This sdc implementation aims to implement process logic in a very lean way using an integrative solidity implementation and according unit tests

The reference SDC implementation can be unit tested with Hardhat to understand the trade process logic.

### Compile and run tests with Hardhat

We provide the essential steps to compile the contracts and run the provided unit tests.

### Provided Contracts and Tests

- `contracts/ISDC.sol` - Interface contract
- `contracts/SDC.sol` - SDC reference implementation contract
- `contracts/SDCToken.sol` - Mintable token contract for unit tests
- `test/SDC.js` - Unit tests for livecycle of sdc implementation
- `contracts/SDC.sol` - SDC abstract contract for an OTC Derivative
- `contracts/SDCPledgedBalance.sol` - SDC full implementation for an OTC Derivative
- `contracts/IERC20Settlement.sol` - Interface (extending the ERC-20) for settlement tokens used in `SDCPledgedBalance`.
- `contracts/ERC20Settlement.sol` - Mintable settlement token contract implementing `IERC20Settlement` for unit tests
- `test/SDCTests.js` - Unit tests for the life-cycle of the sdc implementation

### Used javascript based testing libraries for solidity
- `ethereum-waffle`: Waffle is a Solidity testing library. It allows you to write tests for your contracts with JavaScript.
- `chai`: Chai is an assertion library and provides functions like expect.
- `ethers`: This is a popular Ethereum client library. It allows you to interface with blockchains that implement the Ethereum API.
- `solidity-coverage`: This library gives you coverage reports on unit tests with the help of Istanbul.
### Compile and run tests with Hardhat

### Compile and run tests with hardhat
We provide the essential steps to compile the contracts and run provided unit tests
Check that you have the latest version of npm and node via `npm -version` (should be better than 8.5.0) and `node -v` (should be better than 16.14.2).

1. Check out project
2. Go to folder and initialise a new npm project: `npm init -y`. A basic `package.json` file should occur
3. Install Hardhat as local solidity dev environment: `npx hardhat`
4. Select following option: Create an empty hardhat.config.js
5. Install Hardhat as a development dependency: `npm install --save-dev hardhat`
6. Install further testing dependencies:
`npm install --save-dev @nomiclabs/hardhat-waffle @nomiclabs/hardhat-ethers ethereum-waffle chai ethers solidity-coverage`
7. Install open zeppelin contracts: `npm install @openzeppelin/contracts`
8. add plugins to hardhat.config.ts:
Install dependencies:
```shell
npm i
```
require("@nomiclabs/hardhat-waffle");
require('solidity-coverage');

Run all tests:
```shell
npm test
```

9. Adding commands to `package.json`:
```
"scripts": {
"build": "hardhat compile",
"test:light": "hardhat test",
"test": "hardhat coverage"
},
Run all tests with coverage (alternatively):
```shell
npm run coverage
```
9. run `npm run build`
10. run `npm run test`

### Configuration files

- `package.js` - Javascript package definition.
- `hardhat.config.js` - Hardhat config.

### Used javascript-based testing libraries for solidity

- `ethereum-waffle`: Waffle is a Solidity testing library. It allows you to write tests for your contracts with JavaScript.
- `chai`: Chai is an assertion library and provides functions like expect.
- `ethers`: This is a popular Ethereum client library. It allows you to interface with blockchains that implement the Ethereum API.
- `solidity-coverage`: This library gives you coverage reports on unit tests with the help of Istanbul.
102 changes: 102 additions & 0 deletions assets/erc-6123/contracts/ERC20Settlement.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// SPDX-License-Identifier: CC0-1.0
pragma solidity >=0.7.0 <0.9.0;

import "./ISDC.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/utils/introspection/ERC165Checker.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.
* Token is tied to one SDC address
* 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
*/


modifier onlySDC() {
require(msg.sender == sdcAddress, "Only allowed to be called from SDC Address"); _;
}

using ERC165Checker for address;

address sdcAddress;

constructor() ERC20("SDCToken", "SDCT") {

}

function setSDCAddress(address _sdcAddress) public{
sdcAddress = _sdcAddress;
}

function mint(address to, uint256 amount) public {
_mint(to, amount);
}

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);
}
}

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 checkedBatchTransfer(address[] memory to, uint256[] memory values, uint256 transactionID ) public onlySDC{
require (to.length == values.length, "Array Length mismatch");
uint256 requiredBalance = 0;
for(uint256 i = 0; i < values.length; i++)
requiredBalance += values[i];
if (balanceOf(msg.sender) < requiredBalance){
ISDC(sdcAddress).afterTransfer(transactionID, false);
return;
}
else{
for(uint256 i = 0; i < to.length; i++){
transfer(to[i],values[i]);
}
ISDC(sdcAddress).afterTransfer(transactionID, true);
}
}


function checkedBatchTransferFrom(address[] memory from, address[] memory to, uint256[] memory values, uint256 transactionID ) public onlySDC{
require (from.length == to.length, "Array Length mismatch");
require (to.length == values.length, "Array Length mismatch");
for(uint256 i = 0; i < from.length; i++){
address fromAddress = from[i];
uint256 totalRequiredBalance = 0;
for(uint256 j = 0; j < from.length; j++){
if (from[j] == fromAddress)
totalRequiredBalance += values[j];
}
if (balanceOf(fromAddress) < totalRequiredBalance){
ISDC(sdcAddress).afterTransfer(transactionID, false);
break;
}

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

}
53 changes: 53 additions & 0 deletions assets/erc-6123/contracts/IERC20Settlement.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// SPDX-License-Identifier: CC0-1.0
pragma solidity >=0.7.0 <0.9.0;



import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

/*------------------------------------------- DESCRIPTION ---------------------------------------------------------------------------------------
* @title ERC6123 - Settlement Token Interface
* @dev Settlement Token Interface enhances the ERC20 Token by introducing so called checked transfer functionality which can be used to directly interact with an SDC.
* Checked transfers can be conducted for single or multiple transactions where SDC will receive a success message whether the transfer was executed successfully or not.
*/


interface IERC20Settlement is IERC20 {

/*
* @dev Performs a single transfer from msg.sender balance and checks whether this transfer can be conducted
* @param to - receiver
* @param value - transfer amount
* @param transactionID
*/
function checkedTransfer(address to, uint256 value, uint256 transactionID) external;

/*
* @dev Performs a single transfer to a single addresss and checks whether this transfer can be conducted
* @param from - payer
* @param to - receiver
* @param value - transfer amount
* @param transactionID
*/
function checkedTransferFrom(address from, address to, uint256 value, uint256 transactionID) external ;


/*
* @dev Performs a multiple transfers from msg.sender balance and checks whether these transfers can be conducted
* @param to - receivers
* @param values - transfer amounts
* @param transactionID
*/
function checkedBatchTransfer(address[] memory to, uint256[] memory values, uint256 transactionID ) external;

/*
* @dev Performs a multiple transfers between multiple addresses and checks whether these transfers can be conducted
* @param from - payers
* @param to - receivers
* @param value - transfer amounts
* @param transactionID
*/
function checkedBatchTransferFrom(address[] memory from, address[] memory to, uint256[] memory values, uint256 transactionID ) external;


}
Loading

0 comments on commit d52e58e

Please sign in to comment.