-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b6566a
commit a3fb927
Showing
7 changed files
with
126 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use alloy::{ | ||
network::TxSigner, | ||
primitives::{address, Address}, | ||
providers::ProviderBuilder, | ||
sol, | ||
transports::icp::IcpConfig, | ||
}; | ||
|
||
use crate::{create_icp_sepolia_signer, get_rpc_service_sepolia}; | ||
|
||
// Codegen from ABI file to interact with the contract. | ||
sol!( | ||
#[allow(missing_docs, clippy::too_many_arguments)] | ||
#[sol(rpc)] | ||
USDC, | ||
"abi/USDC.json" | ||
); | ||
|
||
/// Request the balance of an ETH account. | ||
#[ic_cdk::update] | ||
async fn get_balance_usdc(address: Option<String>) -> Result<String, String> { | ||
let address = match address { | ||
Some(val) => val, | ||
None => { | ||
let signer = create_icp_sepolia_signer().await; | ||
signer.address().to_string() | ||
} | ||
}; | ||
let address = address.parse::<Address>().map_err(|e| e.to_string())?; | ||
let rpc_service = get_rpc_service_sepolia(); | ||
let config = IcpConfig::new(rpc_service); | ||
let provider = ProviderBuilder::new().on_icp(config); | ||
|
||
let contract = USDC::new( | ||
address!("1c7d4b196cb0c7b01d743fbc6116a902379c7238"), | ||
provider, | ||
); | ||
|
||
let result = contract.balanceOf(address).call().await; | ||
match result { | ||
Ok(balance) => Ok(balance._0.to_string()), | ||
Err(e) => Err(e.to_string()), | ||
} | ||
} |
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
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,49 @@ | ||
import { Link, createLazyFileRoute } from "@tanstack/react-router"; | ||
|
||
import { backend } from "../../backend/declarations"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { useState } from "react"; | ||
import Source from "../components/source"; | ||
import Spinner from "../components/spinner"; | ||
|
||
export const Route = createLazyFileRoute("/get_balance_usdc")({ | ||
component: Page, | ||
}); | ||
|
||
function Page() { | ||
const [ethAddress, setEthAddress] = useState<string>(""); | ||
|
||
const { | ||
data: accountBalanceResult, | ||
isFetching: isFetchingAccountBalance, | ||
refetch: refetchAccountBalance, | ||
} = useQuery({ | ||
queryKey: ["get_balance_usdc", ethAddress], | ||
queryFn: () => backend.get_balance_usdc([ethAddress]), | ||
enabled: false, | ||
}); | ||
|
||
return ( | ||
<> | ||
<Link to="/"> | ||
<button> Menu</button> | ||
</Link> | ||
<div className="card"> | ||
<p>Request the USDC balance of an ETH account.</p> | ||
<input | ||
type="text" | ||
placeholder="ETH address" | ||
onChange={(e) => setEthAddress(e.target.value)} | ||
value={ethAddress} | ||
/> | ||
<button disabled={isFetchingAccountBalance} onClick={() => void refetchAccountBalance()}> | ||
{isFetchingAccountBalance ? <Spinner /> : "get_balance_usdc(ethAddress)"} | ||
</button> | ||
{accountBalanceResult && ( | ||
<pre>{JSON.stringify(accountBalanceResult, null, 2)}</pre> | ||
)} | ||
<Source file="get_balance_usdc.rs" /> | ||
</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