Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add resource id check to balance transfer system
Browse files Browse the repository at this point in the history
alvrs committed Sep 20, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 800b638 commit 6032b6b
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -20,6 +20,11 @@ contract BalanceTransferSystem is System, IWorldErrors {
ResourceId toNamespaceId,
uint256 amount
) public virtual {
// Require the target ID to be a namespace ID
if (!toNamespaceId.isType(RESOURCE_NAMESPACE)) {
revert InvalidResourceType(string(bytes.concat(toNamespaceId.getType())));
}

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

29 changes: 29 additions & 0 deletions packages/world/test/WorldBalance.t.sol
Original file line number Diff line number Diff line change
@@ -231,6 +231,35 @@ contract WorldBalanceTest is Test, GasReporter {
assertEq(Balances.get(world, ResourceId.unwrap(namespaceId)), 0);
}

function testTransferBalanceToNamespaceRevertInvalidResourceType() {
uint256 value = 1 ether;

// Expect the root namespace to have no balance
assertEq(Balances.get(world, ResourceId.unwrap(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(world, ResourceId.unwrap(ROOT_NAMESPACE_ID)), value);

// Expect revert when attempting to transfer to an invalid namespace
ResourceId invalidNamespace = WorldResourceIdLib.encode("something", "invalid", "xx");
vm.prank(caller);
vm.expectRevert(abi.encodeWithSelector(IWorldErrors.InvalidResourceType.selector, "xx"));
world.transferBalanceToNamespace(ROOT_NAMESPACE_ID, invalidNamespace, value);

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

// Expect the non root namespace to have no balance
assertEq(Balances.get(world, ResourceId.unwrap(invalidNamespace)), 0);
}

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

0 comments on commit 6032b6b

Please sign in to comment.