Skip to content

Commit

Permalink
refactor: smart character module v2 implementation (#133)
Browse files Browse the repository at this point in the history
* adding erc20 eve token deployment as part of the world

* refactor: adding ERC721 puppet contracts

* removed erc721 puppet

* adding erc721 deployments for smart character and smart deployable

* refactor: modify functions to use a struct for parameters.
- improves code readability and maintainability by reducing the number of parameters and ensuring all relevant data is passed together.

* refactor: adding character tables and systems

* refactor: adding erc721 code snippets from mud

* refactor: adding tests for erc721

* refactor: modify static data table to match erc721

* test: add tests for erc721

* refactor: using utils to get systemId

* refactor: refactoring static data to algin with erc721

* adding revert conditions

* chore: creating a single interface for errors

* refactor: added end-end-tests to createCharacter

* fix: imports and merge conflicts

* lint: fixing lint errors
  • Loading branch information
0xxlegolas authored Aug 21, 2024
1 parent 11e046b commit 8e85ef3
Show file tree
Hide file tree
Showing 55 changed files with 5,351 additions and 466 deletions.
10 changes: 10 additions & 0 deletions end-to-end-tests-v2/.env.devnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Enable debug logs for MUD CLI
DEBUG=mud:*
#
# Anvil default private key:
PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80

# Rpc url for devnet blockchain node
RPC_URL=https://devnet-data-sync.nursery.reitnorf.com

WORLD_ADDRESS=0xd4fC27377dd7a2C0F7DF5C8A67Dd555C0b8Bf5D8
12 changes: 12 additions & 0 deletions end-to-end-tests-v2/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Enable debug logs for MUD CLI
DEBUG=mud:*
#
# Anvil default private key:
PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80

# Rpc url for local blockchain node
RPC_URL=http://127.0.0.1:8545


WORLD_ADDRESS=0x036a9e1e4c46449ffd9e7b9d0321395b9096be0c

8 changes: 8 additions & 0 deletions end-to-end-tests-v2/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"plugins": ["prettier-plugin-solidity"],
"printWidth": 120,
"semi": true,
"tabWidth": 2,
"useTabs": false,
"bracketSpacing": true
}
13 changes: 13 additions & 0 deletions end-to-end-tests-v2/.solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": ["solhint:recommended", "mud"],
"plugins": ["mud"],
"rules": {
"compiler-version": ["error", ">=0.8.0"],
"avoid-low-level-calls": "off",
"no-inline-assembly": "off",
"func-visibility": ["warn", { "ignoreConstructors": true }],
"no-empty-blocks": "off",
"func-name-mixedcase": "warn",
"no-complex-fallback": "off"
}
}
35 changes: 35 additions & 0 deletions end-to-end-tests-v2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# World end to end testing suite

This package contains scripts intended to perform end to end tests for the game chain contracts.


# How to test
1. Make sure you have a blockchain node available pointing to a blockchain with the game chain contracts installed.
2. Make sure enviornment values are set for your target environment.
2.1. `.env.local` and `.env.devnet` are available in the root of this package.
3. Run your tests by invoking npm scripts.


The npm scripts follow a naming convention for a `<FUNCTIONALIT>:<ENVIRONMENT>` e.g. in order to create a smart character on local chain you can run:

```bash
pnpm run createSmartCharacter:local

```


# Environment values
Environment values are used for external values that we are dependant on the target environment.
Test paramameters should should not be defined as environment variables for reproduceability purposes.
## PRIVATE_KEY
The `PRIVATE_KEY` variable should be a 32 bytes/256 bits data represented as a 64 hexadecimal character string.

Note: The Externally owned account (EOA) for which this private key belongs to will become the owner of entities created by the test scripts.
## RPC_URL

A valid url for the RPC endpoint of the target environment.

## WORLD_ADDRESS

A hexadecimal string representing the EVM address of the Mud world smart contract entry point.

12 changes: 12 additions & 0 deletions end-to-end-tests-v2/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[profile.default]
solc = "0.8.24"
evm_version="cancun"
ffi = false
fuzz_runs = 256
optimizer = true
optimizer_runs = 300
verbosity = 2
src = 'script'
out = 'out'


38 changes: 38 additions & 0 deletions end-to-end-tests-v2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "end-to-end-tests-v2",
"version": "0.0.0",
"private": true,
"license": "MIT",
"scripts": {
"build": "forge build",
"clean": "rm -rf src/codegen && rm -rf out && rm -rf cache",
"createSmartCharacter:ci": "RPC_URL=$RPC_URL WORLD_ADDRESS=$WORLD_ADDRESS PRIVATE_KEY=$PRIVATE_KEY forge script script/CreateSmartCharacter.s.sol:CreateSmartCharacter --fork-url $RPC_URL --private-key $PRIVATE_KEY --broadcast --sig \"run(address)\" $WORLD_ADDRESS -vvv",
"createSmartCharacter:local": "source ./.env.local && RPC_URL=$RPC_URL WORLD_ADDRESS=$WORLD_ADDRESS PRIVATE_KEY=$PRIVATE_KEY forge script script/CreateSmartCharacter.s.sol:CreateSmartCharacter --fork-url $RPC_URL --private-key $PRIVATE_KEY --broadcast --sig \"run(address)\" $WORLD_ADDRESS -vvv",
"createSmartCharacter:devnet": "source ./.env.devnet && PRIVATE_KEY=$PRIVATE_KEY forge script script/CreateSmartCharacter.s.sol:CreateSmartCharacter --fork-url $RPC_URL --private-key $PRIVATE_KEY --broadcast --sig \"run(address)\" $WORLD_ADDRESS -vvv",
"lint": "pnpm run prettier && pnpm run solhint",
"prettier": "prettier '**/*.sol' --plugin=prettier-plugin-solidity --check",
"fixFormat": "prettier '**/*.sol' --plugin=prettier-plugin-solidity --write",
"solhint": "solhint --config ./.solhint.json 'src/**/*.sol'"
},
"dependencies": {
"@eveworld/world-v2": "link:../mud-contracts/world-v2",
"@latticexyz/cli": "2.0.12",
"@latticexyz/gas-report": "2.0.12",
"@latticexyz/schema-type": "2.0.12",
"@latticexyz/store": "2.0.12",
"@latticexyz/world": "2.0.12",
"@latticexyz/world-modules": "2.0.12"
},
"devDependencies": {
"@types/debug": "4.1.7",
"@types/node": "^18.15.11",
"@types/prettier": "2.7.2",
"ds-test": "https://github.com/dapphub/ds-test.git#e282159d5170298eb2455a6c05280ab5a73a4ef0",
"forge-std": "https://github.com/foundry-rs/forge-std.git#74cfb77e308dd188d2f58864aaf44963ae6b88b1",
"prettier": "^2.6.2",
"prettier-plugin-solidity": "1.1.3",
"solhint": "^3.3.7",
"solhint-config-mud": "2.0.12",
"solhint-plugin-mud": "2.0.12"
}
}
5 changes: 5 additions & 0 deletions end-to-end-tests-v2/remappings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ds-test/=node_modules/ds-test/src/
forge-std/=node_modules/forge-std/src/
@latticexyz/=node_modules/@latticexyz/
@eveworld/=node_modules/@eveworld/

51 changes: 51 additions & 0 deletions end-to-end-tests-v2/script/CreateSmartCharacter.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
pragma solidity >=0.8.24;

import { Script } from "forge-std/Script.sol";
import { console } from "forge-std/console.sol";
import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol";
import { ResourceId, WorldResourceIdLib } from "@latticexyz/world/src/WorldResourceId.sol";
import { IBaseWorld } from "@latticexyz/world/src/codegen/interfaces/IBaseWorld.sol";

import { EntityRecordData, EntityMetadata } from "@eveworld/world-v2/src/systems/entity-record/types.sol";
import { SmartCharacterSystem } from "@eveworld/world-v2/src/systems/smart-character/SmartCharacterSystem.sol";
import { Utils as SmartCharacterUtils } from "@eveworld/world-v2/src/systems/smart-character/Utils.sol";

contract CreateSmartCharacter is Script {
using SmartCharacterUtils for bytes14;

function run(address worldAddress) public {
StoreSwitch.setStoreAddress(worldAddress);
// Load the private key from the `PRIVATE_KEY` environment variable (in .env)
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);
IBaseWorld world = IBaseWorld(worldAddress);

// Test values for creating the smart character
uint256 characterId = 123;
address characterAddress = vm.addr(deployerPrivateKey);
EntityRecordData memory entityRecord = EntityRecordData({
entityId: characterId,
typeId: 123,
itemId: 234,
volume: 100
});

EntityMetadata memory entityRecordMetadata = EntityMetadata({
entityId: characterId,
name: "name",
dappURL: "dappURL",
description: "description"
});

ResourceId systemId = SmartCharacterUtils.smartCharacterSystemId();
world.call(
systemId,
abi.encodeCall(
SmartCharacterSystem.createCharacter,
(characterId, characterAddress, entityRecord, entityRecordMetadata)
)
);

vm.stopBroadcast();
}
}
11 changes: 11 additions & 0 deletions mud-contracts/world-v2/.env
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,14 @@ DEBUG=mud:*
#
# Anvil default private key:
PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
RPC_URL=http://localhost:8545
WORLD_ADDRESS=0x4da5b62e42f6e8e78d587fef9ae524f729f3da17


ERC20_TOKEN_NAME=EVE TOKEN
ERC20_TOKEN_SYMBOL=EVE
ERC20_INITIAL_SUPPLY=10000000000
EVE_TOKEN_NAMESPACE=eveERC20
EVE_TOKEN_ADMIN=0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266

BASE_URI=https://devnet-data-ipfs-gateway.nursery.reitnorf.com/
9 changes: 7 additions & 2 deletions mud-contracts/world-v2/foundry.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[profile.default]
solc = "0.8.24"
evm_version="cancun"
ffi = false
fuzz_runs = 256
optimizer = true
optimizer_runs = 3000
optimizer_runs = 300
seed = '0x2'
verbosity = 2
src = "src"
test = "test"
Expand All @@ -27,3 +28,7 @@ eth_rpc_url = "https://rpc.garnetchain.com"

[profile.redstone]
eth_rpc_url = "https://rpc.redstonechain.com"

[fuzz]
fuzz_runs = 10000
max_test_rejects = 65536
95 changes: 93 additions & 2 deletions mud-contracts/world-v2/mud.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,102 @@ export default defineWorld({
*/
StaticDataMetadata: {
schema: {
classId: "bytes32",
baseURI: "string",
},
key: [],
},
/*************************
* SMART CHARACTER MODULE *
*************************/
Characters: {
schema: {
characterId: "uint256",
characterAddress: "address",
createdAt: "uint256",
},
key: ["characterId"],
},
CharacterToken: {
schema: {
erc721Address: "address",
},
key: [],
},

/*************************
* ERC721 PUPPET MODULE *
************************/
Balances: {
schema: {
account: "address",
value: "uint256",
},
key: ["account"],
codegen: {
tableIdArgument: true,
},
},
ERC721Metadata: {
schema: {
name: "string",
symbol: "string",
baseURI: "string",
},
key: ["classId"],
key: [],
codegen: {
tableIdArgument: true,
},
},
TokenURI: {
schema: {
tokenId: "uint256",
tokenURI: "string",
},
key: ["tokenId"],
codegen: {
tableIdArgument: true,
},
},
Owners: {
schema: {
tokenId: "uint256",
owner: "address",
},
key: ["tokenId"],
codegen: {
tableIdArgument: true,
},
},
TokenApproval: {
schema: {
tokenId: "uint256",
account: "address",
},
key: ["tokenId"],
codegen: {
tableIdArgument: true,
},
},
OperatorApproval: {
schema: {
owner: "address",
operator: "address",
approved: "bool",
},
key: ["owner", "operator"],
codegen: {
tableIdArgument: true,
},
},
ERC721Registry: {
schema: {
namespaceId: "ResourceId",
tokenAddress: "address",
},
key: ["namespaceId"],
codegen: {
tableIdArgument: true,
},
},
/*******************
* LOCATION MODULE *
Expand Down
7 changes: 4 additions & 3 deletions mud-contracts/world-v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
"build": "mud build",
"clean": "forge clean && rm -rf src/codegen && rm -rf out && rm -rf cache",
"deploy:garnet": "mud deploy --profile=garnet",
"deploy:local": "mud deploy",
"deploy:local": "mud deploy --rpc $RPC_URL --alwaysRunPostDeploy",
"deploy:redstone": "mud deploy --profile=redstone",
"dev": "mud dev-contracts",
"lint": "pnpm run prettier && pnpm run solhint",
"prettier": "prettier 'src/**/*.sol' 'test/**/*.sol' './*.ts' --plugin=prettier-plugin-solidity --check",
"fixFormat": "prettier 'src/**/*.sol' 'test/**/*' './*.ts' --plugin=prettier-plugin-solidity --write",
"solhint": "solhint --config ./.solhint.json 'src/**/*.sol' --fix",
"test": "tsc --noEmit && mud test"
"test": "tsc --noEmit && mud test "
},
"dependencies": {
"@latticexyz/cli": "2.0.12",
Expand All @@ -24,6 +24,7 @@
"@latticexyz/world-modules": "2.0.12"
},
"devDependencies": {
"@latticexyz/gas-report": "2.0.12",
"@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",
Expand All @@ -42,4 +43,4 @@
"[email protected]": ">=4.4.1"
}
}
}
}
Loading

0 comments on commit 8e85ef3

Please sign in to comment.