Can anyone explain what is the difference between currentprovider and givenprovider?
I think like this.
givenprovider connects to blockchain thorough web browser, and
currentprovider connects to blockchain thorough metamask.
I'm not sure my understanding is correct.
Do you have any idea?
I think that's a lot of confusion regarding the Ethereum providers because lack of the standard at the begining. I'd like to sort it out as I understand it.
From web3 docs:
import Web3 from 'web3';
// "Web3.providers.givenProvider" will be set if in an Ethereum supported browser.
const web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', net, options);
Please not that to initialise web3 instance, the givenProvider on the Web3 module is used.
Let's see MetaMask docs:
MetaMask injects a global API into websites visited by its users at
window.ethereum (Also available at window.web3.currentProvider for
legacy reasons)
This is based on ERC1193
Basically, Web3 assigns window.ethereum to Web3.givenProvider property if the provider is ERC1193 compliant (as MetaMask), and can perform some fuzzy magic to detect the provider if not.
Following this logic, on the instance web3.currentProvider is the provider that web3 was initialized with, while web3.givenProvider is the provider injected by the environment (like window.ethereum). For example:
import Web3 from 'web3';
export const givenWeb3 = new Web3(Web3.givenProvider)
givenWeb3.currentProvider === givenWeb3.givenProvider // true
export const externalProvider = new Web3('http://localhost:8545')
externalProvider.currentProvider === externalProvider.givenProvider // false
Related
I try to use OpenZeppelin SDK to make my contracts upgradeable using the SimpleProxy approach. I need to test my contracts for upgrade compatibility (e.g prevent mistakes about unstructured storage during upgrading the contracts to a new version).
According to OpenZeppelin docs, I can write these types of tests by something like this
const { deployProxy, upgradeProxy } = require('#openzeppelin/truffle-upgrades');
const Box = artifacts.require('Box');
const BoxV2 = artifacts.require('BoxV2');
describe('upgrades', () => {
it('works', async () => {
const box = await deployProxy(Box, [42]);
const box2 = await upgradeProxy(box.address, BoxV2);
const value = await box2.value();
assert.equal(value.toString(), '42');
});
});
But in the above code, I must import both versions and this means I need to add the version number to the end of the contract's name and create a copy whenever I want to upgrade the contract. But what if I want to only modify the old contract and use a version control system like git?
If this is not possible, how to deal with these versions and files? when I want to upgrade a contract, need to update all of the imports and parent contracts for every child contract? If I want to put the latest version of each contract in a separate directory, do I need to update all import paths whenever upgrading a contract?
Sorry for my bad English.
I'm currently developing a dapp in Solidity and want to frequently test it locally along with the updates - so I don't really want to redeploy it everytime to a test net.
However, everytime I deploy it, the address the smart contract is deployed to changes, so I have to update my front-end code to the new address.
Is there a way to "force" the smart contract to always be deployed at the same address? Or is there any other equivalent solution you might think of?
Thanks!
Faced the same problem. I don't know whether it legal but you can do:
in your migration file (migrations/1_example_migration.js)
var MyContract = artifacts.require("MyContract");
module.exports = function(deployer) {
console.log(deployer);
console.log(arguments);
let n = 5; // it can be any address from list of available
deployer.deploy(MyContract, {from: arguments[2][n]});
};
Documentation
you can get the account list you have by passing in the accounts argument like follows In the deploy_migration files :
module.exports = function(deployer, network, accounts) {
// Use the accounts within your migrations.
}
I'm currently developing a dApp in Solidity and am working on a web3 library to handle communication with it.
I struggle with the process of new account creation and transaction signing in web3. Before I continue it worth noting that I'm running my own local, private blockchain (currently with Ganache).
My code looks as follows:
try{
let a = web3.eth.accounts.create()
let dataTx = someContract.methods.someMethod().encodeABI()
let rawTx = {
to: someContract._address,
from: account.address,
data: dataTx,
gas: 10000000000
}
const transaction = web3.eth.accounts.signTransaction(rawTx, util.toBuffer(account.privateKey))
web3.eth.sendTransaction(rawTx).then(console.log)
}
catch(e){
console.log(e)
}
The problem here is that the web3.eth.sendTransaction() method raises the following exception: Error: Returned error: sender account not recognized.
My understanding is that web3.eth.accounts is used for managing local accounts and web3.eth.personal is used to communicate with a client (e.g. Geth). I wish to keep the private keys of accounts my app creates locally on the device of the web3 client, but it raises this exception.
Where am I going wrong? Should I register the newly created accounts somewhere before running transactions with it? Is there some vital information I'm missing here?
Thanks!
If you want to use an account other than Ganache provided you, you have to start Ganache providing your accounts data in the form private_key,initial_balance:
Example command: ganache-cli --account 0xf38b5679751228eab7d9f3aa02bd0b0c0f7b44e448c0cfd410a1d7053efb6c56,123456789
And it's output:
Ganache CLI v6.1.8 (ganache-core: 2.2.1)
Available Accounts
================== (0) 0x44fa41e453654ccb365a358e994c764a37eea91f (~0 ETH)
Private Keys
================== (0) 0xf38b5679751228eab7d9f3aa02bd0b0c0f7b44e448c0cfd410a1d7053efb6c56
Gas Price
================== 20000000000
Gas Limit
================== 6721975
Listening on 127.0.0.1:8545
I am having same issue in my project. The problem in my case is beacuse i am not using same web3 provider to create contract variable.
example code:
const providerEth= new Web3.providers.HttpProvider(
'HTTP://127.0.0.1:8545'
);
const web3Eth = new Web3(providerEth);
const contract= new web3Eth.eth.Contract(abi,address);
Here, we are not using metamask provider although both on same network Still it not recognize the account. So you should create contract like this
const web3Eth = new Web3(window.web3.currentProvider);
const contract= new web3Eth.eth.Contract(abi,address);
const accounts = await web3.eth.getAccounts();
var receipt=await contract.methods.transfer(receiver,amount).send({from:accounts[0]});
Now, you can able to call smart contract function with account address.
I had the same issue. It happened when I already opened my truffle console and after that I did a restart of my ganache because I wanted to start clean.
What fixed it for me was stopping the truffle console job and starting it again.
You are refering to functionality in web3 1.0.0 which is not yet fully released.
If you go to https://web3js.readthedocs.io/en/1.0/getting-started.html
you would see that they state the following:
This documentation is work in progress and web3.js 1.0 is not yet released! You can find the current documentation for web3 0.x.x at github.com/ethereum/wiki/wiki/JavaScript-API.
Most probably you are using a version 0.20.x or something like that so check this first. To check this open the dApp in the browser and type in the console the following:
web3.version.api
This should show you which version you are using.
I don't think there is a way to create accounts with web3js 0.20.x directly but you can try to update the web3js to the 1.0.0-beta and try to run your code again. You can find it in NPM here - https://www.npmjs.com/package/web3
Apologies in advance for what might be an obvious question/answer, but I keep scouring the docs and cannot find it.
I know FeathersJS has drop-in strategies for Facebook/Twitter/Github -- I see those in the docs. And I know you can do all sorts of customization authorization strategies. All I am looking to do is authenticate users through a Oauth2 provider that doesnt already have a pre-packaged strategy. I cannot find a working example that does this.
More frustratingly, when I try to follow examples/docs, I get errors coming from the feathersjs npm modules, like:
<...>/node_modules/#feathersjs/authentication-oauth2/lib/index.js:96
app.passport.use(name, new Strategy(oauth2Settings, verifier.verify.bind(verifier)));
^
TypeError: Strategy is not a constructor
Does anyone have a working example?
That error means that you didn't pass a Passport oAuth2 strategy. You can set up the general Passport oAuth2 adapter very similar to the example in the documentation:
const oauth2 = require('#feathersjs/authentication-oauth2');
const OAuth2Strategy = require('passport-oauth2').Strategy;
app.configure(oauth2({
name: 'custom',
Strategy: OAuth2Strategy,
authorizationURL: 'https://www.example.com/oauth2/authorize',
tokenURL: 'https://www.example.com/oauth2/token',
clientID: EXAMPLE_CLIENT_ID,
clientSecret: EXAMPLE_CLIENT_SECRET,
callbackURL: "http://localhost:3000/auth/example/callback"
}));
I build an api rest by express js simply to post an data in my server .
app.post("/register", function(request, response){
var username = request.body.username;
});
How i can do this with feathersjs ? and how i can call it from my reactjs app ?
Feathers is a drop-in replacement for Express. That means you can replace your const app = express(); with const app = feathers() and everything will work just the same, so what you are showing above you can also do with Feathers.
The real Feathers way to accomplish this however is through services which - with the other important concepts - are described in the basics guide.
There are pre-build services for several databases (which can be customized through hooks) but you can always create your own service. It is important to note that Feathers services - unlike the Express middleware you showed - will be available via HTTP (REST) and websockets (which also gets you real-time events). See here how service methods map to REST endpoints.
Your example in Feathers simply looks like this:
app.use('/register', {
create(data, params) {
// data is the request body e.g.
// data.username
// Always return a promise with the result data
return Promise.resolve(data);
}
});