MultiAssetHolder.sol

View Source: contracts/MultiAssetHolder.sol

โ†— Extends: IMultiAssetHolder, StatusManager โ†˜ Derived Contracts: NitroAdjudicator

An implementation of the IMultiAssetHolder interface. The AssetHolder contract escrows ETH or tokens against state channels. It allows assets to be internally accounted for, and ultimately prepared for transfer from one channel to other channels and/or external destinations, as well as for guarantees to be claimed.


Contract Members

Constants & Variables

mapping(address => mapping(bytes32 => uint256)) public holdings;

Functions


deposit

Deposit ETH or erc20 tokens against a given channelId.

function deposit(address asset, bytes32 channelId, uint256 expectedHeld, uint256 amount) external payable

Arguments

NameTypeDescription
assetaddresserc20 token address, or zero address to indicate ETH
channelIdbytes32ChannelId to be credited.
expectedHelduint256The number of wei/tokens the depositor believes are already escrowed against the channelId.
amountuint256The intended number of wei/tokens to be deposited.

transfer

Transfers as many funds escrowed against channelId as can be afforded for a specific destination. Assumes no repeated entries.

function transfer(uint256 assetIndex, bytes32 fromChannelId, bytes outcomeBytes, bytes32 stateHash, uint256[] indices) external nonpayable

Arguments

NameTypeDescription
assetIndexuint256Will be used to slice the outcome into a single asset outcome.
fromChannelIdbytes32Unique identifier for state channel to transfer funds from.
outcomeBytesbytesThe encoded Outcome of this state channel
stateHashbytes32The hash of the state stored when the channel finalized.
indicesuint256[]Array with each entry denoting the index of a destination to transfer funds to. An empty array indicates "all".

_apply_transfer_checks

function _apply_transfer_checks(uint256 assetIndex, uint256[] indices, bytes32 channelId, bytes32 stateHash, bytes outcomeBytes) internal view
returns(outcome struct ExitFormat.SingleAssetExit[], asset address, initialAssetHoldings uint256)

Arguments

NameTypeDescription
assetIndexuint256
indicesuint256[]
channelIdbytes32
stateHashbytes32
outcomeBytesbytes

compute_transfer_effects_and_interactions

function compute_transfer_effects_and_interactions(uint256 initialHoldings, struct ExitFormat.Allocation[] allocations, uint256[] indices) public pure
returns(newAllocations struct ExitFormat.Allocation[], allocatesOnlyZeros bool, exitAllocations struct ExitFormat.Allocation[], totalPayouts uint256)

Arguments

NameTypeDescription
initialHoldingsuint256
allocationsstruct ExitFormat.Allocation[]
indicesuint256[]

_apply_transfer_effects

function _apply_transfer_effects(uint256 assetIndex, address asset, bytes32 channelId, bytes32 stateHash, struct ExitFormat.SingleAssetExit[] outcome, struct ExitFormat.Allocation[] newAllocations, uint256 initialHoldings, uint256 totalPayouts) internal nonpayable

Arguments

NameTypeDescription
assetIndexuint256
assetaddress
channelIdbytes32
stateHashbytes32
outcomestruct ExitFormat.SingleAssetExit[]
newAllocationsstruct ExitFormat.Allocation[]
initialHoldingsuint256
totalPayoutsuint256

_apply_transfer_interactions

function _apply_transfer_interactions(struct ExitFormat.SingleAssetExit singleAssetExit, struct ExitFormat.Allocation[] exitAllocations) internal nonpayable

Arguments

NameTypeDescription
singleAssetExitstruct ExitFormat.SingleAssetExit
exitAllocationsstruct ExitFormat.Allocation[]

claim

Transfers as many funds escrowed against sourceChannelId as can be afforded for the destinations specified by targetAllocationIndicesToPayout in the beneficiaries of the target of the channel at indexOfTargetInSource.

function claim(struct IMultiAssetHolder.ClaimArgs claimArgs) external nonpayable

Arguments

NameTypeDescription
claimArgsstruct IMultiAssetHolder.ClaimArgsarguments used in the claim function. Used to avoid stack too deep error.

_apply_claim_checks

Checks that targetAllocationIndicesToPayout are increasing; that the source and target channels are finalized; that the supplied outcomes match the stored fingerprints; that the asset is identical in source and target. Computes and returns: the decoded outcomes, the asset being targetted; the number of assets held against the guarantor.

function _apply_claim_checks(struct IMultiAssetHolder.ClaimArgs claimArgs) internal view
returns(sourceOutcome struct ExitFormat.SingleAssetExit[], targetOutcome struct ExitFormat.SingleAssetExit[], asset address, initialAssetHoldings uint256)

Arguments

NameTypeDescription
claimArgsstruct IMultiAssetHolder.ClaimArgs

compute_claim_effects_and_interactions

Computes side effects for the claim function. First, computes the amount the source channel can afford for the target. Then, computes and returns updated allocations for the source and for the target, as well as exit allocations (to be paid out). It does this by walking the target allocations, testing against the guarantee in the source, and conditionally siphoning money out. See the Nitro paper.

function compute_claim_effects_and_interactions(uint256 initialHoldings, struct ExitFormat.Allocation[] sourceAllocations, struct ExitFormat.Allocation[] targetAllocations, uint256 indexOfTargetInSource, uint256[] targetAllocationIndicesToPayout) public pure
returns(newSourceAllocations struct ExitFormat.Allocation[], newTargetAllocations struct ExitFormat.Allocation[], exitAllocations struct ExitFormat.Allocation[], totalPayouts uint256)

Arguments

NameTypeDescription
initialHoldingsuint256
sourceAllocationsstruct ExitFormat.Allocation[]
targetAllocationsstruct ExitFormat.Allocation[]
indexOfTargetInSourceuint256
targetAllocationIndicesToPayoutuint256[]

_apply_claim_effects

Applies precomputed side effects for claim. Updates the holdings of the source channel. Updates the fingerprint of the outcome for the source and the target channel. Emits an event for each channel.

function _apply_claim_effects(struct IMultiAssetHolder.ClaimArgs claimArgs, address asset, struct ExitFormat.SingleAssetExit[] sourceOutcome, struct ExitFormat.Allocation[] newSourceAllocations, bytes32 targetChannelId, struct ExitFormat.SingleAssetExit[] targetOutcome, struct ExitFormat.Allocation[] newTargetAllocations, uint256 initialHoldings, uint256 totalPayouts) internal nonpayable

Arguments

NameTypeDescription
claimArgsstruct IMultiAssetHolder.ClaimArgs
assetaddress
sourceOutcomestruct ExitFormat.SingleAssetExit[]
newSourceAllocationsstruct ExitFormat.Allocation[]
targetChannelIdbytes32
targetOutcomestruct ExitFormat.SingleAssetExit[]
newTargetAllocationsstruct ExitFormat.Allocation[]
initialHoldingsuint256
totalPayoutsuint256

_apply_claim_interactions

Applies precomputed side effects for claim that interact with external contracts. "Executes" the supplied exit (pays out the money).

function _apply_claim_interactions(struct ExitFormat.SingleAssetExit singleAssetExit, struct ExitFormat.Allocation[] exitAllocations) internal nonpayable

Arguments

NameTypeDescription
singleAssetExitstruct ExitFormat.SingleAssetExit
exitAllocationsstruct ExitFormat.Allocation[]

_executeSingleAssetExit

Executes a single asset exit by paying out the asset and calling external contracts, as well as updating the holdings stored in this contract.

function _executeSingleAssetExit(struct ExitFormat.SingleAssetExit singleAssetExit) internal nonpayable

Arguments

NameTypeDescription
singleAssetExitstruct ExitFormat.SingleAssetExitThe single asset exit to be paid out.

_transferAsset

Transfers the given amount of this AssetHolders's asset type to a supplied ethereum address.

function _transferAsset(address asset, address destination, uint256 amount) internal nonpayable

Arguments

NameTypeDescription
assetaddress
destinationaddressethereum address to be credited.
amountuint256Quantity of assets to be transferred.

_isExternalDestination

Checks if a given destination is external (and can therefore have assets transferred to it) or not.

function _isExternalDestination(bytes32 destination) internal pure
returns(bool)

Returns

True if the destination is external, false otherwise.

Arguments

NameTypeDescription
destinationbytes32Destination to be checked.

_addressToBytes32

Converts an ethereum address to a nitro external destination.

function _addressToBytes32(address participant) internal pure
returns(bytes32)

Returns

The input address left-padded with zeros.

Arguments

NameTypeDescription
participantaddressThe address to be converted.

_bytes32ToAddress

Converts a nitro destination to an ethereum address.

function _bytes32ToAddress(bytes32 destination) internal pure
returns(address payable)

Returns

The rightmost 160 bits of the input string.

Arguments

NameTypeDescription
destinationbytes32The destination to be converted.

_requireMatchingFingerprint

Checks that a given variables hash to the data stored on chain.

function _requireMatchingFingerprint(bytes32 stateHash, bytes32 outcomeHash, bytes32 channelId) internal view

Arguments

NameTypeDescription
stateHashbytes32
outcomeHashbytes32
channelIdbytes32

_requireChannelFinalized

Checks that a given channel is in the Finalized mode.

function _requireChannelFinalized(bytes32 channelId) internal view

Arguments

NameTypeDescription
channelIdbytes32Unique identifier for a channel.

_updateFingerprint

function _updateFingerprint(bytes32 channelId, bytes32 stateHash, bytes32 outcomeHash) internal nonpayable

Arguments

NameTypeDescription
channelIdbytes32
stateHashbytes32
outcomeHashbytes32

_requireIncreasingIndices

Checks that the supplied indices are strictly increasing.

function _requireIncreasingIndices(uint256[] indices) internal pure

Arguments

NameTypeDescription
indicesuint256[]

min

function min(uint256 a, uint256 b) internal pure
returns(uint256)

Arguments

NameTypeDescription
auint256
buint256

decodeGuaranteeData

function decodeGuaranteeData(bytes data) internal pure
returns(bytes32[])

Arguments

NameTypeDescription
databytes