Skip to content

Commit

Permalink
fix(world): check namespace exists before balance transfer [L-03] (#2095
Browse files Browse the repository at this point in the history
)

Co-authored-by: alvarius <alvarius@lattice.xyz>
Co-authored-by: Kevin Ingersoll <kingersoll@gmail.com>
  • Loading branch information
3 people committed Jan 12, 2024
1 parent dd724be commit aee8020
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-carpets-develop.md
@@ -0,0 +1,5 @@
---
"@latticexyz/world": patch
---

Namespace balances can no longer be transferred to non-existent namespaces.
Expand Up @@ -2,6 +2,7 @@
pragma solidity >=0.8.21;

import { ResourceId, ResourceIdInstance } from "@latticexyz/store/src/ResourceId.sol";
import { ResourceIds } from "@latticexyz/store/src/codegen/tables/ResourceIds.sol";

import { System } from "../../../System.sol";
import { revertWithBytes } from "../../../revertWithBytes.sol";
Expand Down Expand Up @@ -37,6 +38,9 @@ contract BalanceTransferSystem is System, IWorldErrors {
revert World_InvalidResourceType(RESOURCE_NAMESPACE, toNamespaceId, toNamespaceId.toString());
}

// Require the namespace to exist
AccessControl.requireExistence(toNamespaceId);

// Require caller to have access to the namespace
AccessControl.requireAccess(fromNamespaceId, _msgSender());

Expand Down
26 changes: 26 additions & 0 deletions packages/world/test/WorldBalance.t.sol
Expand Up @@ -276,6 +276,32 @@ contract WorldBalanceTest is Test, GasReporter {
assertEq(Balances.get(invalidNamespace), 0);
}

function testTransferBalanceToNamespaceRevertResourceNotFound() public {
bytes14 toNamespace = "not_registered";
ResourceId toNamespaceId = WorldResourceIdLib.encodeNamespace(toNamespace);

uint256 value = 1 ether;

// Expect the root namespace to have no balance
assertEq(Balances.get(ROOT_NAMESPACE_ID), 0);

// Send balance to root namespace
vm.deal(caller, value);
vm.prank(caller);
(bool success, bytes memory data) = address(world).call{ value: value }("");
assertTrue(success);
assertEq(data.length, 0);

// Expect the root namespace to have the value as balance
assertEq(Balances.get(ROOT_NAMESPACE_ID), value);

// Expect revert when attempting to transfer to a non-existent namespace
vm.expectRevert(
abi.encodeWithSelector(IWorldErrors.World_ResourceNotFound.selector, toNamespaceId, toNamespaceId.toString())
);
world.transferBalanceToNamespace(ROOT_NAMESPACE_ID, toNamespaceId, value);
}

function testTransferBalanceToAddress() public {
uint256 value = 1 ether;

Expand Down

0 comments on commit aee8020

Please sign in to comment.