Skip to content
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

Add a contract for delivery versus payment settlement #439

Merged
merged 7 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
sudo chown -R runner:docker /home/runner/work/ibet-SmartContract/ibet-SmartContract/output
sudo chmod 777 /home/runner/work/ibet-SmartContract/ibet-SmartContract/output
- name: UnitTest
run: docker-compose run sc-unit-test
run: docker compose run sc-unit-test
- uses: actions/upload-artifact@v4
with:
name: contract-json
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ English | [日本語](README_JA.md)
## Dependencies
- [Python3](https://www.python.org/downloads/)
- Version 3.11
- [Node.js](https://nodejs.org/en/download/)
- Version 20
- [Solidity](https://docs.soliditylang.org/)
- We are using Solidity to implement our smart contracts.
- Currently, we are using v0.8.23.
Expand Down Expand Up @@ -62,6 +64,12 @@ Install openzeppelin-contracts.
$ brownie pm install OpenZeppelin/openzeppelin-contracts@4.9.3
```

Install hardhat as a Node.js package.

```bash
$ npm install
```

## Compile Contracts
Use eth-brownie to compile contracts.

Expand Down
7 changes: 7 additions & 0 deletions README_JA.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
## 依存
- [Python3](https://www.python.org/downloads/)
- バージョン 3.11
- [Node.js](https://nodejs.org/en/download/)
- バージョン 20
- [Solidity](https://docs.soliditylang.org/)
- スマートコントラクトの実装には Solidity を利用しています。
- 現在、私たちは v0.8.23 を利用しています。
Expand Down Expand Up @@ -60,6 +62,11 @@ openzeppelin-contractsをインストールします。
$ brownie pm install OpenZeppelin/openzeppelin-contracts@4.9.3
```

hardhatをインストールします。
```bash
$ npm install
```

## コントラクトのコンパイル

コントラクトのコンパイルには eth-brownie を利用します。
Expand Down
232 changes: 232 additions & 0 deletions contracts/exchange/DVPStorage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
/**
* Copyright BOOSTRY Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

pragma solidity ^0.8.0;

import "../access/Ownable.sol";
import "../utils/Errors.sol";


/// @title DVPコントラクトのステートを永続化するためのEternalStorage
/// @dev Storageのアクセスは認可したDVPコントラクトに限定する
contract DVPStorage is Ownable {

constructor() {}

// -------------------------------------------------------------------
// 最新バージョンのDVPコントラクトアドレス:LatestVersion
// -------------------------------------------------------------------

/// 最新バージョンのDVPコントラクトアドレス
address public latestVersion;

/// @notice DVPコントラクトのバージョン更新
/// @dev コントラクトオーナーのみ実行が可能
/// @param _newVersion 新しいDVPコントラクトのアドレス
function upgradeVersion(address _newVersion)
public
onlyOwner()
{
latestVersion = _newVersion;
}

/// @dev 実行者が最新バージョンのDVPコントラクトアドレスであることをチェック
modifier onlyLatestVersion() {
require(msg.sender == latestVersion, ErrorCode.ERR_DVPStorage_onlyLatestVersion_250001);
_;
}

// -------------------------------------------------------------------
// 残高:Balance
// -------------------------------------------------------------------

/// 残高情報
/// account => token => amount
mapping(address => mapping(address => uint256)) private balances;

/// @notice 残高の更新
/// @dev 最新バージョンのDVPコントラクトのみ実行が可能
/// @param _account アドレス
/// @param _token トークンアドレス
/// @param _value 更新後の残高数量
/// @return 処理結果
function setBalance(address _account, address _token, uint256 _value)
public
onlyLatestVersion()
returns (bool)
{
balances[_account][_token] = _value;
return true;
}

/// @notice 残高数量の参照
/// @param _account アドレス
/// @param _token トークンアドレス
/// @return 残高数量
function getBalance(address _account, address _token)
public
view
returns (uint256)
{
return balances[_account][_token];
}

// -------------------------------------------------------------------
// 拘束数量:Commitment
// -------------------------------------------------------------------

/// 拘束数量
/// account => token => amount
mapping(address => mapping(address => uint256)) public commitments;

/// @notice 拘束数量の更新
/// @dev 最新バージョンのDVPコントラクトのみ実行が可能
/// @param _account アドレス
/// @param _token トークンアドレス
/// @param _value 更新後の数量
/// @return 処理結果
function setCommitment(address _account, address _token, uint256 _value)
public
onlyLatestVersion()
returns (bool)
{
commitments[_account][_token] = _value;
return true;
}

/// @notice 拘束数量の参照
/// @param _account アドレス
/// @param _token トークンアドレス
/// @return 拘束数量
function getCommitment(address _account, address _token)
public
view
returns (uint256)
{
return commitments[_account][_token];
}

// -------------------------------------------------------------------
// DVP情報:Delivery
// -------------------------------------------------------------------

struct Delivery {
address token;
address seller;
address buyer;
uint256 amount;
address agent;
bool confirmed; // Initially false
bool valid; // Initially true
}

/// DVP情報
/// deliveryId => Delivery
mapping(uint256 => Delivery) private delivery;

/// 直近決済ID
uint256 private latestDeliveryId = 0;

/// @notice 直近決済IDの更新
/// @dev 最新バージョンのDVPコントラクトのみ実行が可能
/// @param _latestDeliveryId 直近決済ID
function setLatestDeliveryId(uint256 _latestDeliveryId)
public
onlyLatestVersion()
{
latestDeliveryId = _latestDeliveryId;
}

/// @notice 直近決済IDの参照
/// @return 直近決済ID
function getLatestDeliveryId()
public
view
returns(uint256)
{
return latestDeliveryId;
}

/// @notice DVP情報の更新
/// @param _deliveryId 決済ID
/// @param _token トークンアドレス
/// @param _seller 売り手
/// @param _buyer 買い手
/// @param _amount 数量
/// @param _agent 決済エージェント
/// @param _confirmed 確認状態
/// @param _valid 有効状態
function setDelivery(
uint256 _deliveryId,
address _token,
address _seller,
address _buyer,
uint256 _amount,
address _agent,
bool _confirmed,
bool _valid
)
public
onlyLatestVersion()
{
delivery[_deliveryId].token = _token;
delivery[_deliveryId].seller = _seller;
delivery[_deliveryId].buyer = _buyer;
delivery[_deliveryId].amount = _amount;
delivery[_deliveryId].agent = _agent;
delivery[_deliveryId].confirmed = _confirmed;
delivery[_deliveryId].valid = _valid;
}

/// @notice DVP情報の取得
/// @param _deliveryId 決済ID
/// @return token トークンアドレス
/// @return seller 売り手
/// @return buyer 買い手
/// @return amount 数量
/// @return agent 決済エージェント
/// @return confirmed 確認状態
/// @return valid 有効状態
function getDelivery(
uint256 _deliveryId
)
public
view
returns(
address token,
address seller,
address buyer,
uint256 amount,
address agent,
bool confirmed,
bool valid
)
{
return (
delivery[_deliveryId].token,
delivery[_deliveryId].seller,
delivery[_deliveryId].buyer,
delivery[_deliveryId].amount,
delivery[_deliveryId].agent,
delivery[_deliveryId].confirmed,
delivery[_deliveryId].valid
);
}

}
Loading