@ethersproject/abi#Fragment TypeScript Examples

The following examples show how to use @ethersproject/abi#Fragment. 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: contract.ts    From ethers-multicall with MIT License 6 votes vote down vote up
constructor(address: string, abi: JsonFragment[] | string[] | Fragment[]) {
    this._address = address;

    this._abi = toFragment(abi);

    this._functions = this._abi.filter(x => x.type === 'function').map(x => FunctionFragment.from(x));
    const callFunctions = this._functions.filter(x => x.stateMutability === 'pure' || x.stateMutability === 'view');

    for (const callFunction of callFunctions) {
      const { name } = callFunction;
      const getCall = makeCallFunction(this, name);
      if (!this[name]) {
        defineReadOnly(this, name, getCall);
      }
    }
  }
Example #2
Source File: index.ts    From ccip-read with MIT License 6 votes vote down vote up
/**
   * Adds an interface to the gateway server, with handlers to handle some or all of its functions.
   * @param abi The contract ABI to use. This can be in any format that ethers.js recognises, including
   *        a 'Human Readable ABI', a JSON-format ABI, or an Ethers `Interface` object.
   * @param handlers An array of handlers to register against this interface.
   */
  add(abi: string | readonly (string | Fragment | JsonFragment)[] | Interface, handlers: Array<HandlerDescription>) {
    const abiInterface = toInterface(abi);

    for (const handler of handlers) {
      const fn = abiInterface.getFunction(handler.type);

      this.handlers[Interface.getSighash(fn)] = {
        type: fn,
        func: handler.func,
      };
    }
  }
Example #3
Source File: abi.ts    From snapshot-plugins with MIT License 6 votes vote down vote up
export function getABIWriteFunctions(abi: Fragment[]) {
  const abiInterface = new Interface(abi);
  return (
    abiInterface.fragments
      // Return only contract's functions
      .filter(FunctionFragment.isFunctionFragment)
      .map(FunctionFragment.fromObject)
      // Return only write functions
      .filter(isWriteFunction)
      // Sort by name
      .sort((a, b) => (a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1))
  );
}
Example #4
Source File: decoder.ts    From snapshot-plugins with MIT License 6 votes vote down vote up
public decodeFunction(
    data: string,
    fragmentOrName?: string | Fragment | JsonFragment
  ) {
    const fragment = this.getMethodFragment(data, fragmentOrName);
    if (!FunctionFragment.isFunctionFragment(fragment)) {
      throw new Error(
        `could not resolved to a function fragment fragmentOrName: ${fragmentOrName}`
      );
    }
    const functionFragment = FunctionFragment.fromObject(fragment);
    const decodedValues = this.decodeFunctionData(functionFragment.name, data);

    return functionFragment.inputs.reduce((acc, parameter, index) => {
      const value = decodedValues[index];
      const formattedValue = this.formatParameter(parameter, value);
      acc.push(formattedValue);
      if (parameter.name) {
        acc[parameter.name] = formattedValue;
      }
      return acc;
    }, [] as string[]);
  }
Example #5
Source File: decoder.ts    From snapshot-plugins with MIT License 6 votes vote down vote up
public getMethodFragment(
    data: string,
    fragmentOrName?: string | Fragment | JsonFragment
  ): Fragment | JsonFragment {
    if (typeof fragmentOrName === 'string') {
      return this.getFunction(fragmentOrName);
    } else if (!fragmentOrName) {
      const signature = data.substr(0, 10);
      return this.getFunction(signature);
    }
    return fragmentOrName;
  }
Example #6
Source File: utils.ts    From hardhat-deploy with MIT License 6 votes vote down vote up
export function mergeABIs(
  abis: any[][],
  options: {check: boolean; skipSupportsInterface: boolean}
): any[] {
  if (abis.length === 0) {
    return [];
  }
  const result: any[] = JSON.parse(JSON.stringify(abis[0]));

  for (let i = 1; i < abis.length; i++) {
    const abi = abis[i];
    for (const fragment of abi) {
      const newEthersFragment = Fragment.from(fragment);
      // TODO constructor special handling ?
      const foundSameSig = result.find((v) => {
        const existingEthersFragment = Fragment.from(v);
        if (v.type !== fragment.type) {
          return false;
        }
        if (!existingEthersFragment) {
          return v.name === fragment.name; // TODO fallback and receive hanlding
        }

        if (
          existingEthersFragment.type === 'constructor' ||
          newEthersFragment.type === 'constructor'
        ) {
          return existingEthersFragment.name === newEthersFragment.name;
        }

        if (newEthersFragment.type === 'function') {
          return (
            Interface.getSighash(existingEthersFragment as FunctionFragment) ===
            Interface.getSighash(newEthersFragment as FunctionFragment)
          );
        } else if (newEthersFragment.type === 'event') {
          return existingEthersFragment.format() === newEthersFragment.format();
        } else {
          return v.name === fragment.name; // TODO fallback and receive hanlding
        }
      });
      if (foundSameSig) {
        if (
          options.check &&
          !(
            options.skipSupportsInterface &&
            fragment.name === 'supportsInterface'
          )
        ) {
          if (fragment.type === 'function') {
            throw new Error(
              `function "${fragment.name}" will shadow "${foundSameSig.name}". Please update code to avoid conflict.`
            );
          }
        }
      } else {
        result.push(fragment);
      }
    }
  }

  return result;
}
Example #7
Source File: contract.ts    From ethers-multicall with MIT License 5 votes vote down vote up
private _abi: Fragment[];
Example #8
Source File: contract.ts    From ethers-multicall with MIT License 5 votes vote down vote up
function toFragment(abi: JsonFragment[] | string[] | Fragment[]): Fragment[] {
  return abi.map((item: JsonFragment | string | Fragment) => Fragment.from(item));
}
Example #9
Source File: initialization-check.ts    From perpetual-protocol with GNU General Public License v3.0 5 votes vote down vote up
function instance(
    address: string,
    abi: Array<string | Fragment | JsonFragment>,
    provider: ethers.providers.BaseProvider,
): Contract {
    return new ethers.Contract(address, abi, provider) as Contract
}
Example #10
Source File: status-check.ts    From perpetual-protocol with GNU General Public License v3.0 5 votes vote down vote up
// string | Array<Fragment | JsonFragment | string> | Interface;
function instance(address: string, abi: Array<string | Fragment | JsonFragment>): Contract {
    return new ethers.Contract(address, abi, provider) as Contract
}
Example #11
Source File: index.ts    From ccip-read with MIT License 5 votes vote down vote up
function toInterface(abi: string | readonly (string | Fragment | JsonFragment)[] | Interface) {
  if (Interface.isInterface(abi)) {
    return abi;
  }
  return new Interface(abi);
}