How to use web3 - 10 common examples

To help you get started, we’ve selected a few web3 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dharmaprotocol / dharma.js / __test__ / integration / adapters / collateralized_simple_interest_loan_adapter / collateralized_simple_interest_loan_adapter.spec.ts View on Github external
import { SUCCESSFUL_RETURN_COLLATERAL_SCENARIOS } from "./scenarios/successful_return_collateral_scenarios";

import { SUCCESSFUL_SEIZE_COLLATERAL_SCENARIOS } from "./scenarios/successful_seize_collateral_scenarios";

import { UNSUCCESSFUL_SEIZE_COLLATERAL_SCENARIOS } from "./scenarios/unsuccessful_seize_collateral_scenarios";

// Runners
import { ReturnCollateralRunner, SeizeCollateralRunner } from "./runners";

import { CollateralizedSimpleInterestLoanAdapter } from "src/adapters/collateralized_simple_interest_loan_adapter";

import { ServicingAPI } from "src/apis/servicing_api";

import { TokenAPI } from "../../../../src/apis/token_api";

const provider = new Web3.providers.HttpProvider("http://localhost:8545");
const web3 = new Web3(provider);

// Given that this is an integration test, we unmock the Dharma
// smart contracts artifacts package to pull the most recently
// deployed contracts on the current network.
jest.unmock("@dharmaprotocol/contracts");

describe("Collateralized Simple Interest Loan Adapter (Integration Tests)", () => {
    const contractsApi = new ContractsAPI(web3);

    const adaptersApi = new AdaptersAPI(web3, contractsApi);

    const servicingApi = new ServicingAPI(web3, contractsApi);

    const adapter = new CollateralizedSimpleInterestLoanAdapter(web3, contractsApi);
github lexansoft / etherid.org / test / index.html View on Github external
"This page URL has this ipfs HASH: <b>" + hash + "<b>"
        }
        else
        {
            document.getElementById( "url_hash" ).innerHTML = 
                "This page was not open through IPFS"
        }
            
        try {

            /* first of all set up the web3 object. THis should work in Mist as well */
            if(typeof web3 === 'undefined')
                web3 = require('web3');     

            if( web3.currentProvider == null )
                web3.setProvider( new web3.providers.HttpProvider( ) );                

            /* Get the EtherID API object */
            EID = require( "etherid" )

            BCH_HASH = EID.getHash( web3, "test", "ipfs" )
            
            document.getElementById( "bch_hash" ).innerHTML = 
                "The proper hash for the page sould be: <b>" + BCH_HASH + "<b>"            
            
            if( hash == BCH_HASH )
            {
                document.getElementById( "action" ).innerHTML = 
                    "You see the CORRECT version of this website!" 
                document.getElementById( "action" ).style.color = "green"
            }
            else</b></b></b></b>
github RequestNetwork / Request_SmartContracts / test / requestEthereumAccept.js View on Github external
var event = null;

	for (var i = 0; i &lt; abi.length; i++) {
	  var item = abi[i];
	  if (item.type != "event") continue;
	  var signature = item.name + "(" + item.inputs.map(function(input) {return input.type;}).join(",") + ")";
	  var hash = web3.sha3(signature);
	  if (hash == log.topics[0]) {
	    event = item;
	    break;
	  }
	}

	if (event != null) {
	  var inputs = event.inputs.map(function(input) {return input.type;});
	  var data = SolidityCoder.decodeParams(inputs, log.data.replace("0x", ""));
	  // Do something with the data. Depends on the log and what you're using the data for.
	  return {name:event.name , data:data};
	}
	return null;
}
github RequestNetwork / RequestVesting / test / withdrawVesting.js View on Github external
var event = null;

	for (var i = 0; i &lt; abi.length; i++) {
	  var item = abi[i];
	  if (item.type != "event") continue;
	  var signature = item.name + "(" + item.inputs.map(function(input) {return input.type;}).join(",") + ")";
	  var hash = web3.sha3(signature);
	  if (hash == log.topics[0]) {
	    event = item;
	    break;
	  }
	}

	if (event != null) {
	  var inputs = event.inputs.map(function(input) {return input.type;});
	  var data = SolidityCoder.decodeParams(inputs, log.data.replace("0x", ""));
	  // Do something with the data. Depends on the log and what you're using the data for.
	  return {name:event.name , data:data};
	}
	return null;
}
github RequestNetwork / Request_SmartContracts / test / requestEthereumDecline.js View on Github external
var event = null;

	for (var i = 0; i &lt; abi.length; i++) {
	  var item = abi[i];
	  if (item.type != "event") continue;
	  var signature = item.name + "(" + item.inputs.map(function(input) {return input.type;}).join(",") + ")";
	  var hash = web3.sha3(signature);
	  if (hash == log.topics[0]) {
	    event = item;
	    break;
	  }
	}

	if (event != null) {
	  var inputs = event.inputs.map(function(input) {return input.type;});
	  var data = SolidityCoder.decodeParams(inputs, log.data.replace("0x", ""));
	  // Do something with the data. Depends on the log and what you're using the data for.
	  return {name:event.name , data:data};
	}
	return null;
}
github RequestNetwork / Request_SmartContracts / test / requestEthereumCancel.js View on Github external
var event = null;

	for (var i = 0; i &lt; abi.length; i++) {
	  var item = abi[i];
	  if (item.type != "event") continue;
	  var signature = item.name + "(" + item.inputs.map(function(input) {return input.type;}).join(",") + ")";
	  var hash = web3.sha3(signature);
	  if (hash == log.topics[0]) {
	    event = item;
	    break;
	  }
	}

	if (event != null) {
	  var inputs = event.inputs.map(function(input) {return input.type;});
	  var data = SolidityCoder.decodeParams(inputs, log.data.replace("0x", ""));
	  // Do something with the data. Depends on the log and what you're using the data for.
	  return {name:event.name , data:data};
	}
	return null;
}
github RequestNetwork / Request_SmartContracts / test / requestEthereumWithdraw.js View on Github external
var event = null;

	for (var i = 0; i &lt; abi.length; i++) {
	  var item = abi[i];
	  if (item.type != "event") continue;
	  var signature = item.name + "(" + item.inputs.map(function(input) {return input.type;}).join(",") + ")";
	  var hash = web3.sha3(signature);
	  if (hash == log.topics[0]) {
	    event = item;
	    break;
	  }
	}

	if (event != null) {
	  var inputs = event.inputs.map(function(input) {return input.type;});
	  var data = SolidityCoder.decodeParams(inputs, log.data.replace("0x", ""));
	  // Do something with the data. Depends on the log and what you're using the data for.
	  return {name:event.name , data:data};
	}
	return null;
}
github ConsenSys / reflux-tx / test / utils / mocketh.js View on Github external
var mocketh = function(chainName, blockTime) {
	this.chainId = 0;
	this.blockNumber = 0;

	try {
		this.chain = chains[chainName];
	} catch (e) {
		throw new Error('Could not find chain ' + chainName + ' in chain.json spec file');
	}

	this.chainHash = web3.sha3(JSON.stringify(this.chain));
	this.blockTime = blockTime;
	if (!blockTime) this.blockTime = 500;

	this.running = true;
	this.block = this.Block(this.chainId, this.blockNumber);
	this.blockHashMap = {};
	this.blockCallbacks = [];
	this.blocks = {};
	this.blocks[this.block.hash] = this.block;
	this.filterCallback = null;

	this.getTransactionReceipt.request = this.request('getTransactionReceipt');
	this.getTransaction.request = this.request('getTransaction');

	// Fake the chain here
	this.blockInterval = setInterval(this.incChain.bind(this), this.blockTime);
github ethers-io / ethers.js / tests / make-tests / make-contract-events.js View on Github external
if (baseType.substring(0, 5) === 'bytes') {
                                    return input;
                                }

                                if (typeof(input) === 'boolean') {
                                    return input;
                                }

                                return web3.toBigNumber(input);
                            }

                            var web3Value = dump(param.value);

                            // The web3 coder has lots of bugs, but it does fine as long as there
                            // is only one type and nothing is dynamic
                            var encoded = web3Coder.encodeParams([ compute ], [ web3Value ]);
                            normalizedValues.push(keccak256(Buffer.from(encoded, 'hex')));

                        } else {
                            throw new Error('unknown hashed type');
                        }
                    } else {
                        hashed.push(false);
                        normalizedValues.push(param.value);
                    }
                    values.push(param.value);
                });