Koniverse / SubConnect

Note: We have stopped further development of this version and replaced it with a new and more complete version here
https://github.com/Koniverse/SubConnect-v2
Apache License 2.0
44 stars 14 forks source link

Call a smart contract #1

Closed michielpost closed 2 years ago

michielpost commented 2 years ago

Hi! I'm exploring the Polkadot/Substrate ecosystem and want to create a dApp that calls a SmartContract. I found this library to easily connect with one of the browser extensions, but now I want to call a smart contract after the user has connected. Is that possible? Can it be implemented once and work with all browser extensions? Do you have any tips for me on where to start?

saltict commented 2 years ago

Sorry @michielpost about late reply!

You also can wallet's signer with smart contract call. Here is example code:

const contract = await new ContractPromise(api, abi, address);
const address = '...' // Account address
const wallet = getWalletBySource('subwallet-js') as BaseDotsamaWallet;
if (wallet) {
  await wallet.enable()
  const signer = wallet.signer;
  if (signer && signer.signRaw && signer.signPayload) {
    await contract.tx
      .doSomething({ /* Smart contract method input go here */ })
      .signAndSend(
        address,
        { signer },
        async ({ status, dispatchError }) => {
          // Handle status callback here
        }
      );
    //Run something after fisish
  }
}

michielpost commented 2 years ago

Thanks. I was able to modify the Sign Dummy sample and then use the contract.tx... with the injected signer. That worked.

What would really help me (as a non react dev) is to have a vanilla-js sample. To really show what this project can do. Now it's very much intertwined with React. So using this without React is a bit complex.

saltict commented 2 years ago

I plan to separate wallet connect into a npm module that can use from any js code run on browser.

You can read main concept of connect to Dotsama wallet in this article to do it yourseft. It will help interact with extension and other object like signer to use in yout code.