I am trying to interact with a simple flipper
contract written in rust ink!
and deployed in local node in substrate chain
. I am running the substrate
local node as well.
The code in brief is:
import { web3Enable, web3Accounts } from “@polkadot/extension-dapp”;
import { ApiPromise, WsProvider } from “@polkadot/api”;
import { ContractPromise } from “@polkadot/api-contract”;
import contractAbi from “…/assets/contractAbi.json”;
const CONTRACT_ADDRESS = “…”;
const [modalOpen, setModalOpen] = useState(false);
const [account, setAccount] = useState(null);
const [api, setApi] = useState(null);
const [contract, setContract] = useState(null);
const handleWalletConnect = async (walletType) => {
if (walletType === “polkadot”) {
try {
// Enable the Polkadot extension
const extensions = await web3Enable(“BitCross.fi”);
if (extensions.length === 0) {
setAccount(-1);
setModalOpen(false);
}
// Retrieve accounts from the Polkadot extension
const accounts = await web3Accounts();
if (accounts.length > 0) {
// Use the first account
const account = accounts[0];
setAccount(account.address);
sessionStorage.setItem("account_address", account.address);
setModalOpen(false);
// Initialize Polkadot API and contract
const wsProvider = new WsProvider("ws://127.0.0.1:9944");
const api = await ApiPromise.create({ provider: wsProvider });
const contract = new ContractPromise(
api,
contractAbi,
CONTRACT_ADDRESS
);
setApi(api);
setContract(contract);
} else {
setAccount(-1);
setModalOpen(false);
}
} catch (error) {
setAccount(-1);
setModalOpen(false);
}
}
};
const handleGet = async () => {
if (!contract || !api || !account) return;
try {
const { result, output } = await contract.query.get(account, {});
console.log(result, output);
if (result.isOk) {
console.log("Get operation result:", output?.toHuman());
} else {
console.error("Error querying get function:", result.asError);
}
} catch (error) {
console.error(“Get operation failed:”, error);
}
};
When calling the handleGet
, I am getting:
Type {registry: TypeRegistry, createdAtHash: undefined, initialU8aLength: 7, isStorageFallback: undefined, __internal__def: {…}, …} null
So output is returning 'null`.
I think I am missing something.