Skip to content

Commit

Permalink
feat: icrc21_canister_call_consent_message for ledger-icp
Browse files Browse the repository at this point in the history
Signed-off-by: David Dal Busco <[email protected]>
  • Loading branch information
peterpeterparker committed Sep 3, 2024
1 parent c74b166 commit 9a576fb
Show file tree
Hide file tree
Showing 6 changed files with 460 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Next

## Features

- Add support for `icrc21_canister_call_consent_message` to `@dfinity/ledger-icp`.

# 2024.09.02-0830Z

## Overview
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { arrayOfNumberToUint8Array, toNullable } from "@dfinity/utils";
import {
arrayOfNumberToUint8Array,
isNullish,
toNullable,
} from "@dfinity/utils";
import type {
TransferArg as Icrc1TransferRawRequest,
icrc21_consent_message_request as Icrc21ConsentMessageRawRequest,
ApproveArgs as Icrc2ApproveRawRequest,
Tokens,
TransferArgs as TransferRawRequest,
} from "../../../candid/ledger";
import { TRANSACTION_FEE } from "../../constants/constants";
import type {
import {
Icrc1TransferRequest,
Icrc21ConsentMessageRequest,
Icrc2ApproveRequest,
TransferRequest,
} from "../../types/ledger_converters";
Expand Down Expand Up @@ -75,3 +81,31 @@ export const toIcrc2ApproveRawRequest = ({
expected_allowance: toNullable(expected_allowance),
expires_at: toNullable(expires_at),
});

export const toIcrc21ConsentMessageRawRequest = ({
userPreferences: {
metadata: { utcOffsetMinutes, language },
deriveSpec,
},
...rest
}: Icrc21ConsentMessageRequest): Icrc21ConsentMessageRawRequest => ({
...rest,
user_preferences: {
metadata: {
language,
utc_offset_minutes: toNullable(utcOffsetMinutes),
},
device_spec: isNullish(deriveSpec)
? toNullable()
: toNullable(
"GenericDisplay" in deriveSpec
? { GenericDisplay: null }
: {
LineDisplay: {
characters_per_line: deriveSpec.LineDisplay.charactersPerLine,
lines_per_page: deriveSpec.LineDisplay.linesPerPage,
},
},
),
},
});
39 changes: 39 additions & 0 deletions packages/ledger-icp/src/errors/ledger.errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
Icrc1BlockIndex,
Icrc1Tokens,
icrc21_error as Icrc21RawError,
ApproveError as RawApproveError,
Icrc1TransferError as RawIcrc1TransferError,
TransferError as RawTransferError,
Expand All @@ -11,6 +12,7 @@ export class IcrcError extends Error {}

export class TransferError extends IcrcError {}
export class ApproveError extends IcrcError {}
export class ConsentMessageError extends IcrcError {}

export class InvalidSenderError extends TransferError {}

Expand Down Expand Up @@ -74,6 +76,10 @@ export class ExpiredError extends ApproveError {
}
}

export class InsufficientPaymentError extends ConsentMessageError {}
export class UnsupportedCanisterCallError extends ConsentMessageError {}
export class ConsentMessageUnavailableError extends ConsentMessageError {}

export const mapTransferError = (
rawTransferError: RawTransferError,
): TransferError => {
Expand Down Expand Up @@ -184,3 +190,36 @@ export const mapIcrc2ApproveError = (
`Unknown error type ${JSON.stringify(rawApproveError)}`,
);
};

export const mapIcrc21ConsentMessageError = (
rawError: Icrc21RawError,
): ConsentMessageError => {
if ("GenericError" in rawError) {
return new GenericError(
rawError.GenericError.description,
rawError.GenericError.error_code,
);
}

if ("InsufficientPayment" in rawError) {
return new InsufficientPaymentError(
rawError.InsufficientPayment.description,
);
}

if ("UnsupportedCanisterCall" in rawError) {
return new UnsupportedCanisterCallError(
rawError.UnsupportedCanisterCall.description,
);
}
if ("ConsentMessageUnavailable" in rawError) {
return new ConsentMessageUnavailableError(
rawError.ConsentMessageUnavailable.description,
);
}

// Edge case
return new ConsentMessageError(
`Unknown error type ${JSON.stringify(rawError)}`,
);
};
Loading

0 comments on commit 9a576fb

Please sign in to comment.