[Ethereum] Please provide example for web3.currentProvider

metamasksolidity

The following document states that "MetaMask is going to continue to inject the ethereum provider as web3.currentProvider for the forseeable future.":
https://github.com/MetaMask/faq/blob/master/detecting_metamask.md

That document indicates that using the web3.currentProvider object will NOT require importing a convenience library like Web3 or EthJS. I will only have to add the following code to the onLoad event:

if (typeof web3 !== 'undefined') {
    // Use the browser's ethereum provider
    var provider = web3.currentProvider
} else {
    console.log('No web3? You should consider trying MetaMask!')
}

Therefore, please provide two specific tested examples using the provider.sendAsync(options, callback) method that was described on the above webpage.

For example, if the contract contains a "Deposit()" function that is marked as "payable", please provide the correct conversion of the following JS call:

myContractInstance.Deposit.sendTransaction({from:"0x3d86B4D6f28554428E5AF38490DD1977691A3082", value: web3.toWei(0.2,'ether')}, function(error, result){
    if(!error) {
        console.log("#" + result + "#")
    } else {
        console.error(error);
    }
})

If the contract contains a "getLastDepositor()" function that returns a stored value, please provide the correct conversion of the following JS call:

myContractInstance.getLastDepositor.call(function(error, result){
    if(!error) {
        console.log("##" + result + "##")
    } else {
        console.error("Error:" + error);
    }
})

My project will not allow the installation of Web.js or Ethjs. If specific Javascript files can be downloaded to a Windows machine, and then uploaded to a Linux server, that is allowable. However, any libraries that require an npm installation are not allowed. The reason for this is that the final website must be easily uploaded to any server, without requiring a package installer.

Best Answer

var Web3 = require('web3')
if(typeof web3 !== "undefined"){
 var LocalWeb3 = new Web3(web3.currentProvider);
}

console.log(LocalWeb3);
Related Topic