@ethersproject/abi#JsonFragment TypeScript Examples

The following examples show how to use @ethersproject/abi#JsonFragment. 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: call.ts    From ethcall with MIT License 6 votes vote down vote up
async function callDeployless2(
  provider: BaseProvider,
  callRequests: CallRequest[],
  block?: BlockTag,
): Promise<Result> {
  const inputAbi: JsonFragment[] = deploylessMulticall2Abi;
  const constructor = inputAbi.find((f) => f.type === 'constructor');
  const inputs = constructor?.inputs || [];
  const args = Abi.encodeConstructor(inputs, [false, callRequests]);
  const data = hexConcat([deploylessMulticall2Bytecode, args]);
  const callData = await provider.call(
    {
      data,
    },
    block,
  );
  const outputAbi: JsonFragment[] = multicall2Abi;
  const outputFunc = outputAbi.find(
    (f) => f.type === 'function' && f.name === 'tryAggregate',
  );
  const name = outputFunc?.name || '';
  const outputs = outputFunc?.outputs || [];
  // Note "[0]": low-level calls don't automatically unwrap tuple output
  const response = Abi.decode(name, outputs, callData)[0];
  return response as CallResult[];
}
Example #2
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 #3
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 #4
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 #5
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 #6
Source File: call.ts    From ethcall with MIT License 6 votes vote down vote up
async function callDeployless(
  provider: BaseProvider,
  callRequests: CallRequest[],
  block?: BlockTag,
): Promise<Result> {
  const inputAbi: JsonFragment[] = deploylessMulticallAbi;
  const constructor = inputAbi.find((f) => f.type === 'constructor');
  const inputs = constructor?.inputs || [];
  const args = Abi.encodeConstructor(inputs, [callRequests]);
  const data = hexConcat([deploylessMulticallBytecode, args]);
  const callData = await provider.call(
    {
      data,
    },
    block,
  );
  const outputAbi: JsonFragment[] = multicallAbi;
  const outputFunc = outputAbi.find(
    (f) => f.type === 'function' && f.name === 'aggregate',
  );
  const name = outputFunc?.name || '';
  const outputs = outputFunc?.outputs || [];
  const response = Abi.decode(name, outputs, callData);
  return response;
}
Example #7
Source File: call.ts    From ethcall with MIT License 6 votes vote down vote up
async function callDeployless3(
  provider: BaseProvider,
  callRequests: CallRequest[],
  block?: BlockTag,
): Promise<Result> {
  const inputAbi: JsonFragment[] = deploylessMulticall3Abi;
  const constructor = inputAbi.find((f) => f.type === 'constructor');
  const inputs = constructor?.inputs || [];
  const args = Abi.encodeConstructor(inputs, [callRequests]);
  const data = hexConcat([deploylessMulticall3Bytecode, args]);
  const callData = await provider.call(
    {
      data,
    },
    block,
  );
  const outputAbi: JsonFragment[] = multicall3Abi;
  const outputFunc = outputAbi.find(
    (f) => f.type === 'function' && f.name === 'aggregate3',
  );
  const name = outputFunc?.name || '';
  const outputs = outputFunc?.outputs || [];
  // Note "[0]": low-level calls don't automatically unwrap tuple output
  const response = Abi.decode(name, outputs, callData)[0];
  return response as CallResult[];
}
Example #8
Source File: contract.ts    From ethcall with MIT License 6 votes vote down vote up
/**
   * Create a contract.
   * @param address Address of the contract
   * @param abi ABI of the contract
   */
  constructor(address: string, abi: JsonFragment[]) {
    this.address = address;
    this.abi = abi;

    this.functions = abi.filter((x) => x.type === 'function');
    const callFunctions = this.functions.filter(
      (x) => x.stateMutability === 'pure' || x.stateMutability === 'view',
    );

    for (const callFunction of callFunctions) {
      const name = callFunction.name;
      if (!name) {
        continue;
      }
      const getCall = makeCallFunction(this, name);
      if (!this[name]) {
        Object.defineProperty(this, name, {
          enumerable: true,
          value: getCall,
          writable: false,
        });
      }
    }
  }
Example #9
Source File: uniswap-contract-context-v2.ts    From simple-uniswap-sdk with MIT License 5 votes vote down vote up
/**
   * Uniswap v2 factory
   */
  public static factoryAbi: JsonFragment[] = require('../ABI/uniswap-factory-v2.json');
Example #10
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);
}
Example #11
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 #12
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 #13
Source File: uniswap-contract-context-v3.ts    From simple-uniswap-sdk with MIT License 5 votes vote down vote up
/**
   * Uniswap quoter
   */
  public static quoterAbi: JsonFragment[] = require('../ABI/uniswap-quoter-v3.json');
Example #14
Source File: uniswap-contract-context-v3.ts    From simple-uniswap-sdk with MIT License 5 votes vote down vote up
/**
   * Uniswap factory
   */
  public static factoryAbi: JsonFragment[] = require('../ABI/uniswap-factory-v3.json');
Example #15
Source File: uniswap-contract-context-v3.ts    From simple-uniswap-sdk with MIT License 5 votes vote down vote up
/**
   * Uniswap router
   */
  public static routerAbi: JsonFragment[] = require('../ABI/uniswap-router-v3.json');
Example #16
Source File: uniswap-contract-context-v2.ts    From simple-uniswap-sdk with MIT License 5 votes vote down vote up
/**
   * Uniswap v2 pair
   */
  public static pairAbi: JsonFragment[] = require('../ABI/uniswap-pair-v2.json');
Example #17
Source File: contract.ts    From ethcall with MIT License 5 votes vote down vote up
functions: JsonFragment[];
Example #18
Source File: uniswap-contract-context-v2.ts    From simple-uniswap-sdk with MIT License 5 votes vote down vote up
/**
   * Uniswap v2 router
   */
  public static routerAbi: JsonFragment[] = require('../ABI/uniswap-router-v2.json');
Example #19
Source File: contract-context.ts    From simple-uniswap-sdk with MIT License 5 votes vote down vote up
/**
   * ERC20 abi
   */
  public static erc20Abi: JsonFragment[] = require('../ABI/erc-20-abi.json');
Example #20
Source File: contract-context.ts    From simple-pancakeswap-sdk with MIT License 5 votes vote down vote up
/**
   * ERC20 abi
   */
  public static erc20Abi: JsonFragment[] = require('../ABI/erc-20-abi.json');
Example #21
Source File: contract-context.ts    From simple-pancakeswap-sdk with MIT License 5 votes vote down vote up
/**
   * PancakeSwap v2 pair
   */
  public static pairAbi: JsonFragment[] = require('../ABI/pancakeswap-pair-v2.json');
Example #22
Source File: contract-context.ts    From simple-pancakeswap-sdk with MIT License 5 votes vote down vote up
/**
   * PancakeSwap v2 factory
   */
  public static factoryAbi: JsonFragment[] = require('../ABI/pancakeswap-factory-v2.json');
Example #23
Source File: contract-context.ts    From simple-pancakeswap-sdk with MIT License 5 votes vote down vote up
/**
   * PancakeSwap v2 router
   */
  public static routerAbi: JsonFragment[] = require('../ABI/pancakeswap-router-v2.json');
Example #24
Source File: diamond.ts    From eth with GNU General Public License v3.0 5 votes vote down vote up
// Turns an abiElement into a signature string, like `"init(bytes4)"`
export function toSignature(abiElement: unknown): string {
  return utils.Fragment.fromObject(abiElement as JsonFragment).format();
}
Example #25
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 #26
Source File: contract.ts    From ethcall with MIT License 5 votes vote down vote up
abi: JsonFragment[];