Skip to content

Commit

Permalink
fix: migrated ethersv5 to ethersv6 in dapp-example
Browse files Browse the repository at this point in the history
Signed-off-by: Logan Nguyen <[email protected]>
  • Loading branch information
quiet-node committed Jun 21, 2024
1 parent 9b047a0 commit e4c1fa3
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
14 changes: 7 additions & 7 deletions dapp-example/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ function App() {

useEffect(() => {
if (window.ethereum) {
const provider = new ethers.providers.Web3Provider(window.ethereum, 'any');
const provider = new ethers.BrowserProvider(window.ethereum, 'any');

setSigner(provider.getSigner());
provider.getSigner().then((signer) => setSigner(signer));

window.ethereum.on('accountsChanged', changeConnectedAccount);
window.ethereum.on('chainChanged', (chainId) => {
Expand Down Expand Up @@ -68,7 +68,7 @@ function App() {
method: 'eth_getBalance',
params: [accountAddress.toString(), 'latest'],
});
formattedBalance = ethers.utils.formatEther(accountBalance);
formattedBalance = ethers.formatEther(accountBalance);
}
setBalance(formattedBalance);
} catch (error) {
Expand Down Expand Up @@ -111,12 +111,12 @@ function App() {
const showAccountIdHandler = useCallback(async () => {
try {
const message = address + '_' + Date.now();
const msgHash = ethers.utils.hashMessage(message);
const msgHashBytes = ethers.utils.arrayify(msgHash);
const msgHash = ethers.hashMessage(message);
const msgHashBytes = ethers.getBytes(msgHash);

const signature = await signer.signMessage(message);

const recoveredPubKey = ethers.utils.recoverPublicKey(msgHashBytes, signature);
const recoveredPubKey = ethers.SigningKey.recoverPublicKey(msgHashBytes, signature);
const accountId = recoveredPublicKeyToAccountId(recoveredPubKey);

setAlias(accountId.aliasKey.toStringRaw());
Expand All @@ -134,7 +134,7 @@ function App() {
await tx.wait();

setToBalanceAfterTransfer(
ethers.utils.formatEther(
ethers.formatEther(
await window.ethereum.request({
method: 'eth_getBalance',
params: [hbarsToAddress, 'latest'],
Expand Down
2 changes: 1 addition & 1 deletion dapp-example/src/components/AssociateHTSTokensForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const AssociateHTSTokensForm = ({ signer, isConnected, chain, address }) => {
}, [chain, address]);

const htsTokenAssociate = useCallback(async () => {
const hrcToken = new ethers.Contract(htsTokenAddress, new ethers.utils.Interface(IHRC), signer);
const hrcToken = new ethers.Contract(htsTokenAddress, new ethers.Interface(IHRC), signer);

try {
setIsLoading(true);
Expand Down
6 changes: 3 additions & 3 deletions dapp-example/src/components/ContractInteractions.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ const ContractInteractions = ({ signer, isConnected, chain, address }) => {

const contractFactory = new ethers.ContractFactory(Greeter.abi, Greeter.bytecode, signer);
const contract = await contractFactory.deploy('initial_msg');
const receipt = await contract.deployTransaction.wait();
setContractAddress(receipt.contractAddress);
await contract.waitForDeployment();
setContractAddress(contract.target);

setIsLoading(false);
setDeployContractMsg('Addr: ' + receipt.contractAddress);
setDeployContractMsg('Addr: ' + contract.target);
} catch (error) {
console.error(error.message);
setDeployContractMsg(null);
Expand Down
2 changes: 1 addition & 1 deletion dapp-example/src/hooks/useHederaSdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const client = Client.forTestnet();

const useHederaSdk = () => {
const recoveredPublicKeyToAccountId = (publicKey) => {
const compressed = ethers.utils.computePublicKey(ethers.utils.arrayify(publicKey), true);
const compressed = ethers.SigningKey.computePublicKey(ethers.getBytes(publicKey), true);

return PublicKey.fromString(compressed).toAccountId(0, 0);
};
Expand Down
12 changes: 7 additions & 5 deletions dapp-example/tests/e2e/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ const deployHederaTokenService = async function (wallet) {

const contractFactory = new ethers.ContractFactory(contractArtifact.abi, contractArtifact.bytecode, wallet);
const contract = await contractFactory.deploy({ gasLimit: 1_000_000 });
const { contractAddress } = await contract.deployTransaction.wait();
await contract.waitForDeployment();
const contractAddress = contract.target;

return contractAddress;
};
Expand All @@ -145,7 +146,8 @@ const deployAndFundContractTransferTx = async function (wallet) {

const contractFactory = new ethers.ContractFactory(contractArtifact.abi, contractArtifact.bytecode, wallet);
const contract = await contractFactory.deploy({ gasLimit: 1_000_000 });
const { contractAddress } = await contract.deployTransaction.wait();
await contract.waitForDeployment();
const contractAddress = contract.target;

await new HederaSDK.TransferTransaction()
.addHbarTransfer(HederaSDK.AccountId.fromEvmAddress(0, 0, contractAddress), new HederaSDK.Hbar(100))
Expand All @@ -161,8 +163,8 @@ const deployAndFundContractTransferTx = async function (wallet) {
if (mainPrivateKeyString === '') {
mainPrivateKeyString = HederaSDK.PrivateKey.generateECDSA().toStringRaw();
}
const mainWallet = new ethers.Wallet(mainPrivateKeyString, new ethers.providers.JsonRpcProvider(process.env.RPC_URL));
const mainCompressedKey = mainWallet._signingKey().compressedPublicKey.replace('0x', '');
const mainWallet = new ethers.Wallet(mainPrivateKeyString, new ethers.JsonRpcProvider(process.env.RPC_URL));
const mainCompressedKey = mainWallet.signingKey.compressedPublicKey.replace('0x', '');
const mainAccountId = (await createAccountFromCompressedPublicKey(mainCompressedKey)).accountId;
console.log(
`Primary wallet account private: ${mainPrivateKeyString}, public: ${mainCompressedKey}, id: ${mainAccountId}`,
Expand All @@ -173,7 +175,7 @@ const deployAndFundContractTransferTx = async function (wallet) {
receiverPrivateKeyString = HederaSDK.PrivateKey.generateECDSA().toStringRaw();
}
const receiverWallet = new ethers.Wallet(receiverPrivateKeyString);
const receiverCompressedKey = receiverWallet._signingKey().compressedPublicKey.replace('0x', '');
const receiverCompressedKey = receiverWallet.signingKey.compressedPublicKey.replace('0x', '');
const receiverAccountId = (await createAccountFromCompressedPublicKey(receiverCompressedKey)).accountId;
console.log(
`Receiver wallet account private: ${receiverPrivateKeyString}, public: ${receiverCompressedKey}, id: ${receiverAccountId}`,
Expand Down

0 comments on commit e4c1fa3

Please sign in to comment.