UNPKG

@metamask/keyring-api

Version:
88 lines 2.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AssetStruct = exports.NonFungibleAssetStruct = exports.FungibleAssetStruct = exports.FungibleAssetAmountStruct = void 0; const keyring_utils_1 = require("@metamask/keyring-utils"); const superstruct_1 = require("@metamask/superstruct"); const utils_1 = require("@metamask/utils"); /** * Fungible asset amount struct. */ exports.FungibleAssetAmountStruct = (0, keyring_utils_1.object)({ /** * Asset unit. */ unit: (0, superstruct_1.string)(), /** * Asset amount. */ amount: keyring_utils_1.StringNumberStruct, }); /** * Fungible asset struct. */ exports.FungibleAssetStruct = (0, keyring_utils_1.object)({ /** * It is a fungible asset. */ fungible: (0, superstruct_1.literal)(true), /** * Asset type (CAIP-19). */ type: utils_1.CaipAssetTypeStruct, ...exports.FungibleAssetAmountStruct.schema, }); /** * Non-fungible asset struct. */ exports.NonFungibleAssetStruct = (0, keyring_utils_1.object)({ /** * It is a non-fungible asset. */ fungible: (0, superstruct_1.literal)(false), /** * Asset ID (CAIP-19). */ id: utils_1.CaipAssetIdStruct, }); /** * Asset struct. This represents a fungible or non-fungible asset. Fungible * assets include an amount and a unit in addition to the asset type. While * non-fungible assets include only an asset ID. * * See {@link NonFungibleAssetStruct} and {@link FungibleAssetStruct}. * * All assets have a `fungible` property that is used to tag the union and * allow the following pattern: * * ```ts * if (asset.fungible) { * // Use asset.type * } else { * // Use asset.id * } * ``` * * @example * ```ts * { * fungible: true, * type: 'eip155:1/slip44:60', * unit: 'ETH', * amount: '0.01', * } * ``` * * @example * ```ts * { * fungible: false, * id: 'eip155:1/erc721:0x06012c8cf97BEaD5deAe237070F9587f8E7A266d/771769', * } * ``` */ exports.AssetStruct = (0, keyring_utils_1.selectiveUnion)((value) => { return (0, utils_1.isPlainObject)(value) && !value.fungible ? exports.NonFungibleAssetStruct : exports.FungibleAssetStruct; }); //# sourceMappingURL=asset.cjs.map