Giter Club home page Giter Club logo

Comments (6)

haidoan avatar haidoan commented on May 24, 2024 6

@nitika-goel I have successfully signed with web3-provider-engine and @0xproject/subproviders
here is snippet code for kovan infura(with out meta mask) , hope it helps
// import lib

var PrivateKeyWalletSubprovider = require('@0xproject/subproviders').PrivateKeyWalletSubprovider
var ProviderEngine = require('web3-provider-engine')
var RpcSubprovider = require('web3-provider-engine/subproviders/rpc.js')
var Web3Wrapper = require('@0xproject/web3-wrapper').Web3Wrapper

// usage

engine = new ProviderEngine()
engine.addProvider(new PrivateKeyWalletSubprovider(mainAccount.privateKey))
engine.addProvider(new RpcSubprovider({ rpcUrl: NET_WORK_INFURA }))
engine.start()
configs = {
networkId: 42
}
web3Wrapper = new Web3Wrapper(engine)
zeroEx = new ZeroEx(engine, configs)

now sign as normal.

from 0x-monorepo.

fabioberger avatar fabioberger commented on May 24, 2024

@nitika-goel, you cannot simply use new Web3.providers.HttpProvider(KOVAN_ENDPOINT) as your provider since the infura node does not have your Ethereum address on file. You need to use providerEngine to create a provider that forwards requests specific to your accounts to a signer you can use, and all other requests to the Infura node. An example of such a subprovider can be found here.

To get started though, I would use TestRPC since we have an article on how to get started with it.

from 0x-monorepo.

nitika-goel avatar nitika-goel commented on May 24, 2024

Thanks @fabioberger . I did try using the providerengine but somehow the code breaks whenever I access any of the ZeroEx methods.

I am successfully able to create an order and sign it through metamask but I do not intend to do it that way as I need the entire process to be automated without the signer popup. Also, I need to integrate with this with a project running on Kovan and hence do not intend to use testrpc.

Following is the code I am using. It breaks after printing the nulladdress. I might be missing something really small. Could you please help me with it.

const Web3 = require('web3');
const Accounts=require('web3-eth-accounts');
const ZeroEx = require('0x.js').ZeroEx;
const BigNumber = require('bignumber.js');
const lightwallet= require('eth-lightwallet');
const Transaction = require('ethereumjs-tx');
const ProviderEngine = require("web3-provider-engine");
const Web3Subprovider = require("web3-provider-engine/subproviders/web3.js");
const HookedWalletProvider = require('web3-provider-engine/subproviders/hooked-wallet.js');
const CacheSubprovider = require('web3-provider-engine/subproviders/cache.js');
const FixtureSubprovider = require('web3-provider-engine/subproviders/fixture.js');
const FilterSubprovider = require('web3-provider-engine/subproviders/filters.js');
const VmSubprovider = require('web3-provider-engine/subproviders/vm.js');
const HookedWalletSubprovider = require("hooked-web3-provider");
const NonceSubprovider = require('web3-provider-engine/subproviders/nonce-tracker.js');
const RpcSubprovider = require('web3-provider-engine/subproviders/rpc.js');
const injectMetrics = require('web3-provider-engine/test/util/inject-metrics');
const KOVAN_ENDPOINT='https://kovan.infura.io/';

(async () => {
	var engine=new ProviderEngine();
    var web3=new Web3(engine);
//    console.log(logAcc.address);
     engine.addProvider(new FixtureSubprovider({
                        web3_clientVersion: 'ProviderEngine/v0.0.0/javascript',
                        net_listening: true,
                        eth_hashrate: '0x00',
                        eth_mining: false,
                        eth_syncing: true
                    }));
      engine.addProvider(new CacheSubprovider());

                            // filters
                            engine.addProvider(new FilterSubprovider());

                            // pending nonce
                            engine.addProvider(new NonceSubprovider());

                            // vm
                            engine.addProvider(new VmSubprovider());

      
	 var providerA = injectMetrics(new HookedWalletProvider({
	    getAccounts: function(cb){
	      cb(null, ["0x7f4d8f26d33FDF092f713979FEc2753F9f14C331"])
	    },
	    signTransaction: function(txParams, cb){
	      var tx = new Transaction(txParams)
	      tx.sign(privateKey)
	      var rawTx = '0x'+tx.serialize().toString('hex')
	      cb(null, rawTx)
	    },
	  }));
 	engine.addProvider(providerA);
 

    
        engine.addProvider(new RpcSubprovider({
          rpcUrl: 'https://kovan.infura.io/',
        }))
       
                           

        // log new blocks
        engine.on('block', function(block){
          console.log('================================')
          console.log('BLOCK CHANGED:', '#'+block.number.toString('hex'), '0x'+block.hash.toString('hex'))
          console.log('================================')
        })

        // network connectivity error
        engine.on('error', function(err){
          // report connectivity errors
          console.error(err.stack)
        })

        // start polling for blocks
        engine.start();
  
   
    var zeroEx = new ZeroEx(web3.currentProvider);
	// Number of decimals to use (for ETH and ZRX)
	const DECIMALS = 18; 
	// Addresses
	const NULL_ADDRESS = ZeroEx.NULL_ADDRESS;  
	console.log("Null address is "+NULL_ADDRESS); 
    
   
	const WETH_ADDRESS = await zeroEx.etherToken.getContractAddressAsync();  
	console.log("ETH token address is "+ WETH_ADDRESS);    // The wrapped ETH token contract
	const ZRX_ADDRESS  = await zeroEx.exchange.getZRXTokenAddressAsync(); 
	console.log("ZeroEx Address is "+ ZRX_ADDRESS)       // The ZRX token contract
	const EXCHANGE_ADDRESS   = await zeroEx.exchange.getContractAddressAsync();  // The Exchange.sol address (0x exchange smart contract)
	console.log("Exchange address is "+ EXCHANGE_ADDRESS);
   	
 //    console.log("WEB get accounts " +web3.eth.getAccounts());
	// // list of accounts
	 var account =  await zeroEx.getAvailableAddressesAsync();
	 console.log(account);
  

})().catch(console.log);

from 0x-monorepo.

LogvinovLeon avatar LogvinovLeon commented on May 24, 2024

What's the error message?

from 0x-monorepo.

nitika-goel avatar nitika-goel commented on May 24, 2024

There is no error message. It just breaks the execution.

from 0x-monorepo.

fabioberger avatar fabioberger commented on May 24, 2024

Breaks or hangs? If it is hanging, that is because of web3/web3.js#844. The solution is to import 0x.js and anything else that has web3 as a dependency after importing all ProviderEngine packages.

from 0x-monorepo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.