DappConfig.sol
Overview
DappConfig.sol defines the DappConfig contract, which serves as the configuration manager for an individual decentralized application (DApp). Its primary role is to store and manage credit configuration parameters, including balances and limits related to token usage within the DApp. The contract ensures secure updates to these configurations and handles incoming token transfers, adjusting balances accordingly. It enforces access control to sensitive operations, allowing only authorized entities to modify the configuration data.
Contract: DappConfig
Purpose
The DappConfig contract maintains the credit configuration state for a single DApp instance. It provides mechanisms for initializing the configuration, updating credit balances, managing authorized public keys, and processing incoming token transfers. This contract is designed to interact primarily with the DappRoot entity (as the deployer and owner) and internal system messages for secure updates.
State Variables
Variable | Type | Visibility | Description |
|---|---|---|---|
|
| internal | Contract version identifier ("1.0.0"). |
|
| internal | Stores credit configuration data including balances and limits. |
|
| internal | Owner address, set to a fixed address expected to be |
|
| internal | Unique identifier for the DApp instance. |
|
| public | Optional address authorized to append new public keys. |
|
| public | Dynamic array storing authorized public keys. |
Constructor
constructor (
uint256 dapp_id,
CreditConfig data,
optional(address) authorityAddress
)
Parameters:
dapp_id: The unique identifier for the DApp, provided during deployment.data: Initial credit configuration, including balances and limits.authorityAddress: Optional address authorized to manage public keys.
Functionality:
Extracts the
dapp_idfrom the contract's code salt.Sets
_ownerto a fixed address (expected to be theDappRoot).Requires the deploying address to be
_owner, enforcing deployment restrictions.Initializes
_datawith the supplied credit config.Sets
_authorityAddressif provided.
Usage Example:
CreditConfig initialConfig = CreditConfig({ /* fields initialized */ }); optional(address) authority = someAuthorityAddress; DappConfig config = new DappConfig(dappId, initialConfig, authority);Requirements:
Deployment transaction sender must be the
DappRootcontract.
Methods
appendPubkey
function appendPubkey(uint256 pubkey) public senderIs(_authorityAddress.get()) accept
Purpose: Appends a new public key to the
_keysarray, allowing authorized entities to extend the list of recognized keys.Parameters:
pubkey: The public key to be appended.
Access Control: Only callable by the
_authorityAddress.Implementation Details:
Checks if the maximum size of
_keys(MAX_SIZE_OF_KEYS) is reached; if so, silently returns.Otherwise, appends the new
pubkey.
Usage Example:
config.appendPubkey(newPubkey);
setNewConfig
function setNewConfig(int128 value) public internalMsg senderIs(address(this)) functionID(5)
Purpose: Updates the credit configuration by deducting a specified amount from the available balance.
Parameters:
value: The amount to deduct (signed integer).
Access Control: Only callable internally by the contract itself (i.e., through an internal message).
Functionality: Decreases
_data.available_balancebyvalue.Usage Context: Expected to be invoked by the internal system or block producer to reflect token minting or spending.
setNewUnlimit
function setNewUnlimit(bool is_unlimit) public internalMsg senderIs(address(this))
Purpose: Sets or clears the "unlimited" flag in the credit configuration.
Parameters:
is_unlimit: Boolean flag indicating whether the credit is unlimited.
Access Control: Only callable internally by the contract itself.
Functionality: Updates
_data.is_unlimitaccordingly.
receive (Fallback function)
receive() external
Purpose: Handles incoming currency transfers to the contract.
Functionality:
Accepts the incoming message.
Adds the amount of
ECC_SHELLcurrency received to_data.available_balance.Checks the contract's balance against a minimum threshold (
MIN_BALANCE).If below minimum, triggers conversion of
ECC_SHELLtokens to increase the balance.Burns surplus
ECC_SHELLtokens beyond a defined threshold (BALANCE_ECC).
Implementation Details:
Uses constants
CURRENCIES_ID_SHELL,MIN_BALANCE,BALANCE_ECC.Performs conversions and burning via external calls to
gosh.cnvrtshellqandgosh.burnecc.
Usage: Automatically invoked when the contract receives funds.
getDetails
function getDetails() external view returns(uint256 dapp_id, CreditConfig data)
Purpose: Provides read-only access to the DApp configuration details.
Returns:
dapp_id: The unique DApp identifier.data: The current credit configuration structure.
Usage Example:
(uint256 id, CreditConfig configData) = config.getDetails();
Important Implementation Details
Access Control: Enforced via modifiers such as
senderIsandinternalMsg, ensuring that only authorized addresses or internal contract calls can modify sensitive state.CreditConfig: A custom data structure (not defined within this file) that encapsulates credit-related data including balances and flags. Refer to the relevant
CreditConfigtopic for its definition.Token Handling: The contract deals with
ECC_SHELLcurrency tokens, managing their balance, conversion, and burning through interactions with an externalgoshcontract.Owner Address: The
_owneris set to a hardcoded address, expected to represent theDappRootcontract. This ensures that only the root can deployDappConfiginstances.Public Key Management: Authorized keys for signature verification or authorization can be appended by an
_authorityAddress. The maximum number of keys is capped byMAX_SIZE_OF_KEYS.
Interactions with Other System Components
DappRoot Contract: Only the
DappRootcontract can deploy instances ofDappConfig, enforcing a hierarchical relationship.gosh Contract: The contract interacts with
goshfor operations such as converting and burningECC_SHELLtokens.Modifiers Library: Inherits from
Modifiers, which provides access control and other behavioral checks. SeeModifiersfor details.DappLib Library: Imports
DappLib.sol, although its functions are not explicitly invoked in the shown code. This may provide additional utilities related to DApp configuration or credit management.CreditConfig Structure: This data structure is crucial for storing credit parameters and is manipulated throughout the contract. Its definition and usage are documented in
CreditConfig.
Visual Diagram
classDiagram
class DappConfig {
-string version
-CreditConfig _data
-address _owner
-uint256 _dapp_id
-optional(address) _authorityAddress
-uint256[] _keys
+constructor(dapp_id, data, authorityAddress)
+appendPubkey()
+setNewConfig()
+setNewUnlimit()
+receive()
+getDetails()
}
DappConfig ..|> Modifiers
This class diagram highlights the main attributes and methods of the DappConfig contract, illustrating its role as a specialized configuration manager with secure update capabilities and token handling logic.