ethers/lib/utils#ParamType TypeScript Examples

The following examples show how to use ethers/lib/utils#ParamType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.ts    From nova with GNU Affero General Public License v3.0 6 votes vote down vote up
/** Returns a valid value for a param type. */
export function getValueForParamType(paramType: ParamType) {
  const baseType = paramType.baseType;

  if (baseType === "array") {
    return [];
  } else if (baseType === "tuple") {
    let obj = {};
    for (const subParam of paramType.components) {
      obj[subParam.name] = getValueForParamType(subParam);
    }
    return obj;
  } else if (baseType === "address") {
    return "0xFEEDFACECAFEBEEFFEEDFACECAFEBEEFFEEDFACE";
  } else if (baseType === "bool") {
    return true;
  } else if (baseType.includes("bytes")) {
    if (baseType === "bytes") {
      return "0x00000000";
    }
    const numberOfBytes = parseInt(baseType.replace("bytes", ""));
    return ethers.utils.hexZeroPad("0x00000000", numberOfBytes);
  } else if (baseType.includes("uint")) {
    if (baseType === "uint") {
      return 1e18;
    }
    const numberSize = parseInt(baseType.replace("uint", ""));
    return Math.min(100000000000, Math.floor(2 ** numberSize / 100));
  } else if (baseType.includes("int")) {
    if (baseType === "int") {
      return 1e18;
    }
    const numberSize = parseInt(baseType.replace("int", ""));
    return Math.min(100000000000, Math.floor(2 ** numberSize / 100));
  }
}
Example #2
Source File: multicall.ts    From defillama-sdk with GNU Affero General Public License v3.0 5 votes vote down vote up
async function executeCalls(
  contractCalls: {
    to: string;
    data: string;
  }[],
  chain: Chain,
  block?: number
) {
  if (await networkSupportsMulticall(chain)) {
    try {
      const multicallData = ethers.utils.defaultAbiCoder.encode(
        [
          ParamType.fromObject({
            components: [
              { name: "target", type: "address" },
              { name: "callData", type: "bytes" },
            ],
            name: "data",
            type: "tuple[]",
          }),
        ],
        [contractCalls.map((call) => [call.to, call.data])]
      );
      const address = await multicallAddressOrThrow(chain);

      const callData = AGGREGATE_SELECTOR + multicallData.substr(2);

      const tx = {
        to: address,
        data: callData,
      };

      const returnData = await call(getProvider(chain), tx, block ?? "latest", chain)

      const [blockNumber, returnValues] = ethers.utils.defaultAbiCoder.decode(
        ["uint256", "bytes[]"],
        returnData
      );
      return returnValues;
    } catch (e) {
      if (!process.env.DEFILLAMA_SDK_MUTED) {
        console.log("Multicall failed, defaulting to single transactions...");
      }
    }
  }
  const values = await Promise.all(
    contractCalls.map(async ({ to, data }) => {
      try {
        return await call(getProvider(chain),
          { to, data },
          block ?? "latest",
          chain,
        );
      } catch (e) {
        return null;
      }
    })
  );
  return values;
}