Skip to content

Ownership transfer

Example

An example that is meant to illustrate how protocol ownership may be transferred in exchange for ownership of another protocol.

Specifically, this example involves a Deliverable that is exchanged for Money. The transfer is accomplished via a Swap protocol. When this protocol comes into effect, it permits either deliverableOwner or moneyOwner to perform the swap() under the agreed upon conditions. For each of the underlying protocols the transfer permission requires that both the original owner as well as the external party newOwner invoke. This orchestration is the job of the Swap protocol.

Source

protocol[owner] Deliverable() {
    permission[owner & *newOwner] transfer() {
        this.owner = newOwner;
    };
};

protocol[owner] Money() {
    permission[owner & *newOwner] transfer() {
        this.owner = newOwner;
    };
};

protocol[deliverableOwner, moneyOwner] Swap(var deliverable: Deliverable, var money: Money) {
    initial state unswapped;
    final state swapped;

    permission[deliverableOwner | moneyOwner] swap() | unswapped {
        deliverable.transfer[deliverableOwner, moneyOwner]();
        money.transfer[moneyOwner, deliverableOwner]();

        become swapped;
    };
};

Tests

const OWNER_DELIVERABLE = 'd';
const OWNER_MONEY = 'm';

@test
function testSwap(test: Test) -> {
    // Two assets
    var deliverable = Deliverable[OWNER_DELIVERABLE]();
    var money = Money[OWNER_MONEY]();

    // An agreed-upon swap
    var swap = Swap[OWNER_DELIVERABLE, OWNER_MONEY](deliverable, money);

    // Attempt a swap
    swap.swap[OWNER_DELIVERABLE]();
};