Skip to content

Commit

Permalink
Merge pull request #14 from srdarkseer/dev
Browse files Browse the repository at this point in the history
functionality: updated logic to send transaction with $AI token
  • Loading branch information
srdarkseer authored Apr 22, 2024
2 parents 178a4eb + dc5327c commit e4c5cd7
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 30 deletions.
133 changes: 133 additions & 0 deletions src/lib/contractABI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,137 @@ export const dummyABI = [
name: "PersonCreated",
type: "event",
},
{
inputs: [
{
internalType: "string",
name: "_name",
type: "string",
},
{
internalType: "uint256",
name: "_age",
type: "uint256",
},
],
name: "createPerson",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
{
inputs: [
{
internalType: "address",
name: "_personAddress",
type: "address",
},
],
name: "getPerson",
outputs: [
{
internalType: "string",
name: "name",
type: "string",
},
{
internalType: "uint256",
name: "age",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
{
anonymous: false,
inputs: [
{
indexed: true,
internalType: "address",
name: "personAddress",
type: "address",
},
{
indexed: false,
internalType: "string",
name: "name",
type: "string",
},
{
indexed: false,
internalType: "uint256",
name: "age",
type: "uint256",
},
],
name: "PersonCreated",
type: "event",
},

// Adding ERC-20 Token Standard Functions
{
inputs: [
{ internalType: "address", name: "spender", type: "address" },
{ internalType: "uint256", name: "amount", type: "uint256" },
],
name: "approve",
outputs: [{ internalType: "bool", name: "", type: "bool" }],
stateMutability: "nonpayable",
type: "function",
},
{
inputs: [{ internalType: "address", name: "account", type: "address" }],
name: "balanceOf",
outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
stateMutability: "view",
type: "function",
},
{
inputs: [
{ internalType: "address", name: "recipient", type: "address" },
{ internalType: "uint256", name: "amount", type: "uint256" },
],
name: "transfer",
outputs: [{ internalType: "bool", name: "", type: "bool" }],
stateMutability: "nonpayable",
type: "function",
},
{
inputs: [
{ internalType: "address", name: "sender", type: "address" },
{ internalType: "address", name: "recipient", type: "address" },
{ internalType: "uint256", name: "amount", type: "uint256" },
],
name: "transferFrom",
outputs: [{ internalType: "bool", name: "", type: "bool" }],
stateMutability: "nonpayable",
type: "function",
},
// Additional ERC-20 Event for Token Transfer
{
anonymous: false,
inputs: [
{
indexed: true,
internalType: "address",
name: "from",
type: "address",
},
{
indexed: true,
internalType: "address",
name: "to",
type: "address",
},
{
indexed: false,
internalType: "uint256",
name: "value",
type: "uint256",
},
],
name: "Transfer",
type: "event",
},
];
38 changes: 8 additions & 30 deletions src/views/Landing/components/PopUpModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from "@/components/ui/select";
import { useToast } from "@/components/CustomToast";

import { saiContractABI, dummyABI } from "@/lib/contractABI";
import { saiContractABI } from "@/lib/contractABI";

import { generateData } from "@/store/api";

Expand All @@ -45,13 +45,6 @@ const PopUpModal = ({ isOpen }: { isOpen: boolean }) => {

const fileInputRef = useRef<HTMLInputElement | null>(null);

const handleBatchSizeChange = (e: any) => {
const newValue = parseInt(e.target.value, 10);
if (!isNaN(newValue)) {
setBatchSize(newValue.toString());
}
};

// Attempt to open the dialog if isOpen changes to true
useEffect(() => {
if (isOpen && openButtonRef.current) {
Expand Down Expand Up @@ -139,20 +132,7 @@ const PopUpModal = ({ isOpen }: { isOpen: boolean }) => {
const contractAddress = `${process.env.SAI_CONTRACT_ADDRESS}`;

// SAI's Contract ABI
const contractABI = dummyABI;

// Check if the ABI contains the 'createPerson' function
useEffect(() => {
if (contractABI && Array.isArray(contractABI)) {
const hasCreatePerson = contractABI.some(
(item) => item.name === "createPerson"
);
console.log("Does ABI contain 'createPerson'? ", hasCreatePerson);
if (!hasCreatePerson) {
addToast("createPerson function not found in ABI", "error");
}
}
}, [contractABI, addToast]);
const contractABI = saiContractABI;

let contract: ethers.Contract | null = null;
if (ethersProvider) {
Expand All @@ -165,30 +145,28 @@ const PopUpModal = ({ isOpen }: { isOpen: boolean }) => {

const sendTransaction = async () => {
try {
// Get the signer from the connected wallet
const signer = ethersProvider?.getSigner();
const gasLimit = 300000; // You may need to adjust this based on the contract's requirements

// Define a manual gas limit for the transaction
const gasLimit = 300000;
const recipientAddress = `${process.env.SAI_CONTRACT_ADDRESS}`; // Address of the recipient
const decimals = 18; // Number of decimals the $AI token uses
const amountToSend = ethers.utils.parseUnits("1.0", decimals); // '1.0' represents 1 $AI token

// Call a contract method
if (contract && signer) {
const tx = await contract
.connect(signer)
.createPerson("John", 30, { gasLimit: gasLimit });
.transfer(recipientAddress, amountToSend, { gasLimit: gasLimit });
await tx.wait(); // Wait for the transaction to be mined
console.log("Transaction successful:", tx.hash);
addToast("Transaction successful", "success");
return tx; // Resolve the promise with the transaction
} else {
console.error("Contract or signer is not available");
addToast("Contract or signer is not available", "error");
throw new Error("Contract or signer is not available");
}
} catch (error) {
console.error("Error sending transaction:", error);
addToast("Error sending transaction", "error");
throw error; // Reject the promise with the error
throw error;
}
};

Expand Down

0 comments on commit e4c5cd7

Please sign in to comment.