How to use the @requestnetwork/utils.amount function in @requestnetwork/utils

To help you get started, we’ve selected a few @requestnetwork/utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github RequestNetwork / requestNetwork / packages / advanced-logic / src / extensions / payment-network / declarative.ts View on Github external
function applyDeclareSentRefund(
  extensionState: ExtensionTypes.IState,
  extensionAction: ExtensionTypes.IAction,
  requestState: RequestLogicTypes.IRequest,
  actionSigner: IdentityTypes.IIdentity,
  timestamp: number,
): ExtensionTypes.IState {
  if (!requestState.payee) {
    throw Error(`The request must have a payee`);
  }
  if (!Utils.identity.areEqual(actionSigner, requestState.payee)) {
    throw Error(`The signer must be the payee`);
  }
  if (!Utils.amount.isValid(extensionAction.parameters.amount)) {
    throw Error(`The amount is not an payee`);
  }

  const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState);

  // increment sentRefundAmount
  copiedExtensionState.values.sentRefundAmount = Utils.amount.add(
    copiedExtensionState.values.sentRefundAmount,
    extensionAction.parameters.amount,
  );

  // update events
  copiedExtensionState.events.push({
    name: ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_SENT_REFUND,
    parameters: {
      amount: extensionAction.parameters.amount,
github RequestNetwork / requestNetwork / packages / advanced-logic / src / extensions / payment-network / declarative.ts View on Github external
timestamp: number,
): ExtensionTypes.IState {
  if (!requestState.payee) {
    throw Error(`The request must have a payee`);
  }
  if (!Utils.identity.areEqual(actionSigner, requestState.payee)) {
    throw Error(`The signer must be the payee`);
  }
  if (!Utils.amount.isValid(extensionAction.parameters.amount)) {
    throw Error(`The amount is not an integer`);
  }

  const copiedExtensionState: ExtensionTypes.IState = Utils.deepCopy(extensionState);

  // increment receivedPaymentAmount
  copiedExtensionState.values.receivedPaymentAmount = Utils.amount.add(
    copiedExtensionState.values.receivedPaymentAmount,
    extensionAction.parameters.amount,
  );

  // update events
  copiedExtensionState.events.push({
    name: ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_RECEIVED_PAYMENT,
    parameters: {
      amount: extensionAction.parameters.amount,
      note: extensionAction.parameters.note,
    },
    timestamp,
  });

  return copiedExtensionState;
}
github RequestNetwork / requestNetwork / packages / request-logic / src / actions / increaseExpectedAmount.ts View on Github external
}

  const signer: IdentityTypes.IIdentity = Action.getSignerIdentityFromAction(action);
  const signerRole = Request.getRoleInRequest(signer, request);

  // avoid to mutate the request
  let requestCopied: RequestLogicTypes.IRequest = Utils.deepCopy(request);
  requestCopied = Request.pushExtensionsData(requestCopied, action.data.parameters.extensionsData);
  requestCopied.events.push(generateEvent(action, timestamp, signer));

  if (signerRole === RequestLogicTypes.ROLE.PAYER) {
    if (request.state === RequestLogicTypes.STATE.CANCELED) {
      throw new Error('the request must not be canceled');
    }
    // increase the expected amount and store it as string
    requestCopied.expectedAmount = Utils.amount.add(
      request.expectedAmount,
      action.data.parameters.deltaAmount,
    );

    return requestCopied;
  }

  throw new Error('signer must be the payer');
}
github RequestNetwork / requestNetwork / packages / request-logic / src / actions / increaseExpectedAmount.ts View on Github external
function applyActionToRequest(
  action: RequestLogicTypes.IAction,
  timestamp: number,
  request: RequestLogicTypes.IRequest,
): RequestLogicTypes.IRequest {
  if (!action.data.parameters.requestId) {
    throw new Error('requestId must be given');
  }
  if (!request.payer) {
    throw new Error('the request must have a payer');
  }
  if (!action.data.parameters.deltaAmount) {
    throw new Error('deltaAmount must be given');
  }
  if (!Utils.amount.isValid(action.data.parameters.deltaAmount)) {
    throw new Error('deltaAmount must be a string representing a positive integer');
  }

  const signer: IdentityTypes.IIdentity = Action.getSignerIdentityFromAction(action);
  const signerRole = Request.getRoleInRequest(signer, request);

  // avoid to mutate the request
  let requestCopied: RequestLogicTypes.IRequest = Utils.deepCopy(request);
  requestCopied = Request.pushExtensionsData(requestCopied, action.data.parameters.extensionsData);
  requestCopied.events.push(generateEvent(action, timestamp, signer));

  if (signerRole === RequestLogicTypes.ROLE.PAYER) {
    if (request.state === RequestLogicTypes.STATE.CANCELED) {
      throw new Error('the request must not be canceled');
    }
    // increase the expected amount and store it as string
github RequestNetwork / requestNetwork / packages / request-logic / src / actions / reduceExpectedAmount.ts View on Github external
}

  const signer: IdentityTypes.IIdentity = Action.getSignerIdentityFromAction(action);
  const signerRole = Request.getRoleInRequest(signer, request);

  // avoid to mutate the request
  let requestCopied: RequestLogicTypes.IRequest = Utils.deepCopy(request);
  requestCopied = Request.pushExtensionsData(requestCopied, action.data.parameters.extensionsData);
  requestCopied.events.push(generateEvent(action, timestamp, signer));

  if (signerRole === RequestLogicTypes.ROLE.PAYEE) {
    if (request.state === RequestLogicTypes.STATE.CANCELED) {
      throw new Error('the request must not be canceled');
    }
    // reduce the expected amount and store it as string or throw if the result is not valid
    requestCopied.expectedAmount = Utils.amount.reduce(
      request.expectedAmount,
      action.data.parameters.deltaAmount,
    );

    return requestCopied;
  }

  throw new Error('signer must be the payee');
}
github RequestNetwork / requestNetwork / packages / request-logic / src / actions / reduceExpectedAmount.ts View on Github external
function applyActionToRequest(
  action: RequestLogicTypes.IAction,
  timestamp: number,
  request: RequestLogicTypes.IRequest,
): RequestLogicTypes.IRequest {
  if (!action.data.parameters.requestId) {
    throw new Error('requestId must be given');
  }
  if (!request.payee) {
    throw new Error('the request must have a payee');
  }
  if (!action.data.parameters.deltaAmount) {
    throw new Error('deltaAmount must be given');
  }
  if (!Utils.amount.isValid(action.data.parameters.deltaAmount)) {
    throw new Error('deltaAmount must be a string representing a positive integer');
  }

  const signer: IdentityTypes.IIdentity = Action.getSignerIdentityFromAction(action);
  const signerRole = Request.getRoleInRequest(signer, request);

  // avoid to mutate the request
  let requestCopied: RequestLogicTypes.IRequest = Utils.deepCopy(request);
  requestCopied = Request.pushExtensionsData(requestCopied, action.data.parameters.extensionsData);
  requestCopied.events.push(generateEvent(action, timestamp, signer));

  if (signerRole === RequestLogicTypes.ROLE.PAYEE) {
    if (request.state === RequestLogicTypes.STATE.CANCELED) {
      throw new Error('the request must not be canceled');
    }
    // reduce the expected amount and store it as string or throw if the result is not valid
github RequestNetwork / requestNetwork / packages / request-logic / src / request.ts View on Github external
throw Error('request.creator is missing');
  }

  if (!request.payee && !request.payer) {
    throw Error('request.payee and request.payer are missing');
  }
  if (request.creator.type !== IdentityTypes.TYPE.ETHEREUM_ADDRESS) {
    throw Error('request.creator.type not supported');
  }
  if (request.payee && request.payee.type !== IdentityTypes.TYPE.ETHEREUM_ADDRESS) {
    throw Error('request.payee.type not supported');
  }
  if (request.payer && request.payer.type !== IdentityTypes.TYPE.ETHEREUM_ADDRESS) {
    throw Error('request.payer.type not supported');
  }
  if (!Utils.amount.isValid(request.expectedAmount)) {
    throw Error('expectedAmount must be a positive integer');
  }
  return true;
}
github RequestNetwork / requestNetwork / packages / request-logic / src / actions / create.ts View on Github external
function format(
  requestParameters: RequestLogicTypes.ICreateParameters,
  signerIdentity: IdentityTypes.IIdentity,
  signatureProvider: SignatureProviderTypes.ISignatureProvider,
): Promise {
  if (!requestParameters.payee && !requestParameters.payer) {
    throw new Error('payee or PayerId must be given');
  }

  if (!Utils.amount.isValid(requestParameters.expectedAmount)) {
    throw new Error('expectedAmount must be a positive integer');
  }

  if (
    requestParameters.payee &&
    requestParameters.payee.type !== IdentityTypes.TYPE.ETHEREUM_ADDRESS
  ) {
    throw new Error('payee.type not supported');
  }

  if (
    requestParameters.payer &&
    requestParameters.payer.type !== IdentityTypes.TYPE.ETHEREUM_ADDRESS
  ) {
    throw new Error('payer.type not supported');
  }