Skip to content

Commit

Permalink
feat(world-modules): add totalSupply table, use OpenZeppelin IERC20 (
Browse files Browse the repository at this point in the history
…#1799)

Co-authored-by: Fraser Scott <[email protected]>
  • Loading branch information
alvrs and yonadaa committed Oct 30, 2023
1 parent 7d6ffd0 commit 0a5d5ee
Show file tree
Hide file tree
Showing 16 changed files with 282 additions and 246 deletions.
4 changes: 2 additions & 2 deletions packages/world-modules/gas-report.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
"file": "test/ERC20.t.sol",
"test": "testBurn",
"name": "burn",
"gasUsed": 75963
"gasUsed": 75987
},
{
"file": "test/ERC20.t.sol",
"test": "testMint",
"name": "mint",
"gasUsed": 161802
"gasUsed": 161826
},
{
"file": "test/ERC20.t.sol",
Expand Down
9 changes: 8 additions & 1 deletion packages/world-modules/mud.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,18 @@ export default mudConfig({
},
tableIdArgument: true,
},
Metadata: {
TotalSupply: {
directory: "modules/erc20-puppet/tables",
keySchema: {},
valueSchema: {
totalSupply: "uint256",
},
tableIdArgument: true,
},
Metadata: {
directory: "modules/erc20-puppet/tables",
keySchema: {},
valueSchema: {
decimals: "uint8",
name: "string",
symbol: "string",
Expand Down
1 change: 1 addition & 0 deletions packages/world-modules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@latticexyz/schema-type": "workspace:*",
"@latticexyz/store": "workspace:*",
"@latticexyz/world": "workspace:*",
"@openzeppelin/contracts": "^5.0.0",
"zod": "^3.21.4"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/world-modules/remappings.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ds-test/=node_modules/ds-test/src/
forge-std/=node_modules/forge-std/src/
@latticexyz/=node_modules/@latticexyz/
@openzeppelin/=node_modules/@openzeppelin/
1 change: 1 addition & 0 deletions packages/world-modules/src/index.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ import { TimeboundDelegations, TimeboundDelegationsTableId } from "./modules/std
import { PuppetRegistry } from "./modules/puppet/tables/PuppetRegistry.sol";
import { Balances } from "./modules/erc20-puppet/tables/Balances.sol";
import { Allowances } from "./modules/erc20-puppet/tables/Allowances.sol";
import { TotalSupply } from "./modules/erc20-puppet/tables/TotalSupply.sol";
import { Metadata, MetadataData } from "./modules/erc20-puppet/tables/Metadata.sol";
import { ERC20Registry } from "./modules/erc20-puppet/tables/ERC20Registry.sol";
19 changes: 9 additions & 10 deletions packages/world-modules/src/modules/erc20-puppet/ERC20System.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ import { ALLOWANCES_NAME, BALANCES_NAME, METADATA_NAME } from "./constants.sol";
import { AccessControlLib } from "../../utils/AccessControlLib.sol";
import { PuppetMaster } from "../puppet/PuppetMaster.sol";

import { IERC20Errors } from "./IERC20Errors.sol";
import { IERC20Mintable } from "./IERC20Mintable.sol";
import { IERC20Events } from "./IERC20Events.sol";

import { Allowances } from "./tables/Allowances.sol";
import { Balances } from "./tables/Balances.sol";
import { TotalSupply } from "./tables/TotalSupply.sol";
import { Metadata } from "./tables/Metadata.sol";

import { _allowancesTableId, _balancesTableId, _metadataTableId, _toBytes32 } from "./utils.sol";
import { _allowancesTableId, _balancesTableId, _totalSupplyTableId, _metadataTableId, _toBytes32 } from "./utils.sol";

contract ERC20System is System, IERC20Mintable, IERC20Errors, PuppetMaster {
contract ERC20System is System, IERC20Mintable, PuppetMaster {
using WorldResourceIdInstance for ResourceId;

/**
Expand Down Expand Up @@ -61,7 +60,7 @@ contract ERC20System is System, IERC20Mintable, IERC20Errors, PuppetMaster {
* @dev Returns the value of tokens in existence.
*/
function totalSupply() external view returns (uint256) {
return Metadata.getTotalSupply(_metadataTableId(_namespace()));
return TotalSupply.get(_totalSupplyTableId(_namespace()));
}

/**
Expand Down Expand Up @@ -203,12 +202,12 @@ contract ERC20System is System, IERC20Mintable, IERC20Errors, PuppetMaster {
*/
function _update(address from, address to, uint256 value) internal virtual {
bytes14 namespace = _namespace();
ResourceId metadataTableId = _metadataTableId(namespace);
ResourceId totalSupplyTableId = _totalSupplyTableId(namespace);
ResourceId balanceTableId = _balancesTableId(namespace);

if (from == address(0)) {
// Overflow check required: The rest of the code assumes that totalSupply never overflows
Metadata.setTotalSupply(metadataTableId, Metadata.getTotalSupply(metadataTableId) + value);
TotalSupply.set(totalSupplyTableId, TotalSupply.get(totalSupplyTableId) + value);
} else {
uint256 fromBalance = Balances.get(balanceTableId, from);
if (fromBalance < value) {
Expand All @@ -223,7 +222,7 @@ contract ERC20System is System, IERC20Mintable, IERC20Errors, PuppetMaster {
if (to == address(0)) {
unchecked {
// Overflow not possible: value <= totalSupply or value <= fromBalance <= totalSupply.
Metadata.setTotalSupply(metadataTableId, Metadata.getTotalSupply(metadataTableId) - value);
TotalSupply.set(totalSupplyTableId, TotalSupply.get(totalSupplyTableId) - value);
}
} else {
unchecked {
Expand All @@ -233,7 +232,7 @@ contract ERC20System is System, IERC20Mintable, IERC20Errors, PuppetMaster {
}

// Emit Transfer event on puppet
puppet().log(IERC20Events.Transfer.selector, _toBytes32(from), _toBytes32(to), abi.encode(value));
puppet().log(Transfer.selector, _toBytes32(from), _toBytes32(to), abi.encode(value));
}

/**
Expand All @@ -254,7 +253,7 @@ contract ERC20System is System, IERC20Mintable, IERC20Errors, PuppetMaster {
Allowances.set(_allowancesTableId(_namespace()), owner, spender, value);

// Emit Approval event on puppet
puppet().log(IERC20Events.Approval.selector, _toBytes32(owner), _toBytes32(spender), abi.encode(value));
puppet().log(Approval.selector, _toBytes32(owner), _toBytes32(spender), abi.encode(value));
}

/**
Expand Down
93 changes: 0 additions & 93 deletions packages/world-modules/src/modules/erc20-puppet/IERC20.sol

This file was deleted.

49 changes: 0 additions & 49 deletions packages/world-modules/src/modules/erc20-puppet/IERC20Errors.sol

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

pragma solidity >=0.8.21;

import { IERC20 } from "./IERC20.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { IERC20Metadata } from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import { IERC20Errors } from "@openzeppelin/contracts/interfaces/draft-IERC6093.sol";

/**
* @dev Extending the ERC20 standard with permissioned mint and burn functions.
*/
interface IERC20Mintable is IERC20 {
interface IERC20Mintable is IERC20, IERC20Metadata, IERC20Errors {
/**
* @dev Creates a `value` amount of tokens and assigns them to `account`, by transferring it from address(0).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ResourceId constant MODULE_NAMESPACE_ID = ResourceId.wrap(

bytes16 constant ALLOWANCES_NAME = "Allowances";
bytes16 constant BALANCES_NAME = "Balances";
bytes16 constant TOTAL_SUPPLY_NAME = "TotalSupply";
bytes16 constant METADATA_NAME = "Metadata";
bytes16 constant ERC20_SYSTEM_NAME = "ERC20System";

Expand Down
Loading

0 comments on commit 0a5d5ee

Please sign in to comment.