Archived Content from Web3Auth Community
This topic was originally posted by githubdiscussions on 11/19/2022.
This content has been migrated from our previous community forum to preserve valuable discussions.
Hello, im having trouble interacting with my smart contract (in solidity, deployed on IOTA EVM chain) because i didnt find any example code on how to use that generated private key to sign and send contract transactions. Could you point any example on how to interact with contract after openlogin? Or could you tell what is wrong with my code, because not even calls wont get through because it somehow wont find the method but it clearly is present in my abi.
Im getting error:
jquery.min.js:2 Uncaught TypeError: Cannot read properties of undefined (reading 'getContractMethod')
at renderUserAccount (test-torus.html:577:40)
at HTMLDocument. (test-torus.html:621:19)
My code looks like:
`<script>
(function(){
var sdkInstance = null;
var openloginUtils = null;
var OpenLogin = null;
async function renderUserAccount(){
const IOTA_JSON_RPC = “http://localhost:9090/chain/someLettersAndNumbers/evm/jsonrpc”;
const web3 = new window.Web3(new Web3.providers.HttpProvider(IOTA_JSON_RPC)
);
const abi = [abiOfMyContract];
const contractAddress = “0xSomethingSomething”;
const BlockieContract = new web3.eth.Contract(abi, contractAddress);
// BELOW IS THE CONTRACT CALL !!!
await BlockieContract.method.getContractMethod().call(function (err, res) {
if (err) {
$("#tx-results").text(`An error occured ${err}`);
}else {$("#tx-results").text(`Transaction: ${res}`);}
});
const account = web3.eth.accounts.privateKeyToAccount(sdkInstance.privKey);
const address = account.address;
const balance = await web3.eth.getBalance(address);
$("#public-address").text(`Public address: ${address} `);
$("#private-key").text(`Private key: ${sdkInstance.privKey} `);
$("#account-bal").text(`Account balance: ${balance}`);
$("#logout").show("fadein");
$("#login").hide();
}
function generateAppCreds() {
let priv = window.eccryptoJS.generatePrivate()
let pub = window.eccryptoJS.getPublic(priv)
let clientId = openloginUtils.base64url.encode(pub)
return { clientId, appKey: priv.toString(“hex”) };
}
async function whitelistOrigin(clientId, appKey) {
const sig = await OpenLogin.whitelistUrl(clientId, appKey, window.location.origin);
return sig;
}
$(document).ready(async function () {
openloginUtils = window.OpenloginUtils;
OpenLogin = window.Openlogin;
const { clientId, appKey } = generateAppCreds();
console.log(await whitelistOrigin(clientId, appKey))
sdkInstance = new OpenLogin.default({
clientId: “random_id” , // client id can be anything for localhost
iframeUrl: “https://beta.openlogin.com”
});
await sdkInstance.init();
if (sdkInstance.privKey) {
await renderUserAccount()
} else {
$(“#login”).show();
}
});
$(“#login”).click(async function (event) {
if (!sdkInstance.privKey) {
const sig = “”;
await sdkInstance.login({
loginProvider: “google”,
redirectUrl: ${window.origin}/test-torus.html,
originData: {
[window.location.origin]: sig
}
});
return
}
await renderUserAccount();
$("#error").hide();
$("#login").hide();
});
$(“#logout”).click(async function (event) {
$(“#text”).text(“Logging out…”)
$(“#logout”).hide();
await sdkInstance.logout();
window.location.reload();
});
})()
</script>`
Originally posted by: AndrejGross
Check the discussion at: https://github.com/orgs/Web3Auth/discussions/906