Skip to content

Commit

Permalink
Merge pull request #8 from mybucks-online/feat/integrate-alchemy
Browse files Browse the repository at this point in the history
Integrate Alchemy APIs
  • Loading branch information
koko37 authored Dec 3, 2024
2 parents bdf26e2 + 804a718 commit 6fecaaf
Show file tree
Hide file tree
Showing 16 changed files with 874 additions and 618 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VITE_INFURA_API_KEY =
VITE_COVALENT_API_KEY =
VITE_ALCHEMY_API_KEY =
VITE_TRONGRID_API_KEY =
2 changes: 1 addition & 1 deletion .github/workflows/main-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ jobs:
- name: Build the project
env:
VITE_INFURA_API_KEY: ${{ secrets.VITE_INFURA_API_KEY }}
VITE_COVALENT_API_KEY: ${{ secrets.VITE_COVALENT_API_KEY }}
VITE_TRONGRID_API_KEY: ${{ secrets.VITE_TRONGRID_API_KEY }}
VITE_ALCHEMY_API_KEY: ${{ secrets.VITE_ALCHEMY_API_KEY }}
run: yarn build
- name: Setup Pages
uses: actions/configure-pages@v4
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ This is a fully open-source project built with React, Vite, and related third-pa
- **ethers**
A standard library for manipulating EVM accounts.

- **@covalenthq/client-sdk**
- **alchemy-sdk**
Provides query functions for massive crypto assets in multiple networks.

- **@sushiswap/default-token-list**
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@
"preview": "vite preview"
},
"dependencies": {
"@covalenthq/client-sdk": "^1.0.2",
"@sushiswap/default-token-list": "^43.3.0",
"alchemy-sdk": "^3.5.0",
"buffer": "^6.0.3",
"camelcase-keys": "^9.1.3",
"clipboard-copy": "^4.0.1",
"date-fns": "^4.1.0",
"ethers": "^6.13.0",
Expand Down
45 changes: 35 additions & 10 deletions src/contexts/Store.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ const StoreProvider = ({ children }) => {
!hash
? null
: network === NETWORK.EVM
? new EvmAccount(hash, chainId)
: new TronAccount(hash),
? new EvmAccount(hash, chainId)
: new TronAccount(hash),
[hash, network, chainId]
);

Expand All @@ -68,26 +68,33 @@ const StoreProvider = ({ children }) => {
const [inMenu, openMenu] = useState(false);
const [showBalances, setShowBalances] = useState(false);

// active token
const [selectedTokenAddress, selectToken] = useState("");

// balances related
const [nativeTokenName, setNativeTokenName] = useState("");
const [nativeTokenBalance, setNativeTokenBalance] = useState(0);
const [tokenBalances, setTokenBalances] = useState([]);
const [nftBalances, setNftBalances] = useState([]);

// transfers history
const [transfers, setTransfers] = useState([]);

// prices related
const [nativeTokenPrice, setNativeTokenPrice] = useState(0);

// active token
const [selectedTokenAddress, selectToken] = useState("");
const token = useMemo(
() => tokenBalances.find((t) => t.address === selectedTokenAddress),
[tokenBalances, selectedTokenAddress]
);

// unique counter that increments regularly
const [tick, setTick] = useState(0);

useEffect(() => {
if (!account) {
return;
}
setNativeTokenName("")
setNativeTokenName("");
setTokenBalances([]);
account.getNetworkStatus().then(() => {
setTick((_tick) => _tick + 1);
Expand All @@ -113,6 +120,21 @@ const StoreProvider = ({ children }) => {
};
}, [account]);

useEffect(() => {
if (!selectedTokenAddress) {
setTransfers([]);
return;
}
account
.queryTokenHistory(
token.native ? "" : selectedTokenAddress,
token.decimals
)
.then((result) => {
setTransfers(result);
});
}, [selectedTokenAddress]);

const reset = () => {
setPassword("");
setPasscode("");
Expand All @@ -129,6 +151,7 @@ const StoreProvider = ({ children }) => {
setNativeTokenBalance(0);
setTokenBalances([]);
setNftBalances([]);
setTransfers([]);

selectToken("");
};
Expand All @@ -149,10 +172,10 @@ const StoreProvider = ({ children }) => {
const result = await account.queryBalances();

if (result) {
setNativeTokenName(result[0]);
setNativeTokenBalance(result[1]);
setNativeTokenPrice(result[2]);
setTokenBalances(result[3]);
setNativeTokenName(result[0].name);
setNativeTokenBalance(result[0].balance);
setNativeTokenPrice(result[0].price);
setTokenBalances(result);

setConnectivity(true);
} else {
Expand Down Expand Up @@ -184,11 +207,13 @@ const StoreProvider = ({ children }) => {
nativeTokenBalance,
tokenBalances,
nftBalances,
transfers,
nativeTokenPrice,
tick,
fetchBalances,
selectedTokenAddress,
selectToken,
token,
}}
>
{children}
Expand Down
Loading

0 comments on commit 6fecaaf

Please sign in to comment.