# React interact with ink!

**URL:** <https://forum.polkadot.network/t/react-interact-with-ink/9658>\
**Category:** Tech Talk\
**Tags:** ink, stackexchange\
**Created:** [August 14, 2024, 2:07pm UTC](https://forum.polkadot.network/t/react-interact-with-ink/9658 "2024-08-14T14:07:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zubayr1](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.polkadot.network/zubayr1/32/6128_2.png) [@zubayr1](https://forum.polkadot.network/u/zubayr1)\
**Post date:** [August 14, 2024, 2:07pm UTC](https://forum.polkadot.network/t/react-interact-with-ink/9658/1 "2024-08-14T14:07:48Z")

</div>

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](http://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.

---

<div class="post-metadata">

**Author:** ![sinzii](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.polkadot.network/sinzii/32/6947_2.png) [@sinzii](https://forum.polkadot.network/u/sinzii)\
**Post date:** [August 14, 2024, 3:55pm UTC](https://forum.polkadot.network/t/react-interact-with-ink/9658/2 "2024-08-14T15:55:19Z")

</div>

Hi @zubayr1,

Interacting with ink! smart contracts using [Polkadot.js](https://polkadot.js.org/docs/api-contract) might be challenging, especially if you’re new to ink!.

From what you describe, it’s a bit hard to see & debug what went wrong, can you host the script somewhere e.g on [https://stackblitz.com/](https://stackblitz.com/), so it’s easier for us to go there and help you check the issue.

Alternatively, I suggest you trying out [Dedot](https://github.com/dedotdev/dedot), a new JavaScript Client for Polkadot & Substrate which supports [fully-typed APIs](https://forum.polkadot.network/t/type-safe-apis-to-interact-with-ink-smart-contracts-dedot/9485) to interact with [ink! contracts](https://github.com/dedotdev/dedot/tree/main?tab=readme-ov-file#interact-with-ink-smart-contracts). There is an [example](https://github.com/dedotdev/dedot/blob/b69096fc366497f267e7c7173f2baeacc64e8419/zombienet-tests/src/0001-check-contract-api.ts) (e2e test) to deploy & interact with a flipper contract in the source code, so you could go there and check it out.

If you need more help, you can reach out to me directly on Telegram (@realsinzii) or ask your question on Telegram group of [Dedot](https://t.me/JoinDedot), I’m happy to help you out with the issue.

---

<div class="post-metadata">

**Author:** ![mister\_cole](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.polkadot.network/mister_cole/32/537_2.png) [@mister\_cole](https://forum.polkadot.network/u/mister_cole)\
**Post date:** [August 14, 2024, 8:27pm UTC](https://forum.polkadot.network/t/react-interact-with-ink/9658/3 "2024-08-14T20:27:51Z")

</div>

Closed on August 14, 2024, 8:27pm UTC



---

<div class="post-metadata">

**Author:** ![mister\_cole](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.polkadot.network/mister_cole/32/537_2.png) [@mister\_cole](https://forum.polkadot.network/u/mister_cole)\
**Post date:** [August 14, 2024, 8:28pm UTC](https://forum.polkadot.network/t/react-interact-with-ink/9658/4 "2024-08-14T20:28:03Z")

</div>

Opened on August 14, 2024, 8:28pm UTC



---

<div class="post-metadata">

**Author:** ![shawntabrizi](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.polkadot.network/shawntabrizi/32/14_2.png) [@shawntabrizi](https://forum.polkadot.network/u/shawntabrizi)\
**Post date:** [August 14, 2024, 10:00pm UTC](https://forum.polkadot.network/t/react-interact-with-ink/9658/5 "2024-08-14T22:00:09Z")

</div>

Hi @zubayr1

Unfortunately I can’t help you with this question, but I will suggest you take your technical questions to the `StackExchange` site:

> **[Substrate and Polkadot Stack Exchange](https://substrate.stackexchange.com/)**
>
> Q&A for developers building blockchains with the Substrate SDK

Thanks!

---

<div class="post-metadata">

**Author:** ![shawntabrizi](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.polkadot.network/shawntabrizi/32/14_2.png) [@shawntabrizi](https://forum.polkadot.network/u/shawntabrizi)\
**Post date:** [August 14, 2024, 10:00pm UTC](https://forum.polkadot.network/t/react-interact-with-ink/9658/6 "2024-08-14T22:00:13Z")

</div>

Closed on August 14, 2024, 10:00pm UTC


