Skip to content

Commit

Permalink
Merge pull request #1134 from layerswap/dev-bakofix
Browse files Browse the repository at this point in the history
Stopped bako requests
  • Loading branch information
babkenmes authored Nov 4, 2024
2 parents 8aa68e0 + 38be937 commit 04ef4a1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 28 deletions.
39 changes: 23 additions & 16 deletions components/WalletProviders/FuelProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

import { defaultConnectors } from '@fuels/connectors';
import { BakoSafeConnector, FuelWalletConnector, FueletWalletConnector, SolanaConnector, WalletConnectConnector, defaultConnectors } from '@fuels/connectors';
import { FuelProvider } from '@fuels/react';
import { CHAIN_IDS, Provider } from '@fuel-ts/account';
import { useConfig } from 'wagmi';
import KnownInternalNames from '../../lib/knownIds';
import { useSettingsState } from '../../context/settings';
import { BaskoRequestAPI } from '../../lib/wallets/fuel/Basko';
export const HOST_URL = 'https://api.bako.global';

const FuelProviderWrapper = ({
children,
Expand All @@ -16,23 +18,27 @@ const FuelProviderWrapper = ({

const network = networks.find(network => network.name === KnownInternalNames.Networks.FuelMainnet || network.name === KnownInternalNames.Networks.FuelTestnet)
const isMainnet = network?.name === KnownInternalNames.Networks.FuelMainnet

if (!network)
return <FuelProvider theme='dark' fuelConfig={{
connectors: defaultConnectors({ devMode: false, wcProjectId: WALLETCONNECT_PROJECT_ID }),
}}>
{children}
</FuelProvider>

const fuelConfig = {
connectors: defaultConnectors({
devMode: false,
wcProjectId: WALLETCONNECT_PROJECT_ID,
chainId: isMainnet ? CHAIN_IDS.fuel.mainnet : CHAIN_IDS.fuel.testnet,
fuelProvider: Provider.create(network?.node_url),
ethWagmiConfig: config
}),
};
connectors: [
new FuelWalletConnector(),
new BakoSafeConnector({
api: new BaskoRequestAPI(HOST_URL)
}),
new FueletWalletConnector(),
new WalletConnectConnector({
projectId: WALLETCONNECT_PROJECT_ID,
wagmiConfig: config,
chainId: isMainnet ? CHAIN_IDS.fuel.mainnet : CHAIN_IDS.fuel.testnet,
...(network?.node_url ? { fuelProvider: Provider.create(network?.node_url) } : {}),
}),
new SolanaConnector({
projectId: WALLETCONNECT_PROJECT_ID,
chainId: isMainnet ? CHAIN_IDS.fuel.mainnet : CHAIN_IDS.fuel.testnet,
...(network?.node_url ? { fuelProvider: Provider.create(network?.node_url) } : {}),
})
]
}

return (
<FuelProvider uiConfig={{ suggestBridge: false }} theme={'dark'} fuelConfig={fuelConfig}>
Expand All @@ -41,4 +47,5 @@ const FuelProviderWrapper = ({
);
};


export default FuelProviderWrapper;
43 changes: 43 additions & 0 deletions lib/wallets/fuel/Basko.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { urlJoin } from "@fuel-ts/account";

export class BAKO_STATE {
static state: { last_req?: Date, data: boolean, req_count: number, period_start?: Date } = { data: false, req_count: 0 }
static period_durtion: number = 10_000
}

export class BaskoRequestAPI {
baseUrl: string;

constructor(baseUrl: string) {
this.baseUrl = baseUrl;
}

async get(pathname: string) {

if (!pathname.includes('/state')) {
const data = await fetch(urlJoin(this.baseUrl, pathname)).then((res) =>
res.json(),
);
return data;
}

const period_elapsed = BAKO_STATE.state?.period_start && new Date().getTime() - BAKO_STATE.state?.period_start?.getTime() > BAKO_STATE.period_durtion;
const skip = BAKO_STATE.state?.last_req && new Date().getTime() - BAKO_STATE.state?.last_req?.getTime() < 1000 * 60 * 2 && period_elapsed;

if (skip)
return BAKO_STATE.state?.data;

const data = await fetch(urlJoin(this.baseUrl, pathname)).then((res) =>
res.json(),
);
const count = BAKO_STATE.state?.req_count || 0;
BAKO_STATE.state = { last_req: new Date(), data, req_count: count + 1, period_start: period_elapsed ? new Date() : BAKO_STATE.state?.period_start || new Date() };
return data;
}

async delete(pathname: string) {
await fetch(urlJoin(this.baseUrl, pathname), {
method: 'DELETE',
});
}
}
19 changes: 7 additions & 12 deletions lib/wallets/fuel/useFuel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ import useStorage from "../../../hooks/useStorage";
import resolveWalletConnectorIcon from "../utils/resolveWalletIcon";
import { useAccount, useConnections } from "wagmi";
import {
FuelConnector,
Predicate,
getPredicateRoot,
} from '@fuel-ts/account';
import { Address } from '@fuel-ts/address';
import { useWallet as useSolanaWallet } from "@solana/wallet-adapter-react"

import shortenAddress from "../../../components/utils/ShortenAddress";
import { BAKO_STATE } from "./Basko";

export default function useFuel(): WalletProvider {
const autofillSupportedNetworks = [KnownInternalNames.Networks.FuelTestnet, KnownInternalNames.Networks.FuelMainnet]
Expand All @@ -31,20 +30,11 @@ export default function useFuel(): WalletProvider {
const { address: evmAddress, connector: evmConnector } = useAccount()
const { connectors } = useConnectors()



const getWallet = () => {

// if (!isConnecting && !isFetching && !isRefetching && storageAvailable && !wallet?.address && !isLoading) {
// const fuelCurrentConnector = getItem('fuel-current-connector', 'localStorage')

// if (fuelCurrentConnector && fuelCurrentConnector === 'Bako Safe') {
// setItem('fuel-current-connector', '', 'localStorage')
// }
//

if (wallet) {
let fuelCurrentConnector = getItem('fuel-current-connector', 'localStorage')

let customConnectorname: string | undefined = undefined
const fuelEvmConnector = connectors.find(c => c.name === 'Ethereum Wallets')
const fuelSolanaConnector = connectors.find(c => c.name === 'Solana Wallets')
Expand Down Expand Up @@ -73,16 +63,21 @@ export default function useFuel(): WalletProvider {
providerName: name,
icon: resolveWalletConnectorIcon({ connector: customConnectorname || fuelCurrentConnector, address: address })
}

return w
}
}

const connectWallet = () => {
BAKO_STATE.state.last_req = undefined
BAKO_STATE.period_durtion = 120_000
return connect()
}

const disconnectWallet = async () => {
try {
BAKO_STATE.state.last_req = undefined
BAKO_STATE.period_durtion = 10_000
await disconnectAsync()
}
catch (e) {
Expand Down

0 comments on commit 04ef4a1

Please sign in to comment.