|
| 1 | +# Kolektivo Trinidad & Tobago Dollar Specification |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +**Name**: Kolektivo Trinidad & Tobago Dollar |
| 6 | + |
| 7 | +**Symbol**: KTTD |
| 8 | + |
| 9 | +**Decimals**: Default (18) |
| 10 | + |
| 11 | +## Token Standards |
| 12 | + |
| 13 | +The standards used in this specification are **ERC20**, using the **OpenZeppelin Contracts** library, for the Kolektivo Trinidad & Tobago Dollar token. The faucet is implemented using custom logic, underpinned by the use of Roles assigned to specific addresses. |
| 14 | + |
| 15 | +### Third Party Libraries |
| 16 | + |
| 17 | +[Ownable](https://docs.openzeppelin.com/contracts/2.x/access-control#ownership-and-ownable) - OpenZeppelin Contracts |
| 18 | + |
| 19 | +[Pausable](https://docs.openzeppelin.com/contracts/5.x/api/utils#Pausable) - OpenZeppelin Contracts |
| 20 | + |
| 21 | +[Merkle-Proof](https://docs.openzeppelin.com/contracts/3.x/api/cryptography) - OpenZeppelin Contracts |
| 22 | + |
| 23 | +## Roles |
| 24 | + |
| 25 | +Since there only needs to be one role or a simple access control mechanism, the **Ownable** contract from OpenZeppelin may be used to assign the deployer of the contract as the owner. |
| 26 | + |
| 27 | +## Contracts |
| 28 | + |
| 29 | +| Contract | Description | Standard | |
| 30 | +| --- | --- | --- | |
| 31 | +| KTTD | Kolektivo Trinidad & Tobago Dollar token | ERC20 | |
| 32 | +| Treasury | Manages the Cash-In and Cash-Out processes of the platform | Custom | |
| 33 | + |
| 34 | +### Kolektivo Trinidad & Tobago Dollar |
| 35 | + |
| 36 | +The Kolektivo Trinidad & Tobago Dollar token contract must have some concept of access control, either via using **Ownable** by OpenZeppelin Contracts, or by implementing a custom access control mechanism using roles. While an RBAC approach would be more verbose it is unnecessary overhead for this simple token. |
| 37 | + |
| 38 | +```solidity |
| 39 | +contract KTTD is ERC20, Ownable { |
| 40 | + constructor() ERC20("Kolektivo Trinidad & Tobago Dollar", "KTTD") { |
| 41 | + _mint(msg.sender, amount); |
| 42 | + } |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +> The **Ownable** contract assigns the deployer of the contract as the owner, and provides a modifier `onlyOwner` that can be used to restrict access to certain functions. |
| 47 | +
|
| 48 | +The Ownership of the token contract should be transferred after deployment to the Treasury contract so that the Treasury can manage the token supply. |
| 49 | + |
| 50 | +### Treasury |
| 51 | + |
| 52 | +The faucet contract must have some concept of access control, either via using **Ownable** by OpenZeppelin Contracts, or by implementing a custom access control mechanism using roles. While an RBAC approach would be more verbose it is unnecessary overhead for this simple faucet. We may also want to include a way to control whether the supply can be modified, or if the faucet can be paused, we can achieve this using the **Pausable** contract from OpenZeppelin Contracts. |
| 53 | + |
| 54 | +Only Merchants may withdraw or cash-out from the protocol, as such a Merkle root will be used to maintain the list of Merchants that are allowed to cash-out. The Merkle root will be set by the Treasury contract and can only be updated by the Treasury contract. |
| 55 | + |
| 56 | +```solidity |
| 57 | +contract Treasury is Ownable, Pausable { |
| 58 | + IERC20 public token; |
| 59 | + bytes32 public merkleRoot; |
| 60 | +
|
| 61 | + constructor(IERC20 _token, bytes32 _merkleRoot) { |
| 62 | + token = _token; |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +> The **Ownable** contract assigns the deployer of the contract as the owner, and provides a modifier `onlyOwner` that can be used to restrict access to certain functions. |
| 68 | +
|
| 69 | +## Functions & Flows |
| 70 | + |
| 71 | +The Cash-In and Cash-Out flows both are role restricted, meaning that the internal logic is dependent on being authorized by the Treasury. We can do this by leveraging a `EIP712 Typed Structs` (the digest) approach to sign the request and verify the signature. The digest is a hash signed by the Treasury's private key, and the signature is verified by the Treasury contract. |
| 72 | + |
| 73 | +```ts |
| 74 | +const Types = { |
| 75 | + CashIn: [ |
| 76 | + { name: "user", type: "address" }, |
| 77 | + { name: "amount", type: "uint256" }, |
| 78 | + { name: "nonce", type: "uint256" } |
| 79 | + ], |
| 80 | + ... |
| 81 | +} |
| 82 | + |
| 83 | +const signature = sign(Domain, Types.CashIn, message) |
| 84 | +``` |
| 85 | + |
| 86 | +```sh |
| 87 | +user: address - The address of the account that wants to perform an action |
| 88 | +amount: uint256 - The amount of kTTD that the action specifies |
| 89 | +nonce: uint256 - A unique identifier or counter of all actions signed and executed by the Treasury |
| 90 | +``` |
| 91 | + |
| 92 | +> This is just a rudimentary implementation. |
| 93 | +
|
| 94 | +```solidity |
| 95 | +contract Treasury is Ownable { |
| 96 | + function cashIn(address user, uint256 amount, uint256 nonce, bytes memory signature) public { |
| 97 | + require(hasBeenSignedByTreasurer(user, amount, nonce, signature), "Invalid Signature"); |
| 98 | + token.mint(user, amount); |
| 99 | + } |
| 100 | +
|
| 101 | + function hasBeenSignedByTreasurer(address user, uint256 amount, uint256 nonce, bytes memory signature) public view returns (bool) { |
| 102 | + bytes32 digest = keccak256(abi.encodePacked("\x19\x01", Domain, keccak256(abi.encode(Types.CashIn, user, amount, nonce)))); |
| 103 | + return ECDSA.recover(digest, signature) == owner(); |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +> This is just a rudimentary implementation. |
| 109 | +
|
| 110 | +### Cash-In |
| 111 | + |
| 112 | +```mermaid |
| 113 | +sequenceDiagram |
| 114 | + participant User |
| 115 | + participant Treasurer |
| 116 | + participant Treasury |
| 117 | + participant KTTD |
| 118 | +
|
| 119 | + User->>Treasurer: Request Cash-In |
| 120 | + Treasurer->>Treasury: Execute Cash-In |
| 121 | + Treasury->>KTTD: Mint KTTD |
| 122 | + KTTD-->>Treasury: Minted KTTD |
| 123 | + Treasury-->>Treasurer: Cash-In Successful |
| 124 | +``` |
| 125 | + |
| 126 | +### Cash-Out |
| 127 | + |
| 128 | +```mermaid |
| 129 | +sequenceDiagram |
| 130 | + participant User |
| 131 | + participant Treasurer |
| 132 | + participant Treasury |
| 133 | + participant KTTD |
| 134 | +
|
| 135 | + User->>Treasurer: Request Cash-Out |
| 136 | + Treasurer->>Treasury: Execute Cash-Out |
| 137 | + Treasury->>KTTD: Burn KTTD |
| 138 | + KTTD-->>Treasury: Burned KTTD |
| 139 | + Treasury-->>Treasurer: Cash-Out Successful |
| 140 | +``` |
| 141 | + |
| 142 | +* Collapse treasury functions into ERC20 token contract |
| 143 | +* Is it a transferrable token? |
| 144 | +* use `mint` and `burn` on ERC20 instead of `cashIn` and `cashOut` on the treasury contract |
0 commit comments