This repository has been archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Add channelId argument to AssetTransferred event #595
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
16ef94d
Add origin argument to AssetTransferred event
snario 7f7e51b
Rename variable
snario 7fef250
Handle AssetTransferred scenario
snario cb0528e
Simplify code a bit
snario d48b486
Renem ETHAssetHolderWatcher
snario 9f9a65e
Rename origin to channelId
snario b62f3f5
Merge branch 'master' into liam-add-origin
snario File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// The last value in a result from an ethers event emission (i.e., Contract.on(<filter>, <result>)) | ||
// is an object with keys as the names of the fields emitted by the event. | ||
export function parseEventResult(result: any[]): {[fieldName: string]: any} { | ||
return result.slice(-1)[0].args; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -56,9 +56,12 @@ export interface DisplayMessageSent { | |||||
|
||||||
export interface AssetTransferredEvent { | ||||||
type: "WALLET.ASSET_HOLDER.ASSET_TRANSFERRED"; | ||||||
assetHolderAddress: string; | ||||||
channelId: string; | ||||||
|
||||||
// This is either a `channelId` or an external destination (both bytes32). | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
destination: string; | ||||||
|
||||||
amount: BigNumber; | ||||||
} | ||||||
|
||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,34 @@ | ||
import {AssetHoldersState, recordDeposit} from "./state"; | ||
import * as actions from "../actions"; | ||
import {AssetHolderEventAction, DepositedEvent, AssetTransferredEvent} from "../actions"; | ||
import {unreachable} from "../../utils/reducer-utils"; | ||
|
||
import {AssetHoldersState, recordDeposit, recordAssetTransfer} from "./state"; | ||
|
||
export const assetHolderStateReducer = ( | ||
state: AssetHoldersState, | ||
action: actions.AssetHolderEventAction | ||
action: AssetHolderEventAction | ||
): AssetHoldersState => { | ||
switch (action.type) { | ||
case "WALLET.ASSET_HOLDER.ASSET_TRANSFERRED": | ||
throw Error("cant handle this"); | ||
return assetTransferredReducer(state, action); | ||
case "WALLET.ASSET_HOLDER.DEPOSITED": | ||
return depositedReducer(state, action); | ||
default: | ||
return unreachable(action); | ||
} | ||
}; | ||
|
||
const depositedReducer = (state: AssetHoldersState, action: actions.DepositedEvent) => { | ||
const depositedReducer = (state: AssetHoldersState, action: DepositedEvent) => { | ||
return recordDeposit( | ||
state, | ||
action.assetHolderAddress, | ||
action.destination, | ||
action.destinationHoldings | ||
); | ||
}; | ||
|
||
const assetTransferredReducer = ( | ||
state: AssetHoldersState, | ||
{assetHolderAddress, channelId, destination, amount}: AssetTransferredEvent | ||
) => { | ||
return recordAssetTransfer(state, assetHolderAddress, channelId, destination, amount); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import {Zero} from "ethers/constants"; | ||
import {BigNumber} from "ethers/utils"; | ||
import {BigNumber, bigNumberify} from "ethers/utils"; | ||
import {Uint256} from "@statechannels/nitro-protocol/src/contract/types"; | ||
|
||
export interface AssetHoldersState { | ||
|
@@ -82,3 +82,29 @@ export function recordDeposit( | |
newAssetHolderChannelState | ||
); | ||
} | ||
|
||
export function recordAssetTransfer( | ||
assetHoldersState: AssetHoldersState, | ||
assetHolderAddress: string, | ||
channelId: string, | ||
// @ts-ignore | ||
destination: string, // TODO: Should we take this into account? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's possible in future that |
||
amount: BigNumber | ||
): AssetHoldersState { | ||
const assetHolderChannelState = getOrCreateAssetHolderChannelState( | ||
assetHoldersState, | ||
assetHolderAddress, | ||
channelId | ||
); | ||
const newAssetHolderChannelState = { | ||
...assetHolderChannelState, | ||
holdings: bigNumberify(assetHolderChannelState.holdings) | ||
.sub(amount) | ||
.toHexString() | ||
}; | ||
return setAssetHolderChannelState( | ||
assetHoldersState, | ||
assetHolderAddress, | ||
newAssetHolderChannelState | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My comments above are because the event is only emitted inside a
_isExternalDestination
block. Do you think we ought to also emit an event when the holdings are updated? This could be the same event or a new one (with some appropriate name(s)). As it is now, no events will be emitted during e.g. atransferAll
on a ledger channel that only allocates to other channels. The holdings mapping is public, however: but clients won't have a "trigger" to remind them to inspect it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should make a decision on this at standup; it's not clear to me that it is important information for the wallet to keep track of.