-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(world-modules): system bound delegation control (#1885)
Co-authored-by: alvarius <[email protected]> Co-authored-by: Kevin Ingersoll <[email protected]>
- Loading branch information
1 parent
1327ea8
commit ca226bb
Showing
8 changed files
with
423 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/world-modules/src/modules/std-delegations/SystemboundDelegationControl.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.21; | ||
|
||
import { DelegationControl } from "@latticexyz/world/src/DelegationControl.sol"; | ||
import { ResourceId } from "@latticexyz/world/src/WorldResourceId.sol"; | ||
import { SystemboundDelegations } from "./tables/SystemboundDelegations.sol"; | ||
|
||
contract SystemboundDelegationControl is DelegationControl { | ||
/** | ||
* Verify a delegation by checking if the caller (delegatee) has any available calls left for the given delegator in the SystemboundDelegations table and decrementing the available calls if so. | ||
*/ | ||
function verify(address delegator, ResourceId systemId, bytes memory) public returns (bool) { | ||
// Get the number of available calls for the given delegator, systemId and callData | ||
uint256 availableCalls = SystemboundDelegations.get({ | ||
delegator: delegator, | ||
delegatee: _msgSender(), | ||
systemId: systemId | ||
}); | ||
|
||
if (availableCalls == 1) { | ||
// Remove the delegation from the SystemboundDelegations table | ||
SystemboundDelegations.deleteRecord({ delegator: delegator, delegatee: _msgSender(), systemId: systemId }); | ||
return true; | ||
} | ||
|
||
if (availableCalls > 0) { | ||
// Decrement the number of available calls | ||
unchecked { | ||
availableCalls--; | ||
} | ||
SystemboundDelegations.set({ | ||
delegator: delegator, | ||
delegatee: _msgSender(), | ||
systemId: systemId, | ||
availableCalls: availableCalls | ||
}); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Initialize a delegation by setting the number of available calls in the SystemboundDelegations table | ||
*/ | ||
function initDelegation(address delegatee, ResourceId systemId, uint256 numCalls) public { | ||
SystemboundDelegations.set({ | ||
delegator: _msgSender(), | ||
delegatee: delegatee, | ||
systemId: systemId, | ||
availableCalls: numCalls | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.