-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): Add support for abstracted assets selection ✨
- Loading branch information
1 parent
0fd4655
commit b5ffed8
Showing
153 changed files
with
1,911 additions
and
942 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import type { TAssetJsonMap, TNode } from '../../src/types' | ||
import { isForeignAsset } from '../../src/utils/assets' | ||
|
||
function collectDuplicateSymbolsInChains(assetsMap: TAssetJsonMap): { | ||
[node: string]: { [symbol: string]: string[] } | ||
} { | ||
const chainDuplicates: { [node: string]: { [symbol: string]: string[] } } = {} | ||
|
||
for (const node in assetsMap) { | ||
const nodeData = assetsMap[node as TNode] | ||
const symbolToAssetIds: { [symbol: string]: Set<string> } = {} | ||
|
||
const allAssets = [...(nodeData.nativeAssets || []), ...(nodeData.otherAssets || [])] | ||
for (const asset of allAssets) { | ||
const symbol = asset.symbol | ||
const assetId = isForeignAsset(asset) ? asset.assetId : '' | ||
if (symbol && assetId) { | ||
if (!symbolToAssetIds[symbol]) { | ||
symbolToAssetIds[symbol] = new Set() | ||
} | ||
symbolToAssetIds[symbol].add(assetId) | ||
} | ||
} | ||
|
||
const duplicates: { [symbol: string]: string[] } = {} | ||
for (const symbol in symbolToAssetIds) { | ||
const assetIds = Array.from(symbolToAssetIds[symbol]) | ||
if (assetIds.length > 1) { | ||
duplicates[symbol] = assetIds | ||
} | ||
} | ||
|
||
if (Object.keys(duplicates).length > 0) { | ||
chainDuplicates[node] = duplicates | ||
} | ||
} | ||
|
||
return chainDuplicates | ||
} | ||
|
||
function assignAliasNumbers(assetIds: string[]): { [assetId: string]: number } { | ||
const aliasMapping: { [assetId: string]: number } = {} | ||
const sortedAssetIds = [...assetIds].sort() | ||
|
||
sortedAssetIds.forEach((assetId, index) => { | ||
aliasMapping[assetId] = index + 1 | ||
}) | ||
|
||
return aliasMapping | ||
} | ||
|
||
export function addAliasesToDuplicateSymbols(assetsMap: TAssetJsonMap): TAssetJsonMap { | ||
const chainDuplicates = collectDuplicateSymbolsInChains(assetsMap) | ||
|
||
for (const node in chainDuplicates) { | ||
const duplicates = chainDuplicates[node] | ||
for (const symbol in duplicates) { | ||
const assetIds = duplicates[symbol] | ||
const aliasNumbers = assignAliasNumbers(assetIds) | ||
|
||
const nodeData = assetsMap[node as TNode] | ||
const allAssets = [...(nodeData.nativeAssets || []), ...(nodeData.otherAssets || [])] | ||
|
||
for (const asset of allAssets) { | ||
if (asset.symbol === symbol && isForeignAsset(asset)) { | ||
const aliasNumber = aliasNumbers[asset.assetId] | ||
if (aliasNumber !== undefined) { | ||
asset.alias = `${symbol}${aliasNumber}` | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return assetsMap | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import assetsMapJson from '../../src/maps/assets.json' assert { type: 'json' } | ||
import type { TAssetJsonMap } from '../../src/types' | ||
|
||
const assetsMap = assetsMapJson as TAssetJsonMap | ||
|
||
const findDuplicates = () => { | ||
Object.entries(assetsMap).forEach(([networkName, network]) => { | ||
const assetSymbols = new Map<string, Record<string, number>>() | ||
|
||
const addSymbol = (symbol: string | undefined, type: string) => { | ||
if (!symbol) return // Skip if symbol is undefined | ||
if (!assetSymbols.has(symbol)) { | ||
assetSymbols.set(symbol, { nativeAssets: 0, otherAssets: 0 }) | ||
} | ||
assetSymbols.get(symbol)![type] += 1 | ||
} | ||
|
||
network.nativeAssets.forEach(asset => addSymbol(asset.symbol, 'nativeAssets')) | ||
network.otherAssets.forEach(asset => addSymbol(asset.symbol, 'otherAssets')) | ||
|
||
const duplicates = Array.from(assetSymbols.entries()) | ||
.filter( | ||
([_, types]) => | ||
types.nativeAssets > 1 || | ||
types.otherAssets > 1 || | ||
(types.nativeAssets > 0 && types.otherAssets > 0) | ||
) | ||
.map(([symbol, counts]) => ({ | ||
symbol, | ||
counts: `nativeAssets: ${counts.nativeAssets}, otherAssets: ${counts.otherAssets}` | ||
})) | ||
|
||
if (duplicates.length > 0) { | ||
console.log(`Duplicates in ${networkName}:`, duplicates) | ||
} | ||
}) | ||
} | ||
|
||
findDuplicates() |
Oops, something went wrong.