Skip to content

Commit

Permalink
show error message
Browse files Browse the repository at this point in the history
  • Loading branch information
shahbaz17 committed Sep 18, 2024
1 parent 025340e commit 9b4c4ba
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export default class EthereumRpc {

return privateKey;
} catch (error) {
return error as string;
return (error as any).message;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
import { createWalletClient, createPublicClient, custom, formatEther, parseEther} from 'viem'
import { mainnet, polygonAmoy, sepolia } from 'viem/chains'
import { createWalletClient, createPublicClient, custom, formatEther, parseEther } from "viem";
import { mainnet, polygonAmoy, sepolia } from "viem/chains";

/* eslint-disable @typescript-eslint/no-explicit-any */
import type { IProvider } from "@web3auth/base";


export default class EthereumRpc {
private provider: IProvider;
private contractABI = [
private provider: IProvider;

private contractABI = [
{
"inputs": [],
"name": "retrieve",
"outputs": [
inputs: [],
name: "retrieve",
outputs: [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
internalType: "uint256",
name: "",
type: "uint256",
},
],
"stateMutability": "view",
"type": "function"
stateMutability: "view",
type: "function",
},
{
"inputs": [
inputs: [
{
"internalType": "uint256",
"name": "num",
"type": "uint256"
}
internalType: "uint256",
name: "num",
type: "uint256",
},
],
"name": "store",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
name: "store",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
];

constructor(provider: IProvider) {
Expand All @@ -43,53 +42,52 @@ export default class EthereumRpc {

getViewChain() {
switch (this.provider.chainId) {
case "1":
return mainnet;
case "0x13882":
return polygonAmoy;
case "0xaa36a7":
return sepolia;
default:
return mainnet;
case "1":
return mainnet;
case "0x13882":
return polygonAmoy;
case "0xaa36a7":
return sepolia;
default:
return mainnet;
}
}

async getChainId(): Promise<any> {
async getChainId(): Promise<any> {
try {
const walletClient = createWalletClient({
transport: custom(this.provider)
})
const address = await walletClient.getAddresses()
console.log(address)
const chainId = await walletClient.getChainId()
return chainId.toString();
const walletClient = createWalletClient({
transport: custom(this.provider),
});

const address = await walletClient.getAddresses();
console.log(address);

const chainId = await walletClient.getChainId();
return chainId.toString();
} catch (error) {
return error;
return error;
}
}
}

async getAddresses(): Promise<any> {
async getAddresses(): Promise<any> {
try {
const walletClient = createWalletClient({
chain: this.getViewChain(),
transport: custom(this.provider)
});
return await walletClient.getAddresses();
const walletClient = createWalletClient({
chain: this.getViewChain(),
transport: custom(this.provider),
});

return await walletClient.getAddresses();
} catch (error) {
return error;
return error;
}
}
}
async getAccounts(): Promise<any> {
try {

const address = this.getAddresses();

return address;
const address = this.getAddresses();

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

Expand All @@ -101,20 +99,20 @@ async getAddresses(): Promise<any> {

return privateKey;
} catch (error) {
return error as string;
return (error as any).message;
}
}

async getBalance(): Promise<string> {
try {
const publicClient = createPublicClient({
chain: this.getViewChain(),
transport: custom(this.provider)
})
chain: this.getViewChain(),
transport: custom(this.provider),
});

const address = await this.getAccounts();
const balance = await publicClient.getBalance({address: address[0]});
console.log(balance)
const balance = await publicClient.getBalance({ address: address[0] });
console.log(balance);
return formatEther(balance);
} catch (error) {
return error as string;
Expand All @@ -124,31 +122,30 @@ async getAddresses(): Promise<any> {
async sendTransaction(): Promise<any> {
try {
const publicClient = createPublicClient({
chain: this.getViewChain(),
transport: custom(this.provider)
})
chain: this.getViewChain(),
transport: custom(this.provider),
});

const walletClient = createWalletClient({
chain: this.getViewChain(),
transport: custom(this.provider)
});
chain: this.getViewChain(),
transport: custom(this.provider),
});

// data for the transaction
const destination = "0x40e1c367Eca34250cAF1bc8330E9EddfD403fC56";
const amount = parseEther("0.0001");
const address = await this.getAccounts();
const address = await this.getAccounts();

// Submit transaction to the blockchain
const hash = await walletClient.sendTransaction({
account: address[0],
to: destination,
value: amount,
});
console.log(hash)
account: address[0],
to: destination,
value: amount,
});
console.log(hash);
const receipt = await publicClient.waitForTransactionReceipt({ hash });


return this.toObject(receipt);
return this.toObject(receipt);
} catch (error) {
return error;
}
Expand All @@ -157,23 +154,23 @@ async getAddresses(): Promise<any> {
async signMessage() {
try {
const walletClient = createWalletClient({
chain: this.getViewChain(),
transport: custom(this.provider)
});
chain: this.getViewChain(),
transport: custom(this.provider),
});

// data for signing
const address = await this.getAccounts();
const address = await this.getAccounts();
const originalMessage = "YOUR_MESSAGE";

// Sign the message
const hash = await walletClient.signMessage({
account: address[0],
message: originalMessage
});
account: address[0],
message: originalMessage,
});

console.log(hash)
console.log(hash);

return hash.toString();
return hash.toString();
} catch (error) {
return error;
}
Expand All @@ -182,16 +179,16 @@ async getAddresses(): Promise<any> {
async readContract() {
try {
const publicClient = createPublicClient({
chain: this.getViewChain(),
transport: custom(this.provider)
})

const number = await publicClient.readContract({
address: "0x9554a5CC8F600F265A89511e5802945f2e8A5F5D",
abi: this.contractABI,
functionName: 'retrieve'
})
chain: this.getViewChain(),
transport: custom(this.provider),
});

const number = await publicClient.readContract({
address: "0x9554a5CC8F600F265A89511e5802945f2e8A5F5D",
abi: this.contractABI,
functionName: "retrieve",
});

return this.toObject(number);
} catch (error) {
return error;
Expand All @@ -201,46 +198,43 @@ async getAddresses(): Promise<any> {
async writeContract() {
try {
const publicClient = createPublicClient({
chain: this.getViewChain(),
transport: custom(this.provider)
})
chain: this.getViewChain(),
transport: custom(this.provider),
});

const walletClient = createWalletClient({
chain: this.getViewChain(),
transport: custom(this.provider)
});
chain: this.getViewChain(),
transport: custom(this.provider),
});

// data for writing to the contract
const address = await this.getAccounts();
const address = await this.getAccounts();
const randomNumber = Math.floor(Math.random() * 9000) + 1000;

// Submit transaction to the blockchain
const hash = await walletClient.writeContract(
{
account: address[0],
address: "0x9554a5CC8F600F265A89511e5802945f2e8A5F5D",
abi: this.contractABI,
functionName: 'store',
args: [randomNumber]
}
)
const hash = await walletClient.writeContract({
account: address[0],
address: "0x9554a5CC8F600F265A89511e5802945f2e8A5F5D",
abi: this.contractABI,
functionName: "store",
args: [randomNumber],
});

const receipt = await publicClient.waitForTransactionReceipt({ hash });


return this.toObject(receipt);
return this.toObject(receipt);
} catch (error) {
return error;
}
}

toObject(data: any) {
// can't serialize a BigInt so this hack
return JSON.parse(JSON.stringify(data, (key, value) =>
typeof value === 'bigint'
? value.toString()
: value // return everything else unchanged
));
return JSON.parse(
JSON.stringify(
data,
(key, value) => (typeof value === "bigint" ? value.toString() : value) // return everything else unchanged
)
);
}

}
Loading

0 comments on commit 9b4c4ba

Please sign in to comment.