Hi guys!
After updating fuels to 0.62.0 all my code for Swaylend and Spark project stopped working.
Main error that I get when try to call any smart contract is “TypeError: provider.getGasConfig is not a function”
Here is AccountStore where I keep my connection with Fuel Wallet]
import RootStore from "@stores/RootStore";
import { makeAutoObservable, reaction, when } from "mobx";
import { Address, Provider, Wallet, WalletLocked, WalletUnlocked } from "fuels";
import { IToken, NODE_URL, TOKENS_LIST } from "@src/constants";
import Balance from "@src/entities/Balance";
import BN from "@src/utils/BN";
import { FuelWalletProvider } from "@fuel-wallet/sdk";
export enum LOGIN_TYPE {
FUEL_WALLET = "FUEL_WALLET",
FUELET = "FUELET",
}
export interface ISerializedAccountStore {
address: string | null;
loginType: LOGIN_TYPE | null;
}
class AccountStore {
public readonly rootStore: RootStore;
public provider: Provider | null = null;
private setProvider = (provider: Provider | null | any) =>
(this.provider = provider);
constructor(rootStore: RootStore, initState?: ISerializedAccountStore) {
makeAutoObservable(this);
this.rootStore = rootStore;
if (initState) {
this.setLoginType(initState.loginType);
this.setAddress(initState.address);
if (initState.loginType != null) {
document.addEventListener("FuelLoaded", this.onFuelLoaded);
}
}
this.initProvider();
when(() => this.provider != null, this.updateAccountBalances);
setInterval(this.updateAccountBalances, 10 * 1000);
reaction(
() => this.address,
() => Promise.all([this.updateAccountBalances()])
);
}
initProvider = async () => {
Provider.create(NODE_URL)
.then((provider) => this.setProvider(provider))
.catch(console.error);
};
onFuelLoaded = () => {
if (this.walletInstance == null) return;
this.walletInstance.on(
window?.fuel.events.currentAccount,
this.handleAccEvent
);
this.walletInstance.on(
window?.fuel.events?.network,
this.handleNetworkEvent
);
};
handleAccEvent = (account: string) => this.setAddress(account);
handleNetworkEvent = (network: FuelWalletProvider) => {
if (network.url !== NODE_URL) {
this.rootStore.notificationStore.toast(
`Please change network url to Testnet Beta 4`,
{
copyTitle: "Copy beta-4 RPC",
copyText: NODE_URL,
type: "error",
title: "Attention",
}
);
}
};
public address: string | null = null;
setAddress = (address: string | null) => (this.address = address);
public privateKey: string | null = null;
setPrivateKey = (key: string | null) => (this.privateKey = key);
public loginType: LOGIN_TYPE | null = null;
setLoginType = (loginType: LOGIN_TYPE | null) => (this.loginType = loginType);
public assetBalances: Balance[] | null = null;
setAssetBalances = (v: Balance[] | null) => (this.assetBalances = v);
updateAccountBalances = async () => {
if (this.address == null) {
this.setAssetBalances([]);
return;
}
const address = Address.fromString(this.address);
const balances = (await this.provider?.getBalances(address)) ?? [];
const assetBalances = TOKENS_LIST.map((asset) => {
const t = balances.find(({ assetId }) => asset.assetId === assetId);
const balance = t != null ? new BN(t.amount.toString()) : BN.ZERO;
if (t == null)
return new Balance({ balance, usdEquivalent: BN.ZERO, ...asset });
return new Balance({ balance, ...asset });
});
this.setAssetBalances(assetBalances);
};
findBalanceByAssetId = (assetId: string) =>
this.assetBalances &&
this.assetBalances.find((balance) => balance.assetId === assetId);
get balances() {
const { accountStore } = this.rootStore;
return TOKENS_LIST.map((t) => {
const balance = accountStore.findBalanceByAssetId(t.assetId);
return balance ?? new Balance(t);
})
.filter((v) => v.usdEquivalent != null && v.usdEquivalent.gt(0))
.sort((a, b) => {
if (a.usdEquivalent == null && b.usdEquivalent == null) return 0;
if (a.usdEquivalent == null && b.usdEquivalent != null) return 1;
if (a.usdEquivalent == null && b.usdEquivalent == null) return -1;
return a.usdEquivalent!.lt(b.usdEquivalent!) ? 1 : -1;
});
}
serialize = (): ISerializedAccountStore => ({
address: this.address,
loginType: this.loginType,
});
login = async (loginType: LOGIN_TYPE) => {
this.setLoginType(loginType);
await this.loginWithWallet();
await this.onFuelLoaded();
};
get walletInstance() {
switch (this.loginType) {
case LOGIN_TYPE.FUEL_WALLET:
return window.fuel;
case LOGIN_TYPE.FUELET:
return window.fuelet;
default:
return null;
}
}
disconnect = async () => {
try {
this.walletInstance.disconnect();
} catch (e) {
this.setAddress(null);
this.setLoginType(null);
}
this.setAddress(null);
this.setLoginType(null);
};
loginWithWallet = async () => {
if (this.walletInstance == null)
throw new Error("There is no wallet instance");
// const res = await this.walletInstance.connect({ url: NODE_URL });
const res = await this.walletInstance.connect();
if (!res) {
this.rootStore.notificationStore.toast("User denied", {
type: "error",
});
return;
}
const account = await this.walletInstance.currentAccount();
const provider = await this.walletInstance.getProvider();
if (provider.url !== NODE_URL) {
this.rootStore.notificationStore.toast(
`Please change network url to beta 4`,
{
copyTitle: "Copy beta-4 RPC",
copyText: NODE_URL,
type: "error",
title: "Attention",
}
);
}
this.setAddress(account);
};
getFormattedBalance = (token: IToken): string | null => {
const balance = this.findBalanceByAssetId(token.assetId);
if (balance == null) return null;
return BN.formatUnits(balance.balance ?? BN.ZERO, token.decimals).toFormat(
2
);
};
getBalance = (token: IToken): BN | null => {
const balance = this.findBalanceByAssetId(token.assetId);
if (balance == null) return null;
return BN.formatUnits(balance.balance ?? BN.ZERO, token.decimals);
};
get isLoggedIn() {
return this.address != null;
}
getWallet = async (): Promise<WalletLocked | WalletUnlocked | null> => {
if (this.address == null || window.fuel == null) return null;
return window.fuel.getWallet(this.address);
};
get walletToRead(): WalletLocked | null {
return this.provider == null
? null
: Wallet.fromAddress(
"fuel1m56y48mej3366h6460y4rvqqt62y9vn8ad3meyfa5wkk5dc6mxmss7rwnr",
this.provider ?? ""
);
}
get addressInput(): null | { value: string } {
if (this.address == null) return null;
return { value: Address.fromString(this.address).toB256() };
}
get addressB256(): null | string {
if (this.address == null) return null;
return Address.fromString(this.address).toB256();
}
}
export default AccountStore;
And here is the code of the mint
function from the multi-token smart and console logs for some of variable to make sure that they are not empty
mint = async (assetId?: string) => {
console.log("mint assetId", assetId);
if (assetId == null) return;
if (this.rootStore.accountStore.loginType === LOGIN_TYPE.FUEL_WALLET) {
const addedAssets: Array<any> = await window?.fuel.assets();
if (
addedAssets != null &&
!addedAssets.some((v) => v.assetId === assetId)
) {
await this.addAsset(assetId);
}
}
this._setLoading(true);
this.setActionTokenAssetId(assetId);
const { accountStore, notificationStore } = this.rootStore;
const { tokenFactory } = this.rootStore.settingsStore.currentVersionConfig;
const wallet = await accountStore.getWallet();
console.log("wallet in mint fn", wallet);
console.log("provider ", accountStore.provider);
if (wallet == null) return;
const tokenFactoryContract = TokenFactoryAbi__factory.connect(
tokenFactory,
wallet
);
console.log("tokenFactoryContract", tokenFactoryContract);
try {
const token = TOKENS_BY_ASSET_ID[assetId];
const amount = BN.parseUnits(faucetAmounts[token.symbol], token.decimals);
const hash = hashMessage(token.symbol);
const userAddress = wallet.address.toB256();
const { transactionResult } = await tokenFactoryContract.functions
.mint({ value: userAddress }, hash, amount.toString())
.txParams({ gasPrice: 2 })
.call();
if (transactionResult != null) {
const token = TOKENS_BY_ASSET_ID[assetId];
if (token !== TOKENS_BY_SYMBOL.ETH) {
this.rootStore.settingsStore.addMintedToken(assetId);
}
const txId = transactionResult.id ?? "";
this.rootStore.notificationStore.toast(
`You have successfully minted ${token.symbol}`,
{
link: `${EXPLORER_URL}/transaction/${txId}`,
linkTitle: "View on Explorer",
type: "success",
copyTitle: `Copy tx id: ${centerEllipsis(txId, 8)}`,
copyText: transactionResult.id,
title: "Transaction is completed!",
}
);
}
await this.rootStore.accountStore.updateAccountBalances();
} catch (e) {
const errorText = e?.toString();
console.log(errorText);
notificationStore.toast(errorText ?? "", { type: "error" });
} finally {
this.setActionTokenAssetId(null);
this._setLoading(false);
}
};
AccountStore code - https://github.com/compolabs/sway-lend/blob/fuels-62-fails/frontend/src/stores/AccountStore.ts
Faucet Vm with mint fn - https://github.com/compolabs/sway-lend/blob/fuels-62-fails/frontend/src/screens/Faucet/FaucetVm.tsx