forked from hedera-dev/hedera-code-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (66 loc) · 2.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Verifying if your browser is running MetaMask
// Note This property is non-standard. Non-MetaMask providers may also set this property to true.
if (!window.ethereum) {
throw new Error("Metamask is not installed! Go install the extension!");
}
const provider = new ethers.providers.Web3Provider(window.ethereum, "any");
async function switchToHederaNetwork() {
try {
await ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: 0x128 }] // chainId must be in hexadecimal numbers
});
} catch (error) {
// If the Hedera Testnet network has not been added
// Add it to MetaMask using wallet_addEthereumChain
if (error.code === 4902) {
try {
await ethereum.request({
method: 'wallet_addEthereumChain',
params: [
{
chainName: `Hedera Testnet`,
chainId: 0x128,
nativeCurrency: {
name: 'HBAR',
symbol: 'HBAR',
decimals: 18
},
rpcUrls: ["https://testnet.hashio.io/api"]
},
],
});
} catch (addError) {
console.error(addError);
}
}
console.error(error);
}
}
/* Dissociate an account with the provided HTS token solidity address by
treating HTS tokens as if they were ERC20/ERC721 tokens.
This allows us to call the dissociate function directly on the HTS token.
*/
async function dissociateToken() {
await switchToHederaNetwork(window.ethereum);
// request access to the user's account
await provider.send("eth_requestAccounts", []);
// get signer
const signer = provider.getSigner();
// define a hardcoded ABI that exposes (a subset of the) known interface of all HTS tokens
const abi = ["function dissociate()"];
const hederaTokenSolidityAddress = '0x000000000000000000000000000000000008bcc1';
// create contract instance using token solidity address, hardcoded ABI, and signer
const contract = new ethers.Contract(hederaTokenSolidityAddress, abi, signer);
try {
// call the dissociate function
const transactionResult = await contract.dissociate({
gasLimit: 800_000
});
return transactionResult.hash;
} catch (error) {
console.warn(error.message ? error.message : error);
return null;
}
};
dissociateToken();