Skip to content

Commit

Permalink
ETH Testrun Bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kewbish committed Dec 30, 2022
1 parent 9815315 commit 3afdb24
Show file tree
Hide file tree
Showing 21 changed files with 274 additions and 311 deletions.
44 changes: 36 additions & 8 deletions src/pages/Background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
DELETE_STREAM,
BLOCK_TAG,
UPDATE_SETTING,
EDIT_CURRENT_STREAM,
UPDATE_STREAM,
FETCH_BALANCE,
APPROVE_AMT,
DOWNGRADE_TOKEN,
Expand Down Expand Up @@ -52,7 +52,7 @@ import { isDev } from "./lib/isDev";
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === chrome.runtime.OnInstalledReason.INSTALL) {
setDefaultSettings();
chrome.runtime.setUninstallURL("https://kewbi.sh/cobweb");
chrome.runtime.setUninstallURL("https://kewbi.sh/cobweb/removed");
}
});

Expand All @@ -64,6 +64,10 @@ metamaskProvider?.on("error", (error) => {
storage.local.set({ mmNotFound: true });
});

metamaskProvider.on("disconnect", () => {
storage.local.set({ mmNotFound: true });
});

metamaskProvider.on("accountsChanged", (accounts) => {
setNewAddress({
address: (accounts as Array<string>)[0],
Expand Down Expand Up @@ -99,7 +103,6 @@ try {
toast("Error - failed to initialize provider");
throw new Error("Error - failed to initialize provider");
}

let sf: Framework | null = null;
try {
sf = await Framework.create({
Expand Down Expand Up @@ -184,8 +187,11 @@ const montagFound = async ({
if (rate.payWhen === PayRates.BLOCKED) {
return;
}
const { address: mmAddress } = (await storage.local.get("address")) ?? {
address: "",
};
createStream({
from: walletRes.address,
from: mmAddress,
toTag: request.options.address,
to: address,
tabId,
Expand All @@ -195,7 +201,9 @@ const montagFound = async ({
sfSigner,
sfToken,
infuraProvider: infuraProvider as InfuraProvider,
mmSigner: mmProvider.getSigner(),
});
cleanUpStreams({ sfSigner, sfToken, sf, mmSigner: mmProvider.getSigner() });
};

const deleteStream = async ({ request }: { request: any }) => {
Expand All @@ -209,6 +217,7 @@ const deleteStream = async ({ request }: { request: any }) => {
sf,
sfSigner,
sfToken,
mmSigner: mmProvider.getSigner(),
});
}
};
Expand Down Expand Up @@ -245,6 +254,7 @@ const editCurrentStream = async ({ request }: { request: any }) => {
sf,
sfSigner,
sfToken,
mmSigner: mmProvider.getSigner(),
});
};

Expand Down Expand Up @@ -359,7 +369,7 @@ const handleMessaging = async (
sendResponse();
return;
}
case EDIT_CURRENT_STREAM: {
case UPDATE_STREAM: {
editCurrentStream({ request });
sendResponse();
return;
Expand Down Expand Up @@ -412,7 +422,13 @@ chrome.runtime.onMessage.addListener(handleMessaging);
chrome.tabs.onRemoved.addListener(async (tabId, _) => {
const { signer: sfSigner } = await getWalletAndSigner();
if (sf && sfSigner && sfToken) {
deleteStreamByTabId({ tabId, sf, sfSigner, sfToken });
deleteStreamByTabId({
tabId,
sf,
sfSigner,
sfToken,
mmSigner: mmProvider.getSigner(),
});
}
});

Expand All @@ -422,7 +438,14 @@ chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, _) => {
}
const { signer: sfSigner } = await getWalletAndSigner();
if (changeInfo.url && sf && sfSigner && sfToken) {
deleteStreamByTabId({ tabId, sf, sfSigner, sfToken, checkFocus: true });
deleteStreamByTabId({
tabId,
sf,
sfSigner,
sfToken,
checkFocus: true,
mmSigner: mmProvider.getSigner(),
});
}
});

Expand Down Expand Up @@ -453,7 +476,12 @@ chrome.alarms.onAlarm.addListener(async (alarm) => {
return;
}
if (name === "cobwebStreamCleanup") {
cleanUpStreams({ sfSigner, sfToken, sf });
cleanUpStreams({
sfSigner,
sfToken,
sf,
mmSigner: mmProvider.getSigner(),
});
} else if (name === "cobwebAllowanceCheck") {
const { address } = await storage.local.get("address");
if (!wallet || !address) {
Expand Down
18 changes: 16 additions & 2 deletions src/pages/Background/lib/cleanUpStreams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ const cleanUpStreams = async ({
sfToken,
sf,
all = false,
mmSigner,
}: {
sf: Framework;
sfSigner: Signer;
sfToken: SuperToken;
all?: boolean;
mmSigner: Signer;
}) => {
const { streams }: { streams: Array<Stream> } = await storage.local.get(
"streams"
Expand All @@ -25,13 +27,25 @@ const cleanUpStreams = async ({

for (const stream of streams) {
if (all) {
deleteStreamByTabId({ tabId: stream.tabId, sf, sfSigner, sfToken });
deleteStreamByTabId({
tabId: stream.tabId,
sf,
sfSigner,
sfToken,
mmSigner,
});
continue;
}
try {
await chrome.tabs.get(stream.tabId);
} catch {
deleteStreamByTabId({ tabId: stream.tabId, sf, sfSigner, sfToken });
deleteStreamByTabId({
tabId: stream.tabId,
sf,
sfSigner,
sfToken,
mmSigner,
});
}
}
};
Expand Down
87 changes: 43 additions & 44 deletions src/pages/Background/lib/createStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
startMonetization,
stopMonetization,
} from "../../shared/monetization";
import TOKEN_MAP from "../../shared/tokens";

const createStream = async ({
from,
Expand All @@ -25,6 +24,7 @@ const createStream = async ({
sfSigner,
sfToken,
infuraProvider,
mmSigner,
}: {
from: string;
to: string;
Expand All @@ -36,8 +36,8 @@ const createStream = async ({
sfSigner: Signer;
sfToken: SuperToken;
infuraProvider: InfuraProvider;
mmSigner: Signer;
}) => {
/*
const possibleName = await infuraProvider.resolveName(to);
if (!possibleName || !utils.isAddress(to)) {
return;
Expand All @@ -51,7 +51,7 @@ const createStream = async ({

const response = await fetch(
"https://cobweb-worker.kewbish.workers.dev/get?" +
new URLSearchParams({ address: to })
new URLSearchParams({ address: to.toLowerCase() }).toString()
);
const responseJson = await response.json();
if (responseJson.error) {
Expand All @@ -60,7 +60,6 @@ const createStream = async ({
// hasn't been added to Cobweb network
return;
}
*/

const uuid = fromString(
to + tabId.toString() + new Date().toString(),
Expand All @@ -75,7 +74,6 @@ const createStream = async ({

let newStream = null;

/*
try {
if (
(
Expand All @@ -90,56 +88,56 @@ const createStream = async ({
) {
const newStreamOperation = sf.cfaV1.createFlowByOperator({
sender: from,
flowRate: rateAmount.toString(),
flowRate: BigNumber.from(rateAmount).toString(),
receiver: to,
superToken: sfToken.address,
userData: `${to} ${new Date()}`,
userData: utils.hexlify(utils.toUtf8Bytes(`${to} ${new Date()}`)),
});
newStream = await newStreamOperation.exec(sfSigner);
newStream = await newStreamOperation.exec(mmSigner);

const { streams } = await storage.local.get("streams");

storage.local.set({
streams: [
...streams,
{
recipient: to,
recipientTag: toTag,
tabId,
rateAmount: BigNumber.from(rateAmount),
requestId: uuid,
startTime: new Date().getTime(),
token: {
name: sfToken.name,
decimals: 18, // TODO - hardcoding for now
symbol: sfToken.symbol,
xAddress: sfToken.address,
},
url,
},
],
});

chrome.scripting.executeScript({
args: [to, uuid],
target: { tabId },
func: startMonetization,
world: "MAIN",
});
return newStream;
}
} catch (e) {
chrome.scripting.executeScript({
args: [to, uuid],
target: { tabId },
func: stopMonetization,
world: "MAIN",
world: "MAIN",
});
errorToast(e as Error);
toast(
"Check that you've approved enough spending tokens in your account page."
);
}*/

const { streams } = await storage.local.get("streams");

storage.local.set({
streams: [
...streams,
{
recipient: to,
recipientTag: toTag,
tabId,
rateAmount,
requestId: uuid,
startTime: new Date().getTime(),
token: {
name: sfToken.name,
decimals: 18, // TODO - hardcoding for now
symbol: sfToken.symbol,
xAddress: sfToken.address,
},
url,
},
],
});

chrome.scripting.executeScript({
args: [to, uuid],
target: { tabId },
func: startMonetization,
world: "MAIN",
});
return newStream;
}
};

export const updateStream = async ({
Expand All @@ -150,6 +148,7 @@ export const updateStream = async ({
sf,
sfSigner,
sfToken,
mmSigner,
}: {
from: string;
to: string;
Expand All @@ -158,6 +157,7 @@ export const updateStream = async ({
sf: Framework;
sfSigner: Signer;
sfToken: SuperToken;
mmSigner: Signer;
}) => {
const uuid = fromString(
to + tabId.toString() + new Date().toString(),
Expand All @@ -174,18 +174,17 @@ export const updateStream = async ({
try {
const updateStreamOperation = sf.cfaV1.updateFlowByOperator({
sender: from,
flowRate: rateAmount.toString(),
flowRate: BigNumber.from(rateAmount).toString(),
receiver: to,
superToken: sfToken.address,
userData: `${to} ${new Date()}`,
});
updateStream = await updateStreamOperation.exec(sfSigner);
updateStream = await updateStreamOperation.exec(mmSigner);
} catch (e) {
errorToast(e as Error);
}

const { streams } = await storage.local.get("streams");

storage.local.set({
streams: [
...streams.map((stream: Stream) =>
Expand Down
15 changes: 11 additions & 4 deletions src/pages/Background/lib/deleteStreamByTabId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,29 @@ const deleteStreamByTabId = async ({
sfSigner,
sfToken,
checkFocus = false,
mmSigner,
}: {
tabId: number;
sf: Framework;
sfSigner: Signer;
sfToken: SuperToken;
checkFocus?: boolean;
mmSigner: Signer;
}) => {
const cancelStream = async (recipient: string) => {
const { wallet }: { wallet: Wallet } = await storage.local.get("wallet");
const { address }: { address: string | null } = await storage.local.get(
"address"
);
if (!address) {
return;
}
try {
const deleteStreamOperation = sf.cfaV1.deleteFlowByOperator({
sender: wallet.address,
sender: address,
receiver: recipient,
superToken: sfToken.address,
});
await deleteStreamOperation.exec(sfSigner);
await deleteStreamOperation.exec(mmSigner);
} catch (e) {
errorToast(e as Error);
return;
Expand All @@ -52,7 +59,7 @@ const deleteStreamByTabId = async ({
streams: streams.filter((stream: Stream) => stream.tabId !== tabId),
});

// cancelStream(stream.recipient);
cancelStream(stream.recipient);
};

export default deleteStreamByTabId;
Loading

0 comments on commit 3afdb24

Please sign in to comment.