From a23e2b186bc4ea5a1f2eaa1b0c7aeb020bb1fb9f Mon Sep 17 00:00:00 2001 From: Mohammad Shahbaz Alam Date: Mon, 16 Oct 2023 12:39:59 +0530 Subject: [PATCH 1/2] Update Smart Constract Read and Write functions --- .../docs/_smart-contract-solidity-read.mdx | 38 +++++++++----- .../docs/_smart-contract-solidity-write.mdx | 50 ++++++++++++------- src/common/docs/_smart-contract-solidity.mdx | 3 +- 3 files changed, 61 insertions(+), 30 deletions(-) diff --git a/src/common/docs/_smart-contract-solidity-read.mdx b/src/common/docs/_smart-contract-solidity-read.mdx index d6b6bf3f5..7ef771eec 100644 --- a/src/common/docs/_smart-contract-solidity-read.mdx +++ b/src/common/docs/_smart-contract-solidity-read.mdx @@ -17,13 +17,19 @@ import Tabs from "@theme/Tabs"; // web3 is const web3 = new Web3(web3authProvider); from above. -// Get user's Ethereum public address -const fromAddress = (await web3.eth.getAccounts())[0]; - -const contractABI = - '[{"inputs":[{"internalType":"string","name":"initMessage","type":"string"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"string","name":"newMessage","type":"string"}],"name":"update","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"message","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}]'; -const contractAddress = "0xD65AF91Bbb4334711A598dFD293596E6dc2d8313#code"; -const contract = new web3.eth.Contract(JSON.parse(contractABI), contractAddress); +const contractABI = [ + { inputs: [{ internalType: "string", name: "initMessage", type: "string" }], stateMutability: "nonpayable", type: "constructor" }, + { inputs: [], name: "message", outputs: [{ internalType: "string", name: "", type: "string" }], stateMutability: "view", type: "function" }, + { + inputs: [{ internalType: "string", name: "newMessage", type: "string" }], + name: "update", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, +]; +const contractAddress = "0x04cA407965D60C2B39d892a1DFB1d1d9C30d0334"; +const contract = new web3.eth.Contract(JSON.parse(JSON.stringify(contractABI)), contractAddress); // Read message from smart contract const message = await contract.methods.message().call(); @@ -41,11 +47,19 @@ const message = await contract.methods.message().call(); const signer = provider.getSigner(); -const contractABI = - '[{"inputs":[{"internalType":"string","name":"initMessage","type":"string"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"string","name":"newMessage","type":"string"}],"name":"update","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"message","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}]'; -const contractAddress = "0xD65AF91Bbb4334711A598dFD293596E6dc2d8313#code"; - -const contract = new ethers.Contract(contractAddress, contractABI, signer); +const contractABI = [ + { inputs: [{ internalType: "string", name: "initMessage", type: "string" }], stateMutability: "nonpayable", type: "constructor" }, + { inputs: [], name: "message", outputs: [{ internalType: "string", name: "", type: "string" }], stateMutability: "view", type: "function" }, + { + inputs: [{ internalType: "string", name: "newMessage", type: "string" }], + name: "update", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, +]; +const contractAddress = "0x04cA407965D60C2B39d892a1DFB1d1d9C30d0334"; +const contract = new ethers.Contract(contractAddress, JSON.parse(JSON.stringify(contractABI)), signer); // Read message from smart contract const message = await contract.message(); diff --git a/src/common/docs/_smart-contract-solidity-write.mdx b/src/common/docs/_smart-contract-solidity-write.mdx index cdb9879b3..0e7352ec4 100644 --- a/src/common/docs/_smart-contract-solidity-write.mdx +++ b/src/common/docs/_smart-contract-solidity-write.mdx @@ -17,19 +17,26 @@ import Tabs from "@theme/Tabs"; // web3 is const web3 = new Web3(web3authProvider); from above. -// Get user's Ethereum public address -const fromAddress = (await web3.eth.getAccounts())[0]; - -const contractABI = - '[{"inputs":[{"internalType":"string","name":"initMessage","type":"string"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"string","name":"newMessage","type":"string"}],"name":"update","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"message","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}]'; -const contractAddress = "0xD65AF91Bbb4334711A598dFD293596E6dc2d8313#code"; -const contract = new web3.eth.Contract(JSON.parse(contractABI), contractAddress); - -// Send transaction to smart contract to update message and wait to finish -const receipt = await contract.methods.update("NEW_MESSAGE").send({ - from: fromAddress, - maxPriorityFeePerGas: "5000000000", // Max priority fee per gas - maxFeePerGas: "6000000000000", // Max fee per gas +const contractABI = [ + { inputs: [{ internalType: "string", name: "initMessage", type: "string" }], stateMutability: "nonpayable", type: "constructor" }, + { inputs: [], name: "message", outputs: [{ internalType: "string", name: "", type: "string" }], stateMutability: "view", type: "function" }, + { + inputs: [{ internalType: "string", name: "newMessage", type: "string" }], + name: "update", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, +]; +const contractAddress = "0x04cA407965D60C2B39d892a1DFB1d1d9C30d0334"; +const contract = new web3.eth.Contract(JSON.parse(JSON.stringify(contractABI)), contractAddress); + +// Write message to smart contract +// @ts-ignore +const receipt = await contract.methods.update("W3A").send({ + from: (await web3.eth.getAccounts())[0], + maxFeePerGas: "300", + maxPriorityFeePerGas: "10", }); ``` @@ -45,10 +52,19 @@ const receipt = await contract.methods.update("NEW_MESSAGE").send({ const signer = provider.getSigner(); -const contractABI = - '[{"inputs":[{"internalType":"string","name":"initMessage","type":"string"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"string","name":"newMessage","type":"string"}],"name":"update","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"message","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}]'; -const contractAddress = "0xD65AF91Bbb4334711A598dFD293596E6dc2d8313#code"; -const contract = new ethers.Contract(contractAddress, contractABI, signer); +const contractABI = [ + { inputs: [{ internalType: "string", name: "initMessage", type: "string" }], stateMutability: "nonpayable", type: "constructor" }, + { inputs: [], name: "message", outputs: [{ internalType: "string", name: "", type: "string" }], stateMutability: "view", type: "function" }, + { + inputs: [{ internalType: "string", name: "newMessage", type: "string" }], + name: "update", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, +]; +const contractAddress = "0x04cA407965D60C2B39d892a1DFB1d1d9C30d0334"; +const contract = new ethers.Contract(contractAddress, JSON.parse(JSON.stringify(contractABI)), signer); // Send transaction to smart contract to update message const tx = await contract.update("NEW_MESSAGE"); diff --git a/src/common/docs/_smart-contract-solidity.mdx b/src/common/docs/_smart-contract-solidity.mdx index 525adf125..0ccb7cb75 100644 --- a/src/common/docs/_smart-contract-solidity.mdx +++ b/src/common/docs/_smart-contract-solidity.mdx @@ -4,7 +4,8 @@ In this example, we'll be demonstrating how to use Web3Auth with web3.js or ethe World contract allows anyone to read and write a message to it. ```tsx -pragma solidity ^0.8.18; +// SPDX-License-Identifier: GPL-3.0 +pragma solidity >=0.7.0 <0.9.0; contract HelloWorld { From c49f6b2ef95d2d1ca5fcd3ad849d1f7d55b5ed26 Mon Sep 17 00:00:00 2001 From: Naveed Elahi Date: Mon, 16 Oct 2023 16:53:33 +0800 Subject: [PATCH 2/2] update OpenloginAdapter imports otherwise code examples for instantiating don't work --- src/common/sdk/pnp/web/_custom-authentication-core-example.mdx | 2 +- src/common/sdk/pnp/web/_openlogin-whitelabel-example.mdx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common/sdk/pnp/web/_custom-authentication-core-example.mdx b/src/common/sdk/pnp/web/_custom-authentication-core-example.mdx index 48d63ace5..822c97bbf 100644 --- a/src/common/sdk/pnp/web/_custom-authentication-core-example.mdx +++ b/src/common/sdk/pnp/web/_custom-authentication-core-example.mdx @@ -1,5 +1,5 @@ ```javascript -import OpenloginAdapter from "@web3auth/openlogin-adapter"; +import { OpenloginAdapter } from "@web3auth/openlogin-adapter"; const openloginAdapter = new OpenloginAdapter({ adapterSettings: { diff --git a/src/common/sdk/pnp/web/_openlogin-whitelabel-example.mdx b/src/common/sdk/pnp/web/_openlogin-whitelabel-example.mdx index f7dd3d85e..efdda7431 100644 --- a/src/common/sdk/pnp/web/_openlogin-whitelabel-example.mdx +++ b/src/common/sdk/pnp/web/_openlogin-whitelabel-example.mdx @@ -1,5 +1,5 @@ ```javascript -import OpenloginAdapter from "@web3auth/openlogin-adapter"; +import { OpenloginAdapter } from "@web3auth/openlogin-adapter"; const openloginAdapter = new OpenloginAdapter({ adapterSettings: {