Skip to content

Commit

Permalink
Merge pull request #5 from hackbg/fix/swap
Browse files Browse the repository at this point in the history
Fix/swap
  • Loading branch information
danielgruesso authored Nov 1, 2023
2 parents da1260f + b94ff7a commit cfd5b87
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 13 deletions.
29 changes: 26 additions & 3 deletions app/src/components/trade-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
useAccount,
useBalance,
useContractWrite,
usePrepareSendTransaction,
useSendTransaction,
} from "wagmi";

Expand All @@ -29,6 +28,7 @@ import {
usdcConfig,
avaxConfig,
} from "@/config/contracts";
import { Check } from "lucide-react";

const formSchema = z.object({
from: z.coerce.number().gt(0),
Expand All @@ -37,6 +37,7 @@ const formSchema = z.object({

const TradeDialog = ({ pair }: { pair: Pair }) => {
const [isLoading, setIsLoading] = useState(false);
const [txHash, setTxHash] = useState<Address | undefined>();
const { address } = useAccount();
const { prices } = useDatafeed();
const [tokenA, setTokenA] = useState<Address | undefined>(
Expand Down Expand Up @@ -179,7 +180,7 @@ const TradeDialog = ({ pair }: { pair: Pair }) => {
});
}

await trade({
const result = await trade({
args: [tokenA!, tokenB!, parseEther(`${fromAmount}`), feedId],
});
toast({
Expand All @@ -188,9 +189,31 @@ const TradeDialog = ({ pair }: { pair: Pair }) => {
variant: "success",
});
setIsLoading(false);
setTxHash(result.hash);
}

return (
return txHash ? (
<div className="flex h-96 flex-col items-center justify-center">
<Check className="rounded-full bg-[#2FB96C] p-2" width={60} height={60} />
<h3 className="my-3 text-xl font-medium">Swap completed!</h3>
<a
href={`https://goerli.arbiscan.io/tx/${txHash}`}
target="_blank"
rel="noreferrer"
className="mt-4 flex items-center space-x-[8px] text-base font-bold leading-4 underline hover:brightness-125"
>
<span className="text-sm font-bold leading-4 text-white">
View on Explorer
</span>
<Image
src="/external-link.svg"
width={12}
height={12}
alt="external-link"
/>
</a>
</div>
) : (
<>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
Expand Down
2 changes: 1 addition & 1 deletion app/src/config/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ export const avaxConfig = {
} as const;

export const proxyConfig = {
address: "0xb017Bd229423af4a836baEBBB01110918e1E091A",
address: "0xcB2c15CEe8309A2442a1b0B35c475e1531C4CFE4",
abi: [
{
inputs: [
Expand Down
5 changes: 3 additions & 2 deletions contracts/scripts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { ethers, upgrades } from "hardhat";
async function main() {
const [signer] = await ethers.getSigners();
const router = "0xab7664500b19a7a2362Ab26081e6DfB971B6F1B0";
const verifier = "0xea9B98Be000FBEA7f6e88D08ebe70EbaAD10224c";
const verifier = '0xcB1241Fdf26501fA7A2d47d841dcF72C3CAa9dCe';
const linkToken = "0xe39Ab88f8A4777030A534146A9Ca3B52bd5D43A3";

const feedsId = [
"0x00023496426b520583ae20a66d80484e0fc18544866a5b0bfee15ec771963274",
'0x00029584363bcf642315133c335b3646513c20f049602fc7d933be0d3f6360d3',
'0x0002c407f448ffe50a15fd5f1ffe4791830c5f8fa39cd971a3d6ae337aef51a0',
];

const Consumer = await ethers.getContractFactory("DataFeedsConsumer");
Expand Down
27 changes: 27 additions & 0 deletions contracts/scripts/initialize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ethers } from 'hardhat';
import { abi } from '../artifacts/contracts/DataStreamsConsumer.sol/DataStreamsConsumer.json';

async function main() {
const [signer] = await ethers.getSigners();
const proxyAddress = '0xcB2c15CEe8309A2442a1b0B35c475e1531C4CFE4';
const router = '0xab7664500b19a7a2362Ab26081e6DfB971B6F1B0';
const verifier = '0xcB1241Fdf26501fA7A2d47d841dcF72C3CAa9dCe';
const linkToken = '0xe39Ab88f8A4777030A534146A9Ca3B52bd5D43A3';

const feedsId = [
'0x00029584363bcf642315133c335b3646513c20f049602fc7d933be0d3f6360d3',
'0x0002c407f448ffe50a15fd5f1ffe4791830c5f8fa39cd971a3d6ae337aef51a0',
];

const consumer = new ethers.Contract(proxyAddress, abi, signer);

await consumer.initializer(router, verifier, linkToken, feedsId);
console.log('Successfully initialized');
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
26 changes: 21 additions & 5 deletions contracts/scripts/trade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,33 @@ import { abi } from "../artifacts/contracts/DataStreamsConsumer.sol/DataStreamsC

async function main() {
const [signer] = await ethers.getSigners();
const proxyAddress = "0xb017Bd229423af4a836baEBBB01110918e1E091A";
const proxyAddress = "0xcB2c15CEe8309A2442a1b0B35c475e1531C4CFE4";
const wethAddress = "0xe39ab88f8a4777030a534146a9ca3b52bd5d43a3";
const avaxAddress = "0x586A52Ca64f75b49a72e4CaEf5B91374257e1538";
const usdc = "0x8fb1e3fc51f3b789ded7557e680551d93ea9d892";
const amountIn = ethers.parseEther("0.001");

const weth = await ethers.getContractAt("IERC20", wethAddress);
const feedsId = [
'0x00029584363bcf642315133c335b3646513c20f049602fc7d933be0d3f6360d3',
'0x0002c407f448ffe50a15fd5f1ffe4791830c5f8fa39cd971a3d6ae337aef51a0',
];

const consumer = new ethers.Contract(proxyAddress, abi, signer);
const amountIn = ethers.parseEther('0.001');

// Trade WETH <> USDC
const weth = await ethers.getContractAt("IERC20", wethAddress);

await weth.approve(await consumer.getAddress(), amountIn);
await consumer.trade(wethAddress, usdc, amountIn);
console.log("Successfully traded tokens");
await consumer.trade(wethAddress, usdc, amountIn, feedsId[0]);
console.log("Successfully traded WETH tokens for USDC");

// Trade AVAX <> USDC
const avax = await ethers.getContractAt("IERC20", avaxAddress);

await avax.approve(await consumer.getAddress(), amountIn);
await consumer.trade(avaxAddress, usdc, amountIn, feedsId[1]);
console.log('Successfully traded AVAX tokens for USDC');

}

// We recommend this pattern to be able to use async/await everywhere
Expand Down
4 changes: 2 additions & 2 deletions contracts/scripts/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ async function main() {
const [signer] = await ethers.getSigners();
/*
const contract = new ethers.Contract(
"0xb017Bd229423af4a836baEBBB01110918e1E091A",
"0xcB2c15CEe8309A2442a1b0B35c475e1531C4CFE4",
abi,
signer
);
*/
const proxyAddress = "0xb017bd229423af4a836baebbb01110918e1e091a";
const proxyAddress = "0xcB2c15CEe8309A2442a1b0B35c475e1531C4CFE4";

const Consumer = await ethers.getContractFactory("DataStreamsConsumer");
await upgrades.upgradeProxy(proxyAddress, Consumer);
Expand Down

0 comments on commit cfd5b87

Please sign in to comment.