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

feat(modules): standalone erc20 with external store #3094

Open
wants to merge 92 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
26100d2
add package
TamaraRingas Aug 28, 2024
7889451
update gitignore
TamaraRingas Aug 28, 2024
6a305d1
update README
TamaraRingas Aug 28, 2024
a12c8ce
add tables to config
TamaraRingas Aug 28, 2024
0ce58bc
add event & error interfaces
TamaraRingas Aug 28, 2024
51f8629
constructor, view & store functions
TamaraRingas Aug 28, 2024
d0aebeb
rm old gas-report
TamaraRingas Aug 28, 2024
6e8d675
add dist/
TamaraRingas Aug 28, 2024
7a3d91a
update package.json
TamaraRingas Aug 28, 2024
9bcf408
update lockfile
TamaraRingas Aug 28, 2024
7070f43
tablegen
TamaraRingas Aug 28, 2024
d48134c
comments
TamaraRingas Aug 28, 2024
64a8ed9
natspec
TamaraRingas Aug 28, 2024
1293538
transfer functions
TamaraRingas Aug 28, 2024
56bcd40
implement _spendAllowance()
TamaraRingas Aug 28, 2024
83af90f
implement _update() function
TamaraRingas Aug 28, 2024
4463f2c
format
TamaraRingas Aug 28, 2024
947d8b8
implement transferFrom()
TamaraRingas Aug 28, 2024
0f64286
implement _burn()
TamaraRingas Aug 28, 2024
1d6ab4b
implement _mint() function
TamaraRingas Aug 28, 2024
d9bc309
reorder functions
TamaraRingas Aug 28, 2024
13b9273
add mint test
TamaraRingas Aug 28, 2024
d2a1968
add burn test
TamaraRingas Aug 28, 2024
d485af9
update imports
TamaraRingas Aug 28, 2024
e97d283
comments
TamaraRingas Aug 28, 2024
ab4e0a4
update approve() implementation
TamaraRingas Aug 28, 2024
94cb254
add Transfer() test
TamaraRingas Aug 28, 2024
9f5b2c3
make mint & burn external
TamaraRingas Aug 28, 2024
150ee12
add GasReporter to tests
TamaraRingas Aug 28, 2024
32c87b3
reorder brackets in _update()
TamaraRingas Aug 28, 2024
9ce92f9
add GasReporter to tests
TamaraRingas Aug 29, 2024
2f43d48
add charlie address
TamaraRingas Aug 29, 2024
1f18db5
set Store address
TamaraRingas Aug 29, 2024
36958a0
add owner to Token
TamaraRingas Aug 29, 2024
b1d2790
add owner to constructor
TamaraRingas Aug 29, 2024
46e6047
add owner to Token
TamaraRingas Aug 29, 2024
5243007
add owner to constructor
TamaraRingas Aug 29, 2024
938be58
add fuzz tests
TamaraRingas Aug 29, 2024
914b0d5
update natspec
TamaraRingas Aug 29, 2024
1ba9cca
rename MUDERC20 to ERC20
TamaraRingas Aug 29, 2024
31d4192
add gas report
TamaraRingas Aug 29, 2024
2f84632
bump pragma
TamaraRingas Aug 29, 2024
3332e1e
add changeset
TamaraRingas Aug 29, 2024
281a554
add changeset
TamaraRingas Aug 29, 2024
a41870a
rm table directory from codegen
TamaraRingas Aug 29, 2024
d512f84
update gasReport() implementation
TamaraRingas Aug 29, 2024
5cefafb
update gas-report.json
TamaraRingas Aug 29, 2024
92b0b40
Merge branch 'main' of https://github.com/latticexyz/mud into tr/erc2…
TamaraRingas Aug 29, 2024
b081cc5
Update package.json
TamaraRingas Aug 29, 2024
6b4c925
Update package.json
TamaraRingas Aug 29, 2024
41971c7
Update package.json
TamaraRingas Aug 29, 2024
43c37b6
update lockfile
TamaraRingas Aug 29, 2024
a6534e5
Merge branch 'main' of https://github.com/latticexyz/mud into tr/erc2…
TamaraRingas Aug 29, 2024
6a56b71
add new package
TamaraRingas Aug 29, 2024
414389f
update lockfile
TamaraRingas Aug 29, 2024
57a45b5
Contract skeletons
TamaraRingas Aug 29, 2024
d50f24a
add Store
TamaraRingas Aug 29, 2024
1415c02
update imports
TamaraRingas Aug 30, 2024
be4ef5c
update imports
TamaraRingas Aug 30, 2024
2267b95
rm Store function implementations
TamaraRingas Aug 30, 2024
2182594
update mud.config.ts
TamaraRingas Aug 30, 2024
8fd79d4
tablegen
TamaraRingas Aug 30, 2024
5ac0213
convert _symbol to bytes30 to get tableId
TamaraRingas Aug 30, 2024
fa0b455
set store address
TamaraRingas Aug 30, 2024
e033211
Update imports
TamaraRingas Aug 30, 2024
c15a3d8
rm old gas report
TamaraRingas Aug 30, 2024
7ba1a10
Merge branch 'tr/erc20-external-store' of https://github.com/latticex…
TamaraRingas Aug 30, 2024
fe813ce
talegen
TamaraRingas Aug 30, 2024
e4de969
rm id field
TamaraRingas Aug 30, 2024
f533220
update setters & getters & store tableId as state var
TamaraRingas Aug 30, 2024
ef022a8
implement Store contract
TamaraRingas Aug 30, 2024
201d9b8
update imports
TamaraRingas Aug 30, 2024
c7697f9
rm IStore
TamaraRingas Aug 30, 2024
15cda8c
add tests
TamaraRingas Aug 30, 2024
9d64323
add store to getters & setters
TamaraRingas Aug 30, 2024
1c2aad6
update tests
TamaraRingas Aug 30, 2024
8e935a8
add getStaticField()
TamaraRingas Aug 30, 2024
6437647
add getDynamicField()
TamaraRingas Aug 30, 2024
3b287bb
rm commented code
TamaraRingas Aug 30, 2024
b97631a
add gas report
TamaraRingas Aug 30, 2024
3c50195
update README
TamaraRingas Aug 30, 2024
7d43058
add changeset
TamaraRingas Aug 30, 2024
0e635fa
update package.json
TamaraRingas Aug 30, 2024
69fc634
add changeset
TamaraRingas Aug 30, 2024
1367813
Merge branch 'main' into tr/erc20-external-store
TamaraRingas Aug 30, 2024
c094ba4
Merge branch 'main' into tr/erc20-external-store
TamaraRingas Aug 30, 2024
f94000b
Merge branch 'main' of https://github.com/latticexyz/mud into tr/erc2…
TamaraRingas Sep 1, 2024
0eba5e5
Merge branch 'tr/erc20-external-store' of https://github.com/latticex…
TamaraRingas Sep 1, 2024
c8f0f38
add getDynamicField()
TamaraRingas Sep 1, 2024
c8109b0
add getStaticField()
TamaraRingas Sep 1, 2024
f7bba1a
Merge branch 'main' into tr/erc20-external-store
TamaraRingas Sep 2, 2024
3ea7a95
Merge branch 'main' into tr/erc20-external-store
TamaraRingas Sep 2, 2024
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
45 changes: 39 additions & 6 deletions packages/store/test/StoreMock.sol
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;

import { IStore } from "../src/IStore.sol";
import { IStoreHook } from "../src/IStoreHook.sol";
import { StoreSwitch } from "../src/StoreSwitch.sol";
import { Store } from "../src/Store.sol";
import { EncodedLengths } from "../src/EncodedLengths.sol";
import { StoreCore } from "../src/StoreCore.sol";
import { Schema } from "../src/Schema.sol";
import { FieldLayout } from "../src/FieldLayout.sol";
import { IStore } from "../src/IStore.sol";
import { StoreRead } from "../src/StoreRead.sol";
import { StoreCore } from "../src/StoreCore.sol";
import { IStoreRead } from "../src/IStoreRead.sol";
import { ResourceId } from "../src/ResourceId.sol";
import { IStoreHook } from "../src/IStoreHook.sol";
import { StoreSwitch } from "../src/StoreSwitch.sol";
import { FieldLayout } from "../src/FieldLayout.sol";
import { EncodedLengths } from "../src/EncodedLengths.sol";

/**
* StoreMock is a contract wrapper around the StoreCore library for testing purposes.
Expand Down Expand Up @@ -87,6 +88,23 @@ contract StoreMock is Store {
StoreCore.setStaticField(tableId, keyTuple, fieldIndex, data, fieldLayout);
}

/**
* @notice Retrieves data for a specific static (fixed length) field in a record.
* @param tableId The ID of the table.
* @param keyTuple The tuple used as a key to fetch the static field.
* @param fieldIndex Index of the static field to retrieve.
* @param fieldLayout The layout of fields for the retrieval.
* @return data The static data of the specified field.
*/
function getStaticField(
ResourceId tableId,
bytes32[] calldata keyTuple,
uint8 fieldIndex,
FieldLayout fieldLayout
) public view virtual override(IStoreRead, StoreRead) returns (bytes32 data) {
data = StoreCore.getStaticField(tableId, keyTuple, fieldIndex, fieldLayout);
}

// Set partial data at dynamic field index
function setDynamicField(
ResourceId tableId,
Expand Down Expand Up @@ -117,6 +135,21 @@ contract StoreMock is Store {
StoreCore.popFromDynamicField(tableId, keyTuple, dynamicFieldIndex, byteLengthToPop);
}

/**
* @notice Retrieves data for a specific dynamic (variable length) field in a record.
* @param tableId The ID of the table.
* @param keyTuple The tuple used as a key to fetch the dynamic field.
* @param dynamicFieldIndex Index of the dynamic field to retrieve.
* @return data The dynamic data of the specified field.
*/
function getDynamicField(
ResourceId tableId,
bytes32[] calldata keyTuple,
uint8 dynamicFieldIndex
) public view virtual override(IStoreRead, StoreRead) returns (bytes memory data) {
data = StoreCore.getDynamicField(tableId, keyTuple, dynamicFieldIndex);
}

// Set full record (including full dynamic data)
function deleteRecord(ResourceId tableId, bytes32[] memory keyTuple) public virtual {
StoreCore.deleteRecord(tableId, keyTuple);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changesets

Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
with multi-package repos, or single-package repos to help you version and publish your code. You can
find the full documentation for it [in our repository](https://github.com/changesets/changesets)

We have a quick list of common questions to get you started engaging with this project in
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/world-module-erc20-external-store": major
---

Add erc20 module with external store
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": false,
"fixed": [],
"linked": [],
"access": "restricted",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
}
3 changes: 3 additions & 0 deletions packages/world-module-erc20-external-store/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cache
out
src/codegen/world/
8 changes: 8 additions & 0 deletions packages/world-module-erc20-external-store/.solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "solhint:recommended",
"rules": {
"compiler-version": ["error", ">=0.8.0"],
"avoid-low-level-calls": "off",
"func-visibility": ["warn", { "ignoreConstructors": true }]
}
}
5 changes: 5 additions & 0 deletions packages/world-module-erc20-external-store/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Standalone ERC20 Module with external Store

This is an `ERC20` token template that is compatible with the MUD framework. The logic of the token contract is near native and resides within the token contract, while its storage makes use of `Store` and is reflected in corresponding tables.

The `OpenZeppelin` implementation of ERC20 is used as reference but tests are written in `Solidity`.
15 changes: 15 additions & 0 deletions packages/world-module-erc20-external-store/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[profile.default]
solc = "0.8.24"
ffi = false
fuzz_runs = 256
optimizer = true
optimizer_runs = 3000
verbosity = 2
allow_paths = ["../../node_modules", "../"]
src = "src"
out = "out"
bytecode_hash = "none"
extra_output_files = [
"abi",
"evm.bytecode"
]
170 changes: 170 additions & 0 deletions packages/world-module-erc20-external-store/gas-report.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
[
{
"file": "test/ERC20.t.sol",
"test": "testApprove",
"name": "token constructor",
"gasUsed": 8474513
},
{
"file": "test/ERC20.t.sol",
"test": "testApprove",
"name": "token approve",
"gasUsed": 70155
},
{
"file": "test/ERC20.t.sol",
"test": "testBalanceOf",
"name": "token constructor",
"gasUsed": 8474513
},
{
"file": "test/ERC20.t.sol",
"test": "testBalanceOf",
"name": "token constructor",
"gasUsed": 8474513
},
{
"file": "test/ERC20.t.sol",
"test": "testBalanceOf",
"name": "token constructor",
"gasUsed": 8474513
},
{
"file": "test/ERC20.t.sol",
"test": "testBalanceOf",
"name": "token constructor",
"gasUsed": 8474513
},
{
"file": "test/ERC20.t.sol",
"test": "testBalanceOf",
"name": "token constructor",
"gasUsed": 8474513
},
{
"file": "test/ERC20.t.sol",
"test": "testTokenBurn",
"name": "token constructor",
"gasUsed": 8474513
},
{
"file": "test/ERC20.t.sol",
"test": "testTokenBurn",
"name": "token burn",
"gasUsed": 79906
},
{
"file": "test/ERC20.t.sol",
"test": "testTokenBurnInvalidCaller",
"name": "token constructor",
"gasUsed": 8474513
},
{
"file": "test/ERC20.t.sol",
"test": "testTokenBurnInvalidCaller",
"name": "token burn invalid caller",
"gasUsed": 38433
},
{
"file": "test/ERC20.t.sol",
"test": "testTokenBurnZeroAddress",
"name": "token constructor",
"gasUsed": 8474513
},
{
"file": "test/ERC20.t.sol",
"test": "testTokenBurnZeroAddress",
"name": "token burn zero address",
"gasUsed": 38348
},
{
"file": "test/ERC20.t.sol",
"test": "testTokenMint",
"name": "token constructor",
"gasUsed": 8474513
},
{
"file": "test/ERC20.t.sol",
"test": "testTokenMint",
"name": "token mint",
"gasUsed": 101554
},
{
"file": "test/ERC20.t.sol",
"test": "testTokenMintInvalidCaller",
"name": "token constructor",
"gasUsed": 8474513
},
{
"file": "test/ERC20.t.sol",
"test": "testTokenMintInvalidCaller",
"name": "token mint invalid caller",
"gasUsed": 38434
},
{
"file": "test/ERC20.t.sol",
"test": "testTokenMintToZeroAddress",
"name": "token constructor",
"gasUsed": 8474513
},
{
"file": "test/ERC20.t.sol",
"test": "testTokenMintToZeroAddress",
"name": "token mint to zero address",
"gasUsed": 38349
},
{
"file": "test/ERC20.t.sol",
"test": "testTokenSetUp",
"name": "token constructor",
"gasUsed": 8474513
},
{
"file": "test/ERC20.t.sol",
"test": "testTokenTransfer",
"name": "token constructor",
"gasUsed": 8474513
},
{
"file": "test/ERC20.t.sol",
"test": "testTokenTransfer",
"name": "token transfer",
"gasUsed": 86103
},
{
"file": "test/ERC20.t.sol",
"test": "testTokenTransferInsufficientBalance",
"name": "token constructor",
"gasUsed": 8474513
},
{
"file": "test/ERC20.t.sol",
"test": "testTokenTransferInsufficientBalance",
"name": "token transfer insufficient balance",
"gasUsed": 34297
},
{
"file": "test/ERC20.t.sol",
"test": "testTransferFrom",
"name": "token constructor",
"gasUsed": 8474513
},
{
"file": "test/ERC20.t.sol",
"test": "testTransferFrom",
"name": "token transferFrom",
"gasUsed": 102755
},
{
"file": "test/ERC20.t.sol",
"test": "testTransferFromInsufficientAllowance",
"name": "token constructor",
"gasUsed": 8474513
},
{
"file": "test/ERC20.t.sol",
"test": "testTransferFromInsufficientAllowance",
"name": "token transferFrom insufficient allowance",
"gasUsed": 36861
}
]
38 changes: 38 additions & 0 deletions packages/world-module-erc20-external-store/mud.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { defineWorld } from "@latticexyz/world";

export default defineWorld({
namespace: "erc20-store",
tables: {
Token: {
schema: {
decimals: "uint8",
totalSupply: "uint256",
owner: "address",
name: "string",
symbol: "string",
},
key: [],
codegen: {
tableIdArgument: true,
storeArgument: true,
},
},
Balances: {
schema: {
tokenAddress: "address",
account: "address",
balance: "uint256",
},
key: ["tokenAddress", "account"],
},
Allowances: {
schema: {
tokenAddress: "address",
account: "address",
spender: "address",
approval: "uint256",
},
key: ["tokenAddress", "account", "spender"],
},
},
});
56 changes: 56 additions & 0 deletions packages/world-module-erc20-external-store/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "@latticexyz/world-module-erc20-external-store",
"version": "2.1.1",
"description": "ERC20 World Module with External Store",
"repository": {
"type": "git",
"url": "https://github.com/latticexyz/mud.git",
"directory": "packages/world-module-erc20-own-store"
},
"license": "MIT",
"type": "module",
"exports": {
"./mud.config": "./dist/mud.config.js",
"./out/*": "./out/*"
},
"typesVersions": {
"*": {
"mud.config": [
"./dist/mud.config.d.ts"
]
}
},
"files": [
"dist",
"out",
"src"
],
"scripts": {
"build": "mud build",
"clean": "forge clean && shx rm -rf src/**/codegen",
"gas-report": "gas-report --save gas-report.json",
"lint": "solhint --config ./.solhint.json 'src/**/*.sol'",
"test": "forge test",
"test:ci": "pnpm run test"
},
"dependencies": {
"@latticexyz/schema-type": "workspace:*",
"@latticexyz/store": "workspace:*",
"@latticexyz/world": "workspace:*"
},
"devDependencies": {
"@latticexyz/abi-ts": "workspace:*",
"@latticexyz/cli": "workspace:*",
"@latticexyz/gas-report": "workspace:*",
"@types/node": "^18.15.11",
"ds-test": "https://github.com/dapphub/ds-test.git#e282159d5170298eb2455a6c05280ab5a73a4ef0",
"forge-std": "https://github.com/foundry-rs/forge-std.git#74cfb77e308dd188d2f58864aaf44963ae6b88b1",
"solhint": "^3.3.7",
"tsup": "^6.7.0",
"tsx": "^3.12.6",
"vitest": "0.34.6"
},
"publishConfig": {
"access": "public"
}
}
Loading
Loading