Refactor methods from blockchain to use new code structure
Co-authored-by: brunoedcf <brest.dallacosta@outlook.com>
This commit is contained in:
parent
f40db935d3
commit
6fd2120b63
@ -0,0 +1 @@
|
||||
export {};
|
159
src/blockchain/methods.ts
Normal file
159
src/blockchain/methods.ts
Normal file
@ -0,0 +1,159 @@
|
||||
import { useEtherStore } from "@/store/ether";
|
||||
|
||||
import { getProvider } from "./provider";
|
||||
import { getTokenAddress, getP2PixAddress } from "./addresses";
|
||||
|
||||
import p2pix from "../utils/smart_contract_files/P2PIX.json";
|
||||
import mockToken from "../utils/smart_contract_files/MockToken.json";
|
||||
|
||||
import { BigNumber, ethers, type Event } from "ethers";
|
||||
import { formatEther, parseEther } from "ethers/lib/utils";
|
||||
|
||||
const addDeposit = async (tokenQty: string, pixKey: string) => {
|
||||
const provider = getProvider();
|
||||
|
||||
const signer = provider.getSigner();
|
||||
const p2pContract = new ethers.Contract(getP2PixAddress(), p2pix.abi, signer);
|
||||
|
||||
const deposit = await p2pContract.deposit(
|
||||
getTokenAddress(),
|
||||
parseEther(tokenQty),
|
||||
pixKey,
|
||||
ethers.utils.formatBytes32String("")
|
||||
);
|
||||
await deposit.wait();
|
||||
|
||||
// await updateWalletStatus();
|
||||
// await updateDepositAddedEvents();
|
||||
// await updateValidDeposits();
|
||||
};
|
||||
|
||||
const approveTokens = async (tokenQty: string) => {
|
||||
const provider = getProvider();
|
||||
|
||||
const signer = provider.getSigner();
|
||||
|
||||
const tokenContract = new ethers.Contract(
|
||||
getTokenAddress(),
|
||||
mockToken.abi,
|
||||
signer
|
||||
);
|
||||
|
||||
const apprv = await tokenContract.approve(
|
||||
getP2PixAddress(),
|
||||
parseEther(tokenQty)
|
||||
);
|
||||
|
||||
await apprv.wait();
|
||||
return apprv;
|
||||
};
|
||||
|
||||
const cancelDeposit = async (depositId: BigNumber): Promise<Boolean> => {
|
||||
const provider = getProvider();
|
||||
|
||||
const signer = provider.getSigner();
|
||||
const contract = new ethers.Contract(getP2PixAddress(), p2pix.abi, signer);
|
||||
await contract.cancelDeposit(depositId);
|
||||
|
||||
// // await updateWalletBalance();
|
||||
// await updateValidDeposits();
|
||||
return true;
|
||||
};
|
||||
|
||||
const withdrawDeposit = async (depositId: BigNumber): Promise<Boolean> => {
|
||||
const provider = getProvider();
|
||||
|
||||
if (!provider) return false;
|
||||
|
||||
const signer = provider.getSigner();
|
||||
const contract = new ethers.Contract(getP2PixAddress(), p2pix.abi, signer);
|
||||
await contract.withdraw(depositId, []);
|
||||
|
||||
// // await updateWalletBalance();
|
||||
// await updateValidDeposits();
|
||||
return true;
|
||||
};
|
||||
|
||||
const addLock = async (
|
||||
depositId: BigNumber,
|
||||
amount: number
|
||||
): Promise<Event> => {
|
||||
const etherStore = useEtherStore();
|
||||
const provider = getProvider();
|
||||
|
||||
const signer = provider.getSigner();
|
||||
const p2pContract = new ethers.Contract(getP2PixAddress(), p2pix.abi, signer);
|
||||
|
||||
// Make lock
|
||||
// const oldEventsLen = etherStore.locksAddedList.length;
|
||||
const lock = await p2pContract.lock(
|
||||
depositId, // BigNumber
|
||||
etherStore.walletAddress, // String "0x70997970C51812dc3A010C7d01b50e0d17dc79C8" (Example)
|
||||
ethers.constants.AddressZero, // String "0x0000000000000000000000000000000000000000"
|
||||
0,
|
||||
formatEther(String(amount)), // BigNumber
|
||||
[],
|
||||
[]
|
||||
);
|
||||
lock.wait();
|
||||
|
||||
// while (etherStore.locksAddedList.length === oldEventsLen) {
|
||||
// await updateLockAddedEvents();
|
||||
// await updateValidDeposits();
|
||||
// }
|
||||
|
||||
return lock;
|
||||
};
|
||||
|
||||
// Releases lock by specific ID and other additional data
|
||||
const releaseLock = async (
|
||||
pixKey: string,
|
||||
amount: Number,
|
||||
e2eId: string,
|
||||
lockId: string
|
||||
) => {
|
||||
const provider = getProvider();
|
||||
|
||||
const mockBacenSigner = new ethers.Wallet(
|
||||
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
|
||||
);
|
||||
|
||||
const messageToSign = ethers.utils.solidityKeccak256(
|
||||
["string", "uint256", "bytes32"],
|
||||
[
|
||||
pixKey,
|
||||
formatEther(String(amount)),
|
||||
ethers.utils.formatBytes32String(e2eId),
|
||||
]
|
||||
);
|
||||
|
||||
const messageHashBytes = ethers.utils.arrayify(messageToSign);
|
||||
const flatSig = await mockBacenSigner.signMessage(messageHashBytes);
|
||||
const sig = ethers.utils.splitSignature(flatSig);
|
||||
|
||||
const signer = provider.getSigner();
|
||||
const p2pContract = new ethers.Contract(getP2PixAddress(), p2pix.abi, signer);
|
||||
|
||||
const release = await p2pContract.release(
|
||||
lockId,
|
||||
ethers.constants.AddressZero,
|
||||
ethers.utils.formatBytes32String(e2eId),
|
||||
sig.r,
|
||||
sig.s,
|
||||
sig.v
|
||||
);
|
||||
release.wait();
|
||||
// await updateLockReleasedEvents();
|
||||
// await updateValidDeposits();
|
||||
|
||||
return release;
|
||||
};
|
||||
|
||||
export {
|
||||
approveTokens,
|
||||
addDeposit,
|
||||
cancelDeposit,
|
||||
withdrawDeposit,
|
||||
addLock,
|
||||
releaseLock,
|
||||
};
|
@ -61,6 +61,9 @@ const listenToNetworkChange = (connection: any) => {
|
||||
};
|
||||
|
||||
const requestNetworkChange = async (network: string): Promise<boolean> => {
|
||||
const etherStore = useEtherStore();
|
||||
if (!etherStore.walletAddress) return true;
|
||||
|
||||
try {
|
||||
const window_ = window as any;
|
||||
await window_.ethereum.request({
|
||||
|
@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import blockchain from "@/utils/blockchain";
|
||||
import { formatEther } from "@ethersproject/units";
|
||||
import { ref, watch } from "vue";
|
||||
|
||||
// props
|
||||
@ -17,7 +17,7 @@ const showInitialItems = () => {
|
||||
|
||||
const formatEventsAmount = (amount: any) => {
|
||||
try {
|
||||
const formated = blockchain.formatBigNumber(amount);
|
||||
const formated = formatEther(amount);
|
||||
return formated;
|
||||
} catch {
|
||||
return "";
|
||||
|
@ -4,7 +4,7 @@ import CustomButton from "../components/CustomButton.vue";
|
||||
import { debounce } from "@/utils/debounce";
|
||||
import { useEtherStore } from "@/store/ether";
|
||||
import { storeToRefs } from "pinia";
|
||||
import blockchain from "../utils/blockchain";
|
||||
import { connectProvider } from "@/blockchain/provider";
|
||||
|
||||
// Store reference
|
||||
const etherStore = useEtherStore();
|
||||
@ -23,7 +23,7 @@ const emit = defineEmits(["tokenBuy"]);
|
||||
|
||||
// Blockchain methods
|
||||
const connectAccount = async () => {
|
||||
await blockchain.connectProvider();
|
||||
await connectProvider();
|
||||
verifyLiquidity();
|
||||
};
|
||||
|
||||
|
@ -168,6 +168,7 @@ const getValidDeposits = async (): Promise<any[] | undefined> => {
|
||||
|
||||
return depositList;
|
||||
};
|
||||
|
||||
// Update events at store methods
|
||||
const updateValidDeposits = async () => {
|
||||
const etherStore = useEtherStore();
|
||||
@ -230,48 +231,6 @@ const updateLockReleasedEvents = async () => {
|
||||
console.log("RELEASES", eventsReleases);
|
||||
};
|
||||
|
||||
// Deposit methods
|
||||
const approveTokens = async (tokenQty: Number) => {
|
||||
const provider = getProvider();
|
||||
if (!provider) return;
|
||||
|
||||
const signer = provider.getSigner();
|
||||
|
||||
const tokenContract = new ethers.Contract(
|
||||
addresses.token,
|
||||
mockToken.abi,
|
||||
signer
|
||||
);
|
||||
|
||||
const apprv = await tokenContract.approve(
|
||||
addresses.p2pix,
|
||||
formatEther(String(tokenQty))
|
||||
);
|
||||
await apprv.wait();
|
||||
return apprv;
|
||||
};
|
||||
|
||||
// Gets value and pix key from user's form to create a deposit in the blockchain
|
||||
const addDeposit = async (tokenQty: Number, pixKey: String) => {
|
||||
const provider = getProvider();
|
||||
if (!provider) return;
|
||||
|
||||
const signer = provider.getSigner();
|
||||
const p2pContract = new ethers.Contract(addresses.p2pix, p2pix.abi, signer);
|
||||
|
||||
const deposit = await p2pContract.deposit(
|
||||
addresses.token,
|
||||
formatEther(String(tokenQty)),
|
||||
pixKey,
|
||||
ethers.utils.formatBytes32String("")
|
||||
);
|
||||
await deposit.wait();
|
||||
|
||||
// await updateWalletStatus();
|
||||
await updateDepositAddedEvents();
|
||||
await updateValidDeposits();
|
||||
};
|
||||
|
||||
const mockDeposit = async (tokenQty: Number, pixKey: String) => {
|
||||
const provider = getProvider();
|
||||
if (!provider) return;
|
||||
@ -305,36 +264,6 @@ const mockDeposit = async (tokenQty: Number, pixKey: String) => {
|
||||
await updateDepositAddedEvents();
|
||||
};
|
||||
|
||||
// cancel a deposit by its Id
|
||||
const cancelDeposit = async (depositId: BigNumber): Promise<Boolean> => {
|
||||
const provider = getProvider();
|
||||
|
||||
if (!provider) return false;
|
||||
|
||||
const signer = provider.getSigner();
|
||||
const contract = new ethers.Contract(addresses.p2pix, p2pix.abi, signer);
|
||||
await contract.cancelDeposit(depositId);
|
||||
|
||||
// await updateWalletBalance();
|
||||
await updateValidDeposits();
|
||||
return true;
|
||||
};
|
||||
|
||||
// withdraw a deposit by its Id
|
||||
const withdrawDeposit = async (depositId: BigNumber): Promise<Boolean> => {
|
||||
const provider = getProvider();
|
||||
|
||||
if (!provider) return false;
|
||||
|
||||
const signer = provider.getSigner();
|
||||
const contract = new ethers.Contract(addresses.p2pix, p2pix.abi, signer);
|
||||
await contract.withdraw(depositId, []);
|
||||
|
||||
// await updateWalletBalance();
|
||||
await updateValidDeposits();
|
||||
return true;
|
||||
};
|
||||
|
||||
// Get specific deposit data by its ID
|
||||
const mapDeposits = async (depositId: BigNumber): Promise<any> => {
|
||||
const provider = getProvider();
|
||||
@ -348,37 +277,6 @@ const mapDeposits = async (depositId: BigNumber): Promise<any> => {
|
||||
return deposit;
|
||||
};
|
||||
|
||||
// Lock methods
|
||||
// Gets value from user's form to create a lock in the blockchain
|
||||
const addLock = async (depositId: BigNumber, amount: Number) => {
|
||||
const etherStore = useEtherStore();
|
||||
const provider = getProvider();
|
||||
|
||||
if (!provider) return;
|
||||
const signer = provider.getSigner();
|
||||
const p2pContract = new ethers.Contract(addresses.p2pix, p2pix.abi, signer);
|
||||
|
||||
// Make lock
|
||||
const oldEventsLen = etherStore.locksAddedList.length;
|
||||
const lock = await p2pContract.lock(
|
||||
depositId, // BigNumber
|
||||
etherStore.walletAddress, // String "0x70997970C51812dc3A010C7d01b50e0d17dc79C8" (Example)
|
||||
ethers.constants.AddressZero, // String "0x0000000000000000000000000000000000000000"
|
||||
0,
|
||||
formatEther(String(amount)), // BigNumber
|
||||
[],
|
||||
[]
|
||||
);
|
||||
lock.wait();
|
||||
|
||||
while (etherStore.locksAddedList.length === oldEventsLen) {
|
||||
await updateLockAddedEvents();
|
||||
await updateValidDeposits();
|
||||
}
|
||||
|
||||
return lock;
|
||||
};
|
||||
|
||||
// Get specific lock data by its ID
|
||||
const mapLocks = async (lockId: string) => {
|
||||
const provider = getProvider();
|
||||
@ -392,51 +290,6 @@ const mapLocks = async (lockId: string) => {
|
||||
return lock;
|
||||
};
|
||||
|
||||
// Releases lock by specific ID and other additional data
|
||||
const releaseLock = async (
|
||||
pixKey: string,
|
||||
amount: Number,
|
||||
e2eId: string,
|
||||
lockId: string
|
||||
) => {
|
||||
const provider = getProvider();
|
||||
if (!provider) return;
|
||||
|
||||
const mockBacenSigner = new ethers.Wallet(
|
||||
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
|
||||
);
|
||||
|
||||
const messageToSign = ethers.utils.solidityKeccak256(
|
||||
["string", "uint256", "bytes32"],
|
||||
[
|
||||
pixKey,
|
||||
formatEther(String(amount)),
|
||||
ethers.utils.formatBytes32String(e2eId),
|
||||
]
|
||||
);
|
||||
|
||||
const messageHashBytes = ethers.utils.arrayify(messageToSign);
|
||||
const flatSig = await mockBacenSigner.signMessage(messageHashBytes);
|
||||
const sig = ethers.utils.splitSignature(flatSig);
|
||||
|
||||
const signer = provider.getSigner();
|
||||
const p2pContract = new ethers.Contract(addresses.p2pix, p2pix.abi, signer);
|
||||
|
||||
const release = await p2pContract.release(
|
||||
lockId,
|
||||
ethers.constants.AddressZero,
|
||||
ethers.utils.formatBytes32String(e2eId),
|
||||
sig.r,
|
||||
sig.s,
|
||||
sig.v
|
||||
);
|
||||
release.wait();
|
||||
await updateLockReleasedEvents();
|
||||
await updateValidDeposits();
|
||||
|
||||
return release;
|
||||
};
|
||||
|
||||
// Formatting methods
|
||||
const formatEther = (num: string) => {
|
||||
const formattedNum = ethers.utils.parseEther(num);
|
||||
@ -456,17 +309,12 @@ export default {
|
||||
listReleaseTransactionByWalletAddress,
|
||||
listDepositTransactionByWalletAddress,
|
||||
listLockTransactionByWalletAddress,
|
||||
approveTokens,
|
||||
addDeposit,
|
||||
cancelDeposit,
|
||||
withdrawDeposit,
|
||||
mockDeposit,
|
||||
mapDeposits,
|
||||
formatBigNumber,
|
||||
addLock,
|
||||
mapLocks,
|
||||
releaseLock,
|
||||
updateLockAddedEvents,
|
||||
updateValidDeposits,
|
||||
getValidDeposits,
|
||||
updateLockReleasedEvents,
|
||||
};
|
||||
|
@ -8,6 +8,8 @@ import { ref } from "vue";
|
||||
import { useEtherStore } from "@/store/ether";
|
||||
import QrCodeComponent from "../components/QrCodeComponent.vue";
|
||||
import { storeToRefs } from "pinia";
|
||||
import { addLock, releaseLock } from "@/blockchain/methods";
|
||||
import { updateWalletStatus } from "@/blockchain/wallet";
|
||||
|
||||
enum Step {
|
||||
Search,
|
||||
@ -40,10 +42,9 @@ const confirmBuyClick = async ({ selectedDeposit, tokenValue }: any) => {
|
||||
flowStep.value = Step.Buy;
|
||||
etherStore.setLoadingLock(true);
|
||||
|
||||
await blockchain
|
||||
.addLock(depositId, tokenValue)
|
||||
await addLock(depositId, tokenValue)
|
||||
.then((lock) => {
|
||||
lockTransactionHash.value = lock.hash;
|
||||
lockTransactionHash.value = lock.transactionHash;
|
||||
})
|
||||
.catch(() => {
|
||||
flowStep.value = Step.Search;
|
||||
@ -66,7 +67,7 @@ const releaseTransaction = async ({ e2eId }: any) => {
|
||||
});
|
||||
|
||||
if (findLock && tokenAmount.value) {
|
||||
const release = await blockchain.releaseLock(
|
||||
const release = await releaseLock(
|
||||
pixTarget.value,
|
||||
tokenAmount.value,
|
||||
e2eId,
|
||||
@ -81,7 +82,7 @@ const releaseTransaction = async ({ e2eId }: any) => {
|
||||
lastWalletReleaseTransactions.value = releaseTransactions;
|
||||
});
|
||||
|
||||
await blockchain.updateWalletStatus();
|
||||
await updateWalletStatus();
|
||||
loadingRelease.value = false;
|
||||
}
|
||||
};
|
||||
|
@ -5,6 +5,7 @@ import blockchain from "../utils/blockchain";
|
||||
import ListingComponent from "@/components/ListingComponent.vue";
|
||||
import type { BigNumber } from "ethers";
|
||||
import { ref, watch } from "vue";
|
||||
import { cancelDeposit, withdrawDeposit } from "@/blockchain/methods";
|
||||
|
||||
const etherStore = useEtherStore();
|
||||
|
||||
@ -22,7 +23,7 @@ if (walletAddress.value) {
|
||||
}
|
||||
|
||||
const handleCancelDeposit = async (depositID: BigNumber, index: number) => {
|
||||
const response = await blockchain.cancelDeposit(depositID);
|
||||
const response = await cancelDeposit(depositID);
|
||||
if (response == true) {
|
||||
console.log("Depósito cancelado com sucesso.");
|
||||
depositList.value.splice(index, 1);
|
||||
@ -30,7 +31,7 @@ const handleCancelDeposit = async (depositID: BigNumber, index: number) => {
|
||||
};
|
||||
|
||||
const handleWithDrawDeposit = async (depositID: BigNumber, index: number) => {
|
||||
const response = await blockchain.withdrawDeposit(depositID);
|
||||
const response = await withdrawDeposit(depositID);
|
||||
if (response == true) {
|
||||
console.log("Token retirado com sucesso.");
|
||||
depositList.value.splice(index, 1);
|
||||
|
@ -1,4 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { formatEther } from "@ethersproject/units";
|
||||
import type { BigNumber } from "ethers";
|
||||
import { storeToRefs } from "pinia";
|
||||
import { ref } from "vue";
|
||||
@ -89,7 +90,7 @@ const mapLock = (lockId: string) => {
|
||||
@click="mapDeposit(deposit.args.depositID)"
|
||||
>
|
||||
Seller:<br />{{ formatWalletAddress(deposit.args.seller) }}<br />
|
||||
MRBZ: {{ blockchain.formatBigNumber(deposit.args.amount) }}
|
||||
MRBZ: {{ formatEther(deposit.args.amount) }}
|
||||
</li>
|
||||
</ul>
|
||||
<ul class="flex flex-col justify-center items-center gap-4">
|
||||
@ -100,7 +101,7 @@ const mapLock = (lockId: string) => {
|
||||
@click="mapLock(lock.args.lockID)"
|
||||
>
|
||||
Buyer:<br />{{ formatWalletAddress(lock.args.buyer) }}<br />
|
||||
MRBZ: {{ blockchain.formatBigNumber(lock.args.amount) }}
|
||||
MRBZ: {{ formatEther(lock.args.amount) }}
|
||||
</li>
|
||||
</ul>
|
||||
<ul class="flex flex-col justify-center items-center gap-4">
|
||||
|
@ -2,7 +2,7 @@
|
||||
import WantSellComponent from "../components/SellerSteps/WantSellComponent.vue";
|
||||
import SendNetwork from "../components/SellerSteps/SendNetwork.vue";
|
||||
import ValidationComponent from "../components/LoadingComponent.vue";
|
||||
import blockchain from "../utils/blockchain";
|
||||
import { approveTokens, addDeposit } from "../blockchain/methods";
|
||||
|
||||
import { ref } from "vue";
|
||||
import { useEtherStore } from "@/store/ether";
|
||||
@ -28,10 +28,11 @@ const approveOffer = async ({ offer, pixKey }: any) => {
|
||||
try {
|
||||
offerValue.value = offer;
|
||||
pixKeyBuyer.value = pixKey;
|
||||
await blockchain.approveTokens(Number(offerValue.value));
|
||||
await approveTokens(String(offerValue.value));
|
||||
flowStep.value = Step.Network;
|
||||
loading.value = false;
|
||||
} catch {
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
flowStep.value = Step.Sell;
|
||||
loading.value = false;
|
||||
}
|
||||
@ -41,7 +42,7 @@ const sendNetwork = async () => {
|
||||
loading.value = true;
|
||||
try {
|
||||
if (offerValue.value && pixKeyBuyer.value) {
|
||||
await blockchain.addDeposit(offerValue.value, pixKeyBuyer.value);
|
||||
await addDeposit(String(offerValue.value), pixKeyBuyer.value);
|
||||
flowStep.value = Step.Sell;
|
||||
loading.value = false;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user