This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create test snap for ethereum-provider endowment (#169)
- Loading branch information
1 parent
30f258f
commit 494b723
Showing
12 changed files
with
329 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Changelog | ||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | ||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
||
## [Unreleased] | ||
|
||
[Unreleased]: https://github.com/MetaMask/test-snaps/ |
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,3 @@ | ||
# ethereum-provider endowment Test Snap | ||
|
||
This Snap is used to test the ethereum-provider endowment. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,65 @@ | ||
{ | ||
"name": "@metamask/test-snap-ethprovider", | ||
"version": "5.3.0", | ||
"description": "MetaMask ethereum-provider Test Snap", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/MetaMask/test-snaps.git" | ||
}, | ||
"publishConfig": { | ||
"access": "public", | ||
"registry": "https://registry.npmjs.org/" | ||
}, | ||
"license": "ISC", | ||
"files": [ | ||
"dist/", | ||
"images/icon.svg", | ||
"snap.manifest.json" | ||
], | ||
"scripts": { | ||
"build": "mm-snap build --eval false", | ||
"build:clean": "yarn clean && yarn build", | ||
"build:dev": "yarn build", | ||
"clean": "rimraf 'dist/*'", | ||
"start": "mm-snap watch", | ||
"test": "jest --passWithNoTests", | ||
"test:watch": "jest --watch", | ||
"lint:eslint": "eslint . --cache --ext js,ts", | ||
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' '**/*.yml' --single-quote --ignore-path ../../.prettierignore --no-error-on-unmatched-pattern", | ||
"lint": "yarn lint:eslint && yarn lint:misc --check", | ||
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write", | ||
"lint:changelog": "yarn auto-changelog validate" | ||
}, | ||
"dependencies": { | ||
"@metamask/snaps-types": "^0.30.0", | ||
"@metamask/snaps-ui": "^0.32.2" | ||
}, | ||
"devDependencies": { | ||
"@metamask/auto-changelog": "^2.5.0", | ||
"@metamask/eslint-config": "^6.0.0", | ||
"@metamask/eslint-config-jest": "^6.0.0", | ||
"@metamask/eslint-config-nodejs": "^6.0.0", | ||
"@metamask/eslint-config-typescript": "^6.0.0", | ||
"@metamask/snaps-cli": "^0.30.0", | ||
"@types/jest": "^26.0.13", | ||
"@typescript-eslint/eslint-plugin": "^5.19.0", | ||
"@typescript-eslint/parser": "^5.19.0", | ||
"eslint": "^7.30.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-import": "^2.23.4", | ||
"eslint-plugin-jest": "^24.4.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
"jest": "^26.4.2", | ||
"prettier": "^2.2.1", | ||
"rimraf": "^3.0.2", | ||
"ts-jest": "^26.3.0", | ||
"ts-node": "^9.0.0", | ||
"typescript": "^4.4.0" | ||
}, | ||
"engines": { | ||
"node": ">=16.0.0" | ||
} | ||
} |
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,6 @@ | ||
module.exports = { | ||
cliOptions: { | ||
port: 8013, | ||
src: './src/index.ts', | ||
}, | ||
}; |
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,28 @@ | ||
{ | ||
"version": "5.3.0", | ||
"description": "A MetaMask Test Snap for ethereum-provider endowment.", | ||
"proposedName": "ethereum-provider test Snap", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/MetaMask/test-snaps.git" | ||
}, | ||
"source": { | ||
"shasum": "8hZn2A9PZFbF2saRx0vTW2td742r2HMMEYJ+FZH+BNQ=", | ||
"location": { | ||
"npm": { | ||
"filePath": "dist/bundle.js", | ||
"iconPath": "images/icon.svg", | ||
"packageName": "@metamask/test-snap-ethprovider", | ||
"registry": "https://registry.npmjs.org/" | ||
} | ||
} | ||
}, | ||
"initialPermissions": { | ||
"endowment:rpc": { | ||
"dapps": true, | ||
"snaps": false | ||
}, | ||
"endowment:ethereum-provider": {} | ||
}, | ||
"manifestVersion": "0.1" | ||
} |
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,19 @@ | ||
import { OnRpcRequestHandler } from '@metamask/snaps-types'; | ||
|
||
/** | ||
* Fetches the gas price with ethereum.request. | ||
*/ | ||
async function getGas() { | ||
const gasPriceHex = await ethereum.request({ method: 'eth_gasPrice' }); | ||
const gasPrice = parseInt(gasPriceHex, 16); | ||
return gasPrice > 0; | ||
} | ||
|
||
export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => { | ||
switch (request.method) { | ||
case 'ethproviderTest': | ||
return getGas(); | ||
default: | ||
throw new Error('Method not found.'); | ||
} | ||
}; |
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,10 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"lib": ["DOM", "ES2020"], | ||
"outDir": "build", | ||
"rootDir": "." | ||
}, | ||
"exclude": ["./src/**/*.test.ts", "./build"], | ||
"include": ["./src/**/*.ts"] | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/site/src/features/ethprovider-snap/Ethprovider.tsx
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,44 @@ | ||
import { FunctionComponent } from 'react'; | ||
import { Button } from 'react-bootstrap'; | ||
import { Result, Snap } from '../../components'; | ||
import { useInvokeMutation } from '../../api'; | ||
import { getSnapId } from '../../utils/id'; | ||
|
||
const ETHPROVIDER_SNAP_ID = 'npm:@metamask/test-snap-ethprovider'; | ||
const ETHPROVIDER_SNAP_PORT = 8013; | ||
|
||
export const Ethprovider: FunctionComponent = () => { | ||
const [invokeSnap, { isLoading, data, error }] = useInvokeMutation(); | ||
|
||
const handleSubmit = () => { | ||
invokeSnap({ | ||
snapId: getSnapId(ETHPROVIDER_SNAP_ID, ETHPROVIDER_SNAP_PORT), | ||
method: 'ethproviderTest', | ||
}); | ||
}; | ||
|
||
return ( | ||
<Snap | ||
name="ethereum-provider Snap" | ||
snapId={ETHPROVIDER_SNAP_ID} | ||
port={ETHPROVIDER_SNAP_PORT} | ||
testId="EthproviderSnap" | ||
> | ||
<Button | ||
variant="primary" | ||
id="sendEthprovider" | ||
className="mb-3" | ||
disabled={isLoading} | ||
onClick={handleSubmit} | ||
> | ||
Send Test to ethereum-provider Snap | ||
</Button> | ||
<Result> | ||
<span id="ethproviderResult"> | ||
{JSON.stringify(data, null, 2)} | ||
{JSON.stringify(error, null, 2)} | ||
</span> | ||
</Result> | ||
</Snap> | ||
); | ||
}; |
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 @@ | ||
export * from './Ethprovider'; |
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