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

Migrate to manifest v3 #84

Merged
merged 3 commits into from
Jun 7, 2024
Merged
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
16 changes: 0 additions & 16 deletions background/index.html

This file was deleted.

8 changes: 6 additions & 2 deletions background/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"private": true,
"type": "module",
"scripts": {
"serve": "vite preview --port 8002",
"build": "vite build --base /background --outDir ../build/background",
"build": "tsc --noEmit && rollup --config",
"fmt": "pnpm dlx @biomejs/biome format --write ./src",
"test": "vitest"
},
Expand All @@ -25,12 +24,17 @@
},
"devDependencies": {
"@biomejs/biome": "^1.7.3",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.6",
"@types/chrome": "^0.0.266",
"@types/node": "20.12.7",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@vue/test-utils": "^1.3.0",
"eslint": "^7.32.0",
"eslint-plugin-vue": "^8.7.1",
"fast-check": "^3.19.0",
"rollup": "^4.16.4",
"typescript": "^5.4.5",
"vite": "^5.2.8",
"vitest": "^1.6.0",
Expand Down
17 changes: 17 additions & 0 deletions background/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from "@rollup/plugin-commonjs";
import typescript from "@rollup/plugin-typescript";

export default {
input: "src/main.ts",
output: {
dir: '../build/background',
format: 'iife'
},
plugins: [nodeResolve({
browser: true,
preferBuiltins: false,
}),
typescript(),
commonjs({ include: /node_modules/ })]
};
98 changes: 62 additions & 36 deletions background/src/api/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from "axios";
import type LocalStorage from "@/storage/local";
import type { IStorage } from "../storage/index.js";
import {
CURRENT_NETWORK,
NETWORKS,
Expand All @@ -8,10 +8,9 @@ import {
} from "../constants/constants";

async function getLastBlockIndex(endpoint: string) {
const { data } = await axios.create({ timeout: 10000 })({
const response = await fetch(endpoint, {
method: "POST",
url: endpoint,
data: {
body: JSON.stringify({
variables: { offset: 0 },
query: `
query getLastBlockIndex($offset: Int!) {
Expand All @@ -24,12 +23,17 @@ async function getLastBlockIndex(endpoint: string) {
}
}
`,
},
}),
headers: [
['content-type', 'application/json']
]
});

const data = await response.json();
return data["data"]["chainQuery"]["blockQuery"]["blocks"][0]["index"];
}

async function getEndpoints(storage: LocalStorage): Promise<string[]> {
async function getEndpoints(storage: IStorage): Promise<string[]> {
const currentNetworkId = await storage.get<NetworkId>(CURRENT_NETWORK);
const networks = await storage.get<Network[]>(NETWORKS);
const network = networks.find((n) => n.id === currentNetworkId);
Expand Down Expand Up @@ -59,7 +63,7 @@ async function getEndpoints(storage: LocalStorage): Promise<string[]> {
}

export default class Graphql {
private readonly storage: LocalStorage;
private readonly storage: Storage;
private readonly endpoints: string[];
private readonly canCall: string[];

Expand All @@ -80,7 +84,7 @@ export default class Graphql {
return this.canCall.indexOf(method) >= 0;
}

static async createInstance(storage: LocalStorage) {
static async createInstance(storage: IStorage) {
const endpoints = await getEndpoints(storage);
return new Graphql(storage, endpoints);
}
Expand All @@ -92,6 +96,7 @@ export default class Graphql {
const result = await fn(endpoint);
return result;
} catch (e) {
console.error('e', e);
exceptions.push(e);
}
}
Expand All @@ -109,28 +114,30 @@ export default class Graphql {

async getBalance(address: string): Promise<string> {
return this.callEndpoint(async (endpoint) => {
let { data } = await axios.create({ timeout: 10000 })({
const response = await fetch(endpoint, {
method: "POST",
url: endpoint,
data: {
body: JSON.stringify({
variables: { address: address },
query: `
query getBalance($address: Address!) {
goldBalance(address: $address)
}
`,
},
}),
headers: [
['content-type', 'application/json']
]
});

const data = await response.json();
return data["data"]["goldBalance"];
});
}
async getNextTxNonce(address: string): Promise<number> {
return this.callEndpoint(async (endpoint) => {
let { data } = await axios.create({ timeout: 10000 })({
const response = await fetch(endpoint, {
method: "POST",
url: endpoint,
data: {
body: JSON.stringify({
variables: { address: address },
query: `
query getNextTxNonce($address: Address!){
Expand All @@ -139,9 +146,13 @@ export default class Graphql {
}
}
`,
},
}),
headers: [
['content-type', 'application/json']
]
});

const data = await response.json();
return data["data"]["transaction"]["nextTxNonce"];
});
}
Expand All @@ -157,10 +168,9 @@ export default class Graphql {
decimalPlaces: 18,
};
return this.callEndpoint(async (endpoint) => {
let { data } = await axios({
const response = await fetch(endpoint, {
method: "POST",
url: endpoint,
data: {
body: JSON.stringify({
variables: {
publicKey: publicKey,
plainValue: plainValue,
Expand All @@ -174,8 +184,12 @@ export default class Graphql {
}
}
`,
},
}),
headers: [
['content-type', 'application/json']
]
});
const data = await response.json();
return data["data"]["transaction"]["unsignedTransaction"];
});
}
Expand All @@ -186,10 +200,9 @@ export default class Graphql {
amount: string,
): Promise<string> {
return this.callEndpoint(async (endpoint) => {
let { data } = await axios({
const response = await fetch(endpoint, {
method: "POST",
url: endpoint,
data: {
body: JSON.stringify({
variables: { sender: sender, receiver: receiver, amount: amount },
query: `
query getTransferAsset($sender: Address!, $receiver: Address!, $amount: String!){
Expand All @@ -198,8 +211,12 @@ export default class Graphql {
}
}
`,
},
}),
headers: [
['content-type', 'application/json']
]
});
const data = await response.json();
return data["data"]["actionQuery"]["transferAsset"];
});
}
Expand All @@ -209,29 +226,31 @@ export default class Graphql {
endpoint: string;
}> {
return this.callEndpoint(async (endpoint) => {
let { data } = await axios({
const response = await fetch(endpoint, {
method: "POST",
url: endpoint,
data: {
body: JSON.stringify({
variables: { payload },
query: `
mutation transfer($payload: String!) {
stageTransaction(payload: $payload)
}
`,
},
}),
headers: [
['content-type', 'application/json']
]
});
const data = await response.json();
return { txId: data["data"]["stageTransaction"], endpoint };
});
}

async getActivationStatus(address: string): Promise<boolean> {
console.log("getActivationStatus", this.endpoints, address);
return this.callEndpoint(async (endpoint) => {
let { data } = await axios({
const response = await fetch(endpoint, {
method: "POST",
url: endpoint,
data: {
body: JSON.stringify({
variables: { address },
query: `
query getPledge($address: Address!) {
Expand All @@ -242,18 +261,21 @@ export default class Graphql {
}
}
`,
},
}),
headers: [
['content-type', 'application/json']
]
});
const data = await response.json();
console.log("getActivationStatus", data);
return data["data"]["stateQuery"]["pledge"]["approved"];
});
}

async getTransactionStatus({ txId, endpoint }) {
let { data } = await axios({
const response = await fetch(endpoint, {
method: "POST",
url: endpoint,
data: {
body: JSON.stringify({
variables: { txId },
query: `
query query($txId: TxId!) {
Expand All @@ -264,8 +286,12 @@ export default class Graphql {
}
}
`,
},
}),
headers: [
['content-type', 'application/json']
]
});
const data = await response.json();
return data["data"]["transaction"]["transactionResult"]["txStatus"];
}
}
4 changes: 2 additions & 2 deletions background/src/controllers/confirmation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { APPROVAL_REQUESTS } from "@/constants/constants";
import { APPROVAL_REQUESTS } from "../constants/constants";
import { nanoid } from "nanoid";
import { PopupController } from "./popup";
import { IStorage } from "@/storage";
import { IStorage } from "../storage";

interface Request {
id: string;
Expand Down
6 changes: 3 additions & 3 deletions background/src/controllers/network.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { IStorage } from "@/storage/index.js";
import { IStorage } from "../storage/index.js";
import {
CURRENT_NETWORK,
NETWORKS,
type Network,
type NetworkId,
} from "@/constants/constants";
import { Emitter } from "@/event";
} from "../constants/constants";
import { Emitter } from "../event";

export class NetworkController {
constructor(
Expand Down
Loading