-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add transaction insight for FilForwarder transfers (#48)
- Loading branch information
Showing
7 changed files
with
163 additions
and
19 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export { FilsnapAdapter } from './snap' | ||
export { filForwarderMetadata } from './filforwarder-metadata' | ||
|
||
export { filForwarderMetadata } from 'filsnap' | ||
export type { SnapConfig, AccountInfo } from 'filsnap' |
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 |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import type { | ||
OnTransactionHandler, | ||
OnTransactionResponse, | ||
} from '@metamask/snaps-types' | ||
import { heading, panel, text } from '@metamask/snaps-ui' | ||
import { fromHex, type Hex } from 'viem' | ||
import * as Address from 'iso-filecoin/address' | ||
import { Token } from 'iso-filecoin/token' | ||
import { filForwarderMetadata } from './filforwarder-metadata' | ||
import { decodeFunctionData } from 'viem' | ||
|
||
/** | ||
* | ||
* @param message | ||
*/ | ||
function invalidTransferMessage(message: string): OnTransactionResponse { | ||
return { | ||
content: panel([heading('Invalid FIL Transfer'), text(message)]), | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param chainId | ||
*/ | ||
function humanReadableNetwork(chainId: string): string { | ||
if (chainId === filForwarderMetadata.chainIds.filecoinMainnet) { | ||
return 'Filecoin Mainnet' | ||
} else if ( | ||
chainId === filForwarderMetadata.chainIds.filecoinCalibrationTestnet | ||
) { | ||
return 'Filecoin Calibration Testnet' | ||
} else { | ||
throw new Error(`Unknown chain ID: ${chainId}`) | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param chainId | ||
*/ | ||
function chainMatches(chainId: string): boolean { | ||
return Object.values(filForwarderMetadata.chainIds).includes(chainId) | ||
} | ||
|
||
/** | ||
* | ||
* @param transactionTo | ||
*/ | ||
function contractAddressMatches(transactionTo: string | undefined): boolean { | ||
return ( | ||
transactionTo?.toLowerCase() === | ||
filForwarderMetadata.contractAddress.toLowerCase() | ||
) | ||
} | ||
|
||
// Note: currently MetaMask Flask shows the transaction insight tab by default even if we don't display any information | ||
// in it. This is a bug that the MetaMask team is addressing in this PR: | ||
// https://github.com/MetaMask/metamask-extension/pull/20267 | ||
export const onTransaction: OnTransactionHandler = async ({ | ||
transaction, | ||
chainId, | ||
}) => { | ||
if ( | ||
!chainMatches(chainId) || | ||
!contractAddressMatches(transaction.to as string | undefined) | ||
) { | ||
// Don't show any insights if the transaction is not a FIL transfer. | ||
return { | ||
content: null, | ||
} | ||
} | ||
|
||
let transferAmount | ||
try { | ||
transferAmount = new Token(fromHex(transaction.value as Hex, 'bigint')) | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error(error) | ||
return invalidTransferMessage( | ||
`Transfer amount is missing from the transaction.` | ||
) | ||
} | ||
|
||
let recipient | ||
try { | ||
const callData = decodeFunctionData({ | ||
abi: filForwarderMetadata.abi, | ||
data: transaction.data as Hex, | ||
}) | ||
if (callData.functionName !== 'forward') { | ||
return invalidTransferMessage(`Transaction tries to call wrong method.`) | ||
} | ||
if (callData.args === undefined || callData.args.length !== 1) { | ||
return invalidTransferMessage(`Missing recipient in transaction.`) | ||
} | ||
|
||
const isMainNet = chainId === filForwarderMetadata.chainIds.filecoinMainnet | ||
recipient = Address.fromContractDestination( | ||
callData.args[0] as Hex, | ||
isMainNet ? 'mainnet' : 'testnet' | ||
) | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error(error) | ||
return invalidTransferMessage(`Transaction recipient is invalid.`) | ||
} | ||
|
||
return { | ||
content: panel([ | ||
heading('Transfer FIL'), | ||
text( | ||
`You are transferring ${transferAmount | ||
.toFIL() | ||
.toString()} FIL to ${recipient.toString()} on ${humanReadableNetwork( | ||
chainId | ||
)}` | ||
), | ||
]), | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.