Skip to content

Commit

Permalink
nits
Browse files Browse the repository at this point in the history
  • Loading branch information
yashovardhan committed Sep 29, 2024
1 parent 80638f9 commit 4cc977f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,14 @@ const sendTransaction = async (provider: IProvider): Promise<any> => {
const web3 = new Web3(provider as any);

// Get user's Ethereum public address
const address = (await web3.eth.getAccounts())[0];

// Get user's balance in ether
const balance = web3.utils.fromWei(
await web3.eth.getBalance(address), // Balance is in wei
"ether"
);
const fromAddress = (await web3.eth.getAccounts())[0];

if (balance === "0") {
throw new Error("Insufficient balance, please fund your account to use this");
}
const destination = fromAddress;

const amount = web3.utils.toWei("0.001", "ether"); // Convert 1 ether to wei
let transaction = {
from: address,
to: address,
from: fromAddress,
to: destination,
data: "0x",
value: amount,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { IProvider } from "@web3auth/base";
import Web3 from "web3";


const getChainId = async (provider: IProvider): Promise<string> => {
try {
const web3 = new Web3(provider);
Expand All @@ -13,20 +12,20 @@ const getChainId = async (provider: IProvider): Promise<string> => {
} catch (error) {
return error as string;
}
}
};

const getAccounts = async (provider: IProvider): Promise<any> => {
try {
const web3 = new Web3(provider as any);

// Get user's Ethereum public address
const address = (await web3.eth.getAccounts());
const address = await web3.eth.getAccounts();

return address;
} catch (error) {
return error;
}
}
};

const getBalance = async (provider: IProvider): Promise<string> => {
try {
Expand All @@ -45,7 +44,7 @@ const getBalance = async (provider: IProvider): Promise<string> => {
} catch (error) {
return error as string;
}
}
};

const signMessage = async (provider: IProvider): Promise<string> => {
try {
Expand All @@ -67,7 +66,7 @@ const signMessage = async (provider: IProvider): Promise<string> => {
} catch (error) {
return error as string;
}
}
};

const sendTransaction = async (provider: IProvider): Promise<any> => {
try {
Expand All @@ -84,24 +83,21 @@ const sendTransaction = async (provider: IProvider): Promise<any> => {
to: destination,
data: "0x",
value: amount,
}
};

// calculate gas transaction before sending
transaction = { ...transaction, gas: await web3.eth.estimateGas(transaction) } as any;

// Submit transaction to the blockchain and wait for it to be mined
const receipt = await web3.eth.sendTransaction(transaction);

return JSON.stringify(receipt, (key, value) =>
typeof value === 'bigint'
? value.toString()
: value // return everything else unchanged
return JSON.stringify(
receipt,
(key, value) => (typeof value === "bigint" ? value.toString() : value) // return everything else unchanged
);
} catch (error) {
return error as string;
}
}


};

export default {getChainId, getAccounts, getBalance, sendTransaction, signMessage};
export default { getChainId, getAccounts, getBalance, sendTransaction, signMessage };
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,26 @@ let web3auth = null;
$(".btn-logged-in").hide();
$("#sign-tx").hide();

// IMP START - SDK Initialization
// IMP START - Dashboard Registration
const clientId = "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ"; // get your clientId from https://dashboard.web3auth.io
// IMP END - Dashboard Registration

// IMP START - Chain Config
const chainConfig = {
chainNamespace: "eip155",
chainId: "0x1", // Please use 0x1 for Mainnet
rpcTarget: "https://rpc.ankr.com/eth",
displayName: "Ethereum Mainnet",
blockExplorer: "https://etherscan.io/",
chainId: "0xaa36a7",
rpcTarget: "https://rpc.ankr.com/eth_sepolia",
// Avoid using public rpcTarget in production.
// Use services like Infura, Quicknode etc
displayName: "Ethereum Sepolia Testnet",
blockExplorerUrl: "https://sepolia.etherscan.io",
ticker: "ETH",
tickerName: "Ethereum",
logo: "https://cryptologos.cc/logos/ethereum-eth-logo.png",
};
// IMP END - Chain Config

// IMP START - SDK Initialization
const ethereumPrivateKeyProvider = new window.EthereumProvider.EthereumPrivateKeyProvider({
config: { chainConfig },
});
Expand Down

0 comments on commit 4cc977f

Please sign in to comment.