Skip to content

Commit

Permalink
document code 📝
Browse files Browse the repository at this point in the history
  • Loading branch information
sid86-dev committed Jun 14, 2022
1 parent 3c9a997 commit ac623a7
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions client/context/TransactionContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ export const TransactionContext = React.createContext()

let eth

// get ethereum Object
if (typeof window !== 'undefined') {
eth = window.ethereum
}

// get deployed contract
const getEtheriumContract = () => {
const provider = new ethers.providers.Web3Provider(eth)
const signer = provider.getSigner()
Expand All @@ -20,20 +22,21 @@ const getEtheriumContract = () => {
contractABI,
signer
)

return transactionContract;
};


export const TransactionProvider = ({ children }) => {
const router = useRouter()
// global app states
const [currentAccount, setCurrentAccount] = useState(null);
const [isLoading, setIsLoading] = useState(false);
const [formData, setFormData] = useState({
addressTo: '',
amount: ''
});
const router = useRouter()

// check connection of wallet
useEffect(() => {
checkIfWallterIsConnected()
}, [])
Expand All @@ -53,6 +56,7 @@ export const TransactionProvider = ({ children }) => {
})()
}, [currentAccount])

// to connect to the wallet
const connectWallet = async (metamask = eth) => {
try {
if (!metamask) return alert('Please install metamask ')
Expand All @@ -66,7 +70,7 @@ export const TransactionProvider = ({ children }) => {
}
}


// to check wallet connection
const checkIfWallterIsConnected = async (metamask = eth) => {
try {
if (!metamask) return alert('Please install metamask ')
Expand All @@ -83,18 +87,28 @@ export const TransactionProvider = ({ children }) => {
}
}

/*
to send transaction
*/
const sendTransaction = async (
metamask = eth,
connectedAccount = currentAccount
) => {
try {
if (!metamask) return alert('Please install metamask ')

const { addressTo, amount } = formData;

// get contract
const transactionContract = getEtheriumContract();

const parsedAmount = ethers.utils.parseEther(amount);

/*
send transaction
request to
metamask and take gas fee confirmation
*/
await metamask.request({
method: 'eth_sendTransaction',
params: [{
Expand All @@ -106,23 +120,25 @@ export const TransactionProvider = ({ children }) => {
]
});

// public the transaction to blockchain
const transactionHash = await transactionContract.publishTransaction(
addressTo,
parsedAmount,
`Transaction ETH ${parsedAmount} to ${addressTo}`,
`TRANSFER`
)

// setting app state
setIsLoading(true);

setFormData({
addressTo: '',
amount: ''
})

// wait for transaction to complete
await transactionHash.wait()

// db
// save transaction data to db
await saveTransaction(
transactionHash.hash,
amount,
Expand All @@ -135,15 +151,16 @@ export const TransactionProvider = ({ children }) => {
catch (error) {
console.error(error)
}

};

// handle form data
const handleChange = (e, name) => {
setFormData((prevState) => ({
...prevState, [name]: e.target.value
}))
};

// save transaction to db
const saveTransaction = async (
txHash,
amount,
Expand All @@ -162,6 +179,7 @@ export const TransactionProvider = ({ children }) => {

await client.createIfNotExists(txDoc)

// link the transaction to the user
await client
.patch(currentAccount)
.setIfMissing({ transactions: [] })
Expand All @@ -173,9 +191,6 @@ export const TransactionProvider = ({ children }) => {
},
])
.commit()



return
};

Expand All @@ -189,6 +204,7 @@ export const TransactionProvider = ({ children }) => {
}
}, [isLoading])


return (
<TransactionContext.Provider
value={{
Expand Down

0 comments on commit ac623a7

Please sign in to comment.