How to connect to a deployed Polygon testnet contract? - javascript

I have a front-end / web3 application that connects to a deployed contract in the rinkeby ethereum networt.
That is possible with the Web3 library, and creating an instance of web3, like the code below:
web3.js
import Web3 from "web3";
window.ethereum.request({ method: "eth_requestAccounts" });
const web3 = new Web3(window.ethereum);
export default web3;
So, in another file, i connect to the deployed contract via contract address and ABI:
contract.js
import web3 from "./web3";
const address = "0x(...)fEb";
const abi = ["(...)"]
export default new web3.eth.Contract(abi, address);
Ok. Then i can call methods on my etherium contract.
But i would like to do it in the mumbai polygon testnet. I already have a deployed / verified contract, and then in the contract.js, I replaced the values, address and abi, but it doesn't work.
I saw something about magic sdk library, but the examples is kind different from this model, and i got kind confused.
So, someone have a hint about how to connect to a mumbai polygon testnet to front-end?

If you need to communicate with Polygon, you should use Matic.js and use this guide: https://medium.com/the-polygon-blog/beginner-friendly-tutorial-to-matic-js-58f7c24c9744
Answer to the question: To connect to Mumbai Polygon you use:
const polygonNodeOptions = {
rpcUrl: 'https://rpc-mumbai.maticvigil.com/',
chainId: 80001,
};

From https://mumbai.polygonscan.com/tx/xxxxxx
Get the contract address from your deployed contract.
which you can find in To: section
deployed contract
then
const getContract = new web3.eth.Contract(yourabi, deployedcontractAddress);
const res = await getContract.methods.your-method().call();
Hope this helps.

Related

I cannot work with Smart Contract using Node.js

I've been trying to link a smart contract with ethers.js in my project.
const Provider= new ethers.providers.JsonRpcProvider(
`https://goerli.infura.io/v3/`
);
const TokenContract = new ethers.Contract(
TokenAddress,
abiSource.token.abi,
Provider
);`
I tried to run this but couldn't get the balance of the token contract.
Extracted RPC link from the Metamask.
Who can help me with this problem?
Thanks in advance.
It seems like you didn't use Infura georli api key.
const Provider= new ethers.providers.JsonRpcProvider(
`https://goerli.infura.io/v3/ca11249dabe247c1a6e0877c24376dda`
);
Please try the above.
However, you must bring your own Infura API Key.

Undefined web3.eth.getAccounts() in React Native, metamask authentication

I want to authenticate the user by his Metamask wallet. I am using web3 package in order to interact with the blocks and sign transactions. When I try to get the user accounts I get an empty result:
const Web3 = require('web3');
const web3 = new Web3(
new Web3.providers.HttpProvider('https://api.avax.network/ext/bc/C/rpc')
);
const addresses = await web3.eth.getAccounts();
I suppose that I need to request the accounts like this await window.ethereum.request({ method: 'eth_requestAccounts'}); but it doesn't exist window mobile app.
I guess in a normal flow the user hit the auth button and will be redirected to Metamask wallet to authorize the app, how can I do this?
You are correct, but you also need to connect to MetaMask, not your own RPC. Here is your fixed code:
import detectEthereumProvider from '#metamask/detect-provider';
const web3 = await detectEthereumProvider(); // Use Metamask-injected web3
await web3.request({ method: 'eth_requestAccounts'});
const addresses = await web3.eth.getAccounts();
I found out a solution, I do not know if is the best way to do it.
In order to interact with the blocks and sign transactions the users authenticate their MetaMask wallet through WalletConnect service.
After the user is authenticated I can use the connector to create a provider in order to use it with ethers.js lib (Using web3.js the connection with RPC failed, I decided to use ethers instead):
const walletConnectProvider = new WalletConnectProvider({
rpc: {
43114: RPC
},
chainId: 43114,
connector: this.connector,
qrcode: false
})
let wp = await walletConnectProvider.enable();
if(wp){
this.provider = new providers.Web3Provider(walletConnectProvider);
}
const signer = this.provider.getSigner();
const contract = new Contract(GAMESESSION_ADDRESS, GameSession.abi, signer);
....
It is a little tricky but WalletConnect works fine.

How can I connect to a Metamask wallet using ethers.js from a standalone node.js application?

I can't figure out how to do this. I have seen some documentation talking about ethers.Wallet.fromMnemonic, but how does ethers know my wallet is a metamask one? Or, if the wallet provider is irrelevant (is it?) how does it know its address?
Does it make any difference that I want to then interact with a liquidity pool on BSC? I mean in terms of the wallet. Can a wallet be used on multiple networks?
var web3 = require("web3")
var ethers = require("ethers")
async function MetaMask() {
privateKey = "Your wallet privateKey";
account = new ethers.Wallet(privateKey)
console.log(account)
//from mnemonic
mnemonic = "Your Mnemonic"
const wallet = new ethers.Wallet.fromMnemonic(mnemonic)
console.log(wallet)
}
MetaMaskAddress = MetaMask()

How it is possible to connect with uniswap smart contract with uniswap sdk pakage without giving private key

I am developing the decentralized application of assy indexes where we swap tokens using uniswap sdk. To complete the transaction I need to connect the account with uniswap smart contract. To connect with uniswap it requires a signer and signer connect the wallet with the private key.
You can see the steps below.
provider = ethers.getDefaultProvider('rinkeby', {
infura: 'https://rinkeby.infura.io/v3/38a3d4450c784956877a1bd2fe75c406'
})
console.log('provider', provider)
const signer = new ethers.Wallet(privateKey)
console.log('signer', signer)
const account = signer.connect(provider)
console.log('account', account)
const uniswap = new ethers.Contract(
'0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D',
uniswapAbi,
**account**
)
console.log('Uniswap', uniswap)
const to = this.accountAddress

how can I send an IP messaging using twilio-csharp library

I just start create a live chat app with Twilio.
I have downloaded the twilio-csharp library from twilio.com/docs/csharp/install and started with a sample console application.
code example from Twilio:
// Download the twilio-csharp library from twilio.com/docs/csharp/install
using System;
using Twilio.IpMessaging;
class Example {
static void Main (string[] args) {
// Find your Account Sid and Auth Token at twilio.com/user/account
const string accountSid = "accountSid";
const string authToken = "authToken";
const string serviceSid = "serviceSid";
const string channelSid = "channelSid";
// List all Messages
var client = new IpMessagingClient(accountSid, authToken);
MessageResult result = client.ListMessages(serviceSid, channelSid);
Console.WriteLine(result);
}
}
But this code doesn't work in my case, I got always an errors "type or namespace could not be found".
I have these like reference:
I tried tutorial of IP Messaging , it works. But in the tutorial, they used Javascript SDK to initialize the IP messaging client
tc.accessManager = new Twilio.AccessManager(tokenResponse.token);
tc.messagingClient = new Twilio.IPMessaging.Client(tc.accessManager);
So I just don't understand how can I use this C# library to send an IP Messaging, or maybe I can just control my IP Messaging applications from the client side?
I unstand why I got this error.
I just follow the steps of installation on twilio-csharp library gitHub. But this project's README is only for SMS project. If we want to use Library for IpMessaging, we must also import another Library, Twilio.IpMessaging.
So What I got know like reference and the project IP Messaging works.
Hope this will help the others.

Categories

Resources