@polkadot/types#u64 TypeScript Examples

The following examples show how to use @polkadot/types#u64. 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: GasSpent.ts    From gear-js with GNU General Public License v3.0 6 votes vote down vote up
/**
   * Get gas spent of hanle message
   * @param sourceId Account id
   * @param destinationId Program id
   * @param payload Payload of message
   * @param value Value of message
   * @param typeOfPayload One of the primitives types
   * @returns number in U64 format
   * @example
   * ```javascript
   * const code = fs.readFileSync('demo_ping.opt.wasm');
   * const gas = await gearApi.program.gasSpent.handle(
   *   '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d',
   *   '0xa178362715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d',
   *   'PING',
   *   0,
   *   'String'
   * );
   * console.log(gas.toHuman());
   * ```
   */
  async handle(
    sourceId: Hex,
    destinationId: Hex | Buffer,
    payload: PayloadType,
    value: number | string,
    typeOfPayload?: string,
  ): Promise<u64>;
Example #2
Source File: readState.ts    From gear-js with GNU General Public License v3.0 6 votes vote down vote up
export async function readState(
  wasmBytes: Buffer,
  initialSize: number,
  pages: IGearPages,
  inputValue?: Uint8Array,
  blockTimestamp?: Compact<u64>,
): Promise<Uint8Array> {
  const memory = new WebAssembly.Memory({ initial: initialSize });
  const module = await WebAssembly.instantiate(wasmBytes, importObj(memory, false, inputValue, blockTimestamp));
  Object.keys(pages).forEach((pageNumber: string) => {
    const start = +pageNumber * PAGE_SIZE;
    const end = start + PAGE_SIZE;
    const page = pages[pageNumber];
    for (let i = start; i < end; i++) {
      new Uint8Array(memory.buffer)[i] = page[i % PAGE_SIZE];
    }
  });
  const { exports } = module.instance;
  return exports?.meta_state ? new Uint8Array(getExportValue(memory, exports.meta_state)) : null;
}
Example #3
Source File: GasSpent.ts    From gear-js with GNU General Public License v3.0 6 votes vote down vote up
async reply(
    sourceId: Hex,
    messageId: Hex,
    exitCode: number,
    payload: PayloadType,
    value: number | string,
    metaOrTypeOfPayload?: string | Metadata,
  ): Promise<u64> {
    return await this.api.rpc['gear'].getReplyGasSpent(
      sourceId,
      messageId,
      exitCode,
      this.getPayload(payload, metaOrTypeOfPayload, 'async_handle_input'),
      value || 0,
    );
  }
Example #4
Source File: GasSpent.ts    From gear-js with GNU General Public License v3.0 6 votes vote down vote up
/**
   * Get gas spent of reply message
   * @param sourceId Account id
   * @param messageId Message id of a message waiting for response
   * @param exitCode Exit code of a message waiting for response
   * @param payload Payload of message
   * @param value Value of message
   * @param metaOrTypeOfPayload Metadata or one of the primitives types
   * @returns number in U64 format
   */
  async reply(
    sourceId: Hex,
    messageId: Hex,
    exitCode: number,
    payload: PayloadType,
    value: number | string,
    metaOrTypeOfPayload?: string | Metadata,
  ): Promise<u64>;
Example #5
Source File: GasSpent.ts    From gear-js with GNU General Public License v3.0 6 votes vote down vote up
/**
   * Get gas spent of reply message
   * @param sourceId Account id
   * @param messageId Message id of a message waiting for response
   * @param exitCode Exit code of a message waiting for response
   * @param payload Payload of message
   * @param value Value of message
   * @param typeOfPayload One of the primitives types
   * @returns number in U64 format
   * @example
   * ```javascript
   * const code = fs.readFileSync('demo_async.opt.wasm');
   * const meta = await getWasmMetadata(fs.readFileSync('demo_async.opt.wasm'));
   * const gas = await gearApi.program.gasSpent.reply(
   *   '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d',
   *   '0x518e6bc03d274aadb3454f566f634bc2b6aef9ae6faeb832c18ae8300fd72635',
   *   0,
   *   'PONG',
   *   1000,
   *   'String',
   * );
   * console.log(gas.toHuman());
   * ```
   */
  async reply(
    sourceId: Hex,
    messageId: Hex,
    exitCode: number,
    payload: PayloadType,
    value: number | string,
    typeOfPayload?: string,
  ): Promise<u64>;
Example #6
Source File: GasSpent.ts    From gear-js with GNU General Public License v3.0 6 votes vote down vote up
/**
   * Get gas spent of reply message
   * @param sourceId Account id
   * @param messageId Message id of a message waiting for response
   * @param exitCode Exit code of a message waiting for response
   * @param payload Payload of message
   * @param value Value of message
   * @param meta Metadata
   * @returns number in U64 format
   * @example
   * ```javascript
   * const code = fs.readFileSync('demo_async.opt.wasm');
   * const meta = await getWasmMetadata(fs.readFileSync('demo_async.opt.wasm'));
   * const gas = await gearApi.program.gasSpent.reply(
   *   '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d',
   *   '0x518e6bc03d274aadb3454f566f634bc2b6aef9ae6faeb832c18ae8300fd72635',
   *   0,
   *   'PONG',
   *   1000,
   *   meta,
   * );
   * console.log(gas.toHuman());
   * ```
   */
  async reply(
    sourceId: Hex,
    messageId: Hex,
    exitCode: number,
    payload: PayloadType,
    value: number | string,
    meta?: Metadata,
  ): Promise<u64>;
Example #7
Source File: GasSpent.ts    From gear-js with GNU General Public License v3.0 6 votes vote down vote up
async handle(
    sourceId: Hex,
    destinationId: Hex,
    payload: PayloadType,
    value: number | string,
    metaOrTypeOfPayload?: string | Metadata,
  ): Promise<u64> {
    return await this.api.rpc['gear'].getHandleGasSpent(
      sourceId,
      destinationId,
      this.getPayload(payload, metaOrTypeOfPayload, 'handle_input'),
      value || 0,
    );
  }
Example #8
Source File: GasSpent.ts    From gear-js with GNU General Public License v3.0 6 votes vote down vote up
/**
   * Get gas spent of hanle message
   * @param sourceId Account id
   * @param destinationId Program id
   * @param payload Payload of message
   * @param value Value of message
   * @param metaOrTypeOfPayload Metadata or one of the primitives types
   * @returns number in U64 format
   */
  async handle(
    sourceId: Hex,
    destinationId: Hex | Buffer,
    payload: PayloadType,
    value: number | string,
    metaOrTypeOfPayload?: string | Metadata,
  ): Promise<u64>;
Example #9
Source File: GasSpent.ts    From gear-js with GNU General Public License v3.0 6 votes vote down vote up
/**
   * Get gas spent of hanle message
   * @param sourceId Account id
   * @param destinationId Program id
   * @param payload Payload of message
   * @param value Value of message
   * @param meta Metadata
   * @returns number in U64 format
   * @example
   * ```javascript
   * const code = fs.readFileSync('demo_meta.opt.wasm');
   * const meta = await getWasmMetadata(fs.readFileSync('demo_meta.opt.wasm'));
   * const gas = await gearApi.program.gasSpent.handle(
   *   '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d',
   *   '0xa178362715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d',
   *    {
   *       id: {
   *         decimal: 64,
   *         hex: '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d',
   *       },
   *    },
   *   0,
   *   meta
   * );
   * console.log(gas.toHuman());
   * ```
   */
  async handle(
    sourceId: Hex,
    destinationId: Hex | Buffer,
    payload: PayloadType,
    value: number | string,
    meta?: Metadata,
  ): Promise<u64>;
Example #10
Source File: GasSpent.ts    From gear-js with GNU General Public License v3.0 6 votes vote down vote up
async init(
    sourceId: Hex,
    code: Hex | Buffer,
    payload: PayloadType,
    value: number | string,
    metaOrTypeOfPayload?: string | Metadata,
  ): Promise<u64> {
    return await this.api.rpc['gear'].getInitGasSpent(
      sourceId,
      isHex(code) ? code : this.createType.create('bytes', Array.from(code)).toHex(),
      this.getPayload(payload, metaOrTypeOfPayload, 'init_input'),
      value || 0,
    );
  }
Example #11
Source File: GasSpent.ts    From gear-js with GNU General Public License v3.0 6 votes vote down vote up
/**
   * Get gas spent of init message
   * @param sourceId Account id
   * @param code Program code
   * @param payload Payload of init message
   * @param value Value of message
   * @param metaOrTypeOfPayload Metadata or one of the primitives types
   * @returns number in U64 format
   */
  async init(
    sourceId: Hex,
    code: Hex | Buffer,
    payload: PayloadType,
    value: number | string,
    metaOrTypeOfPayload?: string | Metadata,
  ): Promise<u64>;
Example #12
Source File: GasSpent.ts    From gear-js with GNU General Public License v3.0 6 votes vote down vote up
/**
   * Get gas spent of init message
   * @param sourceId Account id
   * @param code Program code
   * @param payload Payload of init message
   * @param value Value of message
   * @param typeOfPayload One of the primitives types
   * @returns number in U64 format
   * @example
   * ```javascript
   * const code = fs.readFileSync('demo_ping.opt.wasm');
   * const gas = await gearApi.program.gasSpent.init(
   *   '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d',
   *   code,
   *   '0x00',
   *   0
   * );
   * console.log(gas.toHuman());
   * ```
   */
  async init(
    sourceId: Hex,
    code: Hex | Buffer,
    payload: PayloadType,
    value: number | string,
    typeOfPayload?: string,
  ): Promise<u64>;
Example #13
Source File: GasSpent.ts    From gear-js with GNU General Public License v3.0 6 votes vote down vote up
/**
   * Get gas spent of init message
   * @param sourceId Account id
   * @param code Program code
   * @param payload Payload of init message
   * @param value Value of message
   * @param meta Metadata
   * @returns number in U64 format
   * @example
   * ```javascript
   * const code = fs.readFileSync('demo_meta.opt.wasm');
   * const meta = await getWasmMetadata(fs.readFileSync('demo_meta.opt.wasm'));
   * const gas = await gearApi.program.gasSpent.init(
   *   '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d',
   *   code,
   *   {
   *     amount: 255,
   *     currency: 'GRT',
   *   },
   *   0,
   *   meta
   * );
   * console.log(gas.toHuman());
   * ```
   */
  async init(
    sourceId: Hex,
    code: Hex | Buffer,
    payload: PayloadType,
    value: number | string,
    meta?: Metadata,
  ): Promise<u64>;
Example #14
Source File: Blocks.ts    From gear-js with GNU General Public License v3.0 6 votes vote down vote up
/**
   * Get timestamp of block
   * @param hashOrNumber hash or number of particular block
   * @returns
   */
  async getBlockTimestamp(hashOrNumber: `0x${string}` | Uint8Array | number): Promise<Compact<u64>> {
    const block = await this.get(hashOrNumber);
    const tsAsU8a = block.block.extrinsics.find(
      (value) => value.method.method === 'set' && value.method.section === 'timestamp',
    ).data;
    const ts = CreateType.create('Compact<u64>', tsAsU8a);
    return ts as Compact<u64>;
  }
Example #15
Source File: utils.ts    From subsocial-js with GNU General Public License v3.0 6 votes vote down vote up
export class OptionId<T extends SubstrateId> extends Option<u64> {
  constructor (value?: T) {
    const textOrNull = value || new Null(registry)
    super(registry, 'u64', textOrNull)
  }
}
Example #16
Source File: api.ts    From community-repo with GNU General Public License v3.0 5 votes vote down vote up
getProposalThreadCount = async (
  api: ApiPromise
): Promise<number> =>
  Number((await api.query.proposalsDiscussion.threadCount()) as u64)
Example #17
Source File: MessageReply.ts    From gear-js with GNU General Public License v3.0 5 votes vote down vote up
/**
   * Sends reply message
   * @param message Message parameters
   * @param meta Metadata
   * @param messageType MessageType
   * @returns Submitted result
   * @example
   * ```javascript
   * const api = await GearApi.create()
   * const messageId = '0xd7540ae9da85e33b47276e2cb4efc2f0b58fef1227834f21ddc8c7cb551cced6'
   * api.reply.submit({
   *  replyToId: messageId,
   *  payload: 'Reply message',
   *  gasLimit: 20_000_000
   * }, undefiend, 'String')
   * api.reply.signAndSend(account, (events) => {
   *  events.forEach(({event}) => console.log(event.toHuman()))
   * })
   * ```
   */
  submit(
    message: {
      replyToId: H256 | string;
      payload: string | any;
      gasLimit: u64 | AnyNumber;
      value?: BalanceOf | AnyNumber;
    },
    meta?: Metadata,
    messageType?: string,
  ): SubmittableExtrinsic<'promise', ISubmittableResult> {
    let payload = createPayload(
      this.createType,
      messageType || meta?.async_handle_input || meta?.async_init_input,
      message.payload,
      meta,
    );

    try {
      this.submitted = this.api.tx.gear.sendReply(message.replyToId, payload, message.gasLimit, message.value);
      return this.submitted;
    } catch (error) {
      throw new SendReplyError();
    }
  }
Example #18
Source File: Program.ts    From gear-js with GNU General Public License v3.0 5 votes vote down vote up
/**
   * @param program Upload program data
   * @param meta Metadata
   * @returns ProgramId
   * @example
   * ```javascript
   * const code = fs.readFileSync('path/to/program.opt.wasm');
   * const meta = await getWasmMetadata(fs.readFileSync('path/to/program.meta.wasm'));
   * const api = await GearApi.create();
   * const { programId, salt, submitted } = api.program.submit({
   *   code,
   *   initPayload: {field: 'someValue'},
   *   gasLimit: 20_000_000,
   * }, meta)
   * api.program.signAndSend(account, (events) => {
   *   events.forEach(({event}) => console.log(event.toHuman()))
   * })
   * ```
   */
  submit(
    program: {
      code: Buffer | Uint8Array;
      salt?: `0x${string}`;
      initPayload?: string | any;
      gasLimit: u64 | AnyNumber;
      value?: BalanceOf | AnyNumber;
    },
    meta?: Metadata,
    messageType?: string,
  ): { programId: Hex; salt: Hex; submitted: SubmittableExtrinsic<'promise', ISubmittableResult> } {
    const salt = program.salt || randomAsHex(20);
    const code = this.createType.create('bytes', Array.from(program.code)) as Bytes;
    let payload = createPayload(this.createType, messageType || meta?.init_input, program.initPayload, meta);
    try {
      this.submitted = this.api.tx.gear.submitProgram(code, salt, payload, program.gasLimit, program.value || 0);
      const programId = generateProgramId(code, salt);
      return { programId, salt, submitted: this.submitted };
    } catch (error) {
      throw new SubmitProgramError();
    }
  }
Example #19
Source File: api.ts    From community-repo with GNU General Public License v3.0 5 votes vote down vote up
getProposalPostCount = async (api: ApiPromise): Promise<number> =>
  Number((await api.query.proposalsDiscussion.postCount()) as u64)
Example #20
Source File: transactionReceiptHelper.ts    From bodhi.js with Apache License 2.0 4 votes vote down vote up
getPartialTransactionReceipt = (event: FrameSystemEventRecord): PartialTransactionReceipt => {
  // @TODO
  const defaultValue = {
    logsBloom: DUMMY_LOGS_BLOOM,
    byzantium: false,
    // @TODO EIP712
    type: 0,
    cumulativeGasUsed: BIGNUMBER_ZERO,
    effectiveGasPrice: EFFECTIVE_GAS_PRICE
  };

  switch (event.event.method) {
    case 'Created': {
      const [source, evmAddress, logs, used_gas, _used_storage] = event.event.data as unknown as [
        H160,
        H160,
        EvmLog[],
        u64?,
        i32?
      ];

      return {
        to: undefined,
        from: source.toHex(),
        contractAddress: evmAddress.toString(),
        gasUsed: BigNumber.from(used_gas?.toString() || 0),
        status: 1,
        logs: getPartialLogs(logs),
        ...defaultValue
      };
    }
    case 'Executed': {
      const [source, evmAddress, logs, used_gas, _used_storage] = event.event.data as unknown as [
        H160,
        H160,
        EvmLog[],
        u64?,
        i32?
      ];

      return {
        to: evmAddress.toString(),
        from: source.toHex(),
        contractAddress: undefined,
        gasUsed: BigNumber.from(used_gas?.toString() || 0),
        logs: getPartialLogs(logs),
        status: 1,
        ...defaultValue
      };
    }
    case 'CreatedFailed': {
      const [source, evmAddress, _exitReason, logs, used_gas, _used_storage] = event.event.data as unknown as [
        H160,
        H160,
        ExitReason,
        EvmLog[],
        u64?,
        i32?
      ];

      return {
        to: undefined,
        from: source.toHex(),
        contractAddress: evmAddress.toString(),
        gasUsed: BigNumber.from(used_gas?.toString() || 0),
        logs: getPartialLogs(logs),
        status: 0,
        exitReason: _exitReason.toString(),
        ...defaultValue
      };
    }
    case 'ExecutedFailed': {
      const [source, evmAddress, _exitReason, _output, logs, used_gas, _used_storage] = event.event.data as unknown as [
        H160,
        H160,
        ExitReason,
        unknown,
        EvmLog[],
        u64?,
        i32?
      ];

      return {
        to: evmAddress.toString(),
        from: source.toHex(),
        contractAddress: undefined,
        gasUsed: BigNumber.from(used_gas?.toString() || 0),
        status: 0,
        exitReason: _exitReason.toString(),
        logs: getPartialLogs(logs),
        ...defaultValue
      };
    }
  }

  return logger.throwError(`unsupported event: ${event.event.method}`);
}