@collabland/chain
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -11,2 +11,3 @@ import { Amount, AssetRequest, AssetResponse, Chain, ChainConnector, ChainType, NativeCurrency, TokenAsset, TokenContract, TokenId, TokenMetadata } from './types'; | ||
abstract chainType: ChainType; | ||
getChain(chainIdOrNetwork: string | number): Chain; | ||
/** | ||
@@ -13,0 +14,0 @@ * Native currency of the chain |
@@ -21,2 +21,19 @@ "use strict"; | ||
} | ||
getChain(chainIdOrNetwork) { | ||
let chainId = typeof chainIdOrNetwork === 'number' | ||
? chainIdOrNetwork | ||
: parseInt(chainIdOrNetwork); | ||
if (!isNaN(chainId)) { | ||
chainIdOrNetwork = chainId; | ||
chainId = undefined; | ||
} | ||
else { | ||
chainIdOrNetwork = ''; | ||
} | ||
return { | ||
type: this.chainType, | ||
network: chainIdOrNetwork === typeof 'string' ? chainIdOrNetwork : '', | ||
chainId: chainId == null ? undefined : String(chainId), | ||
}; | ||
} | ||
getBalance(chain, account) { | ||
@@ -23,0 +40,0 @@ throw new common_1.HttpErrors.NotImplemented('Method not implemented.'); |
export * from './base-chain-connector'; | ||
export * from './caip'; | ||
export * from './metadata'; | ||
export * from './types'; |
@@ -9,4 +9,5 @@ "use strict"; | ||
tslib_1.__exportStar(require("./base-chain-connector"), exports); | ||
tslib_1.__exportStar(require("./caip"), exports); | ||
tslib_1.__exportStar(require("./metadata"), exports); | ||
tslib_1.__exportStar(require("./types"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -0,1 +1,2 @@ | ||
import { CAIPIdParams } from './caip'; | ||
/** | ||
@@ -36,32 +37,11 @@ * Chain type | ||
} | ||
export declare const ETHEREUM_MAINNET: Chain; | ||
export declare type ChainResource<T extends CAIPIdParams = CAIPIdParams> = T & { | ||
options?: Record<string, string>; | ||
}; | ||
/** | ||
* A blockchain resource | ||
*/ | ||
export interface ChainResource extends Chain { | ||
/** | ||
* Extra parameters | ||
*/ | ||
params?: Record<string, string>; | ||
/** | ||
* The resource path | ||
*/ | ||
path?: string; | ||
/** | ||
* Account address or name | ||
*/ | ||
account?: string; | ||
/** | ||
* Token contract | ||
*/ | ||
contract?: TokenContract; | ||
/** | ||
* Token id | ||
*/ | ||
tokenId?: string; | ||
} | ||
/** | ||
* Get the uri for a given chain resource in the form of | ||
* - `evm:mainnet/<account>` | ||
* - `evm:mainnet/erc20/<erc20-contract-address>` | ||
* - `evm:mainnet/erc721/<erc721-contract-address>/<token-id>` | ||
* - `evm:mainnet/erc20:<erc20-contract-address>` | ||
* - `evm:mainnet/erc721:<erc721-contract-address>/<token-id>` | ||
* | ||
@@ -81,3 +61,3 @@ * Inspired by https://github.com/ChainAgnostic/CAIPs | ||
*/ | ||
export declare function parseChainUri(uri: string): ChainResource; | ||
export declare function parseChainUri<T extends CAIPIdParams>(uri: string): ChainResource<T>; | ||
/** | ||
@@ -176,2 +156,3 @@ * Default type for amount | ||
readonly nativeCurrency: NativeCurrency; | ||
getChain(chainIdOrNetwork: string | number): Chain; | ||
/** | ||
@@ -178,0 +159,0 @@ * Get native token balance for a given account |
@@ -7,4 +7,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.NATIVE_TOKEN = exports.parseChainUri = exports.getChainUri = exports.ChainType = void 0; | ||
exports.NATIVE_TOKEN = exports.parseChainUri = exports.getChainUri = exports.ETHEREUM_MAINNET = exports.ChainType = void 0; | ||
const common_1 = require("@collabland/common"); | ||
const caip_1 = require("./caip"); | ||
const debug = common_1.debugFactory('collabland:chain'); | ||
@@ -22,7 +23,11 @@ /** | ||
})(ChainType = exports.ChainType || (exports.ChainType = {})); | ||
exports.ETHEREUM_MAINNET = { | ||
type: ChainType.evm, | ||
network: 'mainnet', | ||
}; | ||
/** | ||
* Get the uri for a given chain resource in the form of | ||
* - `evm:mainnet/<account>` | ||
* - `evm:mainnet/erc20/<erc20-contract-address>` | ||
* - `evm:mainnet/erc721/<erc721-contract-address>/<token-id>` | ||
* - `evm:mainnet/erc20:<erc20-contract-address>` | ||
* - `evm:mainnet/erc721:<erc721-contract-address>/<token-id>` | ||
* | ||
@@ -35,25 +40,9 @@ * Inspired by https://github.com/ChainAgnostic/CAIPs | ||
function getChainUri(chainResource) { | ||
var _a, _b, _c, _d, _e; | ||
const protocol = chainResource.type; | ||
const path = [ | ||
(_a = chainResource.chainId) !== null && _a !== void 0 ? _a : chainResource.network, | ||
(_c = (_b = chainResource.path) !== null && _b !== void 0 ? _b : chainResource.account) !== null && _c !== void 0 ? _c : (chainResource.contract | ||
? `${chainResource.contract.tokenSpec.toLowerCase()}:${chainResource.contract.address}` | ||
: ''), | ||
(_d = chainResource.tokenId) !== null && _d !== void 0 ? _d : '', | ||
] | ||
.filter(p => !!p) | ||
.join('/'); | ||
const params = (_e = chainResource.params) !== null && _e !== void 0 ? _e : {}; | ||
if (chainResource.rpcUrl) { | ||
params.rpcUrl = chainResource.rpcUrl; | ||
} | ||
if (chainResource.indexerUrl) { | ||
params.indexerUrl = chainResource.indexerUrl; | ||
} | ||
let query = Object.entries(params) | ||
var _a; | ||
const uri = caip_1.formatId(chainResource); | ||
let query = Object.entries((_a = chainResource.options) !== null && _a !== void 0 ? _a : {}) | ||
.map(p => `${p[0]}=${p[1]}`) | ||
.join('&'); | ||
query = query ? `?${query}` : ''; | ||
return `${protocol}:${path}${query}`; | ||
return `${uri}${query}`; | ||
} | ||
@@ -70,52 +59,15 @@ exports.getChainUri = getChainUri; | ||
debug('Parsing chain uri: %s', uri); | ||
const url = new URL(uri); | ||
// The protocol is `evm:` or `near:` | ||
const type = url.protocol.replace(/\:$/, ''); | ||
let parts = (url.hostname + url.pathname).split('/').filter(p => p !== ''); | ||
let network = parts[0]; | ||
let chainId = undefined; | ||
if (!isNaN(parseInt(network))) { | ||
chainId = network; | ||
network = ''; | ||
const [id, search] = uri.split('?'); | ||
const query = {}; | ||
if (search != null) { | ||
const params = new URLSearchParams(search); | ||
params.forEach((val, key) => { | ||
query[key] = val; | ||
}); | ||
} | ||
const query = {}; | ||
url.searchParams.forEach((val, key) => { | ||
query[key] = val; | ||
}); | ||
parts = parts.slice(1); | ||
const caipId = caip_1.parseId(id); | ||
const chainResource = { | ||
type, | ||
network, | ||
chainId, | ||
path: parts.join('/'), | ||
params: query, | ||
...caipId.toJSON(), | ||
options: query, | ||
}; | ||
const rpcUrl = query.rpcUrl; | ||
if (rpcUrl != null) { | ||
chainResource.rpcUrl = rpcUrl; | ||
delete query.rpcUrl; | ||
} | ||
const indexerUrl = query.indexerUrl; | ||
if (indexerUrl != null) { | ||
chainResource.indexerUrl = indexerUrl; | ||
delete query.indexerUrl; | ||
} | ||
if (chainId == null) { | ||
delete chainResource.chainId; | ||
} | ||
if (parts.length) { | ||
if (parts[0].includes(':')) { | ||
const [tokenSpec, address] = parts[0].split(':'); | ||
chainResource.contract = { | ||
address, | ||
tokenSpec: tokenSpec.toUpperCase(), | ||
}; | ||
if (parts[1] != null) { | ||
chainResource.tokenId = parts[1]; | ||
} | ||
} | ||
else { | ||
chainResource.account = parts[0]; | ||
} | ||
} | ||
debug('Chain resource: %O', chainResource); | ||
@@ -122,0 +74,0 @@ return chainResource; |
{ | ||
"name": "@collabland/chain", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "CollabLand Ethereum Integration", | ||
@@ -34,3 +34,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@collabland/common": "^0.19.1", | ||
"@collabland/common": "^0.20.0", | ||
"tslib": "^2.0.0" | ||
@@ -48,3 +48,3 @@ }, | ||
"author": "Abridged, Inc.", | ||
"gitHead": "e0944f19fecd734aa41d1522059cb7c48137a37d" | ||
"gitHead": "bd7b12dbd301b98fecd78442f97f0d065f0babf6" | ||
} |
@@ -37,2 +37,20 @@ // Copyright Abridged, Inc. 2021. All Rights Reserved. | ||
getChain(chainIdOrNetwork: string | number): Chain { | ||
let chainId: number | undefined = | ||
typeof chainIdOrNetwork === 'number' | ||
? chainIdOrNetwork | ||
: parseInt(chainIdOrNetwork); | ||
if (!isNaN(chainId)) { | ||
chainIdOrNetwork = chainId; | ||
chainId = undefined; | ||
} else { | ||
chainIdOrNetwork = ''; | ||
} | ||
return { | ||
type: this.chainType, | ||
network: chainIdOrNetwork === typeof 'string' ? chainIdOrNetwork : '', | ||
chainId: chainId == null ? undefined : String(chainId), | ||
}; | ||
} | ||
/** | ||
@@ -39,0 +57,0 @@ * Native currency of the chain |
@@ -7,3 +7,4 @@ // Copyright Abridged, Inc. 2021. All Rights Reserved. | ||
export * from './base-chain-connector'; | ||
export * from './caip'; | ||
export * from './metadata'; | ||
export * from './types'; |
133
src/types.ts
@@ -7,2 +7,3 @@ // Copyright Abridged, Inc. 2021. All Rights Reserved. | ||
import {debugFactory} from '@collabland/common'; | ||
import {CAIPIdParams, formatId, parseId} from './caip'; | ||
const debug = debugFactory('collabland:chain'); | ||
@@ -51,37 +52,16 @@ | ||
/** | ||
* A blockchain resource | ||
*/ | ||
export interface ChainResource extends Chain { | ||
/** | ||
* Extra parameters | ||
*/ | ||
params?: Record<string, string>; | ||
export const ETHEREUM_MAINNET: Chain = { | ||
type: ChainType.evm, | ||
network: 'mainnet', | ||
}; | ||
/** | ||
* The resource path | ||
*/ | ||
path?: string; | ||
export type ChainResource<T extends CAIPIdParams = CAIPIdParams> = T & { | ||
options?: Record<string, string>; | ||
}; | ||
/** | ||
* Account address or name | ||
*/ | ||
account?: string; | ||
/** | ||
* Token contract | ||
*/ | ||
contract?: TokenContract; | ||
/** | ||
* Token id | ||
*/ | ||
tokenId?: string; | ||
} | ||
/** | ||
* Get the uri for a given chain resource in the form of | ||
* - `evm:mainnet/<account>` | ||
* - `evm:mainnet/erc20/<erc20-contract-address>` | ||
* - `evm:mainnet/erc721/<erc721-contract-address>/<token-id>` | ||
* - `evm:mainnet/erc20:<erc20-contract-address>` | ||
* - `evm:mainnet/erc721:<erc721-contract-address>/<token-id>` | ||
* | ||
@@ -94,28 +74,8 @@ * Inspired by https://github.com/ChainAgnostic/CAIPs | ||
export function getChainUri(chainResource: ChainResource) { | ||
const protocol = chainResource.type; | ||
const path = [ | ||
chainResource.chainId ?? chainResource.network, | ||
chainResource.path ?? | ||
chainResource.account ?? | ||
(chainResource.contract | ||
? `${chainResource.contract.tokenSpec.toLowerCase()}:${ | ||
chainResource.contract.address | ||
}` | ||
: ''), | ||
chainResource.tokenId ?? '', | ||
] | ||
.filter(p => !!p) | ||
.join('/'); | ||
const params = chainResource.params ?? {}; | ||
if (chainResource.rpcUrl) { | ||
params.rpcUrl = chainResource.rpcUrl; | ||
} | ||
if (chainResource.indexerUrl) { | ||
params.indexerUrl = chainResource.indexerUrl; | ||
} | ||
let query = Object.entries(params) | ||
const uri = formatId(chainResource); | ||
let query = Object.entries(chainResource.options ?? {}) | ||
.map(p => `${p[0]}=${p[1]}`) | ||
.join('&'); | ||
query = query ? `?${query}` : ''; | ||
return `${protocol}:${path}${query}`; | ||
return `${uri}${query}`; | ||
} | ||
@@ -130,54 +90,23 @@ | ||
*/ | ||
export function parseChainUri(uri: string): ChainResource { | ||
export function parseChainUri<T extends CAIPIdParams>( | ||
uri: string, | ||
): ChainResource<T> { | ||
debug('Parsing chain uri: %s', uri); | ||
const url = new URL(uri); | ||
// The protocol is `evm:` or `near:` | ||
const type = url.protocol.replace(/\:$/, '') as ChainType; | ||
let parts = (url.hostname + url.pathname).split('/').filter(p => p !== ''); | ||
let network = parts[0]; | ||
let chainId = undefined; | ||
if (!isNaN(parseInt(network))) { | ||
chainId = network; | ||
network = ''; | ||
const [id, search] = uri.split('?'); | ||
const query: Record<string, string> = {}; | ||
if (search != null) { | ||
const params = new URLSearchParams(search); | ||
params.forEach((val, key) => { | ||
query[key] = val; | ||
}); | ||
} | ||
const query: Record<string, string> = {}; | ||
url.searchParams.forEach((val, key) => { | ||
query[key] = val; | ||
}); | ||
parts = parts.slice(1); | ||
const chainResource: ChainResource = { | ||
type, | ||
network, | ||
chainId, | ||
path: parts.join('/'), | ||
params: query, | ||
const caipId = parseId(id); | ||
const chainResource = { | ||
...(caipId.toJSON() as T), | ||
options: query, | ||
}; | ||
const rpcUrl = query.rpcUrl; | ||
if (rpcUrl != null) { | ||
chainResource.rpcUrl = rpcUrl; | ||
delete query.rpcUrl; | ||
} | ||
const indexerUrl = query.indexerUrl; | ||
if (indexerUrl != null) { | ||
chainResource.indexerUrl = indexerUrl; | ||
delete query.indexerUrl; | ||
} | ||
if (chainId == null) { | ||
delete chainResource.chainId; | ||
} | ||
if (parts.length) { | ||
if (parts[0].includes(':')) { | ||
const [tokenSpec, address] = parts[0].split(':'); | ||
chainResource.contract = { | ||
address, | ||
tokenSpec: tokenSpec.toUpperCase(), | ||
}; | ||
if (parts[1] != null) { | ||
chainResource.tokenId = parts[1]; | ||
} | ||
} else { | ||
chainResource.account = parts[0]; | ||
} | ||
} | ||
debug('Chain resource: %O', chainResource); | ||
@@ -301,2 +230,4 @@ return chainResource; | ||
getChain(chainIdOrNetwork: string | number): Chain; | ||
/** | ||
@@ -303,0 +234,0 @@ * Get native token balance for a given account |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
266244
61
8464
7
+ Added@collabland/common@0.20.2(transitive)
+ Added@types/bn.js@5.1.6(transitive)
+ Addedbn.js@5.2.1(transitive)
+ Addedcross-fetch@3.1.8(transitive)
- Removed@collabland/common@0.19.1(transitive)
- Removedcross-fetch@3.2.0(transitive)
Updated@collabland/common@^0.20.0