-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Bumped
@aptos-connect/wallet-adapter-plugin
to "^2.2.1" (#442)
* [core] Bumped `@aptos-connect/wallet-adapter-plugin` to "^2.2.1" * [example-dapp] Added flow for testing claims * [workspace] Generated changeset
- Loading branch information
1 parent
b6959cc
commit 66ad437
Showing
9 changed files
with
139 additions
and
157 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@aptos-labs/wallet-adapter-core": minor | ||
--- | ||
|
||
Bumped the Aptos Connect plugin to support claims |
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 |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
}, | ||
"aliases": { | ||
"components": "@/components", | ||
"hooks": "@/hooks", | ||
"utils": "@/lib/utils" | ||
} | ||
} |
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
14 changes: 14 additions & 0 deletions
14
apps/nextjs-example/src/components/ReactQueryClientProvider.tsx
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,14 @@ | ||
"use client" | ||
|
||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { PropsWithChildren } from 'react'; | ||
|
||
const queryClient = new QueryClient(); | ||
|
||
export function ReactQueryClientProvider({ children }: PropsWithChildren) { | ||
return ( | ||
<QueryClientProvider client={queryClient}> | ||
{children} | ||
</QueryClientProvider> | ||
); | ||
} |
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,64 @@ | ||
import { Account, AccountAddress, Aptos, AptosConfig, Ed25519PrivateKey, Network } from '@aptos-labs/ts-sdk'; | ||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; | ||
import { useSearchParams } from 'next/navigation'; | ||
import { useEffect, useMemo } from 'react'; | ||
|
||
const claimSecretKeyStorageKey = '@wallet-adapter-example-dapp/claimSecretKey'; | ||
|
||
const config = new AptosConfig({ network: Network.TESTNET }); | ||
const aptos = new Aptos(config); | ||
|
||
function getPersistedClaimSecretKey() { | ||
if (typeof window === 'undefined') { | ||
return undefined; | ||
} | ||
|
||
const currentValue = window.localStorage.getItem(claimSecretKeyStorageKey) ?? undefined; | ||
if (currentValue) { | ||
return currentValue; | ||
} | ||
const newValue = Ed25519PrivateKey.generate().toString(); | ||
window.localStorage.setItem(claimSecretKeyStorageKey, newValue); | ||
return newValue; | ||
} | ||
|
||
export function useClaimSecretKey() { | ||
const claimEnabled = typeof window !== 'undefined' && new URL(window.location.href).searchParams.get('claim') !== null; | ||
const claimSecretKey = claimEnabled ? getPersistedClaimSecretKey() : undefined; | ||
const claimAccountAddress = useMemo(() => { | ||
if (!claimSecretKey) { | ||
return undefined; | ||
} | ||
const secretKey = new Ed25519PrivateKey(claimSecretKey); | ||
const account = Account.fromPrivateKey({ privateKey: secretKey }); | ||
return account.accountAddress; | ||
}, [claimSecretKey]); | ||
|
||
const { data: claimableBalance } = useQuery({ | ||
queryKey: ['accounts', claimAccountAddress, 'aptBalance'], | ||
queryFn: async () => aptos.getAccountCoinAmount({ | ||
accountAddress: claimAccountAddress!, | ||
coinType: '0x1::aptos_coin::AptosCoin', | ||
}), | ||
enabled: claimAccountAddress !== undefined, | ||
}); | ||
|
||
const { isPending: isFunding, mutate: fundAccount, isSuccess: isFunded } = useMutation({ | ||
mutationFn: async (accountAddress: AccountAddress) => aptos.fundAccount({ | ||
accountAddress, | ||
amount: 1e8 - (claimableBalance ?? 0), | ||
}), | ||
}); | ||
|
||
useEffect(() => { | ||
if (claimAccountAddress === undefined || claimableBalance === undefined) { | ||
return; | ||
} | ||
|
||
if (claimableBalance < 1e4 && !isFunded && !isFunding) { | ||
fundAccount(claimAccountAddress); | ||
} | ||
}, [claimAccountAddress, claimSecretKey, claimableBalance, fundAccount, isFunded, isFunding]); | ||
|
||
return claimSecretKey; | ||
} |
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
Oops, something went wrong.