Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
Create test snap for ethereum-provider endowment (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
bowensanders authored May 12, 2023
1 parent 30f258f commit 494b723
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/ethprovider/CHANGELOG.md
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/
3 changes: 3 additions & 0 deletions packages/ethprovider/README.md
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.
102 changes: 102 additions & 0 deletions packages/ethprovider/images/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions packages/ethprovider/package.json
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"
}
}
6 changes: 6 additions & 0 deletions packages/ethprovider/snap.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
cliOptions: {
port: 8013,
src: './src/index.ts',
},
};
28 changes: 28 additions & 0 deletions packages/ethprovider/snap.manifest.json
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"
}
19 changes: 19 additions & 0 deletions packages/ethprovider/src/index.ts
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.');
}
};
10 changes: 10 additions & 0 deletions packages/ethprovider/tsconfig.json
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 packages/site/src/features/ethprovider-snap/Ethprovider.tsx
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>
);
};
1 change: 1 addition & 0 deletions packages/site/src/features/ethprovider-snap/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Ethprovider';
2 changes: 2 additions & 0 deletions packages/site/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Rpc } from '../features/rpc-snap';
import { Insights } from '../features/insights-snap';
import { WebAssembly } from '../features/wasm-snap';
import { GetEntropy } from '../features/getentropy-snap';
import { Ethprovider } from '../features/ethprovider-snap';

interface Query {
site: {
Expand Down Expand Up @@ -46,6 +47,7 @@ const Index: FunctionComponent = () => {
<Cronjob />
<WebAssembly />
<GetEntropy />
<Ethprovider />
</Row>
</Container>
);
Expand Down
40 changes: 40 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2972,6 +2972,16 @@ __metadata:
languageName: node
linkType: hard

"@metamask/snaps-ui@npm:^0.32.2":
version: 0.32.2
resolution: "@metamask/snaps-ui@npm:0.32.2"
dependencies:
"@metamask/utils": ^5.0.0
superstruct: ^1.0.3
checksum: 98e8900efb63a0f1ec4893624290a1bf2d0fa416f99d57f33532865063f4e4b9b3787b77630629c64d7dc041e3508cac81044eca067ed76a07aa380bc5997db2
languageName: node
linkType: hard

"@metamask/snaps-utils@npm:^0.30.0":
version: 0.30.0
resolution: "@metamask/snaps-utils@npm:0.30.0"
Expand Down Expand Up @@ -3159,6 +3169,36 @@ __metadata:
languageName: unknown
linkType: soft

"@metamask/test-snap-ethprovider@workspace:packages/ethprovider":
version: 0.0.0-use.local
resolution: "@metamask/test-snap-ethprovider@workspace:packages/ethprovider"
dependencies:
"@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
"@metamask/snaps-types": ^0.30.0
"@metamask/snaps-ui": ^0.32.2
"@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
languageName: unknown
linkType: soft

"@metamask/test-snap-getentropy@workspace:packages/getEntropy":
version: 0.0.0-use.local
resolution: "@metamask/test-snap-getentropy@workspace:packages/getEntropy"
Expand Down

0 comments on commit 494b723

Please sign in to comment.