Skip to content

Commit

Permalink
delegate erc20 namespace access (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xxlegolas authored Feb 7, 2025
1 parent 45e6601 commit 4ed4827
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 197 deletions.
6 changes: 3 additions & 3 deletions .env
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# .env for the docker file
ERC20_TOKEN_NAME=EVE TOKEN
ERC20_TOKEN_SYMBOL=EVE
ERC20_TOKEN_NAME=TEST TOKEN
ERC20_TOKEN_SYMBOL=TEST
ERC20_INITIAL_SUPPLY=10000000000
EVE_TOKEN_NAMESPACE=eveerc20
EVE_TOKEN_NAMESPACE=test
# By default the token is minted to this address, you can transfer from this admin to other accounts or change this address
EVE_TOKEN_ADMIN=0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266

Expand Down
2 changes: 1 addition & 1 deletion mud-contracts/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"clean": "rm -rf src/codegen && rm -rf out && rm -rf cache",
"deploy": "PRIVATE_KEY=$PRIVATE_KEY mud deploy --rpc $RPC_URL",
"setForwarder": "WORLD_ADDRESS=$WORLD_ADDRESS FORWARDER_ADDRESS=$FORWARDER_ADDRESS forge script script/SetForwarder.s.sol:SetForwarder --rpc-url $RPC_URL --private-key $PRIVATE_KEY --broadcast --sig \"run(address)\" $WORLD_ADDRESS",
"delegateNamespaceAccess": "WORLD_ADDRESS=$WORLD_ADDRESS FORWARDER_ADDRESS=$FORWARDER_ADDRESS forge script script/DelegateNamespace.s.sol:DelegateNamespace --rpc-url $RPC_URL --private-key $PRIVATE_KEY --broadcast --sig \"run(address)\" $WORLD_ADDRESS",
"delegateNamespaceAccess": "EVE_TOKEN_NAMESPACE=$EVE_TOKEN_NAMESPACE WORLD_ADDRESS=$WORLD_ADDRESS FORWARDER_ADDRESS=$FORWARDER_ADDRESS forge script script/DelegateNamespace.s.sol:DelegateNamespace --rpc-url $RPC_URL --private-key $PRIVATE_KEY --broadcast --sig \"run(address)\" $WORLD_ADDRESS",
"dev": "pnpm mud dev-contracts",
"lint": "pnpm run prettier && pnpm run solhint",
"prettier": "prettier 'src/**/*.sol' './*.ts' --plugin=prettier-plugin-solidity --check",
Expand Down
43 changes: 37 additions & 6 deletions mud-contracts/core/script/DelegateNamespace.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,39 @@ contract DelegateNamespace is Script {
StoreSwitch.setStoreAddress(worldAddress);
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
address delegatee = vm.envAddress("FORWARDER_ADDRESS");
string memory erc20NamespaceString = vm.envString("EVE_TOKEN_NAMESPACE"); //TODO add this to the common constancts npm package and import it similar to DEPLOYMENT_NAMESPACE
bytes14 erc20Namespace = stringToBytes14(erc20NamespaceString);

ResourceId NAMESPACE_ID = ResourceId.wrap(bytes32(abi.encodePacked(RESOURCE_NAMESPACE, DEPLOYMENT_NAMESPACE)));
ResourceId WORLD_NAMESPACE_ID = ResourceId.wrap(
bytes32(abi.encodePacked(RESOURCE_NAMESPACE, DEPLOYMENT_NAMESPACE))
);
ResourceId ERC20_NAMESPACE_ID = ResourceId.wrap(bytes32(abi.encodePacked(RESOURCE_NAMESPACE, erc20Namespace)));

vm.startBroadcast(deployerPrivateKey);
requireNamespace(NAMESPACE_ID);
console.log(NamespaceOwner.get(NAMESPACE_ID));
requireNamespace(WORLD_NAMESPACE_ID);
requireNamespace(ERC20_NAMESPACE_ID);
console.log(NamespaceOwner.get(WORLD_NAMESPACE_ID));
console.log(NamespaceOwner.get(ERC20_NAMESPACE_ID));

DelegationControlSystem delegationControl = new DelegationControlSystem();
ResourceId delegationControlId = delegationControlSystemId();

IWorld(worldAddress).registerSystem(delegationControlId, delegationControl, true);

//Delegate the World namespace to the delegatee (Forwarder contract)
IWorld(worldAddress).registerNamespaceDelegation(
WORLD_NAMESPACE_ID,
delegationControlId,
abi.encodeWithSelector(delegationControl.initDelegation.selector, WORLD_NAMESPACE_ID, delegatee)
);
console.log(AccessControl.hasAccess(WORLD_NAMESPACE_ID, delegatee));

//Delegate the ERC20 namespace to the delegatee (Forwarder contract)
IWorld(worldAddress).registerNamespaceDelegation(
NAMESPACE_ID,
ERC20_NAMESPACE_ID,
delegationControlId,
abi.encodeWithSelector(delegationControl.initDelegation.selector, NAMESPACE_ID, delegatee)
abi.encodeWithSelector(delegationControl.initDelegation.selector, ERC20_NAMESPACE_ID, delegatee)
);
console.log(AccessControl.hasAccess(NAMESPACE_ID, delegatee));

vm.stopBroadcast();
}
Expand All @@ -46,4 +61,20 @@ contract DelegateNamespace is Script {
return
WorldResourceIdLib.encode({ typeId: RESOURCE_SYSTEM, namespace: DEPLOYMENT_NAMESPACE, name: "DelegationContr" });
}

function stringToBytes14(string memory str) public pure returns (bytes14) {
bytes memory tempBytes = bytes(str);

// Ensure the bytes array is not longer than 14 bytes.
// If it is, this will truncate the array to the first 14 bytes.
// If it's shorter, it will be padded with zeros.
require(tempBytes.length <= 14, "String too long");

bytes14 converted;
for (uint i = 0; i < tempBytes.length; i++) {
converted |= bytes14(tempBytes[i] & 0xFF) >> (i * 8);
}

return converted;
}
}
1 change: 1 addition & 0 deletions mud-contracts/world-v2/src/codegen/common.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity >=0.8.24;

/* Autogenerated file. Do not edit manually. */

enum State {
NULL,
UNANCHORED,
Expand Down
8 changes: 0 additions & 8 deletions mud-contracts/world/.env
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,3 @@ BASE_URI=http://127.0.0.1:8080/ipfs/
# ADMIN ACCESS ACCOUNTS (to assign to the ADMIN access list in the InventoryAccess.s.sol script)
# Add the list of admins by comma separated address
ADMIN_ACCOUNTS=0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266,0x70997970C51812dc3A010C7d01b50e0d17dc79C8,0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC

ERC20_TOKEN_NAME=EVE TOKEN
ERC20_TOKEN_SYMBOL=EVE
ERC20_INITIAL_SUPPLY=10000000000
EVE_TOKEN_NAMESPACE=test
# By default the token is minted to this address, you can transfer from this admin to other accounts or change this address
EVE_TOKEN_ADMIN=0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266

2 changes: 1 addition & 1 deletion mud-contracts/world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"build:abi": "forge build --silent",
"build:abi-ts": "mud abi-ts",
"build:mud": "rm -rf src/codegen && mud tablegen && mud worldgen",
"deploy": "PRIVATE_KEY=$PRIVATE_KEY mud deploy --rpc $RPC_URL --alwaysRunPostDeploy",
"deploy": "ERC20_TOKEN_NAME=$ERC20_TOKEN_NAME && ERC20_TOKEN_SYMBOL=$ERC20_TOKEN_SYMBOL && ERC20_INITIAL_SUPPLY=$ERC20_INITIAL_SUPPLY && EVE_TOKEN_NAMESPACE=$EVE_TOKEN_NAMESPACE && EVE_TOKEN_ADMIN=$EVE_TOKEN_ADMIN && PRIVATE_KEY=$PRIVATE_KEY mud deploy --rpc $RPC_URL --alwaysRunPostDeploy",
"upgrade": "PRIVATE_KEY=$PRIVATE_KEY mud deploy --rpc $RPC_URL --worldAddress $WORLD_ADDRESS",
"clean": "rm -rf src/codegen && rm -rf out && rm -rf cache",
"dev": "pnpm mud dev-contracts",
Expand Down
2 changes: 1 addition & 1 deletion mud-contracts/world/script/PostDeploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ contract PostDeploy is Script {
string memory symbol = vm.envString("ERC20_TOKEN_SYMBOL");

uint8 decimals = uint8(18);
uint256 amount = vm.envUint("ERC20_INITIAL_SUPPLY");
uint256 amount = uint256(vm.envUint("ERC20_INITIAL_SUPPLY"));
address to = vm.envAddress("EVE_TOKEN_ADMIN");

// ERC20 TOKEN DEPLOYMENT
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
"name": "root",
"private": true,
"scripts": {
"dev": "mprocs './scripts/run-local-node.sh' './scripts/deploy-all.sh --rpc-url http://127.0.0.1:8545 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'",
"garnet": "mprocs './scripts/deploy-all.sh --rpc-url https://rpc.garnetchain.com --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'"
"dev": "dotenv -e .env mprocs './scripts/run-local-node.sh' './scripts/deploy-all.sh --rpc-url http://127.0.0.1:8545 --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'",
"garnet": "dotenv -e .env mprocs './scripts/deploy-all.sh --rpc-url https://rpc.garnetchain.com --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'"
},
"devDependencies": {
"dotenv-cli": "^8.0.0",
"mprocs": "^0.6.4",
"nx": "18.2.0"
},
Expand Down
Loading

0 comments on commit 4ed4827

Please sign in to comment.