@pontem/aptos-wallet-adapter
React and Vue WalletProvider
supporting loads of aptos wallets.
Forked and featured with wallet provider based on Vue.js/Pinia.js from Hippo Wallet Adapter.
Supports:
Installation
with yarn
yarn add @pontem/aptos-wallet-adapter
with npm
npm install @pontem/aptos-wallet-adapter
(back to top)
Example for Vue
Dependencies
Vue app required 2 packages as dependency:
Because Vue and Pinia is optional dependency inside @pontem/aptos-wallet-adapter
{
"dependencies": {
"vue": "3.2.40",
"pinia": "2.0.22",
...
}
}
Use Vue Wallet Provider
import { createApp } from "vue";
import { createPinia } from "pinia";
import App from "./App.vue";
import { useWalletProviderStore } from "@pontem/aptos-wallet-adapter";
const app = createApp(App);
const pinia = createPinia();
app.use(pinia);
const _store = useWalletProviderStore(pinia);
app.mount("#app");
Init store inside Vue Component
You can use both composition and options API
<script lang="ts">
import { storeToRefs } from "pinia";
import { computed, ref, defineComponent } from "vue";
import {
AptosWalletAdapter,
MartianWalletAdapter,
PontemWalletAdapter,
useWalletProviderStore,
WalletName,
} from "@pontem/aptos-wallet-adapter";
const defaultWalletName = "Pontem" as WalletName<"Pontem">;
const handleError = (error) => {
}
export default defineComponent({
name: "App",
setup: function () {
const store = useWalletProviderStore();
const walletAdapters = [new PontemWalletAdapter(), new MartianWalletAdapter(), new AptosWalletAdapter()];
const {
select,
connect,
disconnect,
signAndSubmitTransaction,
signTransaction,
signMessage,
init,
} = store;
const {
wallets,
wallet,
connected,
connecting,
account,
network,
disconnecting,
autoConnect
} = storeToRefs(store);
init({
wallets: walletAdapters,
localStorageKey: "VueAdapterLocalStorage",
onError: handleError,
autoConnect: true,
});
</script>
(back to top)
Example for React
Here's an example of how we integrate the adapter into hippo's frontend:
Use React Provider
import React from 'react';
import {
WalletProvider,
PontemWalletAdapter,
HippoWalletAdapter,
AptosWalletAdapter,
HippoExtensionWalletAdapter,
MartianWalletAdapter,
FewchaWalletAdapter,
SpikaWalletAdapter,
RiseWalletAdapter,
FletchWalletAdapter
} from '@pontem/aptos-wallet-adapter';
const wallets = [
new PontemWalletAdapter(),
new HippoWalletAdapter(),
new MartianWalletAdapter(),
new AptosWalletAdapter(),
new FewchaWalletAdapter(),
new HippoExtensionWalletAdapter(),
new SpikaWalletAdapter(),
new RiseWalletAdapter(),
new FletchWalletAdapter()
];
const App: React.FC = () => {
return (
<WalletProvider
wallets={wallets}
autoConnect={true | false} /** allow auto wallet connection or not **/
onError={(error: Error) => {
console.log('Handle Error Message', error);
}}>
{/* your website */}
</WalletProvider>
);
};
export default App;
Web3 Hook
import { useWallet } from '@pontem/aptos-wallet-adapter';
const { connected, account, network, ...rest } = useWallet();
Connect & Disconnect
import { AptosWalletName, useWallet } from "@pontem/aptos-wallet-adapter";
...
const { connect, disconnect, connected, select } = useWallet();
useEffect(() => {
if (!autoConnect && currentWallet?.adapter) {
connect();
}
}, [autoConnect, currentWallet, connect]);
if (!connected) {
return (
<button
onClick={() => {
select(); // E.g. connecting to the Aptos official wallet (Breaking Change)
}}
>
Connect
</button>
);
} else {
return (
<button
onClick={() => {
disconnect();
}}
>
Disconnect
</button>
);
}
(back to top)
Hippo SDK
Hippo Wallet Client
import { HippoSwapClient, HippoWalletClient } from '@manahippo/hippo-sdk';
import { getParserRepo } from '@manahippo/hippo-sdk';
export const hippoWalletClient = async (account: ActiveAptosWallet) => {
if (!account) return undefined;
const { netConf } = readConfig();
const repo = getParserRepo();
const walletClient = await HippoWalletClient.createInTwoCalls(
netConf,
aptosClient,
repo,
account
);
return walletClient;
};
Hippo Swap Client
import { HippoSwapClient, HippoWalletClient } from '@manahippo/hippo-sdk';
import { getParserRepo } from '@manahippo/hippo-sdk/';
export const hippoSwapClient = async () => {
const { netConf } = readConfig();
const repo = getParserRepo();
const swapClient = await HippoSwapClient.createInOneCall(netConf, aptosClient, repo);
return swapClient;
};
(back to top)
Submit and sign transaction
Request faucet
const { signAndSubmitTransaction } = useWallet();
const payload = await hippoWallet?.makeFaucetMintToPayload(uiAmtUsed, symbol);
if (payload) {
const result = await signAndSubmitTransaction(payload);
if (result) {
message.success('Transaction Success');
await hippoWallet?.refreshStores();
}
}
Swap Token
const bestQuote = await hippoSwap.getBestQuoteBySymbols(fromSymbol, toSymbol, uiAmtIn, 3);
if (!bestQuote) {
throw new Error(`No route exists from ${fromSymbol} to ${toSymbol}`);
}
const payload = await bestQuote.bestRoute.makeSwapPayload(uiAmtIn, uiAmtOutMin);
const result = await signAndSubmitTransaction(payload);
if (result) {
message.success('Transaction Success');
setRefresh(true);
}
Deposit Transaction
const pool = hippoSwap.getDirectPoolsBySymbolsAndPoolType(lhsSymbol, rhsSymbol, poolType);
if (pool.length === 0) {
throw new Error('Desired pool does not exist');
}
const payload = await pool[0].makeAddLiquidityPayload(lhsUiAmt, rhsUiAmt);
const result = await signAndSubmitTransaction(payload);
if (result) {
message.success('Transaction Success');
setRefresh(true);
}
(back to top)