Fixing release function and formatting functions
This commit is contained in:
parent
f25c61b21e
commit
50835c94ed
@ -24,9 +24,7 @@ const formatWalletAddress = (): string => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const formatWalletBalance = (): string => {
|
const formatWalletBalance = (): string => {
|
||||||
const formattedBalance = blockchain.formatEther(balance.value);
|
const fixed = balance.value.substring(0, 8);
|
||||||
const fixed = formattedBalance.substring(0, 8);
|
|
||||||
|
|
||||||
return fixed;
|
return fixed;
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
@ -22,7 +22,7 @@ const updateWalletStatus = async () => {
|
|||||||
|
|
||||||
const balance = await contract.balanceOf(walletAddress[0]);
|
const balance = await contract.balanceOf(walletAddress[0]);
|
||||||
|
|
||||||
etherStore.setBalance(String(balance));
|
etherStore.setBalance(formatBigNumber(balance));
|
||||||
etherStore.setWalletAddress(ethers.utils.getAddress(walletAddress[0]));
|
etherStore.setWalletAddress(ethers.utils.getAddress(walletAddress[0]));
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -63,8 +63,8 @@ const listTransactionByWalletAddress = async (walletAddress: string): Promise<an
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update store
|
// Update events at store methods
|
||||||
const updateStore = async () => {
|
const updateDepositAddedEvents = async () => {
|
||||||
const etherStore = useEtherStore();
|
const etherStore = useEtherStore();
|
||||||
const window_ = window as any;
|
const window_ = window as any;
|
||||||
const connection = window_.ethereum;
|
const connection = window_.ethereum;
|
||||||
@ -78,26 +78,41 @@ const updateStore = async () => {
|
|||||||
|
|
||||||
const filterDeposits = p2pContract.filters.DepositAdded(null);
|
const filterDeposits = p2pContract.filters.DepositAdded(null);
|
||||||
const eventsDeposits = await p2pContract.queryFilter(filterDeposits);
|
const eventsDeposits = await p2pContract.queryFilter(filterDeposits);
|
||||||
console.log("Deposits Added: ", eventsDeposits);
|
|
||||||
etherStore.setDepositsAddedList(eventsDeposits);
|
etherStore.setDepositsAddedList(eventsDeposits);
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateLockAddedEvents = async () => {
|
||||||
|
const etherStore = useEtherStore();
|
||||||
|
const window_ = window as any;
|
||||||
|
const connection = window_.ethereum;
|
||||||
|
let provider: ethers.providers.Web3Provider | null = null;
|
||||||
|
|
||||||
|
if (!connection) return;
|
||||||
|
provider = new ethers.providers.Web3Provider(connection);
|
||||||
|
|
||||||
|
const signer = provider.getSigner();
|
||||||
|
const p2pContract = new ethers.Contract(addresses.p2pix, p2pix.abi, signer);
|
||||||
|
|
||||||
const filterLocks = p2pContract.filters.LockAdded(null);
|
const filterLocks = p2pContract.filters.LockAdded(null);
|
||||||
const eventsLocks = await p2pContract.queryFilter(filterLocks);
|
const eventsLocks = await p2pContract.queryFilter(filterLocks);
|
||||||
console.log("Locks Added: ", eventsLocks);
|
|
||||||
etherStore.setLocksAddedList(eventsLocks);
|
etherStore.setLocksAddedList(eventsLocks);
|
||||||
|
}
|
||||||
|
|
||||||
const filterReleasedLocks = p2pContract.filters.LockReleased(null);
|
const updateLockReleasedEvents = async () => {
|
||||||
const eventsReleasedLocks = await p2pContract.queryFilter(filterReleasedLocks);
|
const etherStore = useEtherStore();
|
||||||
console.log("Released Locks: ", eventsReleasedLocks);
|
const window_ = window as any;
|
||||||
etherStore.setLocksReleasedList(eventsReleasedLocks);
|
const connection = window_.ethereum;
|
||||||
|
let provider: ethers.providers.Web3Provider | null = null;
|
||||||
|
|
||||||
const filterExpiredLocks = p2pContract.filters.LockReturned(null);
|
if (!connection) return;
|
||||||
const eventsExpiredLocks = await p2pContract.queryFilter(filterExpiredLocks);
|
provider = new ethers.providers.Web3Provider(connection);
|
||||||
console.log("Expired Locks: ", eventsExpiredLocks);
|
|
||||||
etherStore.setLocksExpiredList(eventsExpiredLocks);
|
|
||||||
|
|
||||||
|
const signer = provider.getSigner();
|
||||||
|
const p2pContract = new ethers.Contract(addresses.p2pix, p2pix.abi, signer);
|
||||||
|
|
||||||
// (TO DO) Filter valid deposits
|
const filterLocks = p2pContract.filters.LockReleased(null);
|
||||||
|
const eventsLocks = await p2pContract.queryFilter(filterLocks);
|
||||||
|
etherStore.setLocksReleasedList(eventsLocks);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Provider methods
|
// Provider methods
|
||||||
@ -106,7 +121,9 @@ const connectProvider = async () => {
|
|||||||
const connection = window_.ethereum;
|
const connection = window_.ethereum;
|
||||||
|
|
||||||
await updateWalletStatus();
|
await updateWalletStatus();
|
||||||
await updateStore();
|
await updateDepositAddedEvents();
|
||||||
|
await updateLockAddedEvents();
|
||||||
|
await updateLockReleasedEvents();
|
||||||
|
|
||||||
connection.on("accountsChanged", () => {
|
connection.on("accountsChanged", () => {
|
||||||
updateWalletStatus();
|
updateWalletStatus();
|
||||||
@ -124,7 +141,7 @@ const getProvider = (): ethers.providers.Web3Provider | null => {
|
|||||||
|
|
||||||
// Deposit methods
|
// Deposit methods
|
||||||
// Gets value and pix key from user's form to create a deposit in the blockchain
|
// Gets value and pix key from user's form to create a deposit in the blockchain
|
||||||
const addDeposit = async (tokenQty = "1000.0", pixKey = "00011122233") => {
|
const addDeposit = async (tokenQty: string, pixKey: string) => {
|
||||||
const provider = getProvider();
|
const provider = getProvider();
|
||||||
if (!provider) return;
|
if (!provider) return;
|
||||||
|
|
||||||
@ -144,19 +161,20 @@ const addDeposit = async (tokenQty = "1000.0", pixKey = "00011122233") => {
|
|||||||
// First get the approval
|
// First get the approval
|
||||||
const apprv = await tokenContract.approve(
|
const apprv = await tokenContract.approve(
|
||||||
addresses.p2pix,
|
addresses.p2pix,
|
||||||
ethers.utils.parseEther(tokenQty)
|
formatEther(tokenQty)
|
||||||
);
|
);
|
||||||
await apprv.wait();
|
await apprv.wait();
|
||||||
|
|
||||||
// Now we make the deposit
|
// Now we make the deposit
|
||||||
const deposit = await p2pContract.deposit(
|
const deposit = await p2pContract.deposit(
|
||||||
addresses.token,
|
addresses.token,
|
||||||
ethers.utils.parseEther(tokenQty),
|
formatEther(tokenQty),
|
||||||
pixKey
|
pixKey
|
||||||
);
|
);
|
||||||
await deposit.wait();
|
await deposit.wait();
|
||||||
|
|
||||||
updateStore();
|
updateWalletStatus();
|
||||||
|
updateDepositAddedEvents();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get specific deposit data by its ID
|
// Get specific deposit data by its ID
|
||||||
@ -194,7 +212,7 @@ const addLock = async (depositId: Number, amount: Number) => {
|
|||||||
);
|
);
|
||||||
lock.wait();
|
lock.wait();
|
||||||
|
|
||||||
updateStore();
|
updateLockAddedEvents();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get specific lock data by its ID
|
// Get specific lock data by its ID
|
||||||
@ -214,24 +232,10 @@ const mapLocks = async (lockId: string) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Releases lock by specific ID and other additional data
|
// Releases lock by specific ID and other additional data
|
||||||
const releaseLock = async () => {
|
const releaseLock = async (pixKey: string, amount: Number, e2eId: Number, lockId: string) => {
|
||||||
const etherStore = useEtherStore();
|
|
||||||
const provider = getProvider();
|
const provider = getProvider();
|
||||||
if (!provider) return;
|
if (!provider) return;
|
||||||
|
|
||||||
// pass depositId as a param
|
|
||||||
const myLock = etherStore.locksAddedList[0];
|
|
||||||
const lockId = myLock.args.lockID
|
|
||||||
const depositId = myLock.args.depositID
|
|
||||||
const amount = formatBigNumber(myLock.args.amount);
|
|
||||||
|
|
||||||
const myDeposit = await mapDeposits(depositId);
|
|
||||||
const pixKey = myDeposit.pixTarget
|
|
||||||
|
|
||||||
const e2eId = 123
|
|
||||||
|
|
||||||
console.log(pixKey, amount, e2eId)
|
|
||||||
|
|
||||||
const mockBacenSigner = new ethers.Wallet("0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")
|
const mockBacenSigner = new ethers.Wallet("0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")
|
||||||
|
|
||||||
const messageToSign = ethers.utils.solidityKeccak256(
|
const messageToSign = ethers.utils.solidityKeccak256(
|
||||||
@ -242,7 +246,7 @@ const releaseLock = async () => {
|
|||||||
],
|
],
|
||||||
[
|
[
|
||||||
pixKey,
|
pixKey,
|
||||||
ethers.utils.parseEther(amount),
|
amount,
|
||||||
e2eId
|
e2eId
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
@ -262,15 +266,15 @@ const releaseLock = async () => {
|
|||||||
sig.v
|
sig.v
|
||||||
)
|
)
|
||||||
release.wait()
|
release.wait()
|
||||||
console.log(release)
|
|
||||||
|
|
||||||
updateStore();
|
updateLockReleasedEvents();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Formatting methods
|
// Formatting methods
|
||||||
const formatEther = (balance: string) => {
|
const formatEther = (num: string) => {
|
||||||
const formatted = ethers.utils.formatEther(balance);
|
const formattedNum = ethers.utils.parseEther(num);
|
||||||
return formatted;
|
return formattedNum;
|
||||||
};
|
};
|
||||||
|
|
||||||
const formatBigNumber = (num: BigNumber) => {
|
const formatBigNumber = (num: BigNumber) => {
|
||||||
|
@ -13,51 +13,27 @@ enum Step {
|
|||||||
const flowStep = ref<Step>(Step.Search)
|
const flowStep = ref<Step>(Step.Search)
|
||||||
const tokenAmmount = ref()
|
const tokenAmmount = ref()
|
||||||
|
|
||||||
// (TO DO) Tirar isso tudo daqui
|
|
||||||
// import p2pix from "../utils/smart_contract_files/P2PIX.json";
|
|
||||||
// import addresses from "../utils/smart_contract_files/localhost.json";
|
|
||||||
// import { useEtherStore } from "@/store/ether";
|
|
||||||
// import { ethers } from "ethers";
|
|
||||||
|
|
||||||
const confirmBuyClick = async ({ selectedDeposit, tokenValue }: any) => {
|
const confirmBuyClick = async ({ selectedDeposit, tokenValue }: any) => {
|
||||||
// finish buy screen
|
// finish buy screen
|
||||||
console.log(selectedDeposit);
|
console.log(selectedDeposit);
|
||||||
let depositDetail;
|
let depositDetail;
|
||||||
await blockchain
|
await blockchain
|
||||||
.mapDeposits(selectedDeposit["args"]["depositID"])
|
.mapDeposits(selectedDeposit.args.depositID)
|
||||||
.then((deposit) => (depositDetail = deposit));
|
.then((deposit) => (depositDetail = deposit));
|
||||||
|
|
||||||
console.log(tokenValue);
|
console.log(tokenValue);
|
||||||
tokenAmmount.value = tokenValue
|
tokenAmmount.value = tokenValue
|
||||||
flowStep.value = Step.List
|
flowStep.value = Step.List
|
||||||
console.log(depositDetail);
|
|
||||||
console.log(depositDetail);
|
|
||||||
|
|
||||||
// Makes lock with deposit ID and the Amount
|
// Makes lock with deposit ID and the Amount
|
||||||
// if (depositDetail) {
|
if (depositDetail) {
|
||||||
// const lock = await blockchain.addLock(
|
const lock = await blockchain.addLock(
|
||||||
// depositDetail.args.depositID,
|
selectedDeposit.args.depositID,
|
||||||
// tokenValue
|
tokenValue
|
||||||
// );
|
);
|
||||||
// console.log(lock);
|
console.log(lock);
|
||||||
|
};
|
||||||
|
|
||||||
// // (TO DO) Tirar isso daqui
|
|
||||||
// const window_ = window as any;
|
|
||||||
// const connection = window_.ethereum;
|
|
||||||
// let provider: ethers.providers.Web3Provider | null = null;
|
|
||||||
// if (!connection) return;
|
|
||||||
// provider = new ethers.providers.Web3Provider(connection);
|
|
||||||
// const signer = provider.getSigner();
|
|
||||||
// const etherStore = useEtherStore();
|
|
||||||
// const p2pContract = new ethers.Contract(addresses.p2pix, p2pix.abi, signer);
|
|
||||||
// const filterLocks = p2pContract.filters.LockAdded(null);
|
|
||||||
// const eventsLocks = await p2pContract.queryFilter(filterLocks);
|
|
||||||
// etherStore.setLocksAddedList(eventsLocks);
|
|
||||||
|
|
||||||
// Data to QRCode
|
|
||||||
// Chave Pix = depositDetail.pixTarget
|
|
||||||
// Valor = tokenValue
|
|
||||||
// }
|
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
@ -95,7 +95,7 @@ const mapLock = (lockId: string) => {
|
|||||||
@click="mapDeposit(deposit.args.depositID)"
|
@click="mapDeposit(deposit.args.depositID)"
|
||||||
>
|
>
|
||||||
Seller:<br />{{ formatWalletAddress(deposit.args.seller) }}<br />
|
Seller:<br />{{ formatWalletAddress(deposit.args.seller) }}<br />
|
||||||
MRBZ: {{ blockchain.formatEther(deposit.args.amount) }}
|
MRBZ: {{ blockchain.formatBigNumber(deposit.args.amount) }}
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
<ul class="flex flex-col justify-center items-center gap-4">
|
<ul class="flex flex-col justify-center items-center gap-4">
|
||||||
@ -106,7 +106,7 @@ const mapLock = (lockId: string) => {
|
|||||||
@click="mapLock(lock.args.lockID)"
|
@click="mapLock(lock.args.lockID)"
|
||||||
>
|
>
|
||||||
Buyer:<br />{{ formatWalletAddress(lock.args.buyer) }}<br />
|
Buyer:<br />{{ formatWalletAddress(lock.args.buyer) }}<br />
|
||||||
MRBZ: {{ blockchain.formatEther(lock.args.amount) }}
|
MRBZ: {{ blockchain.formatBigNumber(lock.args.amount) }}
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user