From 3f542315214dc6f8688e18fed69f596d12ac4bd9 Mon Sep 17 00:00:00 2001
From: Chase Fleming <1666730+chasefleming@users.noreply.github.com>
Date: Mon, 26 Feb 2024 14:18:02 -0800
Subject: [PATCH 01/20] Setup balance display
---
components/FundAccountSubmitted.tsx | 23 +++++++++++++++--------
1 file changed, 15 insertions(+), 8 deletions(-)
diff --git a/components/FundAccountSubmitted.tsx b/components/FundAccountSubmitted.tsx
index 318d51f..0a45480 100644
--- a/components/FundAccountSubmitted.tsx
+++ b/components/FundAccountSubmitted.tsx
@@ -60,14 +60,21 @@ export default function FundAccountSubmitted({
result.token
} tokens`}
-
- View Account
-
+ {publicConfig.network !== 'testnet' ? (
+
+ View Account
+
+ ) : (
+ <>
+
+
1000.00
+ >
+ )}
>
From 1c13adffc546c6f1efcabfddfee75b6b7ff3ef8e Mon Sep 17 00:00:00 2001
From: Chase Fleming <1666730+chasefleming@users.noreply.github.com>
Date: Mon, 26 Feb 2024 14:19:27 -0800
Subject: [PATCH 02/20] Fix prettier
---
components/FundAccountSubmitted.tsx | 26 +++++++++++++-------------
1 file changed, 13 insertions(+), 13 deletions(-)
diff --git a/components/FundAccountSubmitted.tsx b/components/FundAccountSubmitted.tsx
index 0a45480..31f1f5f 100644
--- a/components/FundAccountSubmitted.tsx
+++ b/components/FundAccountSubmitted.tsx
@@ -60,20 +60,20 @@ export default function FundAccountSubmitted({
result.token
} tokens`}
- {publicConfig.network !== 'testnet' ? (
-
- View Account
-
+ {publicConfig.network !== "testnet" ? (
+
+ View Account
+
) : (
- <>
-
- 1000.00
- >
+ <>
+
+ 1000.00
+ >
)}
From e52156122b5281474943954de56881004d68a7b3 Mon Sep 17 00:00:00 2001
From: Chase Fleming <1666730+chasefleming@users.noreply.github.com>
Date: Mon, 26 Feb 2024 14:22:20 -0800
Subject: [PATCH 03/20] Fix conditional
---
components/FundAccountSubmitted.tsx | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/components/FundAccountSubmitted.tsx b/components/FundAccountSubmitted.tsx
index 31f1f5f..d83b065 100644
--- a/components/FundAccountSubmitted.tsx
+++ b/components/FundAccountSubmitted.tsx
@@ -60,7 +60,7 @@ export default function FundAccountSubmitted({
result.token
} tokens`}
- {publicConfig.network !== "testnet" ? (
+ {publicConfig.network === "testnet" ? (
Date: Mon, 26 Feb 2024 14:55:23 -0800
Subject: [PATCH 04/20] Set balance
---
components/FundAccountSubmitted.tsx | 68 +++++++++++++++++++++++++++--
lib/flow/send.ts | 13 ++++++
2 files changed, 77 insertions(+), 4 deletions(-)
diff --git a/components/FundAccountSubmitted.tsx b/components/FundAccountSubmitted.tsx
index d83b065..de6ae32 100644
--- a/components/FundAccountSubmitted.tsx
+++ b/components/FundAccountSubmitted.tsx
@@ -4,6 +4,10 @@ import LoadingFeedback from "components/LoadingFeedback"
import {Box, Flex, Link, Themed, ThemeUICSSObject} from "theme-ui"
import {ClientFundAccountResult} from "./FundAccountPanel"
import publicConfig from "lib/publicConfig"
+import {useEffect, useState} from "react";
+import {sendScript, sendTransaction} from "../lib/flow/send";
+import fcl from "@onflow/fcl";
+import t from "@onflow/types";
const styles: Record = {
resultsContainer: {
@@ -29,6 +33,58 @@ export default function FundAccountSubmitted({
}: {
result?: ClientFundAccountResult
}) {
+ const [isFetchingBalance, setIsFetchingBalance] = useState(false)
+ const [balance, setBalance] = useState('')
+
+ const balanceScript = publicConfig.network === 'testnet' ?
+ `import EVM from ${publicConfig.contractEVM}
+
+ /// Returns the Flow balance of a given EVM address in FlowEVM
+ ///
+ access(all) fun main(address: String): UFix64 {
+ let bytes = address.decodeHex()
+ let addressBytes: [UInt8; 20] = [
+ bytes[0], bytes[1], bytes[2], bytes[3], bytes[4],
+ bytes[5], bytes[6], bytes[7], bytes[8], bytes[9],
+ bytes[10], bytes[11], bytes[12], bytes[13], bytes[14],
+ bytes[15], bytes[16], bytes[17], bytes[18], bytes[19]
+ ]
+ return EVM.EVMAddress(bytes: addressBytes).balance().inFLOW()
+ }`:
+ `import FungibleToken from ${publicConfig.contractFungibleToken}
+import FlowToken from ${publicConfig.contractFlowToken}
+
+access(all) fun main(account: Address): UFix64 {
+
+ let vaultRef = getAccount(account)
+ .capabilities.borrow<&{FungibleToken.Balance}>(/public/flowTokenBalance)
+ ?? panic("Could not borrow Balance reference to the Vault")
+
+ return vaultRef.balance
+}`
+
+ useEffect(() => {
+ if (typeof result === "undefined") return
+
+ const fetchBalance = async (addr: string) => {
+ try {
+ setIsFetchingBalance(true);
+ const balance = await sendScript({
+ script: balanceScript,
+ args: [fcl.arg(addr, t.Address)],
+ });
+ console.log("Balance:", balance);
+ setBalance(balance)
+ } catch (error) {
+ console.error("Failed to fetch balance:", error);
+ } finally {
+ setIsFetchingBalance(false);
+ }
+ };
+
+ fetchBalance(result.address)
+ }, [result])
+
return (
<>
@@ -70,10 +126,14 @@ export default function FundAccountSubmitted({
View Account
) : (
- <>
-
- 1000.00
- >
+ <>
+
+ {isFetchingBalance ? (
+ Fetching...
+ ) : (
+ {balance}
+ )}
+ >
)}
diff --git a/lib/flow/send.ts b/lib/flow/send.ts
index 1d73cff..1248350 100644
--- a/lib/flow/send.ts
+++ b/lib/flow/send.ts
@@ -24,3 +24,16 @@ export async function sendTransaction({
return fcl.tx(response).onceSealed()
}
+
+export async function sendScript({
+ script,
+ args,
+ }: {
+ script: string
+ args: fcl.TransactionArg[]
+}) {
+ return await fcl.send([
+ fcl.script(script),
+ fcl.args(args),
+ ])
+}
From c52b597dcb8f84863819502497d95a4b5cb2f8c2 Mon Sep 17 00:00:00 2001
From: Chase Fleming <1666730+chasefleming@users.noreply.github.com>
Date: Mon, 26 Feb 2024 14:57:51 -0800
Subject: [PATCH 05/20] prettier and misc fixes
---
components/FundAccountSubmitted.tsx | 48 ++++++++++++++---------------
lib/flow/send.ts | 11 +++----
2 files changed, 28 insertions(+), 31 deletions(-)
diff --git a/components/FundAccountSubmitted.tsx b/components/FundAccountSubmitted.tsx
index de6ae32..02cf78b 100644
--- a/components/FundAccountSubmitted.tsx
+++ b/components/FundAccountSubmitted.tsx
@@ -4,10 +4,10 @@ import LoadingFeedback from "components/LoadingFeedback"
import {Box, Flex, Link, Themed, ThemeUICSSObject} from "theme-ui"
import {ClientFundAccountResult} from "./FundAccountPanel"
import publicConfig from "lib/publicConfig"
-import {useEffect, useState} from "react";
-import {sendScript, sendTransaction} from "../lib/flow/send";
-import fcl from "@onflow/fcl";
-import t from "@onflow/types";
+import {useEffect, useState} from "react"
+import {sendScript} from "../lib/flow/send"
+import fcl from "@onflow/fcl"
+import t from "@onflow/types"
const styles: Record = {
resultsContainer: {
@@ -34,10 +34,11 @@ export default function FundAccountSubmitted({
result?: ClientFundAccountResult
}) {
const [isFetchingBalance, setIsFetchingBalance] = useState(false)
- const [balance, setBalance] = useState('')
+ const [balance, setBalance] = useState("")
- const balanceScript = publicConfig.network === 'testnet' ?
- `import EVM from ${publicConfig.contractEVM}
+ const balanceScript =
+ publicConfig.network === "testnet"
+ ? `import EVM from ${publicConfig.contractEVM}
/// Returns the Flow balance of a given EVM address in FlowEVM
///
@@ -50,8 +51,8 @@ export default function FundAccountSubmitted({
bytes[15], bytes[16], bytes[17], bytes[18], bytes[19]
]
return EVM.EVMAddress(bytes: addressBytes).balance().inFLOW()
- }`:
- `import FungibleToken from ${publicConfig.contractFungibleToken}
+ }`
+ : `import FungibleToken from ${publicConfig.contractFungibleToken}
import FlowToken from ${publicConfig.contractFlowToken}
access(all) fun main(account: Address): UFix64 {
@@ -68,22 +69,21 @@ access(all) fun main(account: Address): UFix64 {
const fetchBalance = async (addr: string) => {
try {
- setIsFetchingBalance(true);
+ setIsFetchingBalance(true)
const balance = await sendScript({
script: balanceScript,
args: [fcl.arg(addr, t.Address)],
- });
- console.log("Balance:", balance);
+ })
setBalance(balance)
} catch (error) {
- console.error("Failed to fetch balance:", error);
+ setBalance("error")
} finally {
- setIsFetchingBalance(false);
+ setIsFetchingBalance(false)
}
- };
+ }
fetchBalance(result.address)
- }, [result])
+ }, [result, balanceScript])
return (
<>
@@ -126,14 +126,14 @@ access(all) fun main(account: Address): UFix64 {
View Account
) : (
- <>
-
- {isFetchingBalance ? (
- Fetching...
- ) : (
- {balance}
- )}
- >
+ <>
+
+ {isFetchingBalance ? (
+ Fetching...
+ ) : (
+ {balance}
+ )}
+ >
)}
diff --git a/lib/flow/send.ts b/lib/flow/send.ts
index 1248350..d2ace1b 100644
--- a/lib/flow/send.ts
+++ b/lib/flow/send.ts
@@ -26,14 +26,11 @@ export async function sendTransaction({
}
export async function sendScript({
- script,
- args,
- }: {
+ script,
+ args,
+}: {
script: string
args: fcl.TransactionArg[]
}) {
- return await fcl.send([
- fcl.script(script),
- fcl.args(args),
- ])
+ return await fcl.send([fcl.script(script), fcl.args(args)])
}
From 2d42e2b5553691f1505e8f9665c9d524fe0b9db8 Mon Sep 17 00:00:00 2001
From: Chase Fleming <1666730+chasefleming@users.noreply.github.com>
Date: Mon, 26 Feb 2024 15:05:48 -0800
Subject: [PATCH 06/20] Move balance section
---
components/FundAccountSubmitted.tsx | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/components/FundAccountSubmitted.tsx b/components/FundAccountSubmitted.tsx
index 02cf78b..f00b00c 100644
--- a/components/FundAccountSubmitted.tsx
+++ b/components/FundAccountSubmitted.tsx
@@ -116,7 +116,7 @@ access(all) fun main(account: Address): UFix64 {
result.token
} tokens`}
- {publicConfig.network === "testnet" ? (
+ {publicConfig.network === "testnet" &&
View Account
- ) : (
+ }
+
+ {publicConfig.network === "previewnet" &&
<>
{isFetchingBalance ? (
- Fetching...
+ Fetching...
) : (
- {balance}
+ {balance}
)}
>
- )}
-
+ }
>
)}
From 732685b36f1a04a579b27e6395a289fabd9eb2a5 Mon Sep 17 00:00:00 2001
From: Chase Fleming <1666730+chasefleming@users.noreply.github.com>
Date: Mon, 26 Feb 2024 15:07:33 -0800
Subject: [PATCH 07/20] Run lint fix
---
components/FundAccountSubmitted.tsx | 24 ++++++++++++------------
1 file changed, 12 insertions(+), 12 deletions(-)
diff --git a/components/FundAccountSubmitted.tsx b/components/FundAccountSubmitted.tsx
index f00b00c..820acdf 100644
--- a/components/FundAccountSubmitted.tsx
+++ b/components/FundAccountSubmitted.tsx
@@ -116,7 +116,7 @@ access(all) fun main(account: Address): UFix64 {
result.token
} tokens`}
- {publicConfig.network === "testnet" &&
+ {publicConfig.network === "testnet" && (
View Account
- }
+ )}
- {publicConfig.network === "previewnet" &&
- <>
-
- {isFetchingBalance ? (
- Fetching...
- ) : (
- {balance}
- )}
- >
- }
+ {publicConfig.network === "previewnet" && (
+ <>
+
+ {isFetchingBalance ? (
+ Fetching...
+ ) : (
+ {balance}
+ )}
+ >
+ )}
>
)}
From c1d112c08dcd2fa2e868f4266e06cd39fa0707af Mon Sep 17 00:00:00 2001
From: Chase Fleming <1666730+chasefleming@users.noreply.github.com>
Date: Mon, 26 Feb 2024 15:11:07 -0800
Subject: [PATCH 08/20] Switch conditional
---
components/FundAccountSubmitted.tsx | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/components/FundAccountSubmitted.tsx b/components/FundAccountSubmitted.tsx
index 820acdf..1fa3b2d 100644
--- a/components/FundAccountSubmitted.tsx
+++ b/components/FundAccountSubmitted.tsx
@@ -37,7 +37,7 @@ export default function FundAccountSubmitted({
const [balance, setBalance] = useState("")
const balanceScript =
- publicConfig.network === "testnet"
+ publicConfig.network === "previewnet"
? `import EVM from ${publicConfig.contractEVM}
/// Returns the Flow balance of a given EVM address in FlowEVM
From 4c39579b201ff7652857e50daed6479169ec0a64 Mon Sep 17 00:00:00 2001
From: Chase Fleming <1666730+chasefleming@users.noreply.github.com>
Date: Mon, 26 Feb 2024 15:11:56 -0800
Subject: [PATCH 09/20] Change to label component
---
components/FundAccountSubmitted.tsx | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/components/FundAccountSubmitted.tsx b/components/FundAccountSubmitted.tsx
index 1fa3b2d..91a5269 100644
--- a/components/FundAccountSubmitted.tsx
+++ b/components/FundAccountSubmitted.tsx
@@ -129,7 +129,7 @@ access(all) fun main(account: Address): UFix64 {
{publicConfig.network === "previewnet" && (
<>
-
+
{isFetchingBalance ? (
Fetching...
) : (
From ea58ea196601193938b542befb50ba0624730c0f Mon Sep 17 00:00:00 2001
From: Chase Fleming <1666730+chasefleming@users.noreply.github.com>
Date: Mon, 26 Feb 2024 15:18:59 -0800
Subject: [PATCH 10/20] Add address type check for arg
---
components/FundAccountSubmitted.tsx | 10 +++++++++-
lib/common.ts | 8 ++++++++
lib/flow/fund.ts | 10 +---------
3 files changed, 18 insertions(+), 10 deletions(-)
diff --git a/components/FundAccountSubmitted.tsx b/components/FundAccountSubmitted.tsx
index 91a5269..c6d38b7 100644
--- a/components/FundAccountSubmitted.tsx
+++ b/components/FundAccountSubmitted.tsx
@@ -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 = {
resultsContainer: {
@@ -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) {
diff --git a/lib/common.ts b/lib/common.ts
index 2d605d9..8ff9d4e 100644
--- a/lib/common.ts
+++ b/lib/common.ts
@@ -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"
+ }
+}
diff --git a/lib/flow/fund.ts b/lib/flow/fund.ts
index 0a94214..d93d58c 100644
--- a/lib/flow/fund.ts
+++ b/lib/flow/fund.ts
@@ -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}
@@ -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,
From 89126087de89539d5f467d048e6866d39ad9c319 Mon Sep 17 00:00:00 2001
From: Chase Fleming <1666730+chasefleming@users.noreply.github.com>
Date: Mon, 26 Feb 2024 15:20:31 -0800
Subject: [PATCH 11/20] Switch address check logic
---
components/FundAccountSubmitted.tsx | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/components/FundAccountSubmitted.tsx b/components/FundAccountSubmitted.tsx
index c6d38b7..813412f 100644
--- a/components/FundAccountSubmitted.tsx
+++ b/components/FundAccountSubmitted.tsx
@@ -73,7 +73,7 @@ access(all) fun main(account: Address): UFix64 {
setIsFetchingBalance(true)
const addressArgType =
- publicConfig.network === "previewnet" &&
+ publicConfig.network === "testnet" ||
getAddressType(result.address) === "FLOW"
? t.Address
: t.String
From 1d3a80b519702e0cf3a326b573505cd2cc63c809 Mon Sep 17 00:00:00 2001
From: Chase Fleming <1666730+chasefleming@users.noreply.github.com>
Date: Mon, 26 Feb 2024 15:26:24 -0800
Subject: [PATCH 12/20] sansPrefix evm addr
---
components/FundAccountSubmitted.tsx | 19 ++++++++++++++++---
1 file changed, 16 insertions(+), 3 deletions(-)
diff --git a/components/FundAccountSubmitted.tsx b/components/FundAccountSubmitted.tsx
index 813412f..ee4b070 100644
--- a/components/FundAccountSubmitted.tsx
+++ b/components/FundAccountSubmitted.tsx
@@ -72,15 +72,28 @@ access(all) fun main(account: Address): UFix64 {
try {
setIsFetchingBalance(true)
+ const addressType = getAddressType(result.address)
+ let addressArg
+
const addressArgType =
- publicConfig.network === "testnet" ||
- getAddressType(result.address) === "FLOW"
+ publicConfig.network === "testnet" || addressType === "FLOW"
? t.Address
: t.String
+ if (addressType === "FLOWEVM") {
+ const withoutPrefix = fcl.sansPrefix(result.address)
+ if (!withoutPrefix) {
+ throw new Error("Invalid address")
+ }
+
+ addressArg = withoutPrefix
+ } else {
+ addressArg = addr
+ }
+
const balance = await sendScript({
script: balanceScript,
- args: [fcl.arg(addr, addressArgType)],
+ args: [fcl.arg(addressArg, addressArgType)],
})
setBalance(balance)
} catch (error) {
From 48b43451e8289be69df5713aaca5521ad148826b Mon Sep 17 00:00:00 2001
From: Chase Fleming <1666730+chasefleming@users.noreply.github.com>
Date: Mon, 26 Feb 2024 15:49:14 -0800
Subject: [PATCH 13/20] Change imports
---
components/FundAccountSubmitted.tsx | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/components/FundAccountSubmitted.tsx b/components/FundAccountSubmitted.tsx
index ee4b070..9cf2d9b 100644
--- a/components/FundAccountSubmitted.tsx
+++ b/components/FundAccountSubmitted.tsx
@@ -6,8 +6,8 @@ import {ClientFundAccountResult} from "./FundAccountPanel"
import publicConfig from "lib/publicConfig"
import {useEffect, useState} from "react"
import {sendScript} from "../lib/flow/send"
-import fcl from "@onflow/fcl"
-import t from "@onflow/types"
+import * as fcl from "@onflow/fcl"
+import * as t from "@onflow/types"
import {getAddressType} from "../lib/common"
const styles: Record = {
From 0874cbf12f0f04f5af06691a7f23f0a114d4ec4b Mon Sep 17 00:00:00 2001
From: Chase Fleming <1666730+chasefleming@users.noreply.github.com>
Date: Mon, 26 Feb 2024 15:58:54 -0800
Subject: [PATCH 14/20] Set error
---
components/FundAccountSubmitted.tsx | 16 +++++++++++++---
1 file changed, 13 insertions(+), 3 deletions(-)
diff --git a/components/FundAccountSubmitted.tsx b/components/FundAccountSubmitted.tsx
index 9cf2d9b..0059c80 100644
--- a/components/FundAccountSubmitted.tsx
+++ b/components/FundAccountSubmitted.tsx
@@ -36,6 +36,7 @@ export default function FundAccountSubmitted({
}) {
const [isFetchingBalance, setIsFetchingBalance] = useState(false)
const [balance, setBalance] = useState("")
+ const [error, setError] = useState("")
const balanceScript =
publicConfig.network === "previewnet"
@@ -97,7 +98,13 @@ access(all) fun main(account: Address): UFix64 {
})
setBalance(balance)
} catch (error) {
- setBalance("error")
+ setBalance("--")
+
+ if (error instanceof Error) {
+ setError(error.message)
+ } else {
+ setError("An unknown error occurred")
+ }
} finally {
setIsFetchingBalance(false)
}
@@ -109,7 +116,7 @@ access(all) fun main(account: Address): UFix64 {
return (
<>
- Funding account
+ Funding Account
Great! Your request has been submitted.
@@ -154,7 +161,10 @@ access(all) fun main(account: Address): UFix64 {
{isFetchingBalance ? (
Fetching...
) : (
- {balance}
+ <>
+ {balance}
+ {error && error.length > 0 && {error}
}
+ >
)}
>
)}
From c8874b6a9165a5954033b0876971b77711065fe0 Mon Sep 17 00:00:00 2001
From: Chase Fleming <1666730+chasefleming@users.noreply.github.com>
Date: Mon, 26 Feb 2024 16:00:15 -0800
Subject: [PATCH 15/20] Add check on balance script
---
components/FundAccountSubmitted.tsx | 56 ++++++++++++++---------------
1 file changed, 28 insertions(+), 28 deletions(-)
diff --git a/components/FundAccountSubmitted.tsx b/components/FundAccountSubmitted.tsx
index 0059c80..f30112d 100644
--- a/components/FundAccountSubmitted.tsx
+++ b/components/FundAccountSubmitted.tsx
@@ -38,34 +38,6 @@ export default function FundAccountSubmitted({
const [balance, setBalance] = useState("")
const [error, setError] = useState("")
- const balanceScript =
- publicConfig.network === "previewnet"
- ? `import EVM from ${publicConfig.contractEVM}
-
- /// Returns the Flow balance of a given EVM address in FlowEVM
- ///
- access(all) fun main(address: String): UFix64 {
- let bytes = address.decodeHex()
- let addressBytes: [UInt8; 20] = [
- bytes[0], bytes[1], bytes[2], bytes[3], bytes[4],
- bytes[5], bytes[6], bytes[7], bytes[8], bytes[9],
- bytes[10], bytes[11], bytes[12], bytes[13], bytes[14],
- bytes[15], bytes[16], bytes[17], bytes[18], bytes[19]
- ]
- return EVM.EVMAddress(bytes: addressBytes).balance().inFLOW()
- }`
- : `import FungibleToken from ${publicConfig.contractFungibleToken}
-import FlowToken from ${publicConfig.contractFlowToken}
-
-access(all) fun main(account: Address): UFix64 {
-
- let vaultRef = getAccount(account)
- .capabilities.borrow<&{FungibleToken.Balance}>(/public/flowTokenBalance)
- ?? panic("Could not borrow Balance reference to the Vault")
-
- return vaultRef.balance
-}`
-
useEffect(() => {
if (typeof result === "undefined") return
@@ -92,6 +64,34 @@ access(all) fun main(account: Address): UFix64 {
addressArg = addr
}
+ const balanceScript =
+ publicConfig.network === "previewnet" && addressType === "FLOWEVM"
+ ? `import EVM from ${publicConfig.contractEVM}
+
+ /// Returns the Flow balance of a given EVM address in FlowEVM
+ ///
+ access(all) fun main(address: String): UFix64 {
+ let bytes = address.decodeHex()
+ let addressBytes: [UInt8; 20] = [
+ bytes[0], bytes[1], bytes[2], bytes[3], bytes[4],
+ bytes[5], bytes[6], bytes[7], bytes[8], bytes[9],
+ bytes[10], bytes[11], bytes[12], bytes[13], bytes[14],
+ bytes[15], bytes[16], bytes[17], bytes[18], bytes[19]
+ ]
+ return EVM.EVMAddress(bytes: addressBytes).balance().inFLOW()
+ }`
+ : `import FungibleToken from ${publicConfig.contractFungibleToken}
+import FlowToken from ${publicConfig.contractFlowToken}
+
+access(all) fun main(account: Address): UFix64 {
+
+ let vaultRef = getAccount(account)
+ .capabilities.borrow<&{FungibleToken.Balance}>(/public/flowTokenBalance)
+ ?? panic("Could not borrow Balance reference to the Vault")
+
+ return vaultRef.balance
+}`
+
const balance = await sendScript({
script: balanceScript,
args: [fcl.arg(addressArg, addressArgType)],
From c41da49627df25cdfa5a72b91d38863f30407e2c Mon Sep 17 00:00:00 2001
From: Chase Fleming <1666730+chasefleming@users.noreply.github.com>
Date: Mon, 26 Feb 2024 16:00:40 -0800
Subject: [PATCH 16/20] run lint fix
---
components/FundAccountSubmitted.tsx | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/components/FundAccountSubmitted.tsx b/components/FundAccountSubmitted.tsx
index f30112d..5ca648f 100644
--- a/components/FundAccountSubmitted.tsx
+++ b/components/FundAccountSubmitted.tsx
@@ -65,8 +65,8 @@ export default function FundAccountSubmitted({
}
const balanceScript =
- publicConfig.network === "previewnet" && addressType === "FLOWEVM"
- ? `import EVM from ${publicConfig.contractEVM}
+ publicConfig.network === "previewnet" && addressType === "FLOWEVM"
+ ? `import EVM from ${publicConfig.contractEVM}
/// Returns the Flow balance of a given EVM address in FlowEVM
///
@@ -80,7 +80,7 @@ export default function FundAccountSubmitted({
]
return EVM.EVMAddress(bytes: addressBytes).balance().inFLOW()
}`
- : `import FungibleToken from ${publicConfig.contractFungibleToken}
+ : `import FungibleToken from ${publicConfig.contractFungibleToken}
import FlowToken from ${publicConfig.contractFlowToken}
access(all) fun main(account: Address): UFix64 {
@@ -111,7 +111,7 @@ access(all) fun main(account: Address): UFix64 {
}
fetchBalance(result.address)
- }, [result, balanceScript])
+ }, [result])
return (
<>
From 56772d96496d027f2f6ee22b503929e0801a3632 Mon Sep 17 00:00:00 2001
From: Chase Fleming <1666730+chasefleming@users.noreply.github.com>
Date: Mon, 26 Feb 2024 16:04:22 -0800
Subject: [PATCH 17/20] Change error name
---
components/FundAccountSubmitted.tsx | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/components/FundAccountSubmitted.tsx b/components/FundAccountSubmitted.tsx
index 5ca648f..ff631b8 100644
--- a/components/FundAccountSubmitted.tsx
+++ b/components/FundAccountSubmitted.tsx
@@ -36,7 +36,7 @@ export default function FundAccountSubmitted({
}) {
const [isFetchingBalance, setIsFetchingBalance] = useState(false)
const [balance, setBalance] = useState("")
- const [error, setError] = useState("")
+ const [balanceError, setBalanceError] = useState("")
useEffect(() => {
if (typeof result === "undefined") return
@@ -101,9 +101,9 @@ access(all) fun main(account: Address): UFix64 {
setBalance("--")
if (error instanceof Error) {
- setError(error.message)
+ setBalanceError(error.message)
} else {
- setError("An unknown error occurred")
+ setBalanceError("An unknown error occurred")
}
} finally {
setIsFetchingBalance(false)
@@ -163,7 +163,9 @@ access(all) fun main(account: Address): UFix64 {
) : (
<>
{balance}
- {error && error.length > 0 && {error}
}
+ {balanceError && balanceError.length > 0 && (
+ {balanceError}
+ )}
>
)}
>
From 7b6a1ea94ec750294fd5b3d5ad34a5123d2f6f04 Mon Sep 17 00:00:00 2001
From: Chase Fleming <1666730+chasefleming@users.noreply.github.com>
Date: Mon, 26 Feb 2024 16:06:40 -0800
Subject: [PATCH 18/20] change error
---
components/FundAccountSubmitted.tsx | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/components/FundAccountSubmitted.tsx b/components/FundAccountSubmitted.tsx
index ff631b8..1bb5c3a 100644
--- a/components/FundAccountSubmitted.tsx
+++ b/components/FundAccountSubmitted.tsx
@@ -101,9 +101,9 @@ access(all) fun main(account: Address): UFix64 {
setBalance("--")
if (error instanceof Error) {
- setBalanceError(error.message)
+ setBalanceError((error as Error).message)
} else {
- setBalanceError("An unknown error occurred")
+ setBalanceError("An error occurred")
}
} finally {
setIsFetchingBalance(false)
From 502a1664ac4b446e15dace6e3f8d703f63fedb49 Mon Sep 17 00:00:00 2001
From: Chase Fleming <1666730+chasefleming@users.noreply.github.com>
Date: Mon, 26 Feb 2024 16:09:15 -0800
Subject: [PATCH 19/20] update error message
---
components/FundAccountSubmitted.tsx | 7 +------
1 file changed, 1 insertion(+), 6 deletions(-)
diff --git a/components/FundAccountSubmitted.tsx b/components/FundAccountSubmitted.tsx
index 1bb5c3a..530311b 100644
--- a/components/FundAccountSubmitted.tsx
+++ b/components/FundAccountSubmitted.tsx
@@ -99,12 +99,7 @@ access(all) fun main(account: Address): UFix64 {
setBalance(balance)
} catch (error) {
setBalance("--")
-
- if (error instanceof Error) {
- setBalanceError((error as Error).message)
- } else {
- setBalanceError("An error occurred")
- }
+ setBalanceError("An error occurred")
} finally {
setIsFetchingBalance(false)
}
From 765af53624d233293b6f0f643561ae72b081522f Mon Sep 17 00:00:00 2001
From: Chase Fleming <1666730+chasefleming@users.noreply.github.com>
Date: Mon, 26 Feb 2024 16:23:38 -0800
Subject: [PATCH 20/20] decode response
---
components/FundAccountSubmitted.tsx | 4 ++--
lib/flow/send.ts | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/components/FundAccountSubmitted.tsx b/components/FundAccountSubmitted.tsx
index 530311b..6571dd5 100644
--- a/components/FundAccountSubmitted.tsx
+++ b/components/FundAccountSubmitted.tsx
@@ -92,11 +92,11 @@ access(all) fun main(account: Address): UFix64 {
return vaultRef.balance
}`
- const balance = await sendScript({
+ const res = await sendScript({
script: balanceScript,
args: [fcl.arg(addressArg, addressArgType)],
})
- setBalance(balance)
+ setBalance(res)
} catch (error) {
setBalance("--")
setBalanceError("An error occurred")
diff --git a/lib/flow/send.ts b/lib/flow/send.ts
index d2ace1b..25e40ae 100644
--- a/lib/flow/send.ts
+++ b/lib/flow/send.ts
@@ -32,5 +32,5 @@ export async function sendScript({
script: string
args: fcl.TransactionArg[]
}) {
- return await fcl.send([fcl.script(script), fcl.args(args)])
+ return fcl.send([fcl.script(script), fcl.args(args)]).then(fcl.decode)
}