From 7a1176b73ccda2813cb3f24401f3b200e22b4a6f Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Thu, 5 Dec 2024 11:07:32 +0100 Subject: [PATCH 01/21] SIP-29: Snap Assets API --- SIPS/sip-29.md | 196 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 SIPS/sip-29.md diff --git a/SIPS/sip-29.md b/SIPS/sip-29.md new file mode 100644 index 0000000..b52521b --- /dev/null +++ b/SIPS/sip-29.md @@ -0,0 +1,196 @@ +--- +sip: (To be assigned) +title: Snap Assets API +status: Draft +discussions-to (*optional): (Http/Https URL) +author: Daniel Rocha (@danroc), Guillaume Roux (@GuillaumeRx) +created: (Date created on in ISO 8601 format. `yyyy-mm-dd`) +updated (*optional): (Date last updated on in https://en.wikipedia.org/wiki/ISO_8601 format. `yyyy-mm-dd`. This should be only used on SIPs with `Living` status) +--- + +## Abstract + +This SIP aims to define a new API that can be exposed by Snaps to allow clients +get asset information in a chain-agnostic way. + +## Motivation + +To allow clients to be chain-agnostic, the logic of how to get asset +information should be abstracted away from the client. We also need to define +the types that represent the asset information required by the clients. + +## Specification + +> Indented sections like this are considered non-normative. + +### Language + +The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", +"SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" written +in uppercase in this document are to be interpreted as described in [RFC +2119](https://www.ietf.org/rfc/rfc2119.txt) + +### Definitions + +In this document, all definitions are written in TypeScript. Also, any time an +asset need to be identified, it MUST use the [CAIP-19][caip-19] representation. + +### Snap Assets API + +Two methods are defined in the Snap Assets API: + +#### Get Token Description + +```typescript +// Represents a token unit. +type TokenUnit { + // Human-friendly name of the token unit. + name: string; + + // Ticker of the token unit. + ticker: string; + + // Number of decimals of the token unit. + decimals: number; +} + +// Token description. +type TokenDescription { + // Human-friendly name of the token. + name: string; + + // Ticker of the token. + ticker: string; + + // Whether the token is native to the chain. + isNative: boolean; + + // Base64 representation of the token icon. + iconBase64: string; + + // List of token units. + units: TokenUnit[]; +} + +// Returns the description of a non-fungible token. This description can then +// be used by the client to display relevant information about the token. +// +// @example +// ```typescript +// const tokenDescription = await getTokenDescription('eip155:1/slip44:60'); +// +// // Returns: +// // { +// // name: 'Ether', +// // ticker: 'ETH', +// // isNative: true, +// // iconBase64: 'data:image/png;base64,...', +// // units: [ +// // { +// // name: 'Ether', +// // ticker: 'ETH', +// // decimals: 18 +// // }, +// // { +// // name: 'Gwei', +// // ticker: 'Gwei', +// // decimals: 9 +// // }, +// // { +// // name: 'wei', +// // ticker: 'wei', +// // decimals: 0 +// // } +// // ] +// ``` +function getTokenDescription(token: Caip19AssetType): TokenDescription; +``` + +#### Get Token Conversion Rate + +```typescript +type TokenConversionRate { + // The rate of conversion from the source token to the target token. It + // means that 1 unit of the `from` token should be converted to this amount + // of the `to` token. + rate: string; + + // The UNIX timestamp of when the conversion rate was last updated. + conversionTime: number; + + // The UNIX timestamp of when the conversion rate will expire. + expirationTime: number; +} + +// Returns the conversion rate between two assets (tokens or fiat). +// +// @example +// ```typescript +// const conversionRate = await getTokenConversionRate( +// 'eip155:1/slip44:60', +// 'eip155:1/erc20:0x6b175474e89094c44da98b954eedeac495271d0f' +// ); +// +// // Returns: +// // { +// // rate: '3906.38', +// // conversionTime: 1733389786, +// // expirationTime: 1733389816, +// // } +// ``` +function getTokenConversionRate( + from: Caip19AssetType, + to: Caip19AssetType +): TokenConversionRate; +``` + +### Fiat currency representation + +We SHOULD use CAIP-19 to represent fiat currencies as well, it will allow us to +have a consistent way to represent all assets and make the API more +predictable. + +The propose format is: + +``` +asset_type: chain_id + "/" + asset_namespace + ":" + asset_reference +chain_id: namespace + ":" + reference +namespace: "fiat" +reference: country_code +asset_namespace: "currency" +asset_reference: currency_code +``` + +The country code is a two-letter lowercase code as defined by the ISO 3166-1 +alpha-2, with the exception for the European Union, which is represented by +"eu". + +The currency code is a three-letter uppercase code as defined by the ISO 4217. + +Examples: + +``` +# Euro +fiat:eu/currency:eur + +# United States Dollar +fiat:us/currency:usd + +# Brazilian Real +fiat:br/currency:brl + +# Japanese Yen +fiat:jp/currency:jpy +``` + +## Backwards compatibility + +Any SIPs that break backwards compatibility MUST include a section describing +those incompatibilities and their severity. The SIP SHOULD describe how the +author plans on proposes to deal with such these incompatibilities. + +## Copyright + +Copyright and related rights waived via [CC0](../LICENSE). + +[caip-19]: https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-19.md From 8f7b7d04ef7cca015aa6f325569b302da4659000 Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Thu, 5 Dec 2024 11:13:50 +0100 Subject: [PATCH 02/21] Fix typos --- SIPS/sip-29.md | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/SIPS/sip-29.md b/SIPS/sip-29.md index b52521b..036831f 100644 --- a/SIPS/sip-29.md +++ b/SIPS/sip-29.md @@ -11,13 +11,13 @@ updated (*optional): (Date last updated on in https://en.wikipedia.org/wiki/ISO_ ## Abstract This SIP aims to define a new API that can be exposed by Snaps to allow clients -get asset information in a chain-agnostic way. +to retrieve asset information in a chain-agnostic way. ## Motivation -To allow clients to be chain-agnostic, the logic of how to get asset -information should be abstracted away from the client. We also need to define -the types that represent the asset information required by the clients. +To enable clients to be chain-agnostic, the logic for obtaining asset +information should be abstracted away from the client. Additionally, this SIP +defines the types that represent the asset information required by clients. ## Specification @@ -32,8 +32,10 @@ in uppercase in this document are to be interpreted as described in [RFC ### Definitions -In this document, all definitions are written in TypeScript. Also, any time an -asset need to be identified, it MUST use the [CAIP-19][caip-19] representation. +1. In this document, all definitions are written in TypeScript. + +2. Any time an asset needs to be identified, it MUST use the [CAIP-19][caip-19] +representation. ### Snap Assets API @@ -43,7 +45,7 @@ Two methods are defined in the Snap Assets API: ```typescript // Represents a token unit. -type TokenUnit { +type TokenUnit = { // Human-friendly name of the token unit. name: string; @@ -52,10 +54,10 @@ type TokenUnit { // Number of decimals of the token unit. decimals: number; -} +}; // Token description. -type TokenDescription { +type TokenDescription = { // Human-friendly name of the token. name: string; @@ -70,7 +72,7 @@ type TokenDescription { // List of token units. units: TokenUnit[]; -} +}; // Returns the description of a non-fungible token. This description can then // be used by the client to display relevant information about the token. @@ -102,6 +104,7 @@ type TokenDescription { // // decimals: 0 // // } // // ] +// // } // ``` function getTokenDescription(token: Caip19AssetType): TokenDescription; ``` @@ -109,7 +112,7 @@ function getTokenDescription(token: Caip19AssetType): TokenDescription; #### Get Token Conversion Rate ```typescript -type TokenConversionRate { +type TokenConversionRate = { // The rate of conversion from the source token to the target token. It // means that 1 unit of the `from` token should be converted to this amount // of the `to` token. @@ -120,7 +123,7 @@ type TokenConversionRate { // The UNIX timestamp of when the conversion rate will expire. expirationTime: number; -} +}; // Returns the conversion rate between two assets (tokens or fiat). // @@ -146,11 +149,9 @@ function getTokenConversionRate( ### Fiat currency representation -We SHOULD use CAIP-19 to represent fiat currencies as well, it will allow us to -have a consistent way to represent all assets and make the API more -predictable. - -The propose format is: +We SHOULD use CAIP-19 to represent fiat currencies as well. This approach +provides a consistent way to represent all assets, making the API more +predictable. The proposed format is: ``` asset_type: chain_id + "/" + asset_namespace + ":" + asset_reference @@ -161,11 +162,10 @@ asset_namespace: "currency" asset_reference: currency_code ``` -The country code is a two-letter lowercase code as defined by the ISO 3166-1 -alpha-2, with the exception for the European Union, which is represented by -"eu". +The country code is a two-letter lowercase code as defined by ISO 3166-1 +alpha-2, with the exception of the European Union, represented by "eu". -The currency code is a three-letter uppercase code as defined by the ISO 4217. +The currency code is a three-letter uppercase code as defined by ISO 4217. Examples: From f0a9807dd09858c9ae9ee5c83588131d387b0c97 Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Thu, 5 Dec 2024 11:14:55 +0100 Subject: [PATCH 03/21] Update document header --- SIPS/sip-29.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/SIPS/sip-29.md b/SIPS/sip-29.md index 036831f..60e1d1b 100644 --- a/SIPS/sip-29.md +++ b/SIPS/sip-29.md @@ -1,11 +1,9 @@ --- -sip: (To be assigned) +sip: 29 title: Snap Assets API status: Draft -discussions-to (*optional): (Http/Https URL) author: Daniel Rocha (@danroc), Guillaume Roux (@GuillaumeRx) -created: (Date created on in ISO 8601 format. `yyyy-mm-dd`) -updated (*optional): (Date last updated on in https://en.wikipedia.org/wiki/ISO_8601 format. `yyyy-mm-dd`. This should be only used on SIPs with `Living` status) +created: 2024-12-05 --- ## Abstract From 03b33e5e400e0f0f5fd0cc397c2d2f604e8fbe74 Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Thu, 5 Dec 2024 11:16:30 +0100 Subject: [PATCH 04/21] Add precision about emitter country --- SIPS/sip-29.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/SIPS/sip-29.md b/SIPS/sip-29.md index 60e1d1b..d531170 100644 --- a/SIPS/sip-29.md +++ b/SIPS/sip-29.md @@ -160,8 +160,9 @@ asset_namespace: "currency" asset_reference: currency_code ``` -The country code is a two-letter lowercase code as defined by ISO 3166-1 -alpha-2, with the exception of the European Union, represented by "eu". +The country code is a two-letter lowercase code, as defined by ISO 3166-1 +alpha-2, representing the emitter country, with the exception of the European +Union, which is represented by "eu". The currency code is a three-letter uppercase code as defined by ISO 4217. From 5712f64cb95fc78a0498f88e65f58d118581843a Mon Sep 17 00:00:00 2001 From: Guillaume Roux Date: Fri, 6 Dec 2024 17:24:01 +0100 Subject: [PATCH 05/21] SIP-29 Improvements (#155) * add handlers and permission definition * update to use `snaps-sdk` * snap -> Snap * Add CAIP-19 ID reference in description and rename token to asset * Add chains caveat * fix typo * allow batching * add fungible reference to asset description * add appendix for fungible assets * fix typos * update rates handler to support batching * unify * rename `AssetUnit` to `FungibleAssetUnit` --- SIPS/sip-29.md | 200 +++++++++++++++++++++++++++++-------------------- 1 file changed, 118 insertions(+), 82 deletions(-) diff --git a/SIPS/sip-29.md b/SIPS/sip-29.md index d531170..ea7ee1a 100644 --- a/SIPS/sip-29.md +++ b/SIPS/sip-29.md @@ -35,85 +35,93 @@ in uppercase in this document are to be interpreted as described in [RFC 2. Any time an asset needs to be identified, it MUST use the [CAIP-19][caip-19] representation. -### Snap Assets API +### Snap Manifest + +This SIP introduces a new permission named `endowment:assets`. +This permission grants a Snap the ability to provide asset information to the client. + +This permission is specified as follows in `snap.manifest.json` files: + +```json +{ + "initialPermissions": { + "endowment:assets": { + "scopes": [ + "bip122:000000000019d6689c085ae165831e93" + ] + } + } +} +``` + +`scopes` - A non-empty array of CAIP-2 chain IDs that the snap supports. This field is useful for a client in order to avoid unnecessary overhead. + +### Snap Implementation Two methods are defined in the Snap Assets API: -#### Get Token Description +Any Snap that wishes to provide asset information **MUST** implement the following API: -```typescript -// Represents a token unit. -type TokenUnit = { - // Human-friendly name of the token unit. - name: string; +#### Get Asset Metadata - // Ticker of the token unit. - ticker: string; +```typescript +import { OnAssetLookupHandler } from "@metamask/snaps-sdk"; - // Number of decimals of the token unit. - decimals: number; +export const onAssetLookup: OnAssetLookupHandler = async ({ + assets +}) => { + const assetsMetadata = /* Get metadata */; + return { assets: assetsMetadata }; }; +``` -// Token description. -type TokenDescription = { - // Human-friendly name of the token. - name: string; - - // Ticker of the token. - ticker: string; +The type for an `onAssetLookup` handler function’s arguments is: - // Whether the token is native to the chain. - isNative: boolean; - // Base64 representation of the token icon. - iconBase64: string; +```typescript +interface OnAssetLookupArgs { + assets: Caip19AssetType[]; +} +``` +The type for an `onAssetLookup` handler function’s return value is: - // List of token units. - units: TokenUnit[]; +```typescript +type OnAssetLookupReturn = { + assets: Record; }; +``` + +#### Get Asset Conversion Rate + +```typescript +import { OnAssetConversionHandler } from "@metamask/snaps-sdk"; -// Returns the description of a non-fungible token. This description can then -// be used by the client to display relevant information about the token. -// -// @example -// ```typescript -// const tokenDescription = await getTokenDescription('eip155:1/slip44:60'); -// -// // Returns: -// // { -// // name: 'Ether', -// // ticker: 'ETH', -// // isNative: true, -// // iconBase64: 'data:image/png;base64,...', -// // units: [ -// // { -// // name: 'Ether', -// // ticker: 'ETH', -// // decimals: 18 -// // }, -// // { -// // name: 'Gwei', -// // ticker: 'Gwei', -// // decimals: 9 -// // }, -// // { -// // name: 'wei', -// // ticker: 'wei', -// // decimals: 0 -// // } -// // ] -// // } -// ``` -function getTokenDescription(token: Caip19AssetType): TokenDescription; +export const onAssetConversion: OnAssetConversionHandler = async ({ + conversions +}) => { + const conversionRates = /* Get conversion rate */; + return { conversionRates }; +}; ``` +The type for an `onAssetConversion` handler function’s arguments is: + +```typescript +type Conversion = { + from: Caip19AssetType; + to: Caip19AssetType; +}; -#### Get Token Conversion Rate +type OnAssetConversionArgs = { + conversions: Conversion[]; +} +``` +The type for an `onAssetConversion` handler function’s return value is: ```typescript -type TokenConversionRate = { - // The rate of conversion from the source token to the target token. It - // means that 1 unit of the `from` token should be converted to this amount - // of the `to` token. +type AssetConversionRate = { + // The rate of conversion from the source asset to the target asset. It + // means that 1 unit of the `from` asset should be converted to this amount + // of the `to` asset. rate: string; // The UNIX timestamp of when the conversion rate was last updated. @@ -123,26 +131,13 @@ type TokenConversionRate = { expirationTime: number; }; -// Returns the conversion rate between two assets (tokens or fiat). -// -// @example -// ```typescript -// const conversionRate = await getTokenConversionRate( -// 'eip155:1/slip44:60', -// 'eip155:1/erc20:0x6b175474e89094c44da98b954eedeac495271d0f' -// ); -// -// // Returns: -// // { -// // rate: '3906.38', -// // conversionTime: 1733389786, -// // expirationTime: 1733389816, -// // } -// ``` -function getTokenConversionRate( - from: Caip19AssetType, - to: Caip19AssetType -): TokenConversionRate; +type FromAsset = Conversion["from"]; + +type ToAsset = Conversion["to"]; + +type OnAssetConversionReturn = { + conversionRates: Record>; +}; ``` ### Fiat currency representation @@ -182,6 +177,47 @@ fiat:br/currency:brl fiat:jp/currency:jpy ``` +## Appendix I: Fungible Asset Metadata + +The following asset metadata fields for a fungible asset are defined. +As of the time of creation of this SIP, they are the only possible assets requested by clients. + +```typescript +// Represents an asset unit. +type FungibleAssetUnit = { + // Human-friendly name of the asset unit. + name: string; + + // Ticker of the asset unit. + ticker: string; + + // Number of decimals of the asset unit. + decimals: number; +}; + +// Fungible asset metadata. +type FungibleAssetMetadata = { + // Human-friendly name of the asset. + name: string; + + // Ticker of the asset. + ticker: string; + + // Whether the asset is native to the chain. + native: boolean; + + // Represents a fungible asset + fungible: true; + + // Base64 representation of the asset icon. + iconBase64: string; + + // List of asset units. + units: FungibleAssetUnit[]; +}; +``` + + ## Backwards compatibility Any SIPs that break backwards compatibility MUST include a section describing From ec71c691688ae48108079fdb8feb6a8e7b986783 Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Fri, 6 Dec 2024 17:26:06 +0100 Subject: [PATCH 06/21] Fix formatting --- SIPS/sip-29.md | 79 +++++++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 39 deletions(-) diff --git a/SIPS/sip-29.md b/SIPS/sip-29.md index ea7ee1a..47205e6 100644 --- a/SIPS/sip-29.md +++ b/SIPS/sip-29.md @@ -33,7 +33,7 @@ in uppercase in this document are to be interpreted as described in [RFC 1. In this document, all definitions are written in TypeScript. 2. Any time an asset needs to be identified, it MUST use the [CAIP-19][caip-19] -representation. + representation. ### Snap Manifest @@ -46,9 +46,9 @@ This permission is specified as follows in `snap.manifest.json` files: { "initialPermissions": { "endowment:assets": { - "scopes": [ - "bip122:000000000019d6689c085ae165831e93" - ] + "scopes": [ + "bip122:000000000019d6689c085ae165831e93" + ] } } } @@ -77,17 +77,17 @@ export const onAssetLookup: OnAssetLookupHandler = async ({ The type for an `onAssetLookup` handler function’s arguments is: - ```typescript interface OnAssetLookupArgs { - assets: Caip19AssetType[]; + assets: Caip19AssetType[]; } ``` + The type for an `onAssetLookup` handler function’s return value is: ```typescript type OnAssetLookupReturn = { - assets: Record; + assets: Record; }; ``` @@ -103,32 +103,34 @@ export const onAssetConversion: OnAssetConversionHandler = async ({ return { conversionRates }; }; ``` + The type for an `onAssetConversion` handler function’s arguments is: ```typescript type Conversion = { - from: Caip19AssetType; - to: Caip19AssetType; + from: Caip19AssetType; + to: Caip19AssetType; }; type OnAssetConversionArgs = { - conversions: Conversion[]; -} + conversions: Conversion[]; +}; ``` + The type for an `onAssetConversion` handler function’s return value is: ```typescript type AssetConversionRate = { - // The rate of conversion from the source asset to the target asset. It - // means that 1 unit of the `from` asset should be converted to this amount - // of the `to` asset. - rate: string; + // The rate of conversion from the source asset to the target asset. It + // means that 1 unit of the `from` asset should be converted to this amount + // of the `to` asset. + rate: string; - // The UNIX timestamp of when the conversion rate was last updated. - conversionTime: number; + // The UNIX timestamp of when the conversion rate was last updated. + conversionTime: number; - // The UNIX timestamp of when the conversion rate will expire. - expirationTime: number; + // The UNIX timestamp of when the conversion rate will expire. + expirationTime: number; }; type FromAsset = Conversion["from"]; @@ -136,7 +138,7 @@ type FromAsset = Conversion["from"]; type ToAsset = Conversion["to"]; type OnAssetConversionReturn = { - conversionRates: Record>; + conversionRates: Record>; }; ``` @@ -185,39 +187,38 @@ As of the time of creation of this SIP, they are the only possible assets reques ```typescript // Represents an asset unit. type FungibleAssetUnit = { - // Human-friendly name of the asset unit. - name: string; + // Human-friendly name of the asset unit. + name: string; - // Ticker of the asset unit. - ticker: string; + // Ticker of the asset unit. + ticker: string; - // Number of decimals of the asset unit. - decimals: number; + // Number of decimals of the asset unit. + decimals: number; }; // Fungible asset metadata. type FungibleAssetMetadata = { - // Human-friendly name of the asset. - name: string; + // Human-friendly name of the asset. + name: string; - // Ticker of the asset. - ticker: string; + // Ticker of the asset. + ticker: string; - // Whether the asset is native to the chain. - native: boolean; + // Whether the asset is native to the chain. + native: boolean; - // Represents a fungible asset - fungible: true; + // Represents a fungible asset + fungible: true; - // Base64 representation of the asset icon. - iconBase64: string; + // Base64 representation of the asset icon. + iconBase64: string; - // List of asset units. - units: FungibleAssetUnit[]; + // List of asset units. + units: FungibleAssetUnit[]; }; ``` - ## Backwards compatibility Any SIPs that break backwards compatibility MUST include a section describing From 5657a9c97467860f5502ae64066fcfd6308544e6 Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Fri, 13 Dec 2024 10:50:48 +0100 Subject: [PATCH 07/21] Rename `ticker` to `symbol` It should be closer to the terminology used by the Token API. --- SIPS/sip-29.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/SIPS/sip-29.md b/SIPS/sip-29.md index 47205e6..b5de432 100644 --- a/SIPS/sip-29.md +++ b/SIPS/sip-29.md @@ -190,8 +190,8 @@ type FungibleAssetUnit = { // Human-friendly name of the asset unit. name: string; - // Ticker of the asset unit. - ticker: string; + // Ticker symbol of the asset unit. + symbol: string; // Number of decimals of the asset unit. decimals: number; @@ -202,8 +202,8 @@ type FungibleAssetMetadata = { // Human-friendly name of the asset. name: string; - // Ticker of the asset. - ticker: string; + // Ticker symbol of the asset's main unit. + symbol: string; // Whether the asset is native to the chain. native: boolean; From fe5f635783f982ea9735b390bb2cdf319948fa5f Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Mon, 13 Jan 2025 10:22:44 +0100 Subject: [PATCH 08/21] Fix capitalization Co-authored-by: Charly Chevalier --- SIPS/sip-29.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SIPS/sip-29.md b/SIPS/sip-29.md index b5de432..d43047f 100644 --- a/SIPS/sip-29.md +++ b/SIPS/sip-29.md @@ -54,7 +54,7 @@ This permission is specified as follows in `snap.manifest.json` files: } ``` -`scopes` - A non-empty array of CAIP-2 chain IDs that the snap supports. This field is useful for a client in order to avoid unnecessary overhead. +`scopes` - A non-empty array of CAIP-2 chain IDs that the Snap supports. This field is useful for a client in order to avoid unnecessary overhead. ### Snap Implementation From 6fa4590f248ae6c164e3f86834572e2180be3a48 Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Mon, 13 Jan 2025 10:23:05 +0100 Subject: [PATCH 09/21] Update comment Co-authored-by: Charly Chevalier --- SIPS/sip-29.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SIPS/sip-29.md b/SIPS/sip-29.md index d43047f..aa2720a 100644 --- a/SIPS/sip-29.md +++ b/SIPS/sip-29.md @@ -70,7 +70,7 @@ import { OnAssetLookupHandler } from "@metamask/snaps-sdk"; export const onAssetLookup: OnAssetLookupHandler = async ({ assets }) => { - const assetsMetadata = /* Get metadata */; + const assetsMetadata = /* Get metadata for given `assets` */; return { assets: assetsMetadata }; }; ``` From 96f0aa1bd09c851c9bc8277d322aecbc5cefc04a Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Mon, 13 Jan 2025 10:23:16 +0100 Subject: [PATCH 10/21] Update comment Co-authored-by: Charly Chevalier --- SIPS/sip-29.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SIPS/sip-29.md b/SIPS/sip-29.md index aa2720a..fa76bd0 100644 --- a/SIPS/sip-29.md +++ b/SIPS/sip-29.md @@ -99,7 +99,7 @@ import { OnAssetConversionHandler } from "@metamask/snaps-sdk"; export const onAssetConversion: OnAssetConversionHandler = async ({ conversions }) => { - const conversionRates = /* Get conversion rate */; + const conversionRates = /* Get conversion rate for given `conversions` */; return { conversionRates }; }; ``` From 03772b31bef693dbba6a6c21f0963e4c1c2cedd1 Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Mon, 13 Jan 2025 10:23:42 +0100 Subject: [PATCH 11/21] Add link to CAIP-19 Co-authored-by: Charly Chevalier --- SIPS/sip-29.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SIPS/sip-29.md b/SIPS/sip-29.md index fa76bd0..b07e312 100644 --- a/SIPS/sip-29.md +++ b/SIPS/sip-29.md @@ -144,7 +144,7 @@ type OnAssetConversionReturn = { ### Fiat currency representation -We SHOULD use CAIP-19 to represent fiat currencies as well. This approach +We SHOULD use [CAIP-19][caip-19] to represent fiat currencies as well. This approach provides a consistent way to represent all assets, making the API more predictable. The proposed format is: From 2cd9315df1ba0d17639af065f751d7f289d0d7d4 Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Mon, 13 Jan 2025 10:24:19 +0100 Subject: [PATCH 12/21] Fix formatting Co-authored-by: Charly Chevalier --- SIPS/sip-29.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SIPS/sip-29.md b/SIPS/sip-29.md index b07e312..ad57ea8 100644 --- a/SIPS/sip-29.md +++ b/SIPS/sip-29.md @@ -60,7 +60,7 @@ This permission is specified as follows in `snap.manifest.json` files: Two methods are defined in the Snap Assets API: -Any Snap that wishes to provide asset information **MUST** implement the following API: +Any Snap that wishes to provide asset information MUST implement the following API: #### Get Asset Metadata From 2219c5d3e5194c1f124736bb72db2e883a129f64 Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Tue, 14 Jan 2025 08:56:03 +0100 Subject: [PATCH 13/21] Rename `OnAssetLookupReturn` to `OnAssetLookupResponse` Co-authored-by: Frederik Bolding --- SIPS/sip-29.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SIPS/sip-29.md b/SIPS/sip-29.md index ad57ea8..20955da 100644 --- a/SIPS/sip-29.md +++ b/SIPS/sip-29.md @@ -86,7 +86,7 @@ interface OnAssetLookupArgs { The type for an `onAssetLookup` handler function’s return value is: ```typescript -type OnAssetLookupReturn = { +type OnAssetLookupResponse = { assets: Record; }; ``` From 16bea2828bcb67ac5cb6f642231395fa0e851783 Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Tue, 14 Jan 2025 08:56:24 +0100 Subject: [PATCH 14/21] Rename `OnAssetConversionReturn` to `OnAssetConversionResponse` Co-authored-by: Frederik Bolding --- SIPS/sip-29.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SIPS/sip-29.md b/SIPS/sip-29.md index 20955da..472dcb8 100644 --- a/SIPS/sip-29.md +++ b/SIPS/sip-29.md @@ -137,7 +137,7 @@ type FromAsset = Conversion["from"]; type ToAsset = Conversion["to"]; -type OnAssetConversionReturn = { +type OnAssetConversionResponse = { conversionRates: Record>; }; ``` From fb45e9847a2cddde4becaadb18ca5a530d0cf67f Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Tue, 14 Jan 2025 09:30:49 +0100 Subject: [PATCH 15/21] Add missing `AssetMetadata` type --- SIPS/sip-29.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/SIPS/sip-29.md b/SIPS/sip-29.md index 472dcb8..ec0d163 100644 --- a/SIPS/sip-29.md +++ b/SIPS/sip-29.md @@ -217,6 +217,9 @@ type FungibleAssetMetadata = { // List of asset units. units: FungibleAssetUnit[]; }; + +// Represents the metadata of an asset. +type AssetMetadata = FungibleAssetMetadata ``` ## Backwards compatibility From 2047ed768d02854d81192635367ada9a8eb312c1 Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Mon, 20 Jan 2025 13:30:10 +0100 Subject: [PATCH 16/21] Rename `OnAssetLookupArgs` to `OnAssetLookupArguments` Co-authored-by: Frederik Bolding --- SIPS/sip-29.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SIPS/sip-29.md b/SIPS/sip-29.md index ec0d163..9708b32 100644 --- a/SIPS/sip-29.md +++ b/SIPS/sip-29.md @@ -78,7 +78,7 @@ export const onAssetLookup: OnAssetLookupHandler = async ({ The type for an `onAssetLookup` handler function’s arguments is: ```typescript -interface OnAssetLookupArgs { +interface OnAssetLookupArguments { assets: Caip19AssetType[]; } ``` From 6689c5b52704f348a18c4b9a89cb6ba624767725 Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Mon, 20 Jan 2025 13:30:36 +0100 Subject: [PATCH 17/21] Rename `OnAssetConversionArgs` to `OnAssetConversionArguments` Co-authored-by: Frederik Bolding --- SIPS/sip-29.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SIPS/sip-29.md b/SIPS/sip-29.md index 9708b32..799823d 100644 --- a/SIPS/sip-29.md +++ b/SIPS/sip-29.md @@ -112,7 +112,7 @@ type Conversion = { to: Caip19AssetType; }; -type OnAssetConversionArgs = { +type OnAssetConversionArguments = { conversions: Conversion[]; }; ``` From 728d44c475c94eb91e5220991e756d1595a92a1e Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Mon, 20 Jan 2025 13:33:08 +0100 Subject: [PATCH 18/21] Remove placeholder section Co-authored-by: Maarten Zuidhoorn --- SIPS/sip-29.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/SIPS/sip-29.md b/SIPS/sip-29.md index 799823d..8a1a3ef 100644 --- a/SIPS/sip-29.md +++ b/SIPS/sip-29.md @@ -222,12 +222,6 @@ type FungibleAssetMetadata = { type AssetMetadata = FungibleAssetMetadata ``` -## Backwards compatibility - -Any SIPs that break backwards compatibility MUST include a section describing -those incompatibilities and their severity. The SIP SHOULD describe how the -author plans on proposes to deal with such these incompatibilities. - ## Copyright Copyright and related rights waived via [CC0](../LICENSE). From 969335fdbfa930c2793f45afc30ac5ccbdfd1730 Mon Sep 17 00:00:00 2001 From: Guillaume Roux Date: Tue, 21 Jan 2025 14:30:16 +0100 Subject: [PATCH 19/21] Address requested changes - Update the handler names to be pluralised - Describe `Caip19AssetType` - Add some text about how the rates are represented --- SIPS/sip-29.md | 67 ++++++++++++-------------------------------------- 1 file changed, 16 insertions(+), 51 deletions(-) diff --git a/SIPS/sip-29.md b/SIPS/sip-29.md index 8a1a3ef..1af3fdd 100644 --- a/SIPS/sip-29.md +++ b/SIPS/sip-29.md @@ -64,10 +64,12 @@ Any Snap that wishes to provide asset information MUST implement the following A #### Get Asset Metadata +`Caip19AssetType` - A string that represents an asset using the [CAIP-19][caip-19] standard. + ```typescript -import { OnAssetLookupHandler } from "@metamask/snaps-sdk"; +import { OnAssetsLookupHandler } from "@metamask/snaps-sdk"; -export const onAssetLookup: OnAssetLookupHandler = async ({ +export const onAssetsLookup: OnAssetsLookupHandler = async ({ assets }) => { const assetsMetadata = /* Get metadata for given `assets` */; @@ -75,18 +77,18 @@ export const onAssetLookup: OnAssetLookupHandler = async ({ }; ``` -The type for an `onAssetLookup` handler function’s arguments is: +The type for an `onAssetsLookup` handler function’s arguments is: ```typescript -interface OnAssetLookupArguments { +interface OnAssetsLookupArguments { assets: Caip19AssetType[]; } ``` -The type for an `onAssetLookup` handler function’s return value is: +The type for an `onAssetsLookup` handler function’s return value is: ```typescript -type OnAssetLookupResponse = { +type OnAssetsLookupResponse = { assets: Record; }; ``` @@ -94,9 +96,9 @@ type OnAssetLookupResponse = { #### Get Asset Conversion Rate ```typescript -import { OnAssetConversionHandler } from "@metamask/snaps-sdk"; +import { OnAssetsConversionHandler } from "@metamask/snaps-sdk"; -export const onAssetConversion: OnAssetConversionHandler = async ({ +export const onAssetsConversion: OnAssetsConversionHandler = async ({ conversions }) => { const conversionRates = /* Get conversion rate for given `conversions` */; @@ -104,7 +106,7 @@ export const onAssetConversion: OnAssetConversionHandler = async ({ }; ``` -The type for an `onAssetConversion` handler function’s arguments is: +The type for an `onAssetsConversion` handler function’s arguments is: ```typescript type Conversion = { @@ -112,17 +114,17 @@ type Conversion = { to: Caip19AssetType; }; -type OnAssetConversionArguments = { +type OnAssetsConversionArguments = { conversions: Conversion[]; }; ``` -The type for an `onAssetConversion` handler function’s return value is: +The type for an `onAssetsConversion` handler function’s return value is: ```typescript type AssetConversionRate = { - // The rate of conversion from the source asset to the target asset. It - // means that 1 unit of the `from` asset should be converted to this amount + // The rate of conversion from the source asset to the target asset represented as a decimal number in a string. + // It means that 1 unit of the `from` asset should be converted to this amount // of the `to` asset. rate: string; @@ -137,48 +139,11 @@ type FromAsset = Conversion["from"]; type ToAsset = Conversion["to"]; -type OnAssetConversionResponse = { +type OnAssetsConversionResponse = { conversionRates: Record>; }; ``` -### Fiat currency representation - -We SHOULD use [CAIP-19][caip-19] to represent fiat currencies as well. This approach -provides a consistent way to represent all assets, making the API more -predictable. The proposed format is: - -``` -asset_type: chain_id + "/" + asset_namespace + ":" + asset_reference -chain_id: namespace + ":" + reference -namespace: "fiat" -reference: country_code -asset_namespace: "currency" -asset_reference: currency_code -``` - -The country code is a two-letter lowercase code, as defined by ISO 3166-1 -alpha-2, representing the emitter country, with the exception of the European -Union, which is represented by "eu". - -The currency code is a three-letter uppercase code as defined by ISO 4217. - -Examples: - -``` -# Euro -fiat:eu/currency:eur - -# United States Dollar -fiat:us/currency:usd - -# Brazilian Real -fiat:br/currency:brl - -# Japanese Yen -fiat:jp/currency:jpy -``` - ## Appendix I: Fungible Asset Metadata The following asset metadata fields for a fungible asset are defined. From a32161edf2d4c4d9a70f6f3f3d0daf532852aa77 Mon Sep 17 00:00:00 2001 From: Guillaume Roux Date: Tue, 21 Jan 2025 18:11:02 +0100 Subject: [PATCH 20/21] More improvements - Allow data URI and URL for token icons - Make conversion rate expiration time optional - Add note about asset filtering if out of scope - Remove native key from asset description - Typo fixes --- SIPS/sip-29.md | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/SIPS/sip-29.md b/SIPS/sip-29.md index 1af3fdd..00ab453 100644 --- a/SIPS/sip-29.md +++ b/SIPS/sip-29.md @@ -54,7 +54,7 @@ This permission is specified as follows in `snap.manifest.json` files: } ``` -`scopes` - A non-empty array of CAIP-2 chain IDs that the Snap supports. This field is useful for a client in order to avoid unnecessary overhead. +`scopes` - A non-empty array of CAIP-2 chain IDs that the Snap supports. This field is useful for a client in order to avoid unnecessary overhead. Any asset returned by the Snap must be from one of the supported chains otherwise it will get filtered out. ### Snap Implementation @@ -62,7 +62,7 @@ Two methods are defined in the Snap Assets API: Any Snap that wishes to provide asset information MUST implement the following API: -#### Get Asset Metadata +#### Get Assets Metadata `Caip19AssetType` - A string that represents an asset using the [CAIP-19][caip-19] standard. @@ -93,7 +93,7 @@ type OnAssetsLookupResponse = { }; ``` -#### Get Asset Conversion Rate +#### Get Assets Conversion Rate ```typescript import { OnAssetsConversionHandler } from "@metamask/snaps-sdk"; @@ -132,7 +132,7 @@ type AssetConversionRate = { conversionTime: number; // The UNIX timestamp of when the conversion rate will expire. - expirationTime: number; + expirationTime?: number; }; type FromAsset = Conversion["from"]; @@ -170,14 +170,11 @@ type FungibleAssetMetadata = { // Ticker symbol of the asset's main unit. symbol: string; - // Whether the asset is native to the chain. - native: boolean; - // Represents a fungible asset fungible: true; - // Base64 representation of the asset icon. - iconBase64: string; + // data URI or URL representation of the asset icon. + iconUrl: string; // List of asset units. units: FungibleAssetUnit[]; From 519bdeba89884598c0ee84326279a586b9a8c68b Mon Sep 17 00:00:00 2001 From: Guillaume Roux Date: Wed, 22 Jan 2025 11:00:15 +0100 Subject: [PATCH 21/21] address requested changes --- SIPS/sip-29.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/SIPS/sip-29.md b/SIPS/sip-29.md index 00ab453..bd2d9a0 100644 --- a/SIPS/sip-29.md +++ b/SIPS/sip-29.md @@ -54,7 +54,7 @@ This permission is specified as follows in `snap.manifest.json` files: } ``` -`scopes` - A non-empty array of CAIP-2 chain IDs that the Snap supports. This field is useful for a client in order to avoid unnecessary overhead. Any asset returned by the Snap must be from one of the supported chains otherwise it will get filtered out. +`scopes` - A non-empty array of CAIP-2 chain IDs that the Snap supports. This field is useful for a client in order to avoid unnecessary overhead. Any asset returned by the Snap MUST be filtered out if it isn't part of the supported scopes. ### Snap Implementation @@ -64,7 +64,7 @@ Any Snap that wishes to provide asset information MUST implement the following A #### Get Assets Metadata -`Caip19AssetType` - A string that represents an asset using the [CAIP-19][caip-19] standard. +`Caip19AssetType` - A string that represents an asset using the [CAIP-19][caip-19] standard. ```typescript import { OnAssetsLookupHandler } from "@metamask/snaps-sdk"; @@ -173,7 +173,7 @@ type FungibleAssetMetadata = { // Represents a fungible asset fungible: true; - // data URI or URL representation of the asset icon. + // Base64 data URI or URL representation of the asset icon. iconUrl: string; // List of asset units.