-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add filforwarder transaction insights
- Loading branch information
1 parent
64c5f57
commit abf7d5a
Showing
7 changed files
with
198 additions
and
139 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
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,21 +1,28 @@ | ||
import { | ||
Box, | ||
Heading, | ||
Row, | ||
type SnapComponent, | ||
Text, | ||
} from '@metamask/snaps-sdk/jsx' | ||
|
||
import { Box, Image, type SnapComponent, Text } from '@metamask/snaps-sdk/jsx' | ||
import iconError from '../svg/error.svg' | ||
type ErrorBoxProps = { | ||
error: string | ||
name: string | ||
message?: string | ||
} | ||
export const ErrorBox: SnapComponent<ErrorBoxProps> = ({ error }) => { | ||
export const ErrorBox: SnapComponent<ErrorBoxProps> = ({ name, message }) => { | ||
if (message != null) { | ||
return ( | ||
<Box> | ||
<Box direction="horizontal"> | ||
<Image src={iconError} /> | ||
<Text color="error">{name}</Text> | ||
</Box> | ||
<Text color="muted">{message}</Text> | ||
</Box> | ||
) | ||
} | ||
|
||
return ( | ||
<Box> | ||
<Heading>Error</Heading> | ||
<Row label="Message:" variant="critical"> | ||
<Text>{error}</Text> | ||
</Row> | ||
<Box direction="horizontal"> | ||
<Image src={iconError} /> | ||
<Text color="error">{name}</Text> | ||
</Box> | ||
</Box> | ||
) | ||
} |
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,53 @@ | ||
import { | ||
Bold, | ||
Box, | ||
Copyable, | ||
Divider, | ||
Heading, | ||
Icon, | ||
Link, | ||
type SnapComponent, | ||
Text, | ||
} from '@metamask/snaps-sdk/jsx' | ||
import type { SnapConfig } from '../types' | ||
import { formatFIL } from '../utils' | ||
|
||
type ErrorBoxProps = { | ||
config: SnapConfig | ||
address: string | ||
amount: string | ||
} | ||
export const Insights: SnapComponent<ErrorBoxProps> = ({ | ||
amount, | ||
config, | ||
address, | ||
}) => { | ||
return ( | ||
<Box> | ||
<Heading>FilForwarder Transaction</Heading> | ||
<Text> | ||
The FilForwarder smart contract enables FEVM users to send their FIL | ||
safely and securely to other addresses in the Filecoin ecosystem. Review | ||
the <Link href="https://github.com/FilOzone/FilForwarder">source</Link>{' '} | ||
and onchain{' '} | ||
<Link href="https://beryx.io/fil/mainnet/address/f410ffm7pnedefg2ybn5sbag6lsujhpbifqrfspbcqna"> | ||
deployment | ||
</Link>{' '} | ||
for more information. | ||
</Text> | ||
<Divider /> | ||
<Box direction="horizontal"> | ||
<Icon name="user" size="md" /> | ||
<Text>Recipient</Text> | ||
</Box> | ||
<Copyable value={address} /> | ||
<Box direction="horizontal"> | ||
<Text> | ||
<Bold>⨎</Bold> | ||
</Text> | ||
<Text>Amount</Text> | ||
</Box> | ||
<Text color="muted">{formatFIL(amount, config)}</Text> | ||
</Box> | ||
) | ||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
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,115 @@ | ||
import type { | ||
OnTransactionHandler, | ||
OnTransactionResponse, | ||
} from '@metamask/snaps-sdk' | ||
import * as Address from 'iso-filecoin/address' | ||
import { Token } from 'iso-filecoin/token' | ||
import { type Hex, fromHex } from 'viem' | ||
import { decodeFunctionData } from 'viem' | ||
import { ErrorBox } from './components/error' | ||
import { Insights } from './components/insights' | ||
import { filForwarderMetadata } from './filforwarder' | ||
import { State } from './state' | ||
|
||
/** | ||
* Chain matches | ||
* | ||
* @param chainId - The chain ID | ||
*/ | ||
function chainMatches(chainId: string): boolean { | ||
return Object.values(filForwarderMetadata.chainIds).includes(chainId) | ||
} | ||
|
||
/** | ||
* Contract address matches | ||
* | ||
* @param transactionTo - The transaction to address | ||
*/ | ||
function contractAddressMatches(transactionTo: string | undefined): boolean { | ||
return ( | ||
transactionTo?.toLowerCase() === | ||
filForwarderMetadata.contractAddress.toLowerCase() | ||
) | ||
} | ||
|
||
export const onTransaction: OnTransactionHandler = async ({ | ||
transaction, | ||
chainId, | ||
transactionOrigin, | ||
}): Promise<OnTransactionResponse | null> => { | ||
if (!transactionOrigin) { | ||
return { | ||
content: ( | ||
<ErrorBox | ||
name={'Internal error'} | ||
message={'Missing transaction origin'} | ||
/> | ||
), | ||
} | ||
} | ||
const state = new State(snap) | ||
const config = await state.get(transactionOrigin) | ||
if (!config) { | ||
return { | ||
content: <ErrorBox name={'Internal error'} message={'Missing config'} />, | ||
} | ||
} | ||
|
||
// Don't show any insights if the transaction is not a FIL transfer. | ||
if ( | ||
!chainMatches(chainId) || | ||
!contractAddressMatches(transaction.to as string | undefined) | ||
) { | ||
return null | ||
} | ||
|
||
try { | ||
if (!transaction.value) { | ||
return { | ||
content: ( | ||
<ErrorBox name={'Transfer amount is missing from the transaction.'} /> | ||
), | ||
} | ||
} | ||
|
||
const callData = decodeFunctionData({ | ||
abi: filForwarderMetadata.abi, | ||
data: transaction.data as Hex, | ||
}) | ||
|
||
if (callData.functionName !== 'forward') { | ||
return { | ||
content: <ErrorBox name={'Transaction tries to call wrong method.'} />, | ||
} | ||
} | ||
if (callData.args === undefined || callData.args.length !== 1) { | ||
return { | ||
content: <ErrorBox name={'Missing recipient in transaction.'} />, | ||
} | ||
} | ||
|
||
const isMainNet = chainId === filForwarderMetadata.chainIds.filecoinMainnet | ||
const transferAmount = Token.fromFIL( | ||
fromHex(transaction.value as Hex, 'bigint') | ||
) | ||
const recipient = Address.fromContractDestination( | ||
callData.args[0] as Hex, | ||
isMainNet ? 'mainnet' : 'testnet' | ||
) | ||
return { | ||
content: ( | ||
<Insights | ||
config={config} | ||
amount={transferAmount.toFIL().toString()} | ||
address={recipient.toString()} | ||
/> | ||
), | ||
} | ||
} catch (error) { | ||
const err = error as Error | ||
console.error(error) | ||
return { | ||
content: <ErrorBox name={err.name} message={err.message} />, | ||
} | ||
} | ||
} |