Skip to content

Commit

Permalink
feat: gas balance check
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Aug 29, 2024
1 parent 91626b5 commit d6cd25d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 34 deletions.
22 changes: 19 additions & 3 deletions src/handlers/gas-subsidize.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { ethers } from "ethers";
import { Context } from "../types";
import { throwError } from "../utils/logger";
import { faucet } from "./faucet";
import { faucet, getRpcProvider } from "./faucet";
import { NetworkId } from "@ubiquity-dao/rpc-handler";

export async function gasSubsidize(context: Context) {
const {
payload,
config: { networkId, gasSubsidyAmount },
adapters: { supabase },
logger,
} = context;
const { issue } = payload;

Expand All @@ -29,13 +31,22 @@ export async function gasSubsidize(context: Context) {

for (const user of users) {
if (!user?.login) continue;
const userWallet = await supabase.user.getWalletByUserId(user.id, payload.issue.id);
const userWallet = await supabase.user.getWalletByUserId(user.id, payload.issue.number);
if (!userWallet) {
continue;
}

const userGasBalance = await fetchUserBalance(context, userWallet);

logger.info(`User ${user.login} has ${ethers.utils.formatEther(userGasBalance)} ETH`);

if (userGasBalance.gt(gasSubsidyAmount)) {
logger.info(`User ${user.login} already has enough gas`);
continue;
}

if (await supabase.user.hasClaimedBefore(user.id)) {
context.logger.info(`User ${user.login} has already claimed a gas subsidy`);
logger.info(`User ${user.login} has already claimed a gas subsidy`);
continue;
}

Expand All @@ -48,3 +59,8 @@ export async function gasSubsidize(context: Context) {

return txs;
}

async function fetchUserBalance(context: Context, userWallet: string) {
const provider = await getRpcProvider(context.config.networkId as NetworkId);
return await provider.getBalance(userWallet);
}
41 changes: 10 additions & 31 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ const mockRpcHandler = {

(RPCHandler as unknown as jest.Mock).mockImplementation(() => mockRpcHandler);

/**
* This cannot be an anvil address because their balance is > 0
* and would fail the balance checks and would not receive the gas subsidy
*/
const MOCK_ADDRESS = "0x3359ac996a9ED1aD61278D090Deee71d4Db359f9";

let supabaseMock = {
getWalletByUserId: jest.fn().mockResolvedValue("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"),
getWalletByUserId: jest.fn().mockResolvedValue(MOCK_ADDRESS),
hasClaimedBefore: jest.fn().mockResolvedValue(false),
};

Expand Down Expand Up @@ -92,39 +98,12 @@ describe("Plugin tests", () => {
expect(tx.from).toEqual(account);
expect(tx2.from).toEqual(account);
}, 30000);

it("Should handle an issues.closed event", async () => {
const context = createContextInner(
db.repo.findFirst({ where: { id: { equals: 1 } } }) as unknown as Context["payload"]["repository"],
db.users.findFirst({ where: { id: { equals: 1 } } }) as unknown as Context["payload"]["sender"],
db.issue.findFirst({ where: { id: { equals: 1 } } }) as unknown as Context<"issues.closed">["payload"]["issue"]
);
const result = await runPlugin(context);

expect(result).toBeDefined();

if (!result) {
throw new Error();
}

const userOne = result[usersGet[0].login];
const userTwo = result[usersGet[1].login];

expect(userOne).toHaveLength(1);
expect(userTwo).toHaveLength(1);

const tx = userOne[0];
const tx2 = userTwo[0];

verifyTx(tx);
verifyTx(tx2);
}, 30000);
});

describe("", () => {
beforeEach(() => {
supabaseMock = {
getWalletByUserId: jest.fn().mockResolvedValue("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"),
getWalletByUserId: jest.fn().mockResolvedValue(MOCK_ADDRESS),
hasClaimedBefore: jest.fn().mockResolvedValue(true),
};
});
Expand All @@ -145,7 +124,7 @@ describe("", () => {
function verifyTx(tx: ethers.providers.TransactionReceipt) {
expect(tx).toHaveProperty("status", 1);
expect(tx).toHaveProperty("from", "0x70997970C51812dc3A010C7d01b50e0d17dc79C8");
expect(tx).toHaveProperty("to", "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
expect(tx).toHaveProperty("to", MOCK_ADDRESS);
expect(tx).toHaveProperty("transactionHash");
const txHash = tx.transactionHash;
expect(txHash).toBeDefined();
Expand Down Expand Up @@ -191,7 +170,7 @@ function createContextInner(repo: Context["payload"]["repository"], sender: Cont
logger: new Logs("debug"),
config: {
fundingWalletPrivateKey: "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d",
networkId: "100",
networkId: "1337",
gasSubsidyAmount: BigInt(1e18),
},
env: {
Expand Down

0 comments on commit d6cd25d

Please sign in to comment.