Skip to the content.

The exchange pattern

Intent

Creation of a smart contract where 1 instance of the smart contract will allow for an arbitrary amount of trades, where each trade is an exchange between 2 entities.

Consequences

Structure

Participants

Implementation

const start = zcf => {
  const seats = /*some data structure*/;
  const exchangeOfferHandler = seat => {
    //look for matching seat in seats with matching algorithm
    //IF match found THEN trade ELSE add seat to seats
  };
  const publicFacet = Far('publicFacet', {
    makeInvitation: () => zcf.makeInvitation(exchangeOfferHandler, 'exchange')
  });
  return harden({ publicFacet });
};
harden(start);
export { start };

The smart contract stores a collection of seats in some kind of data structure. The smart contract returns a publicFacet with a method exchangeInvitation. The offer handler linked to this exchangeInvitation will look for a seat in the collection of seats that satisfies the proposal. This is done using some kind of matching algorithm. If a matching seat is found, then the trade is executed. If no such seat is found, then the seat is added to the collection of seats in the smart contract. This allows the seat to be included in future searches performed by the matching algorithm.

Known uses