Skip to content

Commit

Permalink
Add address type check for arg
Browse files Browse the repository at this point in the history
  • Loading branch information
chasefleming committed Feb 26, 2024
1 parent 4c39579 commit ea58ea1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
10 changes: 9 additions & 1 deletion components/FundAccountSubmitted.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {useEffect, useState} from "react"
import {sendScript} from "../lib/flow/send"
import fcl from "@onflow/fcl"
import t from "@onflow/types"
import {getAddressType} from "../lib/common"

const styles: Record<string, ThemeUICSSObject> = {
resultsContainer: {
Expand Down Expand Up @@ -70,9 +71,16 @@ access(all) fun main(account: Address): UFix64 {
const fetchBalance = async (addr: string) => {
try {
setIsFetchingBalance(true)

const addressArgType =
publicConfig.network === "previewnet" &&
getAddressType(result.address) === "FLOW"
? t.Address
: t.String

const balance = await sendScript({
script: balanceScript,
args: [fcl.arg(addr, t.Address)],
args: [fcl.arg(addr, addressArgType)],
})
setBalance(balance)
} catch (error) {
Expand Down
8 changes: 8 additions & 0 deletions lib/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ export function verifyAPIKey(req: string, keys: string[]): boolean {
}
return false
}

export const getAddressType = function (address: string): "FLOW" | "FLOWEVM" {
if (address.length <= 18) {
return "FLOW"
} else {
return "FLOWEVM"
}
}
10 changes: 1 addition & 9 deletions lib/flow/fund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as t from "@onflow/types"
import {FLOW_TYPE} from "../constants"
import publicConfig, {TOKEN_FUNDING_AMOUNTS} from "../publicConfig"
import {sendTransaction} from "./send"
import {getAddressType} from "../common"

const txFundAccountFLOW = `
import FlowToken from ${publicConfig.contractFlowToken}
Expand Down Expand Up @@ -86,15 +87,6 @@ export const tokens: Tokens = {
FLOW: {tx: txFundAccountFLOW, amount: TOKEN_FUNDING_AMOUNTS[FLOW_TYPE]},
FLOWEVM: {tx: txFundAccountFlowEVM, amount: TOKEN_FUNDING_AMOUNTS[FLOW_TYPE]},
}

function getAddressType(address: string): "FLOW" | "FLOWEVM" {
if (address.length <= 18) {
return "FLOW"
} else {
return "FLOWEVM"
}
}

export async function fundAccount(
address: string,
token: TokenType,
Expand Down

0 comments on commit ea58ea1

Please sign in to comment.