Skip to content

Commit

Permalink
fix: comments applied. renamed tezos calls
Browse files Browse the repository at this point in the history
  • Loading branch information
dianasavvatina committed Nov 21, 2024
1 parent 1b40b6f commit 8e6fc54
Show file tree
Hide file tree
Showing 10 changed files with 963 additions and 817 deletions.
3 changes: 0 additions & 3 deletions .swcrc

This file was deleted.

40 changes: 21 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,28 @@ This provider manages the connection to the Tezos network, facilitates transacti

```
npm i @walletconnect/tezos-provider @walletconnect/modal
npx install-peerdeps @trili/tezos-provider
```

## Initialization

To use `TezosProvider`, you first need to initialize it with the necessary options:

```typescript
import TezosProvider from 'path-to-tezos-provider';
import TezosProvider from "path-to-tezos-provider";

const provider = await TezosProvider.init({
projectId: 'your-project-id', // REQUIRED WalletConnect project ID
projectId: "your-project-id", // REQUIRED WalletConnect project ID
metadata: {
name: 'Your DApp Name',
description: 'Your DApp Description',
url: 'https://your-dapp-url.com',
icons: ['https://your-dapp-url.com/icon.png'],
name: "Your DApp Name",
description: "Your DApp Description",
url: "https://your-dapp-url.com",
icons: ["https://your-dapp-url.com/icon.png"],
},
relayUrl: 'wss://relay.walletconnect.com', // OPTIONAL WalletConnect relay URL
relayUrl: "wss://relay.walletconnect.com", // OPTIONAL WalletConnect relay URL
storageOptions: {}, // OPTIONAL key-value storage settings
disableProviderPing: false, // OPTIONAL set to true to disable provider ping
logger: 'info', // OPTIONAL log level, default is 'info'
logger: "info", // OPTIONAL log level, default is 'info'
});
```

Expand All @@ -50,11 +51,11 @@ After initializing the provider, you can connect it to the Tezos network:
await provider.connect({
chains: [
{
id: 'tezos:mainnet',
rpc: ['https://mainnet-tezos.giganode.io'],
id: "tezos:mainnet",
rpc: ["https://mainnet-tezos.giganode.io"],
},
],
methods: ['tezos_getAccounts', 'tezos_send', 'tezos_sign'],
methods: ["tezos_getAccounts", "tezos_send", "tezos_sign"],
events: [], // OPTIONAL Tezos events
});
```
Expand All @@ -70,7 +71,7 @@ If you are not using a modal for QR code display, you can subscribe to the `disp
```typescript
provider.on("display_uri", (uri: string) => {
// Handle the connection URI
console.log('Connection URI:', uri);
console.log("Connection URI:", uri);
});

await provider.connect();
Expand All @@ -96,12 +97,12 @@ To send a transaction:

```typescript
const transactionResponse = await provider.tezosSendTransaction({
kind: 'transaction',
destination: 'tz1...',
amount: '1000000', // Amount in mutez
kind: "transaction",
destination: "tz1...",
amount: "1000000", // Amount in mutez
});

console.log('Transaction hash:', transactionResponse.hash);
console.log("Transaction hash:", transactionResponse.hash);
```

### Sign Messages
Expand All @@ -110,14 +111,14 @@ To sign a message, encode it to hex first:

```typescript
const textEncoder = new TextEncoder();
const bytes = textEncoder.encode('Your string here');
const hexBytes = Buffer.from(bytes).toString('hex');
const bytes = textEncoder.encode("Your string here");
const hexBytes = Buffer.from(bytes).toString("hex");

const signResponse = await provider.tezosSign({
payload: hexBytes,
});

console.log('Signature:', signResponse.signature);
console.log("Signature:", signResponse.signature);
```

## Events
Expand All @@ -140,6 +141,7 @@ provider.on("disconnect", handler);
```

## Error Handling

The provider will throw errors if:

- `TezosInitializationError`: If the provider is not initialized correctly.
Expand Down
9 changes: 4 additions & 5 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

import tsParser from "@typescript-eslint/parser"; // Import the TypeScript parser
import tsPlugin from "@typescript-eslint/eslint-plugin"; // Import the TypeScript plugin
import prettierPlugin from "eslint-plugin-prettier"; // Import the Prettier plugin
import importPlugin from 'eslint-plugin-import';
import importPlugin from "eslint-plugin-import";

export default {
languageOptions: {
Expand All @@ -22,10 +21,10 @@ export default {
"no-undef": ["error"],
"no-var": ["error"],
"object-curly-spacing": ["error", "always"],
"quotes": ["error", "double", { "allowTemplateLiterals": true }],
"semi": ["error", "always"],
quotes: ["error", "double", { allowTemplateLiterals: true }],
semi: ["error", "always"],
"no-console": ["error", { allow: ["warn"] }],
"import/no-extraneous-dependencies": ["error"],
"import/no-extraneous-dependencies": ["error"],
"import/order": [
"warn",
{
Expand Down
5 changes: 3 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default {
preset: 'ts-jest',
testEnvironment: 'node',
preset: "ts-jest",
testEnvironment: "node",
setupFiles: ["./test/jest.setup.js"],
};
61 changes: 37 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
{
"name": "@trili/tezos-provider",
"description": "Tezos Provider for WalletConnect Protocol",
"version": "1.0.3",
"version": "1.0.4",
"author": "Trilitech TriliTech <[email protected]> (https://trili.tech)",
"homepage": "https://trili.tech",
"repository": {
"type": "git",
"url": "https://github.com/trilitech/tezos-connect",
"url": "git+https://github.com/trilitech/tezos-connect.git",
"directory": "."
},
"license": "MIT",
"type": "module",
"main": "dist/index.cjs.js",
"module": "dist/index.es.js",
"unpkg": "dist/index.umd.js",
"types": "dist/types/index.d.ts",
"sideEffects": false,
"main": "dist/index.cjs",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
}
Expand All @@ -33,39 +30,52 @@
"scripts": {
"clean": "rm -rf dist",
"build": "tsup-node",
"test:pre": "rm -rf ./test/test.db",
"test:run": "jest",
"test": "yarn test:pre; yarn test:run",
"prettier": "prettier --check '{src,test}/**/*.{js,ts,jsx,tsx}' --write",
"lint": "eslint -c 'eslint.config.mjs' --fix './src/**/*.ts'",
"lint:ci": "eslint 'src/**/*.ts' -f json -o lintReport.json || true"
"test": "jest",
"prettier": "prettier --write .",
"lint": "eslint -c eslint.config.mjs --fix ./src/**/*.ts",
"lint:ci": "eslint src/**/*.ts -f json -o lintReport.json || true"
},
"tsup": {
"dts": true,
"entry": [
"src/index.ts"
],
"dts": true,
"clean": true,
"format": [
"cjs",
"esm"
],
"external": [
"@taquito/taquito",
"@taquito/rpc",
"@airgap/beacon-types",
"@walletconnect/universal-provider",
"@walletconnect/types",
"@walletconnect/keyvaluestorage",
"@walletconnect/logger"
]
},
"dependencies": {
"@airgap/beacon-types": "^4.3.1",
"@taquito/rpc": "^20.1.0",
"@taquito/taquito": "^20.1.0",
"@walletconnect/keyvaluestorage": "^1.1.1",
"@walletconnect/logger": "^2.1.2",
"@walletconnect/types": "^2.17.2",
"@walletconnect/universal-provider": "^2.17.2",
"i": "^0.3.7",
"micromatch": "^4.0.8",
"package.json": "^2.0.1"
},
"peerDependencies": {
"@airgap/beacon-types": "^4.2.2",
"@taquito/rpc": "~20.0.1",
"@taquito/taquito": "~20.0.1",
"@trili/tezos-provider": "^1.0.2",
"@walletconnect/keyvaluestorage": "^1.1.1",
"@walletconnect/logger": "^2.1.2",
"@walletconnect/types": "2.13.3",
"@walletconnect/universal-provider": "^2.17.1",
"axios": "^1.7.7",
"micromatch": "^4.0.8",
"package.json": "^2.0.1",
"rollup": "^4.24.0",
"typescript": "^5.6.3",
"yarn.lock": "^0.0.1-security"
"@walletconnect/universal-provider": "^2.17.1"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
Expand All @@ -74,14 +84,17 @@
"@typescript-eslint/eslint-plugin": "^8.8.1",
"@typescript-eslint/parser": "^8.8.1",
"depcheck": "^1.4.7",
"eslint": "^8.57.0",
"eslint": "^9.0.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-prettier": "^5.2.1",
"install-peerdeps": "^3.0.3",
"jest": "^29.7.0",
"prettier": "^3.3.3",
"rollup": "^4.24.0",
"ts-jest": "^29.2.5",
"tsup": "^8.2.4"
"tsup": "^8.2.4",
"typescript": "^5.6.3"
},
"engines": {
"node": ">=18"
Expand Down
12 changes: 9 additions & 3 deletions src/TezosProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { KeyValueStorageOptions } from "@walletconnect/keyvaluestorage";
import { Logger } from "@walletconnect/logger";
import { SessionTypes } from "@walletconnect/types";
import { UniversalProvider, Metadata } from "@walletconnect/universal-provider";
import axios from "axios";

import {
DefaultTezosMethods,
Expand Down Expand Up @@ -65,6 +64,13 @@ type PartialTezosOperation =
>
| PartialTezosOriginationOperation;

interface Operation {
status: string;
originatedContract: {
kind: string;
address: string;
};
}
export interface TezosProviderOpts {
projectId: string;
metadata: Metadata;
Expand Down Expand Up @@ -227,8 +233,8 @@ export class TezosProvider {

const api = this.chainMap[this.chainId].api;
const path = `${api}/operations/${hash}`;
const response = await axios.get(path);
const data = response.data;
const response = await globalThis.fetch(path);
const data = (await response.json()) as Operation[];

return data
.map((op: any) => {
Expand Down
20 changes: 14 additions & 6 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { describe, it, expect, beforeEach } from "@jest/globals";
import { ScriptedContracts } from "@taquito/rpc";
import { TezosToolkit } from "@taquito/taquito";
import { UniversalProvider } from "@walletconnect/universal-provider";
import axios from "axios";
import fetchMock from "jest-fetch-mock";

import { SAMPLES, SAMPLE_KINDS } from "./samples";
import { TezosChainDataMainnet, UnsupportedOperations } from "../src/constants";
Expand All @@ -19,6 +19,7 @@ import {
TezosInitializationError,
TezosMethod,
} from "../src/types";
fetchMock.enableMocks();

interface PartialTezosOriginationOperation
extends Omit<PartialTezosOriginationOperationOriginal, "script"> {
Expand All @@ -34,7 +35,6 @@ type PartialTezosOperation =

jest.mock("@walletconnect/universal-provider");
jest.mock("@taquito/taquito");
jest.mock("axios");

describe("TezosProvider", () => {
let provider: TezosProvider;
Expand All @@ -51,6 +51,14 @@ describe("TezosProvider", () => {
namespaces: { tezos: { accounts: ["tezos:mainnet:address1"] } },
},
});
global.fetch = jest.fn().mockResolvedValue({
json: jest.fn().mockResolvedValue([
{
status: "applied",
originatedContract: { kind: "smart_contract", address: "KT1..." },
},
]),
});

mockInit.mockClear();
mockConnect.mockClear();
Expand Down Expand Up @@ -257,13 +265,13 @@ describe("TezosProvider", () => {
});

it("should call getContractAddress and return contract addresses", async () => {
(axios.get as jest.Mock).mockResolvedValue({
data: [
global.fetch = jest.fn().mockResolvedValue({
json: jest.fn().mockResolvedValue([
{
status: "applied",
originatedContract: { kind: "smart_contract", address: "KT1..." },
},
],
]),
});
provider = await TezosProvider.init({
projectId: "test",
Expand All @@ -275,7 +283,7 @@ describe("TezosProvider", () => {
expect(contractAddresses).toEqual(["KT1..."]);
const api = provider.chainMap[provider.chainId].api;
const expectedUrl = `${api}/operations/opHash`;
expect(axios.get).toHaveBeenCalledWith(expectedUrl);
expect(fetch).toHaveBeenCalledWith(expectedUrl);
});

it("should handle getCurrentProposal correctly", async () => {
Expand Down
1 change: 1 addition & 0 deletions test/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global.fetch = jest.fn();
Loading

0 comments on commit 8e6fc54

Please sign in to comment.