-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelven-init.js
153 lines (142 loc) · 4.48 KB
/
elven-init.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
import {
ElvenJS,
Transaction,
Address,
TransactionPayload,
TokenTransfer,
} from "https://unpkg.com/[email protected]/build/elven.js";
// UI states helper
const uiLoggedInState = (loggedIn) => {
const loginXPortalButton = window.document.getElementById(
"button-login-mobile"
);
const logoutButton = document.getElementById("button-logout");
const txButton = document.getElementById("button-tx");
if (loggedIn) {
loginXPortalButton.style.setProperty("display", "none");
logoutButton.style.setProperty("display", "block");
txButton.style.setProperty("display", "block");
} else {
loginXPortalButton.style.setProperty("display", "block");
logoutButton.style.setProperty("display", "none");
txButton.style.setProperty("display", "none");
}
};
// UI spinner helper
const uiSpinnerState = (isLoading) => {
const buttonLoginMobile = document.getElementById("button-login-mobile");
const buttonEgld = document.getElementById("button-tx");
const pendingTxt = "Pending...";
if (isLoading) {
buttonLoginMobile.innerText = pendingTxt;
buttonLoginMobile.setAttribute("disabled", true);
buttonEgld.innerText = pendingTxt;
buttonEgld.setAttribute("disabled", true);
} else {
buttonLoginMobile.innerText = "Connect!";
buttonLoginMobile.removeAttribute("disabled");
buttonEgld.innerText = "Donate!";
buttonEgld.removeAttribute("disabled");
}
};
// Update the link to the MultiversX explorer after the transaction is done
const updateTxHashContainer = (txHash) => {
const txHashContainer = document.getElementById("tx-hash");
if (txHash) {
const url = `https://devnet-explorer.multiversx.com/transactions/${txHash}`;
const link = document.createElement("a");
link.setAttribute("href", url);
link.setAttribute("rel", "noopener noreferrer");
link.setAttribute("target", "_blank");
link.classList.add("transaction-link");
link.innerText = url;
txHashContainer.appendChild(link);
} else {
txHashContainer?.querySelector("a")?.remove();
}
};
// Init the elven.js
const initElven = async () => {
await ElvenJS.init({
apiUrl: "https://devnet-api.multiversx.com",
chainType: "devnet",
apiTimeout: 10000,
// Remember to change it. Get yours here: https://cloud.walletconnect.com/sign-in
walletConnectV2ProjectId: "f502675c63610bfe4454080ac86d70e6",
walletConnectV2RelayAddresses: ["wss://relay.walletconnect.com"],
onLoginPending: () => {
uiSpinnerState(true);
},
onLoggedIn: () => {
uiLoggedInState(true);
uiSpinnerState(false);
},
onLogout: () => {
uiLoggedInState(false);
uiSpinnerState(false);
updateTxHashContainer("");
},
onTxStarted: () => {
uiSpinnerState(true);
},
onTxSent: (tx) => {},
onTxFinalized: (tx) => {
tx?.hash && updateTxHashContainer(tx.hash);
uiSpinnerState(false);
},
onTxError: (tx, error) => {
console.log("Error: ", error);
},
onQrPending: () => {
uiSpinnerState(true);
},
onQrLoaded: () => {
uiSpinnerState(false);
},
});
};
initElven();
// Login with xPortal Button mobile app button click listener
document
.getElementById("button-login-mobile")
.addEventListener("click", async () => {
try {
await ElvenJS.login("mobile", {
qrCodeContainer: "multiversx-donate-widget-container",
});
} catch (e) {
console.log("Login: Something went wrong, try again!", e?.message);
}
});
// Send donate transaction, define your address and the price
const egldTransferAddress =
"erd17a4wydhhd6t3hhssvcp9g23ppn7lgkk4g2tww3eqzx4mlq95dukss0g50f";
const donatePrice = 0.5;
document.getElementById("button-tx").addEventListener("click", async () => {
updateTxHashContainer(false);
const demoMessage = "MultiversX donate demo!";
const tx = new Transaction({
nonce: ElvenJS.storage.get("nonce"),
receiver: new Address(egldTransferAddress),
gasLimit: 50000 + 1500 * demoMessage.length,
chainID: "D",
data: new TransactionPayload(demoMessage),
value: TokenTransfer.egldFromAmount(donatePrice),
sender: new Address(ElvenJS.storage.get("address")),
});
try {
const transaction = await ElvenJS.signAndSendTransaction(tx);
updateTxHashContainer(transaction.hash);
} catch (e) {
throw new Error(e?.message);
}
});
// Logout
document.getElementById("button-logout").addEventListener("click", async () => {
try {
await ElvenJS.logout();
} catch (e) {
console.error(e.message);
}
});