-
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.
chore: support fevm to native forward (#26)
- Loading branch information
1 parent
2faba16
commit 7996524
Showing
16 changed files
with
2,046 additions
and
151 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
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,56 @@ | ||
/* eslint-disable unicorn/no-null */ | ||
/* eslint-disable unicorn/no-useless-undefined */ | ||
import { clsx } from 'clsx' | ||
import { useAccount, useBalance, useConnect } from 'wagmi' | ||
import { filecoin, filecoinCalibration } from 'wagmi/chains' | ||
import { MetaMaskConnector } from 'wagmi/connectors/metaMask' | ||
import { useFilsnapContext } from '../hooks/filsnap.js' | ||
import ExplorerLink from './explorer-link.jsx' | ||
|
||
export default function ConnectFEVM() { | ||
const { account } = useFilsnapContext() | ||
const { address, isConnected, isConnecting } = useAccount() | ||
const { connect } = useConnect({ | ||
chainId: | ||
account?.config.network === 'mainnet' | ||
? filecoin.id | ||
: filecoinCalibration.id, | ||
connector: new MetaMaskConnector({ | ||
chains: [filecoinCalibration, filecoin], | ||
}), | ||
}) | ||
|
||
const { data } = useBalance({ | ||
chainId: | ||
account?.config.network === 'mainnet' | ||
? filecoin.id | ||
: filecoinCalibration.id, | ||
address, | ||
formatUnits: 'ether', | ||
}) | ||
|
||
let out = <button onClick={() => connect()}>Connect to Filecoin</button> | ||
if (isConnected) { | ||
out = ( | ||
<> | ||
<h3>FEVM Account</h3> | ||
<div> | ||
<b> | ||
{data?.formatted} {data?.symbol} | ||
</b> | ||
</div> | ||
<ExplorerLink address={address} chain="ethereum" /> | ||
<ExplorerLink address={address} chain="filecoin" /> | ||
</> | ||
) | ||
} | ||
|
||
if (isConnecting) { | ||
out = <div>Connecting...</div> | ||
} | ||
return ( | ||
<div class={clsx('Cell75', 'Box', !isConnected && 'u-AlignCenter')}> | ||
{out} | ||
</div> | ||
) | ||
} |
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,47 @@ | ||
import * as Address from 'iso-filecoin/address' | ||
import { useFilsnapContext } from '../hooks/filsnap.js' | ||
|
||
/** | ||
* | ||
* @param {object} param0 | ||
* @param {string} [param0.address] | ||
* @param {'filecoin' | 'ethereum'} [param0.chain] | ||
*/ | ||
export default function ExplorerLink({ address, chain }) { | ||
const { account } = useFilsnapContext() | ||
|
||
if (!address) return null | ||
|
||
if (chain === 'filecoin') { | ||
const fAddress = Address.from(address, account?.config.network).toString() | ||
return ( | ||
<div> | ||
⨎{' '} | ||
<a | ||
target="_blank" | ||
rel="noreferrer" | ||
href={`https://explorer.glif.io/address/${fAddress}/?network=${ | ||
account?.config.network === 'mainnet' ? 'mainnet' : 'calibrationnet' | ||
}`} | ||
> | ||
{fAddress || 'unknown'} | ||
</a> | ||
</div> | ||
) | ||
} | ||
|
||
return ( | ||
<div> | ||
Ξ{' '} | ||
<a | ||
target="_blank" | ||
rel="noreferrer" | ||
href={`https://explorer.glif.io/address/${address}/?network=${ | ||
account?.config.network === 'mainnet' ? 'mainnet' : 'calibrationnet' | ||
}`} | ||
> | ||
{address || 'unknown'} | ||
</a> | ||
</div> | ||
) | ||
} |
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,114 @@ | ||
/* eslint-disable @typescript-eslint/no-misused-promises */ | ||
/* eslint-disable no-nested-ternary */ | ||
/* eslint-disable @typescript-eslint/strict-boolean-expressions */ | ||
/* eslint-disable unicorn/no-useless-undefined */ | ||
import * as Address from 'iso-filecoin/address' | ||
import { Token } from 'iso-filecoin/token' | ||
import { useForm, type SubmitHandler } from 'react-hook-form' | ||
import { useContractWrite } from 'wagmi' | ||
import { abi } from '../fil-forwarder.js' | ||
import { useFilsnapContext } from '../hooks/filsnap.js' | ||
|
||
interface Inputs { | ||
recipient: string | ||
amount: string | ||
} | ||
|
||
/** | ||
* Send fil to an address | ||
*/ | ||
function Forward() { | ||
const { account } = useFilsnapContext() | ||
const { data, isLoading, isSuccess, error, write } = useContractWrite({ | ||
address: '0x2B3ef6906429b580b7b2080de5CA893BC282c225', | ||
abi, | ||
functionName: 'forward', | ||
value: 0n, | ||
}) | ||
// eslint-disable-next-line no-console | ||
console.log( | ||
'🚀 ~ file: connect-fevm.jsx:33 ~ ConnectFEVM ~ isLoading:', | ||
isLoading, | ||
isSuccess, | ||
error | ||
) | ||
|
||
const { | ||
register, | ||
handleSubmit, | ||
reset, | ||
formState: { errors }, | ||
} = useForm<Inputs>() | ||
|
||
const onSubmit: SubmitHandler<Inputs> = async (data) => { | ||
if (write != null) { | ||
const { recipient, amount } = data | ||
write({ | ||
value: Token.fromFIL(amount).toBigInt(), | ||
args: [Address.from(recipient).toContractDestination()], | ||
}) | ||
reset() | ||
} | ||
} | ||
|
||
return ( | ||
<div class="Box Cell100"> | ||
<h3>Forward ⨎ </h3> | ||
{error != null && <code data-testid="error">{error.message}</code>} | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<label for="recipient" id="recipient"> | ||
Recipient | ||
</label> | ||
<input | ||
style={{ width: '100%' }} | ||
disabled={isLoading} | ||
placeholder="f0, f1, f2, f3, f4" | ||
{...register('recipient', { required: true })} | ||
/> | ||
{errors.recipient != null && ( | ||
<div> | ||
<small>This field is required</small> | ||
</div> | ||
)} | ||
<label for="amount" id="amount"> | ||
Amount | ||
</label> | ||
<input | ||
disabled={isLoading} | ||
placeholder="FIL" | ||
{...register('amount', { required: true })} | ||
/> | ||
{errors.amount != null && ( | ||
<div> | ||
<small>This field is required</small> | ||
</div> | ||
)} | ||
|
||
<button type="submit" data-testid="send-tx" disabled={isLoading}> | ||
{isLoading ? 'Forwarding...' : 'Forward'} | ||
</button> | ||
</form> | ||
|
||
{isSuccess && data && ( | ||
<small> | ||
{' '} | ||
Tx:{' '} | ||
<a | ||
target="_blank" | ||
rel="noreferrer" | ||
title="View on Glif explorer" | ||
href={`https://explorer.glif.io/tx/${data?.hash}/?network=${ | ||
account?.config.network === 'mainnet' | ||
? 'mainnet' | ||
: 'calibrationnet' | ||
}`} | ||
> | ||
{data?.hash} | ||
</a> | ||
</small> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
export default Forward |
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.