Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integrations/viem: initial package work #268

Closed
wants to merge 4 commits into from
Closed
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
9 changes: 7 additions & 2 deletions clients/js/scripts/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const LISTEN_PORT = 3000;
const DIE_ON_UNENCRYPTED = true;
const UPSTREAM_URL = 'http://127.0.0.1:8545';
const SHOW_ENCRYPTED_RESULTS = true;
const LOG_ALL = true;

console.log('DIE_ON_UNENCRYPTED', DIE_ON_UNENCRYPTED);
console.log('UPSTREAM_URL', UPSTREAM_URL);
Expand Down Expand Up @@ -65,7 +66,7 @@ async function onRequest(req: IncomingMessage, response: ServerResponse) {

let showResult = false;
for (const body of bodies) {
const log = loggedMethods.includes(body.method);
const log = LOG_ALL ?? loggedMethods.includes(body.method);

if (log) {
if (body.method === 'oasis_callDataPublicKey') {
Expand Down Expand Up @@ -134,6 +135,10 @@ async function onRequest(req: IncomingMessage, response: ServerResponse) {
);
}
}
else {
showResult = LOG_ALL;
console.log(body);
}
}
}

Expand All @@ -148,7 +153,7 @@ async function onRequest(req: IncomingMessage, response: ServerResponse) {
console.log(' - RESULT', pj);
}

response.writeHead(200, 'OK');
response.writeHead(200, 'OK', { 'Content-Type': 'application/json' });
response.write(JSON.stringify(pj));
response.end();
}
42 changes: 21 additions & 21 deletions examples/ethersv5-ts-esm/package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"name": "example-ethersv5-ts-esm",
"private": true,
"main": "lib/index.js",
"type": "module",
"scripts": {
"lint": "prettier --cache --plugin-search-dir=. --check *.cjs test/**.ts scripts/**.ts contracts/**.sol && solhint contracts/**.sol",
"format": "prettier --cache --plugin-search-dir=. --write *.cjs test/**.ts scripts/**.ts contracts/**.sol && solhint --fix contracts/**.sol",
"build": "tsc -b",
"test": "tsc -b && pnpm node build/examples/ethersv5-ts-esm/src/index.js"
},
"dependencies": {
"@oasisprotocol/sapphire-paratime": "workspace:^",
"ethers": "5.5.0"
},
"devDependencies": {
"@types/node": "^17.0.10",
"@tsconfig/strictest": "2.0.2",
"prettier": "^2.5.1",
"ts-node": "10.9.2",
"typescript": "4.7.4"
}
"name": "example-ethersv5-ts-esm",
"private": true,
"main": "lib/index.js",
"type": "module",
"scripts": {
"lint": "prettier --cache --plugin-search-dir=. --check *.cjs test/**.ts scripts/**.ts contracts/**.sol && solhint contracts/**.sol",
"format": "prettier --cache --plugin-search-dir=. --write *.cjs test/**.ts scripts/**.ts contracts/**.sol && solhint --fix contracts/**.sol",
"build": "tsc -b",
"test": "tsc -b && pnpm node build/examples/ethersv5-ts-esm/src/index.js"
},
"dependencies": {
"@oasisprotocol/sapphire-paratime": "workspace:^",
"ethers": "5.5.0"
},
"devDependencies": {
"@types/node": "^17.0.10",
"@tsconfig/strictest": "2.0.2",
"prettier": "^2.5.1",
"ts-node": "10.9.2",
"typescript": "4.7.4"
}
}
36 changes: 18 additions & 18 deletions examples/ethersv6-ts-esm/package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"name": "example-ethersv6-ts-esm",
"private": true,
"main": "lib/index.js",
"type": "module",
"scripts": {
"build": "tsc -b",
"test": "tsc -b && pnpm node build/examples/ethersv6-ts-esm/src/index.js"
},
"dependencies": {
"@oasisprotocol/sapphire-paratime": "workspace:^",
"ethers": "6.9.0"
},
"devDependencies": {
"@types/node": "^17.0.10",
"@tsconfig/strictest": "2.0.2",
"ts-node": "10.9.2",
"typescript": "5.3.3"
}
"name": "example-ethersv6-ts-esm",
"private": true,
"main": "lib/index.js",
"type": "module",
"scripts": {
"build": "tsc -b",
"test": "tsc -b && pnpm node build/examples/ethersv6-ts-esm/src/index.js"
},
"dependencies": {
"@oasisprotocol/sapphire-paratime": "workspace:^",
"ethers": "6.9.0"
},
"devDependencies": {
"@types/node": "^17.0.10",
"@tsconfig/strictest": "2.0.2",
"ts-node": "10.9.2",
"typescript": "5.3.3"
}
}
1 change: 1 addition & 0 deletions examples/viem/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
21 changes: 21 additions & 0 deletions examples/viem/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "example-viem",
"private": true,
"type": "module",
"main": "lib/index.js",
"scripts": {
"build": "tsc -b",
"test": "tsc -b && node --loader ts-node/esm src/index.ts"
},
"dependencies": {
"@oasisprotocol/sapphire-viem": "workspace:^",
"viem": "^2.7.1"
},
"devDependencies": {
"@tsconfig/strictest": "2.0.2",
"@types/node": "^17.0.10",
"abitype": "^1.0.0",
"ts-node": "10.9.2",
"typescript": "5.3.3"
}
}
41 changes: 41 additions & 0 deletions examples/viem/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Hex, createWalletClient, getContract, http, publicActions } from 'viem';

import OmnibusJSON from "../../../contracts/artifacts/contracts/tests/Omnibus.sol/Omnibus.json" assert { type: "json" };
import { narrow } from 'abitype'
import { privateKeyToAccount } from "viem/accounts";
import { sapphireLocalnet, wrapWalletClient } from '@oasisprotocol/sapphire-viem';


async function main () {
const account = privateKeyToAccount('0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80');

const transport = http('http://127.0.0.1:3000');

const walletClient = wrapWalletClient(createWalletClient({
account,
chain: sapphireLocalnet,
transport
}));

const hash = await walletClient.deployContract({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May not be using Viem right. I get a deploy error.

TransactionExecutionError: The method does not exist / is not available.
...
Details: the method eth_sendTransaction does not exist/is not available
Version: [email protected]
    at getTransactionError (file:///Users/xi/oasis/sapphire-paratime/node_modules/.pnpm/[email protected][email protected]/node_modules/viem/_esm/utils/errors/getTransactionError.js:11:12)
    at sendTransaction (file:///Users/xi/oasis/sapphire-paratime/node_modules/.pnpm/[email protected][email protected]/node_modules/viem/_esm/actions/wallet/sendTransaction.js:123:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async main (file:///Users/xi/oasis/sapphire-paratime/examples/viem/build/examples/viem/src/index.js:38:18)
    at async file:///Users/xi/oasis/sapphire-paratime/examples/viem/build/examples/viem/src/index.js:73:1 {
  details: 'the method eth_sendTransaction does not exist/is not available',
  docsPath: undefined,
  metaMessages: [
    'URL: http://127.0.0.1:8545',
    'Request body: {"method":"eth_sendTransaction","params":[{"data":"...","from":"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"}]}',
    ' ',
    'Request Arguments:',
    '  chain:  Oasis Sapphire Testnet (id: 23293)\n' +
      '  from:   0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266\n' +
      '  data:  ...'
  ],
  shortMessage: 'The method does not exist / is not available.',
  version: '[email protected]',
  cause: MethodNotFoundRpcError: The method does not exist / is not available.
  
  URL: http://127.0.0.1:8545

abi: narrow(OmnibusJSON.abi),
bytecode: OmnibusJSON.bytecode as Hex,
});

const pc = walletClient.extend(publicActions);
const receipt = await pc.waitForTransactionReceipt({hash});
console.log('Receipt', receipt);

const contractAddress = receipt.contractAddress!;

console.log('getContract')
const c = getContract({
address: contractAddress,
abi: narrow(OmnibusJSON.abi),
client: walletClient
})
const x = await c.read['testSignedQueries']!();
console.log(x);
}

await main ();
14 changes: 14 additions & 0 deletions examples/viem/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@tsconfig/strictest/tsconfig.json",
"compilerOptions": {
"outDir": "./build",
"module": "NodeNext",
"target": "ES2022",
"moduleResolution": "NodeNext",
"resolveJsonModule": true
},
"include": [
"./src"
]
}
4 changes: 4 additions & 0 deletions integrations/viem/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
dist
*.tgz
*.tsbuildinfo
83 changes: 83 additions & 0 deletions integrations/viem/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"type": "module",
"name": "@oasisprotocol/sapphire-viem",
"license": "Apache-2.0",
"version": "0.1.0",
"description": "Viem support for the Oasis Sapphire ParaTime.",
"homepage": "https://github.com/oasisprotocol/sapphire-paratime/tree/main/integrations/viem",
"repository": {
"type": "git",
"url": "https://github.com/oasisprotocol/sapphire-paratime.git"
},
"keywords": [
"sapphire",
"paratime",
"oasis",
"web3",
"viem",
"wagmi"
],
"files": [
"dist",
"!dist/*.tsbuildinfo",
"src"
],
"sideEffects": false,
"main": "./dist/_cjs/index.js",
"module": "./dist/_esm/index.js",
"types": "./dist/_types/index.d.ts",
"typings": "./dist/_types/index.d.ts",
"exports": {
"node": {
"import": "./dist/_esm/index.js",
"require": "./dist/_cjs/index.cjs",
"types": "./dist/_types/index.d.ts"
},
"default": "./dist/_esm/index.js"
},
"scripts": {
"lint": "prettier --cache --check . && eslint --ignore-path .gitignore .",
"format": "prettier --cache --write . && eslint --ignore-path .gitignore --fix .",
"build": "npm run clean && npm run build:cjs && npm run build:esm && npm run build:types",
"build:cjs": "tsc --project ./tsconfig.build.json --module commonjs --outDir ./dist/_cjs --removeComments --verbatimModuleSyntax false && printf '{\"type\":\"commonjs\"}' > ./dist/_cjs/package.json",
"build:esm": "tsc --project ./tsconfig.build.json --module es2015 --outDir ./dist/_esm && printf '{\"type\": \"module\",\"sideEffects\":false}' > ./dist/_esm/package.json",
"build:types": "tsc --project ./tsconfig.build.json --module esnext --declarationDir ./dist/_types --emitDeclarationOnly --declaration --declarationMap",
"clean": "rm -rf dist",
"test": "jest",
"coverage": "jest --coverage",
"prepublishOnly": "npm run build"
},
"dependencies": {
"@noble/hashes": "1.3.2",
"@oasisprotocol/deoxysii": "0.0.5",
"tweetnacl": "1.0.3",
"viem": "2.x",
"cborg": "1.10.2",
"type-fest": "2.19.0",
"@oasisprotocol/sapphire-paratime": "workspace:^"
},
"peerDependencies": {
"viem": "2.x",
"typescript": ">=5.0.4"
},
"peerDependenciesMeta": {
"typescript": {
"optional": true
}
},
"devDependencies": {
"@types/jest": "^29.5.11",
"@types/node": "^18.7.18",
"@types/node-fetch": "^2.6.2",
"@typescript-eslint/eslint-plugin": "^5.38.0",
"@typescript-eslint/parser": "^5.38.0",
"eslint": "^8.23.1",
"eslint-config-prettier": "^8.5.0",
"jest": "^29.7.0",
"node-fetch": "^2.6.7",
"prettier": "^2.7.1",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
}
}
97 changes: 97 additions & 0 deletions integrations/viem/src/chain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import type { Chain } from "viem";

export const sapphireLocalnet = {
id: 23293,
name: "Oasis Sapphire Localnet",
//network: "sapphire-localnet",
nativeCurrency: {
decimals: 18,
name: "Test Rose",
symbol: "TEST",
},
rpcUrls: {
public: {
http: ["http://localhost:8545"],
webSocket: ["wss://localhost:8545/ws"],
},
default: {
http: ["https://localhost:8545"],
webSocket: ["wss://localhost:8545/ws"],
},
},
blockExplorers: {
default: {
name: "Oasis Foundation",
url: "https://explorer.sapphire.oasis.io/",
},
},
} as const satisfies Chain;

export const sapphire = {
id: 23294,
name: "Oasis Sapphire",
//network: "sapphire",
nativeCurrency: {
decimals: 18,
name: "Rose",
symbol: "ROSE",
},
rpcUrls: {
public: {
http: ["https://sapphire.oasis.io"],
webSocket: ["wss://sapphire.oasis.io/ws"],
},
default: {
http: ["https://sapphire.oasis.io"],
webSocket: ["wss://sapphire.oasis.io/ws"],
},
},
blockExplorers: {
default: {
name: "Oasis Foundation",
url: "https://explorer.sapphire.oasis.io/",
},
},
} as const satisfies Chain;

export const sapphireTestnet = {
id: 23295,
name: "Oasis Sapphire Testnet",
//network: "sapphireTest",
nativeCurrency: {
decimals: 18,
name: "Rose",
symbol: "ROSE",
},
rpcUrls: {
public: {
http: ["https://testnet.sapphire.oasis.dev"],
webSocket: ["wss://testnet.sapphire.oasis.dev/ws"],
},
default: {
http: ["https://testnet.sapphire.oasis.dev"],
webSocket: ["wss://testnet.sapphire.oasis.dev/ws"],
},
},
blockExplorers: {
default: {
name: "Oasis Foundation",
url: "https://testnet.explorer.sapphire.oasis.dev/",
},
},
} as const satisfies Chain;

export const getSapphireChain = (chainId?: string): Chain => {
if( chainId === undefined ) {
return sapphire;
}
switch (chainId) {
case "23294":
return sapphire;
case "23295":
return sapphireTestnet;
case "23293":
return sapphireLocalnet;
}
throw new Error(`Unknown Sapphire chain id: ${chainId}`);
};
Loading
Loading