How to use web3-eth-abi - 10 common examples

To help you get started, we’ve selected a few web3-eth-abi 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 RequestNetwork / Request_SmartContracts / test / synchrone / ethereum / requestEthereumCreateRequest.js View on Github external
var event = null;

	for (var i = 0; i < 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 = abiUtils.decodeParameters(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 kioskprotocol / contracts / test / standardmarket.js View on Github external
const decodeOrderResult = result => {
        // Exclude the indexed parameter "orderID"
        return ABI.decodeParameters(
            [
                { type: "address", name: "market" },
                { type: "bytes32", name: "nonceHash" },
                { type: "uint256[]", name: "DINs" },
                { type: "uint256[]", name: "quantities" }
            ],
            result.receipt.logs[0].data
        );
    };
github RequestNetwork / Request_SmartContracts / test / utils.js View on Github external
var event = null;

  for (var i = 0; i < 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.filter(function(input) {return !input.indexed;}).map(function(input) {return input.type;});
    var data = abiUtils.decodeParameters(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 clearmatics / ion / test / fluoride.js View on Github external
const overloadedTransferAbi = {
      "constant": false,
      "inputs": [
        { "name": "_to", "type": "address" },
        { "name": "_value", "type": "uint256" },
        { "name": "_data", "type": "bytes" }
      ],
      "name": "transfer",
      "outputs": [],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "function"
    }
    const transferMethodTransactionDataA = web3Abi
      .encodeFunctionCall( overloadedTransferAbi, [ fluorideAddrA,intAmountA, tradeId ]);
    const transferMethodTransactionDataB = web3Abi
      .encodeFunctionCall( overloadedTransferAbi, [ fluorideAddrB,intAmountB, tradeId ]);
    const receiptPayA = await web3.eth
      .sendTransaction({from: addrA, to: tokenA.address, data: transferMethodTransactionDataA, value: 0});
    const receiptPayB = await web3.eth
      .sendTransaction({from: addrB, to: tokenB.address, data: transferMethodTransactionDataB, value: 0});

    const tokenABalA1 = await tokenA.balanceOf(addrA)
    const tokenABalB1 = await tokenA.balanceOf(addrB)
    const tokenBBalA1 = await tokenB.balanceOf(addrA)
    const tokenBBalB1 = await tokenB.balanceOf(addrB)

    // UPDATE SODIUM (this should be done by event relay/lithium)
    const expectEventA = joinHex([fluorideAddrB/*, topic*/,tradeId])
    const expectEventB = joinHex([fluorideAddrA/*, topic*/,tradeId])

    const testDataA = [expectEventA,"2","3","4","5","6","7"]
github kioskprotocol / contracts / test / standardmarket.js View on Github external
before(async () => {
        market = await StandardMarket.deployed();
        registry = await DINRegistry.deployed();
        orders = await Orders.deployed();

        const registryUtils = await DINRegistryUtils.deployed();

        // Register 2 new DINs to the merchant
        const result = await registryUtils.selfRegisterDINs(2);
        const logs = result.receipt.logs;

        DIN1 = parseInt(ABI.decodeParameter("uint256", logs[0].topics[1]), 10);
        DIN2 = parseInt(ABI.decodeParameter("uint256", logs[1].topics[1]), 10);
    });
github aragon / aragon-cli / packages / toolkit / src / dao / encodeActCall.js View on Github external
export default (signature, params = []) => {
  const sigBytes = abi.encodeFunctionSignature(signature)

  const types = signature.replace(')', '').split('(')[1]

  // No params, return signature directly
  if (types === '') {
    return sigBytes
  }

  const paramBytes = abi.encodeParameters(types.split(','), params)

  return `${sigBytes}${paramBytes.slice(2)}`
}
github aragon / aragon-cli / packages / aragon-cli / src / commands / dao_cmds / act.js View on Github external
const encodeCalldata = (signature, params) => {
  const sigBytes = ABI.encodeFunctionSignature(signature)

  const types = signature.replace(')', '').split('(')[1]

  // No params, return signature directly
  if (types === '') {
    return sigBytes
  }

  const paramBytes = ABI.encodeParameters(types.split(','), params)

  return `${sigBytes}${paramBytes.slice(2)}`
}
github celo-org / celo-monorepo / packages / notification-service / src / blockscout / decode.ts View on Github external
function decodeTransferLog(log: Log): Transfer | null {
  if (!log || !log.topics || !log.data) {
    console.error('Invalid transfer log:', log)
    return null
  }

  /**
   * Decode using the parameter signture for an ERC20 Transfer event
   * For unknown reasons, blockscout includes an extra unknown param in the log's topics list
   * Including this unknown param in the input list or decoding won't work
   */
  try {
    // @ts-ignore
    const decodedLog: any = Web3EthAbi.default.decodeLog(
      [
        {
          indexed: true,
          name: 'unknown',
          type: 'address',
        },
        {
          indexed: true,
          name: 'from',
          type: 'address',
        },
        {
          indexed: true,
          name: 'to',
          type: 'address',
        },
github aragon / radspec / src / index.js View on Github external
function evaluate (source, call, { userHelpers = {}, ...options } = {}) {
  // Get method ID
  const methodId = call.transaction.data.substr(0, 10)

  // Find method ABI
  const method = call.abi.find((abi) =>
    abi.type === 'function' &&
    methodId === ABI.encodeFunctionSignature(abi))

  // Decode parameters
  const parameterValues = ABI.decodeParameters(
    method.inputs,
    '0x' + call.transaction.data.substr(10)
  )
  const parameters = method.inputs.reduce((parameters, input) =>
    Object.assign(
      parameters, {
        [input.name]: {
          type: input.type,
          value: parameterValues[input.name]
        }
      }
    ), {})

  const availableHelpers = { ...defaultHelpers, ...userHelpers }

  // Get additional options
github melonproject / protocol / src / contracts / fund / trading / calls / isOasisDexTakePermitted.ts View on Github external
//   makerQuantity.quantity,
  //   takerQuantity.quantity,
  // ).call();

  const policyManager = await getContract(
    environment,
    Contracts.PolicyManager,
    policyManagerAddress,
  );

  const exchangeAddress =
    environment.deployment.exchangeConfigs[Exchanges.MatchingMarket].exchange;

  const result = await policyManager.methods
    .preValidate(
      web3EthAbi.encodeFunctionSignature(FunctionSignatures.takeOrder),
      [
        '0x0000000000000000000000000000000000000000', // orderAddresses[0],
        tradingAddress.toString(), // orderAddresses[1],
        makerQuantity.token.address.toString(), // orderAddresses[2],
        takerQuantity.token.address.toString(), // orderAddresses[3],
        exchangeAddress.toString(), // exchanges[exchangeIndex].exchange
      ],
      [
        makerQuantity.quantity.toString(), // orderValues[0],
        takerQuantity.quantity.toString(), // orderValues[1],
        fillTakerTokenAmount.quantity.toString(), // orderValues[6]
      ],
      `0x${Number(id)
        .toString(16)
        .padStart(64, '0')}`, // identifier
    )