Skip to content
This repository has been archived by the owner on Aug 6, 2021. It is now read-only.

Commit

Permalink
Push progress
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-es committed Sep 4, 2020
1 parent 43f084e commit 0ee85c9
Show file tree
Hide file tree
Showing 27 changed files with 1,527 additions and 448 deletions.
121 changes: 101 additions & 20 deletions packages/ap-contracts/contracts/Core/ANN/ANNActor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ contract ANNActor is BaseActor {
bytes32 assetId = keccak256(abi.encode(terms, block.timestamp));

// compute the initial state of the asset
State memory initialState = IANNEngine(engine).computeInitialState(terms);
ANNState memory initialState = IANNEngine(engine).computeInitialState(terms);

// register the asset in the AssetRegistry
IANNRegistry(address(assetRegistry)).registerAsset(
Expand All @@ -63,30 +63,30 @@ contract ANNActor is BaseActor {
emit InitializedAsset(assetId, ContractType.ANN, ownership.creatorObligor, ownership.counterpartyObligor);
}

function computeStateAndPayoffForEvent(bytes32 assetId, State memory state, bytes32 _event)
internal
view
override
returns (State memory, int256)
{
// ContractType contractType = ContractType(assetRegistry.getEnumValueForTermsAttribute(assetId, "contractType"));
// revert("ANNActor.computePayoffAndStateForEvent: UNSUPPORTED_CONTRACT_TYPE");

address engine = assetRegistry.getEngine(assetId);
ANNTerms memory terms = IANNRegistry(address(assetRegistry)).getTerms(assetId);
function computePayoffForEvent(bytes32 assetId, address engine, ANNTerms memory terms, ANNState memory state, bytes32 _event) internal view returns (int256) {
(EventType eventType, uint256 scheduleTime) = decodeEvent(_event);

int256 payoff = IANNEngine(engine).computePayoffForEvent(
terms,
state,
_event,
getExternalDataForPOF(
bytes32 externalData;
{
externalData = getExternalDataForPOF(
assetId,
eventType,
shiftCalcTime(scheduleTime, terms.businessDayConvention, terms.calendar, terms.maturityDate)
)
);
}

return IANNEngine(engine).computePayoffForEvent(
terms,
state,
_event,
externalData
);
state = IANNEngine(engine).computeStateForEvent(
}

function computeStateForEvent(bytes32 assetId, address engine, ANNTerms memory terms, ANNState memory state, bytes32 _event) internal view returns (ANNState memory) {
(EventType eventType, uint256 scheduleTime) = decodeEvent(_event);

return IANNEngine(engine).computeStateForEvent(
terms,
state,
_event,
Expand All @@ -96,7 +96,88 @@ contract ANNActor is BaseActor {
shiftCalcTime(scheduleTime, terms.businessDayConvention, terms.calendar, terms.maturityDate)
)
);
}

/**
* @notice Contract-type specific logic for processing an event required by the use of
* contract-type specific Terms and State.
*/
function settleEventAndUpdateState(bytes32 assetId, bytes32 _event)
internal
override
returns (bool, int256)
{
ANNTerms memory terms = IANNRegistry(address(assetRegistry)).getTerms(assetId);
ANNState memory state = IANNRegistry(address(assetRegistry)).getState(assetId);
address engine = assetRegistry.getEngine(assetId);

// get finalized state if asset is not performant
if (state.contractPerformance != ContractPerformance.PF) {
state = IANNRegistry(address(assetRegistry)).getFinalizedState(assetId);
}

(EventType eventType, uint256 scheduleTime) = decodeEvent(_event);

// get external data for the next event
// compute payoff and the next state by applying the event to the current state
int256 payoff = computePayoffForEvent(assetId, engine, terms, state, _event);
// IANNEngine(engine).computePayoffForEvent(
// terms,
// state,
// _event,
// getExternalDataForPOF(
// assetId,
// eventType,
// shiftCalcTime(scheduleTime, terms.businessDayConvention, terms.calendar, terms.maturityDate)
// )
// );
ANNState memory nextState = computeStateForEvent(assetId, engine, terms, state, _event);
// IANNEngine(engine).computeStateForEvent(
// terms,
// state,
// _event,
// getExternalDataForSTF(
// assetId,
// eventType,
// shiftCalcTime(scheduleTime, terms.businessDayConvention, terms.calendar, terms.maturityDate)
// )
// );

// try to settle payoff of event
bool settledPayoff = settlePayoffForEvent(assetId, _event, payoff);

if (settledPayoff == false) {
// if the obligation can't be fulfilled and the performance changed from performant to DL, DQ or DF,
// store the last performant state of the asset
// (if the obligation is later fulfilled before the asset reaches default,
// the last performant state is used to derive subsequent states of the asset)
if (state.contractPerformance == ContractPerformance.PF) {
IANNRegistry(address(assetRegistry)).setFinalizedState(assetId, state);
}

// store event as pending event for future settlement
assetRegistry.pushPendingEvent(assetId, _event);

// create CreditEvent
bytes32 ceEvent = encodeEvent(EventType.CE, scheduleTime);

// derive the actual state of the asset by applying the CreditEvent (updates performance of asset)
nextState = computeStateForEvent(assetId, engine, terms, state, ceEvent);
// IANNEngine(engine).computeStateForEvent(
// terms,
// state,
// ceEvent,
// getExternalDataForSTF(
// assetId,
// EventType.CE,
// shiftCalcTime(scheduleTime, terms.businessDayConvention, terms.calendar, terms.maturityDate)
// )
// );
}

// store the resulting state
IANNRegistry(address(assetRegistry)).setState(assetId, nextState);

return (state, payoff);
return (settledPayoff, payoff);
}
}
138 changes: 130 additions & 8 deletions packages/ap-contracts/contracts/Core/ANN/ANNEncoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ library ANNEncoder {
if (asset.packedTerms[attributeKey] == value) return;
asset.packedTerms[attributeKey] = value;
}

function storeInPackedState(Asset storage asset, bytes32 attributeKey, bytes32 value) private {
// skip if value did not change
if (asset.packedState[attributeKey] == value) return;
asset.packedState[attributeKey] = value;
}

/**
* @dev Tightly pack and store only non-zero overwritten terms (LifecycleTerms)
Expand Down Expand Up @@ -220,7 +226,7 @@ library ANNEncoder {
);
}

function decodeAndGetEnumValueForANNAttribute(Asset storage asset, bytes32 attributeKey)
function decodeAndGetEnumValueForANNTermsAttribute(Asset storage asset, bytes32 attributeKey)
external
view
returns (uint8)
Expand Down Expand Up @@ -248,7 +254,7 @@ library ANNEncoder {
}
}

function decodeAndGetAddressValueForForANNAttribute(Asset storage asset, bytes32 attributeKey)
function decodeAndGetAddressValueForANNTermsAttribute(Asset storage asset, bytes32 attributeKey)
external
view
returns (address)
Expand All @@ -262,31 +268,31 @@ library ANNEncoder {
}
}

function decodeAndGetBytes32ValueForForANNAttribute(Asset storage asset, bytes32 attributeKey)
function decodeAndGetBytes32ValueForANNTermsAttribute(Asset storage asset, bytes32 attributeKey)
external
view
returns (bytes32)
{
return asset.packedTerms[attributeKey];
}

function decodeAndGetUIntValueForForANNAttribute(Asset storage asset, bytes32 attributeKey)
function decodeAndGetUIntValueForANNTermsAttribute(Asset storage asset, bytes32 attributeKey)
external
view
returns (uint256)
{
return uint256(asset.packedTerms[attributeKey]);
}

function decodeAndGetIntValueForForANNAttribute(Asset storage asset, bytes32 attributeKey)
function decodeAndGetIntValueForANNTermsAttribute(Asset storage asset, bytes32 attributeKey)
external
view
returns (int256)
{
return int256(asset.packedTerms[attributeKey]);
}

function decodeAndGetPeriodValueForForANNAttribute(Asset storage asset, bytes32 attributeKey)
function decodeAndGetPeriodValueForANNTermsAttribute(Asset storage asset, bytes32 attributeKey)
external
view
returns (IP memory)
Expand All @@ -305,7 +311,7 @@ library ANNEncoder {
}
}

function decodeAndGetCycleValueForForANNAttribute(Asset storage asset, bytes32 attributeKey)
function decodeAndGetCycleValueForANNTermsAttribute(Asset storage asset, bytes32 attributeKey)
external
view
returns (IPS memory)
Expand All @@ -328,7 +334,7 @@ library ANNEncoder {
}
}

function decodeAndGetContractReferenceValueForANNAttribute(Asset storage /* asset */, bytes32 /* attributeKey */)
function decodeAndGetContractReferenceValueForANNTermsAttribute(Asset storage /* asset */, bytes32 /* attributeKey */)
external
pure
returns (ContractReference memory)
Expand All @@ -340,4 +346,120 @@ library ANNEncoder {
ContractReferenceRole(0)
);
}

/**
* @dev Tightly pack and store ANNState
*/
function encodeAndSetANNState(Asset storage asset, ANNState memory state) external {
storeInPackedState(asset, "contractPerformance", bytes32(uint256(uint8(state.contractPerformance))) << 248);
storeInPackedState(asset, "statusDate", bytes32(state.statusDate));
storeInPackedState(asset, "nonPerformingDate", bytes32(state.nonPerformingDate));
storeInPackedState(asset, "maturityDate", bytes32(state.maturityDate));
storeInPackedState(asset, "terminationDate", bytes32(state.terminationDate));
storeInPackedState(asset, "notionalPrincipal", bytes32(state.notionalPrincipal));
storeInPackedState(asset, "accruedInterest", bytes32(state.accruedInterest));
storeInPackedState(asset, "feeAccrued", bytes32(state.feeAccrued));
storeInPackedState(asset, "nominalInterestRate", bytes32(state.nominalInterestRate));
storeInPackedState(asset, "interestScalingMultiplier", bytes32(state.interestScalingMultiplier));
storeInPackedState(asset, "notionalScalingMultiplier", bytes32(state.notionalScalingMultiplier));
storeInPackedState(asset, "nextPrincipalRedemptionPayment", bytes32(state.nextPrincipalRedemptionPayment));
}

/**
* @dev Tightly pack and store finalized ANNState
*/
function encodeAndSetFinalizedANNState(Asset storage asset, ANNState memory state) external {
storeInPackedState(asset, "F_contractPerformance", bytes32(uint256(uint8(state.contractPerformance))) << 248);
storeInPackedState(asset, "F_statusDate", bytes32(state.statusDate));
storeInPackedState(asset, "F_nonPerformingDate", bytes32(state.nonPerformingDate));
storeInPackedState(asset, "F_maturityDate", bytes32(state.maturityDate));
storeInPackedState(asset, "F_terminationDate", bytes32(state.terminationDate));
storeInPackedState(asset, "F_notionalPrincipal", bytes32(state.notionalPrincipal));
storeInPackedState(asset, "F_accruedInterest", bytes32(state.accruedInterest));
storeInPackedState(asset, "F_feeAccrued", bytes32(state.feeAccrued));
storeInPackedState(asset, "F_nominalInterestRate", bytes32(state.nominalInterestRate));
storeInPackedState(asset, "F_interestScalingMultiplier", bytes32(state.interestScalingMultiplier));
storeInPackedState(asset, "F_notionalScalingMultiplier", bytes32(state.notionalScalingMultiplier));
storeInPackedState(asset, "F_nextPrincipalRedemptionPayment", bytes32(state.nextPrincipalRedemptionPayment));
}

/**
* @dev Decode and load the ANNState of the asset
*/
function decodeAndGetANNState(Asset storage asset)
external
view
returns (ANNState memory)
{
return ANNState(
ContractPerformance(uint8(uint256(asset.packedState["contractPerformance"] >> 248))),
uint256(asset.packedState["statusDate"]),
uint256(asset.packedState["nonPerformingDate"]),
uint256(asset.packedState["maturityDate"]),
uint256(asset.packedState["terminationDate"]),

int256(asset.packedState["notionalPrincipal"]),
int256(asset.packedState["accruedInterest"]),
int256(asset.packedState["feeAccrued"]),
int256(asset.packedState["nominalInterestRate"]),
int256(asset.packedState["interestScalingMultiplier"]),
int256(asset.packedState["notionalScalingMultiplier"]),
int256(asset.packedState["nextPrincipalRedemptionPayment"])
);
}

/**
* @dev Decode and load the finalized ANNState of the asset
*/
function decodeAndGetFinalizedANNState(Asset storage asset)
external
view
returns (ANNState memory)
{
return ANNState(
ContractPerformance(uint8(uint256(asset.packedState["F_contractPerformance"] >> 248))),
uint256(asset.packedState["F_statusDate"]),
uint256(asset.packedState["F_nonPerformingDate"]),
uint256(asset.packedState["F_maturityDate"]),
uint256(asset.packedState["F_terminationDate"]),

int256(asset.packedState["F_notionalPrincipal"]),
int256(asset.packedState["F_accruedInterest"]),
int256(asset.packedState["F_feeAccrued"]),
int256(asset.packedState["F_nominalInterestRate"]),
int256(asset.packedState["F_interestScalingMultiplier"]),
int256(asset.packedState["F_notionalScalingMultiplier"]),
int256(asset.packedState["F_nextPrincipalRedemptionPayment"])
);
}

function decodeAndGetEnumValueForANNStateAttribute(Asset storage asset, bytes32 attributeKey)
external
view
returns (uint8)
{
if (attributeKey == bytes32("contractPerformance")) {
return uint8(uint256(asset.packedState["contractPerformance"] >> 248));
} else if (attributeKey == bytes32("F_contractPerformance")) {
return uint8(uint256(asset.packedState["F_contractPerformance"] >> 248));
} else {
return uint8(0);
}
}

function decodeAndGetUIntValueForANNStateAttribute(Asset storage asset, bytes32 attributeKey)
external
view
returns (uint256)
{
return uint256(asset.packedState[attributeKey]);
}

function decodeAndGetIntValueForANNStateAttribute(Asset storage asset, bytes32 attributeKey)
external
view
returns (int256)
{
return int256(asset.packedState[attributeKey]);
}
}

0 comments on commit 0ee85c9

Please sign in to comment.