@ethersproject/abi#JsonFragmentType TypeScript Examples

The following examples show how to use @ethersproject/abi#JsonFragmentType. 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: abi.ts    From ethcall with MIT License 6 votes vote down vote up
static encode(
    name: string,
    jsonInputs: JsonFragmentType[],
    params: Params,
  ): string {
    const inputs = backfillParamNames(jsonInputs);
    const abi = [
      {
        type: 'function',
        name,
        inputs,
      },
    ];
    const coder = new Coder(abi);
    const valueMap = Object.fromEntries(
      inputs.map((input, index) => [input.name, params[index]]),
    );
    return coder.encodeFunction(name, valueMap);
  }
Example #2
Source File: abi.ts    From ethcall with MIT License 6 votes vote down vote up
static encodeConstructor(
    jsonInputs: JsonFragmentType[],
    params: Params,
  ): string {
    const inputs = backfillParamNames(jsonInputs);
    const abi = [
      {
        type: 'constructor',
        inputs,
      },
    ];
    const coder = new Coder(abi);
    const valueMap = Object.fromEntries(
      inputs.map((input, index) => [input.name, params[index]]),
    );
    return coder.encodeConstructor(valueMap);
  }
Example #3
Source File: abi.ts    From ethcall with MIT License 6 votes vote down vote up
static decode(
    name: string,
    jsonOutputs: JsonFragmentType[],
    data: string,
  ): Result {
    const outputs = backfillParamNames(jsonOutputs);
    const abi = [
      {
        type: 'function',
        name,
        outputs,
      },
    ];
    const coder = new Coder(abi);

    const functionOutput = coder.decodeFunctionOutput(name, data);
    return outputs.map((output) => functionOutput.values[output.name || '']);
  }
Example #4
Source File: abi.ts    From ethcall with MIT License 6 votes vote down vote up
// ABI doesn't enforce to specify param names
// However, abi-coder requires names to parse the params.
// Therefore, we "patch" the ABI by assigning a unique param names.
function backfillParamNames(
  jsonParams: JsonFragmentType[],
): JsonFragmentType[] {
  const names = new Set(...jsonParams.map((param) => param.name));
  return jsonParams.map((param) => {
    const { name: originalName, indexed, type, components } = param;
    const name = originalName ? originalName : generateUniqueName(names);
    names.add(name);
    return {
      name,
      indexed,
      type,
      components,
    };
  });
}