Cover photo

Sky's Core Allocator System: Unravel the Mystery

A journey with Vaults, Buffers, Oracles and Capital Deployment through the Obex Prime Agent.

The Core Allocation System

The Core Allocation System, referenced as the Allocation System in the Sky's Atlas, is a layer composed of a set of smart contracts that is part of the Sky Protocol, and it is used to deploy capital into Prime Agents (aka Stars) .

The goal of the Core Allocation smart contracts is to act as the infrastructure that enables to generate USDS, and allocate USDS as collateral, based on their individual Deployment Layers.

In this article, we will move beyond abstraction by invoking the Obex Star as our chosen case study. However, the allocation system is similar for every Star; the only real difference is the ALMController on the Star side.

As we journey deeper into the allocation system, we’ll descend into the spell-book itself, exploring how these Obex contracts work together:

  • Allocator Vault;

  • Allocator Buffer;

  • Allocator Oracle;

  • Dss Blow.

Key Concepts

Difference from crypto-native collateral

Capital loans to the Prime Agents via the Allocation system are issued in an under-collateralized fashion: they are not over-collateralized and they generate internal debt. Furthermore, the Allocation Layer enforces strict capital routing constraints, permitting capital to flow only along predefined and explicitly authorized smart contracts.

Star ALMController

The Star has deployed this set of controller smart contracts - which is not part of the Sky allocation system - as the entrypoint to control the star's capital operations:

  • MainnetController: it contains all logic necessary to interact with the Sky allocation system to mint and burn USDS, swap USDS to USDC in the PSM, as well as interact with mainnet external protocols and CCTP for bridging USDC;

  • ForeignController: contains all logic necessary to deposit, withdraw, and swap assets in L2 PSMs as well as interact with external protocols on L2s and CCTP for bridging USDC.

The Allocator Vault

post image

Code taken as specific example: Obex Allocator Vault

The Allocator Vault is a vault that is individual to each Allocator Star, and it is controlled by the Allocator’s SubDAO Proxy. The Allocator Vault allows operators to cast:

  • draw USDS to the Allocator Buffer;

  • wipe USDS from the Allocator Buffer.

The Allocator Vault uses different permissions: not just wards , the full admin power, very common throughout the whole Sky codebase; but also roles , a more fine-grained access control for managing permissions, a more structured control level which involves different types of users, liked to the Allocator Roles contract. The wards system is an all or nothing type of permission system. The roles is a more extended and refined system, where the level of granularity is way more complex and specific. In Allocator Vault the variable roles points to the address of the Allocator Roles contract, which allows:

  • global administration: via wards;

  • per-Ilk admin: via ilkAdmins;

  • user Roles: via userRoles, using bitmask of roles assigned to each user per ilk, where each bit represents a role (0–255);

  • action permissions: via actionsRoles, which defines the roles allowed to call specific function signatures (sig).

Other than the permission variables, there are also additional parameters, mostly contract addresses, that wire the Core Allocation System into the heart of the protocol: the Jug governs the cost of generated USDS; the Vat acts as the central ledger; the Buffer, that serves as the staging ground where USDS is drawn or wiped. USDS tokens enter and exit through its USDSJoin adapter, with the token itself providing the underlying asset, while the ilk variable ties everything together by defining the specific collateral configuration.

In the specific case of Obex the ilk is ALLOCATOR-OBEX-A, serialized as bytes32: 0x414c4c4f4341544f522d4f4245582d4100000000000000000000000000000000

The Powers of the Allocator Vault

draw()

De facto means: get a loan from Sky to the Allocation Buffer of the Prime Agent:

function draw(uint256 wad) external auth {
    uint256 rate = jug.drip(ilk);
    uint256 dart = _divup(wad * RAY, rate);
    require(dart <= uint256(type(int256).max), "AllocatorVault/overflow");
    vat.frob(ilk, address(this), address(0), address(this), 0, int256(dart));
    usdsJoin.exit(buffer, wad);
    emit Draw(msg.sender, wad);
}
  1. gets the current rate via the tax collector Jug method drip();

    uint256 rate = jug.drip(ilk);

    which performs:

    • the calculation of the new accumulated rate;

    • the fee collection, by increasing the DAI balance (by Art * rate) of the Vow and increasing the global debt.

    Why is this necessary? Because, before drawing new debt, the system must first synchronize all existing debt and rates to the current block.

  2. normalizes the debt amount to be drawn;

    uint256 dart = _divup(wad * RAY, rate);

    Note: here the rate calculation is performed with _divup, because it needs to be considered as excess, the protocol cannot afford losses.

  3. check that there is no overflow error;

    require(dart <= uint256(type(int256).max), "AllocatorVault/overflow");
  4. calls the Vat method core primitive frob(), to modify the urn collateral;

    vat.frob(ilk, address(this), address(0), address(this), 0, int256(dart));
    • positive dart means increasing internal debt for the Allocator Vault;

    • negative dart would mean decreasing internal debt for the Allocator Vault;

        function frob(bytes32 i, address u, address v, address w, int dink, int dart) external note {
            Urn memory urn = urns[i][u];
            Ilk memory ilk = ilks[i];
            // ilk has been initialised
            require(ilk.rate != 0, "Vat/ilk-not-init");
    
            urn.ink = add(urn.ink, dink);
            urn.art = add(urn.art, dart);
            ilk.Art = add(ilk.Art, dart);
    
            int dtab = mul(ilk.rate, dart);
            uint tab = mul(ilk.rate, urn.art);
            debt     = add(debt, dtab);
    
            // require checks...
            ...
    
            gem[i][v] = sub(gem[i][v], dink);
            dai[w]    = add(dai[w],    dtab);
    
            urns[i][u] = urn;
            ilks[i]    = ilk;

    the frob call results in:

    • increments the internal DAI debt of the Allocator Vault;

    • increments the global debt for that collateral, in this case the Obex ilk;

    • increasing internal DAI balance of the Allocator Vault;

  5. moves USDS out to the Allocator Buffer with usdsJoin.exit(), minting USDS:

    usdsJoin.exit(buffer, wad);

    which is:

    function exit(address usr, uint256 wad) external {
        vat.move(msg.sender, address(this), RAY * wad);
        usds.mint(usr, wad);
        emit Exit(msg.sender, usr, wad);
    }

wipe()

It means repay the loan to Sky.

function wipe(uint256 wad) external auth {
    usds.transferFrom(buffer, address(this), wad);
    usdsJoin.join(address(this), wad);
    uint256 rate = jug.drip(ilk);
    uint256 dart = wad * RAY / rate;
    require(dart <= uint256(type(int256).max), "AllocatorVault/overflow");
    vat.frob(ilk, address(this), address(0), address(this), 0, -int256(dart));
    emit Wipe(msg.sender, wad);
}
  1. transfers USDS ERC-20 tokens from the Allocator Buffer to the Allocator Vault;

  2. burns the USDS ERC-20 tokens and converts them to internal DAI balance;

  3. calculates the current rate using Jug.drip();

  4. normalize the debt;

  5. checks that there are no overflows;

  6. repays the debt calling Vat.frob() with negative dart, decreasing internal DAI debt and internal DAI balance.

The Allocator Buffer

post image

Code taken as specific example: OBEX Allocator Buffer

The Allocator Buffer is a simple contract that plays a simple task: collects USDS as ERC-20 tokens drawn from the Allocator Vault. The Allocator Buffer has permissions controlling who can transfer assets out of it, using operator roles. It also has wards permissions that can be managed with the rely() and deny(). And it can approve spenders with approve().

The Power of the Allocator Buffer

approve()

The admin can approve another address to move the Allocator Buffer capital.

function approve(address asset, address spender, uint256 amount) external auth {
    GemLike(asset).approve(spender, amount);
    emit Approve(asset, spender, amount);
}

The Allocator Oracle

post image

Code used as reference: PIP_ALLOCATOR

Also referred to as the PIP_ALLOCATOR, the Allocator Oracle is also a simple smart contract that returns the WAD value price constant. It is important to note that this oracle is not specific to any other Star.

Here is the code that returns the WAD:

uint256 internal constant WAD = 10**18; // For 1:1 price

The Powers of the Allocator Oracle

peek()

Returns PRICE constant and the bool ok value.

function peek() public pure returns (bytes32 val, bool ok) {
    val = bytes32(WAD);
    ok  = true;
}

read()

Returns the price constant in bytes32.

function read() external pure returns (bytes32 val) {
    val = bytes32(WAD);
}

DSS Blow

post image

Code used as reference: MCD_BLOW2

DssBlow serves the purpose of accounting settlement that is outside of the regular flow of a vault.

Historically, the first iteration of this contract was used by teams to return unutilized DAI budget to the protocol.

DssBlow2 is pretty much the same idea, but it accepts USDS as well.

This can somewhat be abused as a mechanism to pay interest to the protocol when on-chain stability fees are set to zero.

The idea is to facilitate accounting of off-chain interest rates paid, without having to keep track of historical values for the debt in the vault.

The Powers of DSS Blow

blow()

/// @notice Transfers any available Dai and USDS balances from this contract to the protocol's Surplus Buffer.
/// @dev For each token, if the balance is greater than zero, the respective join adapter's join function is called.
function blow() public {
    uint256 daiBalance = dai.balanceOf(address(this));
    if (daiBalance > 0) {
        daiJoin.join(vow, daiBalance);
        emit Blow(address(dai), daiBalance);
    }

    uint256 usdsBalance = usds.balanceOf(address(this));
    if (usdsBalance > 0) {
        usdsJoin.join(vow, usdsBalance);
        emit Blow(address(usds), usdsBalance);
    }
}

Who is Dewiz?

post image

We are builders! Dewiz co-founders are former MakerDAO Core Unit contributors, who have been actively contributing with the Maker Protocol since 2021, and have been engaged in the broader Ethereum ecosystem since 2018.

We enable DeFi innovators to build on top of the main DeFi protocols, providing technical expertise on both on-chain and off-chain integrations that can save thousands of hours in engineering resources and put your project on the fast-track of the new financial world.

Our team has expertise in smart contract engineering for EVM chains, front-end engineering, integrations, supporting services and agile project management.

Article written by @riccardopersiani.