Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 20 additions & 24 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
module.exports = {
root: true,
extends: [
'@metamask/eslint-config',
'@metamask/eslint-config/config/mocha',
],
ignorePatterns: [
'!.eslintrc.js',
'dist/',
],
overrides: [{
extends: [
'@metamask/eslint-config/config/nodejs',
],
files: [
'.eslintrc.js',
'test/**/*.js',
],
parserOptions: {
sourceType: 'script',
extends: ['@metamask/eslint-config', '@metamask/eslint-config-mocha'],
ignorePatterns: ['!.eslintrc.js', 'dist/'],
rules: {
// TODO: Fix jsdoc comments and enable rules
'jsdoc/require-description': 'off',
'jsdoc/require-param-description': 'off',
'jsdoc/require-returns': 'off',
},
overrides: [
{
extends: ['@metamask/eslint-config-nodejs'],
files: ['.eslintrc.js', 'test/**/*.js'],
parserOptions: {
sourceType: 'script',
},
},
}, {
extends: [
'@metamask/eslint-config/config/typescript',
],
files: ['**/*.ts'],
}],
{
extends: ['@metamask/eslint-config-typescript'],
files: ['**/*.ts'],
},
],
};
22 changes: 11 additions & 11 deletions .github/workflows/build-lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Build, Lint, and Test

on:
push:
branches: [ main ]
branches: [main]
pull_request:

jobs:
Expand All @@ -13,19 +13,19 @@ jobs:
matrix:
node-version: [18.x, 20.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: yarn --frozen-lockfile
- run: yarn build
- run: yarn lint
- run: yarn test
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: yarn --frozen-lockfile
- run: yarn build
- run: yarn lint
- run: yarn test
all-jobs-pass:
name: All jobs pass
runs-on: ubuntu-latest
needs:
- build-lint-test
steps:
- run: echo "Great success!"
- run: echo "Great success!"
6 changes: 3 additions & 3 deletions .github/workflows/security-code-scanner.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: 'MetaMask Security Code Scanner'
name: "MetaMask Security Code Scanner"

on:
push:
branches: ['main']
branches: ["main"]
pull_request:
branches: ['main']
branches: ["main"]

jobs:
run-security-scan:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.tgz
.eslintcache
dist/
node_modules/
84 changes: 58 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,73 @@
## Usage

```javascript
const etherscanLink = require('@metamask/etherscan-link')
const networkId = '1'
const chainId = '0x1'
const account = '0xFDEa65C8e26263F6d9A1B5de9555D2931A33b825'
const accountLink = etherscanLink.createAccountLink(account, networkId)
const accountLinkForChain = etherscanLink.createAccountLinkForChain(account, chainId)

const hash = '0xa7540793de6b6ca7d3c948a8cc0a163bf107f5535a69353162ea9dec7ee7beca'
const txLink = etherscanLink.createExplorerLink(hash, networkId)
const txtLinkForChain = etherscanLink.createExplorerLinkForChain(hash, chainId)

const token = '0xdac17f958d2ee523a2206206994597c13d831ec7'
const tokenTrackerLink = etherscanLink.createTokenTrackerLink(token, networkId)
const etherscanLink = require("@metamask/etherscan-link");
const networkId = "1";
const chainId = "0x1";
const account = "0xFDEa65C8e26263F6d9A1B5de9555D2931A33b825";
const accountLink = etherscanLink.createAccountLink(account, networkId);
const accountLinkForChain = etherscanLink.createAccountLinkForChain(
account,
chainId
);

const hash =
"0xa7540793de6b6ca7d3c948a8cc0a163bf107f5535a69353162ea9dec7ee7beca";
const txLink = etherscanLink.createExplorerLink(hash, networkId);
const txtLinkForChain = etherscanLink.createExplorerLinkForChain(hash, chainId);

const token = "0xdac17f958d2ee523a2206206994597c13d831ec7";
const tokenTrackerLink = etherscanLink.createTokenTrackerLink(token, networkId);
// You can also track token balances by account
const accountTokenTrackerLink = etherscanLink.createTokenTrackerLink(token, networkId, account)
const accountTokenTrackerLink = etherscanLink.createTokenTrackerLink(
token,
networkId,
account
);

const accountTokenTrackerLinkForChain = etherscanLink.createTokenTrackerLinkForChain(token, chainId, account)
const accountTokenTrackerLinkForChain =
etherscanLink.createTokenTrackerLinkForChain(token, chainId, account);

// Create urls for interacting with custom networks
const customNetworkUrl = 'https://customnetwork.com/'
const customNetworkUrl = "https://customnetwork.com/";

const customTokenTrackerLink = etherscanLink.createCustomTokenTrackerLink(token, customNetworkUrl)
const customTokenTrackerLink = etherscanLink.createCustomTokenTrackerLink(
token,
customNetworkUrl
);

const customAccountLink = etherscanLink.createCustomAccountLink(account, customNetworkUrl)
const customAccountLink = etherscanLink.createCustomAccountLink(
account,
customNetworkUrl
);

const customExplorerLink = etherscanLink.createCustomExplorerLink(hash, customNetworkUrl)
const customExplorerLink = etherscanLink.createCustomExplorerLink(
hash,
customNetworkUrl
);

// Generate custom or native block explorer link based on rcpPrefs
const blockExplorerLink = etherscanLink.getBlockExplorerLink(transaction, rcpPrefs)

// Generate account link for custom or native network
const getAccountLink = etherscanLink.getAccountLink(address, chainId, rpcPrefs, networkId)

// Generate token tracker link for custom or native network
const tokenTrackerLink = etherscanLink.getTokenTrackerLink(tokenAddress, chainId, networkId, holderAddress, rpcPrefs)
const blockExplorerLink = etherscanLink.getBlockExplorerLink(
transaction,
rcpPrefs
);

// Generate account link for custom or native network
const getAccountLink = etherscanLink.getAccountLink(
address,
chainId,
rpcPrefs,
networkId
);

// Generate token tracker link for custom or native network
const tokenTrackerLink = etherscanLink.getTokenTrackerLink(
tokenAddress,
chainId,
networkId,
holderAddress,
rpcPrefs
);
```

## Running tests
Expand Down
33 changes: 23 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
},
"scripts": {
"build": "rm -rf dist/* && tsc",
"lint": "eslint --ext js,ts .",
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn lint:dependencies --check",
"lint:dependencies": "depcheck",
"lint:eslint": "eslint . --cache --ext js,ts",
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write && yarn lint:dependencies",
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' '**/*.yml' '!.yarnrc.yml' --ignore-path .gitignore --no-error-on-unmatched-pattern",
"test": "yarn build && mocha test",
"prepublishOnly": "yarn test"
},
Expand All @@ -35,14 +39,23 @@
},
"homepage": "https://github.com/MetaMask/etherscan-link#readme",
"devDependencies": {
"@metamask/eslint-config": "^5.0.0",
"@typescript-eslint/eslint-plugin": "^4.14.2",
"@typescript-eslint/parser": "^4.14.2",
"eslint": "^7.7.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-mocha": "^8.0.0",
"eslint-plugin-node": "^11.1.0",
"mocha": "^7.1.1",
"typescript": "^4.0.5"
"@metamask/eslint-config": "^12.2.0",
"@metamask/eslint-config-mocha": "^12.1.0",
"@metamask/eslint-config-nodejs": "^12.1.0",
"@metamask/eslint-config-typescript": "^12.1.0",
"@typescript-eslint/eslint-plugin": "^5.30.7",
"@typescript-eslint/parser": "^5.30.7",
"depcheck": "^1.4.7",
"eslint": "^8.36.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "~2.26.0",
"eslint-plugin-jsdoc": "^43.0.7",
"eslint-plugin-mocha": "^10.1.0",
"eslint-plugin-n": "^15.7.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-promise": "^6.1.1",
"mocha": "^9.2.2",
"prettier": "^2.7.1",
"typescript": "~4.8.4"
}
}
52 changes: 45 additions & 7 deletions src/account-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,67 @@ import { addPathToUrl } from './helpers';
import prefixForChain from './prefix-for-chain';
import prefixForNetwork from './prefix-for-network';

interface RpcPrefsInterface {
type RpcPrefsInterface = {
blockExplorerUrl?: string;
}
};

/**
*
* @param address
* @param networkId
*/
export function createAccountLink(address: string, networkId: string): string {
const prefix = prefixForNetwork(networkId);
return prefix === null ? '' : `https://${prefix}etherscan.io/address/${address}`;
return prefix === null
? ''
: `https://${prefix}etherscan.io/address/${address}`;
}

export function createAccountLinkForChain(address: string, chainId: string): string {
/**
*
* @param address
* @param chainId
*/
export function createAccountLinkForChain(
address: string,
chainId: string,
): string {
const prefix = prefixForChain(chainId);
return prefix === null ? '' : `https://${prefix}etherscan.io/address/${address}`;
return prefix === null
? ''
: `https://${prefix}etherscan.io/address/${address}`;
}

export function createCustomAccountLink(address: string, customNetworkUrl: string): string {
/**
*
* @param address
* @param customNetworkUrl
*/
export function createCustomAccountLink(
address: string,
customNetworkUrl: string,
): string {
const parsedUrl = addPathToUrl(customNetworkUrl, 'address', address);
return parsedUrl;
}

export function getAccountLink(address: string, chainId: string, rpcPrefs: RpcPrefsInterface = {}, networkId = '') {
/**
*
* @param address
* @param chainId
* @param rpcPrefs
* @param networkId
*/
export function getAccountLink(
address: string,
chainId: string,
rpcPrefs: RpcPrefsInterface = {},
networkId = '',
) {
if (rpcPrefs.blockExplorerUrl) {
return createCustomAccountLink(address, rpcPrefs.blockExplorerUrl);
}

if (networkId) {
return createAccountLink(address, networkId);
}
Expand Down
Loading