Skip to content

Commit

Permalink
feat(sinpe): Finished UI for sinpe swap
Browse files Browse the repository at this point in the history
  • Loading branch information
onmax committed Aug 7, 2024
1 parent eefcb03 commit daf8d9c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 64 deletions.
2 changes: 1 addition & 1 deletion client/src/PublicRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ export type SignSwapRequestCommon = SimpleRequest & {
& {
amount: number,
fee: number,
recipientLabel?: string,
senderLabel?: string,
}
),
redeem: (
Expand Down
1 change: 1 addition & 0 deletions src/assets/icons/sinpe-movil.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 4 additions & 8 deletions src/request/sign-swap/SignSwap.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,8 @@ class SignSwap {
$leftIdenticon.innerHTML = TemplateTags.hasVars(0)`<img src="../../assets/icons/bank.svg"></img>`;
$leftLabel.textContent = request.fund.bankLabel || I18n.translatePhrase('sign-swap-your-bank');
} else if (request.fund.type === 'CRC') {
$leftIdenticon.innerHTML = 'TODO';
// TODO Translation
$leftLabel.textContent = request.fund.sinpeLabel || I18n.translatePhrase('sign-swap-your-bank');
$leftIdenticon.innerHTML = TemplateTags.hasVars(0)`<img src="../../assets/icons/sinpe-movil.svg"></img>`;
$leftLabel.textContent = request.fund.senderLabel || 'Sinpe Móvil';
}

if (request.redeem.type === 'NIM') {
Expand All @@ -237,11 +236,8 @@ class SignSwap {

$rightLabel.textContent = label;
} else if (request.redeem.type === 'CRC') {
$rightIdenticon.innerHTML = 'TODO';

// TODO Translation
const label = request.redeem.recipientLabel || I18n.translatePhrase('sign-swap-your-bank');
$rightLabel.textContent = label;
$rightIdenticon.innerHTML = TemplateTags.hasVars(0)`<img src="../../assets/icons/sinpe-movil.svg"></img>`;
$rightLabel.textContent = request.redeem.recipientlabel || 'Sinpe Móvil';
}
}

Expand Down
135 changes: 83 additions & 52 deletions src/request/sign-swap/SignSwapApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class SignSwapApi extends PolygonRequestParserMixin(BitcoinRequestParserMixin(To
type: 'CRC',
amount: this.parsePositiveInteger(request.fund.amount, false, 'fund.amount'),
fee: this.parsePositiveInteger(request.fund.fee, true, 'fund.fee'),
sinpeLabel: this.parseLabel(request.fund.recipientLabel, true, 'fund.recipientLabel'),
recipientLabel: this.parseLabel(request.fund.recipientLabel, true, 'fund.recipientLabel'),
};
} else {
throw new Errors.InvalidRequestError('Invalid funding type');
Expand Down Expand Up @@ -152,19 +152,32 @@ class SignSwapApi extends PolygonRequestParserMixin(BitcoinRequestParserMixin(To
amount: this.parsePositiveInteger(request.redeem.amount, false, 'redeem.amount'),
};
} else if (request.redeem.type === 'EUR') {
/** @type MockSettlementInstruction | SepaSettlementInstruction | null */
const settlement = this.parseOasisSepaSettlementInstruction(request.redeem.settlement, 'redeem.settlement')
|| this.parseOasisMockSettlementInstruction(request.redeem.settlement);
if (!settlement) {
throw new Errors.InvalidRequestError('Invalid redeeming settlement');
}
parsedRequest.redeem = {
type: 'EUR',
keyPath: this.parsePath(request.redeem.keyPath, 'redeem.keyPath'),
settlement: this.parseOasisSettlementInstruction(request.redeem.settlement, 'redeem.settlement'),
settlement,
amount: this.parsePositiveInteger(request.redeem.amount, false, 'redeem.amount'),
fee: this.parsePositiveInteger(request.redeem.fee, true, 'redeem.fee'),
bankLabel: this.parseLabel(request.redeem.bankLabel, true, 'redeem.bankLabel'),
};
} else if (request.redeem.type === 'CRC') {
/** @type MockSettlementInstruction | SinpeMovilSettlementInstruction | null */
const settlement = this.parseOasisSinpeMovilSettlementInstruction(request.redeem.settlement)
|| this.parseOasisMockSettlementInstruction(request.redeem.settlement);
if (!settlement) {
throw new Errors.InvalidRequestError('Invalid redeeming settlement');
}

parsedRequest.redeem = {
type: 'CRC',
keyPath: this.parsePath(request.redeem.keyPath, 'redeem.keyPath'),
settlement: this.parseOasisCrcSettlementInstruction(request.redeem.settlement, 'redeem.settlement'),
settlement,
amount: this.parsePositiveInteger(request.redeem.amount, false, 'redeem.amount'),
fee: this.parsePositiveInteger(request.redeem.fee, true, 'redeem.fee'),
recipientLabel: this.parseLabel(request.redeem.recipientLabel, true, 'redeem.recipientLabel'),
Expand Down Expand Up @@ -386,82 +399,100 @@ class SignSwapApi extends PolygonRequestParserMixin(BitcoinRequestParserMixin(To
}

/**
* Checks that the given instruction is a valid OASIS SettlementInstruction
* @typedef {Omit<KeyguardRequest.MockSettlementInstruction, 'contractId'>} MockSettlementInstruction
* @typedef {Omit<KeyguardRequest.SepaSettlementInstruction, 'contractId'>} SepaSettlementInstruction
* @typedef {Omit<KeyguardRequest.SinpeMovilSettlementInstruction, 'contractId'>} SinpeMovilSettlementInstruction
*/


/**
* Checks that the given instruction is a valid OASIS MockSettlementInstruction
* @param {unknown} obj
* @param {string} parameterName
* @returns {Omit<KeyguardRequest.MockSettlementInstruction, 'contractId'> |
* Omit<KeyguardRequest.SepaSettlementInstruction, 'contractId'>}
* @returns {MockSettlementInstruction | null}
* @throws {Errors.InvalidRequestError}
*/
parseOasisSettlementInstruction(obj, parameterName) {
parseOasisMockSettlementInstruction(obj) {
if (typeof obj !== 'object' || obj === null) {
throw new Errors.InvalidRequestError('Invalid settlement');
}

switch (/** @type {{type: unknown}} */ (obj).type) {
case 'mock': {
/** @type {Omit<KeyguardRequest.MockSettlementInstruction, 'contractId'>} */
const settlement = {
type: 'mock',
};
return settlement;
}
case 'sepa': {
const recipient = /** @type {{recipient: unknown}} */ (obj).recipient;
if (typeof recipient !== 'object' || recipient === null) {
throw new Errors.InvalidRequestError('Invalid settlement recipient');
}

/** @type {Omit<KeyguardRequest.SepaSettlementInstruction, 'contractId'>} */
const settlement = {
type: 'sepa',
recipient: {
name: /** @type {string} */ (
this.parseLabel(
/** @type {{name: unknown}} */ (recipient).name,
false,
`${parameterName}.recipient.name`,
)
),
iban: this.parseIban(
/** @type {{iban: unknown}} */ (recipient).iban,
`${parameterName}.recipient.iban`,
),
bic: this.parseBic(
/** @type {{bic: unknown}} */ (recipient).bic,
`${parameterName}.recipient.bic`,
),
},
};
return settlement;
}
default: throw new Errors.InvalidRequestError('Invalid settlement type');
if (/** @type {{type: unknown}} */ (obj).type !== 'mock') {
return null;
}

/** @type {MockSettlementInstruction} */
const settlement = { type: 'mock' };
return settlement;
}

/**
* Checks that the given instruction is a valid OASIS SettlementInstruction
* Checks that the given instruction is a valid OASIS SepaSettlementInstruction
* @param {unknown} obj
* @param {string} parameterName
* @returns {Omit<KeyguardRequest.SinpeMovilSettlementInstruction, 'contractId'>}
* @returns {SepaSettlementInstruction | null}
* @throws {Errors.InvalidRequestError}
*/
parseOasisCrcSettlementInstruction(obj, parameterName) {
parseOasisSepaSettlementInstruction(obj, parameterName) {
if (typeof obj !== 'object' || obj === null) {
throw new Errors.InvalidRequestError('Invalid settlement');
}

if (/** @type {{type: unknown}} */ (obj).type !== 'sepa') {
return null;
}

const recipient = /** @type {{recipient: unknown}} */ (obj).recipient;
if (typeof recipient !== 'object' || recipient === null) {
throw new Errors.InvalidRequestError('Invalid settlement recipient');
}

/** @type {Omit<KeyguardRequest.SinpeMovilSettlementInstruction, 'contractId'>} */
/** @type {SepaSettlementInstruction} */
const settlement = {
type: 'sepa',
recipient: {
name: /** @type {string} */ (
this.parseLabel(
/** @type {{name: unknown}} */(recipient).name,
false,
`${parameterName}.recipient.name`,
)
),
iban: this.parseIban(
/** @type {{iban: unknown}} */(recipient).iban,
`${parameterName}.recipient.iban`,
),
bic: this.parseBic(
/** @type {{bic: unknown}} */(recipient).bic,
`${parameterName}.recipient.bic`,
),
},
};
return settlement;
}

/**
* Checks that the given instruction is a valid OASIS SinpeMovilSettlementInstruction
* @param {unknown} obj
* @returns {SinpeMovilSettlementInstruction | null}
* @throws {Errors.InvalidRequestError}
*/
parseOasisSinpeMovilSettlementInstruction(obj) {
if (typeof obj !== 'object' || obj === null) {
throw new Errors.InvalidRequestError('Invalid settlement');
}

if (/** @type {{type: unknown}} */ (obj).type !== 'sinpemovil') {
return null;
}

/** @type {SinpeMovilSettlementInstruction} */
const settlement = {
type: 'sinpemovil',
phoneNumber: /** @type {string} */ (
this.parseLabel(
/** @type {{phoneNumber: unknown}} */ (recipient).phoneNumber,
/** @type {{phoneNumber: unknown}} */(obj).phoneNumber,
false,
`${parameterName}.phoneNumber`,
'phoneNumber',
)
),
};
Expand Down
6 changes: 3 additions & 3 deletions types/Keyguard.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ type ConstructSwap<T extends KeyguardRequest.SignSwapRequestCommon> = Transform<
type: 'CRC',
amount: number,
fee: number,
sinpeLabel?: string,
senderLabel?: string,
},
redeem: {
type: 'NIM',
Expand Down Expand Up @@ -264,10 +264,10 @@ type ConstructSwap<T extends KeyguardRequest.SignSwapRequestCommon> = Transform<
keyPath: string,
// A SettlementInstruction contains a `type`, so cannot be in the
// root of the object (it conflicts with the 'CRC' type).
settlement: Omit<KeyguardRequest.SinpeMovilSettlementInstruction, 'contractId'>,
settlement: Omit<KeyguardRequest.MockSettlementInstruction, 'contractId'> | Omit<KeyguardRequest.SinpeMovilSettlementInstruction, 'contractId'>,
amount: number,
fee: number,
recipientLabel?: string,
recipientlabel?: string,
},
}>

Expand Down

0 comments on commit daf8d9c

Please sign in to comment.