Skip to content

Commit

Permalink
Setup funding evm tx
Browse files Browse the repository at this point in the history
  • Loading branch information
chasefleming committed Feb 22, 2024
1 parent df81478 commit f3c818e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 21 deletions.
24 changes: 12 additions & 12 deletions components/FundAccountFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "lib/constants"
import {NETWORK_DISPLAY_NAME} from "lib/network"
import {Box, Link, Themed} from "theme-ui"
import {CustomInputComponent, CustomSelectComponent} from "./inputs"
import {CustomInputComponent} from "./inputs"

const FUSD_VAULT_DOCS_LINK = {
url: "https://docs.onflow.org/fusd/#how-do-i-get-an-fusd-enabled-wallet",
Expand Down Expand Up @@ -53,18 +53,18 @@ export default function FundAccountFields({
max={128}
sx={{fontFamily: "monospace"}}
/>
{/*<Box mb={3} mt={4}>*/}
{/* <Themed.h3 sx={{mb: 0}}>Token</Themed.h3>*/}
{/*</Box>*/}
{/*<Box mb={4}>*/}
{/* <Field*/}
{/* component={CustomSelectComponent}*/}
{/* name="token"*/}
{/* inputLabel="Token"*/}
{/* options={TOKEN_OPTIONS}*/}
{/* />*/}
{/*</Box>*/}
<Box mb={3} mt={4}>
<Themed.h3 sx={{mb: 0}}>Token</Themed.h3>
</Box>
<Box mb={4}>
<Field
component={CustomSelectComponent}
name="token"
inputLabel="Token"
options={TOKEN_OPTIONS}
/>
</Box>
<Box mb={3}>
<Captcha onVerify={setCaptchaToken} />
</Box>
<Box mb={3}>
Expand Down
7 changes: 1 addition & 6 deletions flow.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,14 @@
"test": {
"address": "179b6b1cb6755e31",
"key": "6d8452af4ffac3d09276ce58358b0fb7f959b48437bf368c1d4b9c02588cabda"
},
"testnet-account": {
"address": "${NEXT_PUBLIC_SIGNER_ADDRESS}",
"keys": "${SIGNER_PRIVATE_KEY}"
}
},
"deployments": {
"emulator": {
"emulator-account": [
"FungibleTokenMetadataViews",
"Burner",
"FUSD",
"ViewResolver"
"FUSD"
]
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const PUBLIC_KEY_FORMAT_ERROR =
"Public key must be a hexadecimal string with no spaces."
export const PUBLIC_KEY_MISSING_ERROR = "Public key is required."
export const ADDRESS_FORMAT_ERROR =
"Address must be a 16-character hexadecimal string."
"Address must be a 16-character or 42-character hexadecimal string."
export const ADDRESS_MISSING_ERROR = "Address is required."
export const CREATE_ACCOUNT_ERROR = "Account creation has failed"
export const FUND_ACCOUNT_ERROR = "Account funding has failed"
Expand All @@ -33,7 +33,7 @@ export const paths = {
fundAccount: "/fund-account",
}

export const ADDRESS_REGEXP = /^(0x)?[0-9a-fA-F]{16}$/
export const ADDRESS_REGEXP = /^(0x)?([0-9a-fA-F]{16}|[0-9a-fA-F]{40})$/

export const NETWORK_CODEWORDS = {
testnet: "0x6834ba37b3980209",
Expand Down
56 changes: 55 additions & 1 deletion lib/flow/fund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,46 @@ transaction(address: Address, amount: UFix64) {
}
`

const txFundAccountFlowEVM = `
import FungibleToken from ${publicConfig.contractFungibleToken}
import FlowToken from ${publicConfig.contractFlowToken}
import EVM from ${publicConfig.contractEVM}
/// Mints Flow and transfers it to the given EVM address via the signer's CadenceOwnedAccount.
///
transaction(to: EVM.EVMAddress, amount: UFix64, gasLimit: UInt64) {
let tokenAdmin: &FlowToken.Administrator
let coa: &EVM.BridgedAccount
prepare(signer: auth(Storage) &Account) {
self.tokenAdmin = signer.storage.borrow<&FlowToken.Administrator>(from: /storage/flowTokenAdmin)
?? panic("Signer is not the token admin")
if signer.storage.borrow<&EVM.BridgedAccount>(from: /storage/evm) == nil {
signer.storage.save(<-EVM.createBridgedAccount(), to: /storage/evm)
}
self.coa = signer.storage.borrow<&EVM.BridgedAccount>(from: /storage/evm)
?? panic("Could not borrow reference to the signer's COA!")
}
execute {
let minter <- self.tokenAdmin.createNewMinter(allowedAmount: amount)
let mintedVault <- minter.mintTokens(amount: amount)
destroy minter
self.coa.deposit(from: <-mintedVault)
self.coa.call(
to: to,
data: [],
gasLimit: gasLimit,
value: EVM.Balance(flow: amount),
)
}
}
`

const txFundAccountFUSD = `
import FUSD from ${publicConfig.contractFUSD}
import FungibleToken from ${publicConfig.contractFungibleToken}
Expand All @@ -57,7 +97,7 @@ transaction(address: Address, amount: UFix64) {
}
`

type TokenType = "FLOW" | "FUSD"
type TokenType = "FLOW" | "FLOWEVM" | "FUSD"
type Token = {
tx: string
amount: string
Expand All @@ -66,14 +106,28 @@ type Tokens = Record<TokenType, Token>

export const tokens: Tokens = {
FLOW: {tx: txFundAccountFLOW, amount: TOKEN_FUNDING_AMOUNTS[FLOW_TYPE]},
FLOWEVM: {tx: txFundAccountFlowEVM, amount: TOKEN_FUNDING_AMOUNTS[FLOW_TYPE]},
FUSD: {tx: txFundAccountFUSD, amount: TOKEN_FUNDING_AMOUNTS[FUSD_TYPE]},
}

function getAddressType(address: string = ''): 'FLOW' | 'FLOWEVM' {

Check failure on line 113 in lib/flow/fund.ts

View workflow job for this annotation

GitHub Actions / Static checks (20.x)

Type string trivially inferred from a string literal, remove type annotation

Check failure on line 113 in lib/flow/fund.ts

View workflow job for this annotation

GitHub Actions / Static checks (20.x)

Replace `''):·'FLOW'·|·'FLOWEVM'` with `""):·"FLOW"·|·"FLOWEVM"`
if (address.length > 16) {
return 'FLOWEVM'

Check failure on line 115 in lib/flow/fund.ts

View workflow job for this annotation

GitHub Actions / Static checks (20.x)

Replace `'FLOWEVM'` with `"FLOWEVM"`
} else {
return 'FLOW'

Check failure on line 117 in lib/flow/fund.ts

View workflow job for this annotation

GitHub Actions / Static checks (20.x)

Replace `'FLOW'` with `"FLOW"`
}
}

export async function fundAccount(
address: string,
token: TokenType,
authorization: fcl.Authorization
) {

Check failure on line 125 in lib/flow/fund.ts

View workflow job for this annotation

GitHub Actions / Static checks (20.x)

Delete `⏎`

const addressType = getAddressType(address)

console.log("Address Type", addressType)

Check warning on line 129 in lib/flow/fund.ts

View workflow job for this annotation

GitHub Actions / Static checks (20.x)

Unexpected console statement

const {tx, amount} = tokens[token]

await sendTransaction({
Expand Down

0 comments on commit f3c818e

Please sign in to comment.