-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add utils to manage snap ui (#404)
- Loading branch information
1 parent
774cfb1
commit 90f94a5
Showing
7 changed files
with
289 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { constants } from 'starknet'; | ||
|
||
import { getExplorerUrl } from './explorer'; | ||
|
||
describe('getExplorerUrl', () => { | ||
const address = | ||
'0x074aaeb168bbd155d41290e6be09d80c9e937ee3d775eac19519a2fcc76fc61c'; | ||
|
||
it('returns a sepolia testnet explorer url', () => { | ||
const result = getExplorerUrl( | ||
address, | ||
constants.StarknetChainId.SN_SEPOLIA, | ||
); | ||
expect(result).toBe(`https://sepolia.voyager.online/contract/${address}`); | ||
}); | ||
|
||
it('returns a mainnet explorer url', () => { | ||
const result = getExplorerUrl(address, constants.StarknetChainId.SN_MAIN); | ||
expect(result).toBe(`https://voyager.online/contract/${address}`); | ||
}); | ||
|
||
it('throws `Invalid Chain ID` error if the given Chain ID is not support', () => { | ||
expect(() => getExplorerUrl(address, 'some Chain ID')).toThrow( | ||
'Invalid Chain ID', | ||
); | ||
}); | ||
}); |
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,30 @@ | ||
import { constants } from 'starknet'; | ||
|
||
import { Config } from '../config'; | ||
|
||
/** | ||
* Gets the explorer URL for a given address and Chain ID. | ||
* | ||
* @param address - The address to get the explorer URL for. | ||
* @param chainId - The Chain ID. | ||
* @returns The explorer URL as a string. | ||
* @throws An error if an invalid scope is provided. | ||
*/ | ||
export function getExplorerUrl(address: string, chainId: string): string { | ||
switch (chainId) { | ||
case constants.StarknetChainId.SN_MAIN: | ||
return Config.explorer[constants.StarknetChainId.SN_MAIN].replace( | ||
// eslint-disable-next-line no-template-curly-in-string | ||
'${address}', | ||
address, | ||
); | ||
case constants.StarknetChainId.SN_SEPOLIA: | ||
return Config.explorer[constants.StarknetChainId.SN_SEPOLIA].replace( | ||
// eslint-disable-next-line no-template-curly-in-string | ||
'${address}', | ||
address, | ||
); | ||
default: | ||
throw new Error('Invalid Chain ID'); | ||
} | ||
} |
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,130 @@ | ||
import { divider, heading, row, text } from '@metamask/snaps-sdk'; | ||
|
||
import { getExplorerUrl } from './explorer'; | ||
import { toJson } from './serializer'; | ||
import { shortenAddress } from './string'; | ||
|
||
/** | ||
* Build a row component. | ||
* | ||
* @param params - The parameters. | ||
* @param params.label - The label of the row component. | ||
* @param params.value - The value of the row component. | ||
* @returns A row component. | ||
*/ | ||
export function rowUI({ label, value }: { label: string; value: string }) { | ||
return row( | ||
label, | ||
text({ | ||
value, | ||
markdown: false, | ||
}), | ||
); | ||
} | ||
|
||
/** | ||
* Build a row component with the address. | ||
* | ||
* @param params - The parameters. | ||
* @param params.label - The label. | ||
* @param params.address - The address. | ||
* @param [params.chainId] - The chain ID, when the chain ID is set, a exploder URL markdown will be generated. | ||
* @param [params.shortern] - Whether to shorten the address. Default is true. | ||
* @returns A row component with the address. | ||
*/ | ||
export function addressUI({ | ||
label, | ||
address, | ||
chainId, | ||
shortern = true, | ||
}: { | ||
label: string; | ||
address: string; | ||
chainId?: string; | ||
shortern?: boolean; | ||
}) { | ||
let value = address; | ||
|
||
if (shortern) { | ||
value = shortenAddress(address); | ||
} | ||
|
||
if (chainId) { | ||
value = `[${value}](${getExplorerUrl(address, chainId)})`; | ||
} | ||
return rowUI({ | ||
label, | ||
value, | ||
}); | ||
} | ||
|
||
/** | ||
* Build a row component with the network name. | ||
* | ||
* @param params - The parameters. | ||
* @param params.networkName - The network name. | ||
* @returns A row component with the network name. | ||
*/ | ||
export function networkUI({ networkName }: { networkName: string }) { | ||
return rowUI({ | ||
label: 'Network', | ||
value: networkName, | ||
}); | ||
} | ||
|
||
/** | ||
* Build a heading component. | ||
* | ||
* @param value - The header. | ||
* @returns A heading component. | ||
*/ | ||
export function headerUI(value: string) { | ||
return heading(value); | ||
} | ||
|
||
/** | ||
* Build a divider component | ||
* | ||
* @returns A divider component. | ||
*/ | ||
export function dividerUI() { | ||
return divider(); | ||
} | ||
|
||
/** | ||
* Build a row component with the signer address. | ||
* | ||
* @param params - The parameters. | ||
* @param params.address - The signer address. | ||
* @param params.chainId - The chain ID. | ||
* @returns A row component with the signer address. | ||
*/ | ||
export function signerUI({ | ||
address, | ||
chainId, | ||
}: { | ||
address: string; | ||
chainId: string; | ||
}) { | ||
return addressUI({ | ||
label: 'Signer Address', | ||
address, | ||
chainId, | ||
shortern: true, | ||
}); | ||
} | ||
|
||
/** | ||
* Build a row component with the JSON data. | ||
* | ||
* @param params - The parameters. | ||
* @param params.data - The JSON data. | ||
* @param params.label - The label. | ||
* @returns A row component with the JSON data. | ||
*/ | ||
export function jsonDataUI({ data, label }: { data: any; label: string }) { | ||
return rowUI({ | ||
label, | ||
value: toJson(data), | ||
}); | ||
} |
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