Skip to content
Open
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
6 changes: 3 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DATABASE_URL=mysql://root:password@localhost:3306/web-suite-turbo
DATABASE_URL=mysql://root:password@localhost:3306/escrow-gobi
MNEMONIC=<MNEMONIC>
# Make sure to add the correct RPC URL for the chain you want to use.
# For example, if you want to use the localhost chain, you should use:
RPC_1337_CHAIN="http://127.0.0.1:8545/"
NEXT_PUBLIC_CHAIN_ID=1337
RPC_1663_CHAIN="https://gobi-testnet.horizenlabs.io/ethv1"
NEXT_PUBLIC_CHAIN_ID=1663
3 changes: 2 additions & 1 deletion apps/web/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.next
node_modules
node_modules
.vercel
4 changes: 2 additions & 2 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"private": true,
"scripts": {
"build": "next build",
"heisenberg:migration": "npx 0xtender-heisenberg generate-migration -o migration.ts ../../contracts.json",
"heisenberg:schema": "npx 0xtender-heisenberg generate -o prisma/schema.prisma -i prisma/input.prisma ../../contracts.json && npx prisma format",
"heisenberg:migration": "npx 0xtender-heisenberg generate-migration -o migration.ts ../../contracts-gobi.json",
"heisenberg:schema": "npx 0xtender-heisenberg generate -o prisma/schema.prisma -i prisma/input.prisma ../../contracts-gobi.json && npx prisma format",
"heisenberg": "npm run heisenberg:migration && npm run heisenberg:schema",
"db:push": "prisma db push",
"dev": "next dev",
Expand Down
2,876 changes: 2,876 additions & 0 deletions apps/web/prisma/global-bundle.pem

Large diffs are not rendered by default.

53 changes: 48 additions & 5 deletions apps/web/src/components/WagmiWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import { Web3Modal, useWeb3Modal } from "@web3modal/react";
import { useState, useEffect, useContext } from "react";

import { Moon, Sun } from "lucide-react";
import { configureChains, createConfig, useAccount, WagmiConfig } from "wagmi";
import {
type Chain,
configureChains,
createConfig,
useAccount,
WagmiConfig,
} from "wagmi";
import { alchemyProvider } from "wagmi/providers/alchemy";
import { infuraProvider } from "wagmi/providers/infura";
import { localhost } from "viem/chains";
Expand All @@ -17,18 +23,39 @@ import { cn } from "@app/utils";
import type { FCC } from "@app/utils";
import { ThemeContext } from "@app/hooks/useTheme";
import Link from "next/link";

const chains = [localhost];
import Head from "next/head";

const projectId = "a3b72b0c49a06b52469c2ea63d289f26";

export const gobi = {
id: 1663,
name: "Gobi",
network: "gobi",
nativeCurrency: {
decimals: 18,
name: "tZEN",
symbol: "tZEN",
},
rpcUrls: {
public: { http: ["https://gobi-testnet.horizenlabs.io/ethv1"] },
default: { http: ["https://gobi-testnet.horizenlabs.io/ethv1"] },
},
blockExplorers: {
etherscan: {
name: "GobiExplorer",
url: "https://gobi-explorer.horizen.io/",
},
default: { name: "GobiExplorer", url: "https://snowtrace.io" },
},
} as const satisfies Chain;
const chains = [localhost, gobi];

const { publicClient } = configureChains(
chains,
[...chains],
[
w3mProvider({ projectId: projectId }),
alchemyProvider({ apiKey: "UNmYU0zAvct_GRHxQqMsCUFYbThTkhUJ" }),
infuraProvider({ apiKey: "9ae618979ecd4afb9e6826f76deb4475" }),
// publicProvider(),
],
{
stallTimeout: 5_000,
Expand Down Expand Up @@ -66,6 +93,14 @@ export const WagmiWrapper: FCC = ({ children }) => {
return (
<>
<main className={cn("min-h-screen px-6 py-6 md:px-12")}>
{hydrated === false && (
<Head>
<title>Escrow Protocol - 0xTender</title>
<meta name="description" content="Escrow Protocol - 0xTender" />
<link rel="icon" href="/logo.png" />
</Head>
)}

{hydrated && (
<>
<div className={"mb-10 flex items-center justify-between"}>
Expand Down Expand Up @@ -104,6 +139,14 @@ export const WagmiWrapper: FCC = ({ children }) => {
</div>
</div>

{!address && (
<Head>
<title>Escrow Protocol - 0xTender</title>
<meta name="description" content="Escrow Protocol - 0xTender" />
<link rel="icon" href="/logo.png" />
</Head>
)}

<>
{address && (
<>
Expand Down
2 changes: 2 additions & 0 deletions apps/web/src/pages/purchase/[escrowId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ const PurchaseEscrowPage: FC = () => {
enabled: !!data?.details.A_escrowId,
});

console.log(escrowState);

const [hash, setHash] = useState<AddressType>();

const { data: counterAllowance } = useContractRead({
Expand Down
80 changes: 54 additions & 26 deletions apps/web/src/server/api/routers/escrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import {
type E_SwapStateChanged_SwapERC20Extension,
} from "@prisma/client";

const getUniqueByKey = <T>(instances: T[], key: keyof T) => {
const arrayUniqueByKey = [
...new Map(instances.map((item) => [item[key], item])).values(),
];

return arrayUniqueByKey;
};

export const escrowRouter = createTRPCRouter({
salesAndPurchases: publicProcedure
.input(
Expand All @@ -24,24 +32,33 @@ export const escrowRouter = createTRPCRouter({
}

const instances = await Promise.all([
ctx.prisma.e_EscrowStateUpdate_Escrow.count({
where: {
A_sender: input.address,
A_escrowState: `${EscrowState.BEGUN}`,
},
}),
(await ctx.prisma.e_SwapStateChanged_MultiSwapExtension.count({
where: {
A_counter: input.address,
A_state: `${EscrowState.BEGUN}`,
},
})) +
(await ctx.prisma.e_SwapStateChanged_SwapERC20Extension.count({
(
await ctx.prisma.e_EscrowStateUpdate_Escrow.findMany({
distinct: ["A_escrowId"],
where: {
A_sender: input.address,
A_escrowState: `${EscrowState.BEGUN}`,
},
})
).length,
(
await ctx.prisma.e_SwapStateChanged_MultiSwapExtension.findMany({
distinct: ["A_escrowId"],
where: {
A_counter: input.address,
A_state: `${EscrowState.BEGUN}`,
},
})),
})
).length +
(
await ctx.prisma.e_SwapStateChanged_SwapERC20Extension.findMany({
distinct: "A_escrowId",
where: {
A_counter: input.address,
A_state: `${EscrowState.BEGUN}`,
},
})
).length,
]);

return instances;
Expand All @@ -60,7 +77,7 @@ export const escrowRouter = createTRPCRouter({
if (input.address === undefined) {
return { instances: [] };
}
const swapERC20 =
const swapERC20 = getUniqueByKey(
await ctx.prisma.e_SwapStateChanged_SwapERC20Extension.findMany({
where: {
A_counter: input.address,
Expand All @@ -69,9 +86,11 @@ export const escrowRouter = createTRPCRouter({
orderBy: {
createdAt: "desc",
},
});
}),
"A_escrowId"
);

const multiSwap =
const multiSwap = getUniqueByKey(
await ctx.prisma.e_SwapStateChanged_MultiSwapExtension.findMany({
where: {
A_counter: input.address,
Expand All @@ -80,7 +99,9 @@ export const escrowRouter = createTRPCRouter({
orderBy: {
createdAt: "desc",
},
});
}),
"A_escrowId"
);

const tokenAddresses = Array.from(
new Set(
Expand Down Expand Up @@ -139,8 +160,9 @@ export const escrowRouter = createTRPCRouter({
instances: instances.map((e) => {
return {
...e,
initiatorTokenName: tokens.get(e.A_initiatorToken) ?? "",
counterTokenName: tokens.get(e.A_counterToken) ?? "",
initiatorTokenName:
tokens.get(e.A_initiatorToken) ?? e.A_initiatorToken,
counterTokenName: tokens.get(e.A_counterToken) ?? e.A_counterToken,
};
}),
};
Expand All @@ -159,7 +181,7 @@ export const escrowRouter = createTRPCRouter({
if (input.address === undefined) {
return { instances: [] };
}
const swapERC20 =
const swapERC20 = getUniqueByKey(
await ctx.prisma.e_SwapStateChanged_SwapERC20Extension.findMany({
where: {
A_initiator: input.address,
Expand All @@ -168,8 +190,11 @@ export const escrowRouter = createTRPCRouter({
orderBy: {
createdAt: "desc",
},
});
const multiSwap =
}),
"A_escrowId"
);

const multiSwap = getUniqueByKey(
await ctx.prisma.e_SwapStateChanged_MultiSwapExtension.findMany({
where: {
A_initiator: input.address,
Expand All @@ -178,7 +203,9 @@ export const escrowRouter = createTRPCRouter({
orderBy: {
createdAt: "desc",
},
});
}),
"A_escrowId"
);

const tokenAddresses = Array.from(
new Set(
Expand Down Expand Up @@ -242,8 +269,9 @@ export const escrowRouter = createTRPCRouter({
instances: instances.map((e) => {
return {
...e,
initiatorTokenName: tokens.get(e.A_initiatorToken) ?? "",
counterTokenName: tokens.get(e.A_counterToken) ?? "",
initiatorTokenName:
tokens.get(e.A_initiatorToken) ?? e.A_initiatorToken,
counterTokenName: tokens.get(e.A_counterToken) ?? e.A_counterToken,
};
}),
};
Expand Down
9 changes: 5 additions & 4 deletions apps/web/src/utils/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ export const getContractAddress: (
name: keyof (typeof addresses)[keyof typeof addresses]
) => `0x${string}` = (name) => {
const chainId: string = env.NEXT_PUBLIC_CHAIN_ID.toString();
const parsedChainId = z.enum(["1337"]).parse(chainId);
const returnAddress = z
.custom<AddressType>()
.parse(addresses[parsedChainId][name]);
const parsedChainId = z.enum(["1337", "1663"]).parse(chainId);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const chainAddresses = addresses[parsedChainId];
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const returnAddress = z.custom<AddressType>().parse(chainAddresses[name]);
return returnAddress;
};

Expand Down
2 changes: 1 addition & 1 deletion init.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
pnpm start:container
pnpm wait-for-port
pnpm deploy-local:contracts
pnpm deploy-gobi:contracts
pnpm heisenberg
pnpm db:push
pnpm generate-exports
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": true,
"scripts": {
"prepare": "husky install",
"start": "dotenv -- turbo run start",
"start": "dotenv -- pnpm --filter frontend run start",
"dev": "dotenv -- turbo run dev",
"build": "dotenv -- turbo run build",
"start:container": "dotenv -- docker-compose up -d",
Expand Down
23 changes: 16 additions & 7 deletions packages/core/deploy/02_swap_erc20_extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";

import { ethers } from "hardhat";
import type { Escrow } from "../typechain-types";

const deploy_function: DeployFunction = async function (
hre: HardhatRuntimeEnvironment
) {
Expand All @@ -21,13 +24,19 @@ const deploy_function: DeployFunction = async function (

const SwapERC20Extension = await deployments.get("SwapERC20Extension");

await execute(
"Escrow",
{ from: deployer },
"updateEscrowExtension",
SwapERC20Extension.address,
true
);
const EscrowInstance = await ethers.getContract<Escrow>("Escrow");

if (
(await EscrowInstance.escrowExtensions(SwapERC20Extension.address)) !== true
) {
await execute(
"Escrow",
{ from: deployer },
"updateEscrowExtension",
SwapERC20Extension.address,
true
);
}
};

export default deploy_function;
Expand Down
23 changes: 15 additions & 8 deletions packages/core/deploy/04_multi_swap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { DeployFunction } from "hardhat-deploy/types";

import { ethers } from "hardhat";
import type { Escrow } from "../typechain-types";

const deploy_function: DeployFunction = async function (
hre: HardhatRuntimeEnvironment
) {
Expand All @@ -20,14 +23,18 @@ const deploy_function: DeployFunction = async function (
});

const MultiSwapExtension = await deployments.get("MultiSwapExtension");

await execute(
"Escrow",
{ from: deployer },
"updateEscrowExtension",
MultiSwapExtension.address,
true
);
const EscrowInstance = await ethers.getContract<Escrow>("Escrow");
if (
(await EscrowInstance.escrowExtensions(MultiSwapExtension.address)) !== true
) {
await execute(
"Escrow",
{ from: deployer },
"updateEscrowExtension",
MultiSwapExtension.address,
true
);
}
};

export default deploy_function;
Expand Down
Loading