This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
215 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,94 @@ | ||
// Initializing contract | ||
async function initContract() { | ||
console.log("nearConfig", nearConfig); | ||
console.log('nearConfig', nearConfig); | ||
|
||
// Initializing connection to the NEAR DevNet. | ||
window.near = await nearlib.connect(Object.assign({ deps: { keyStore: new nearlib.keyStores.BrowserLocalStorageKeyStore() } }, nearConfig)); | ||
// Initializing connection to the NEAR DevNet. | ||
window.near = await nearlib.connect(Object.assign({ deps: { keyStore: new nearlib.keyStores.BrowserLocalStorageKeyStore() } }, nearConfig)); | ||
|
||
// Initializing Wallet based Account. It can work with NEAR DevNet wallet that | ||
// is hosted at https://wallet.nearprotocol.com | ||
window.walletAccount = new nearlib.WalletAccount(window.near); | ||
// Initializing Wallet based Account. It can work with NEAR DevNet wallet that | ||
// is hosted at https://wallet.nearprotocol.com | ||
window.walletAccount = new nearlib.WalletAccount(window.near); | ||
|
||
// Getting the Account ID. If unauthorized yet, it's just empty string. | ||
window.accountId = window.walletAccount.getAccountId(); | ||
// Getting the Account ID. If unauthorized yet, it's just empty string. | ||
window.accountId = window.walletAccount.getAccountId(); | ||
|
||
// Initializing our contract APIs by contract name and configuration. | ||
window.contract = await near.loadContract(nearConfig.contractName, { | ||
// Initializing our contract APIs by contract name and configuration. | ||
window.contract = await near.loadContract(nearConfig.contractName, { | ||
// NOTE: This configuration only needed while NEAR is still in development | ||
// View methods are read only. They don't modify the state, but usually return some value. | ||
viewMethods: ["whoSaidHi"], | ||
// Change methods can modify the state. But you don't receive the returned value when called. | ||
changeMethods: ["sayHi"], | ||
// Sender is the account ID to initialize transactions. | ||
sender: window.accountId, | ||
}); | ||
viewMethods: ['whoSaidHi'], | ||
// Change methods can modify the state. But you don't receive the returned value when called. | ||
changeMethods: ['sayHi'], | ||
// Sender is the account ID to initialize transactions. | ||
sender: window.accountId, | ||
}); | ||
} | ||
|
||
// Using initialized contract | ||
async function doWork() { | ||
// Setting up refresh button | ||
document.getElementById('refresh-button').addEventListener('click', updateWhoSaidHi); | ||
// Setting up refresh button | ||
document.getElementById('refresh-button').addEventListener('click', updateWhoSaidHi); | ||
|
||
// Based on whether you've authorized, checking which flow we should go. | ||
if (!window.walletAccount.isSignedIn()) { | ||
signedOutFlow(); | ||
} else { | ||
signedInFlow(); | ||
} | ||
// Based on whether you've authorized, checking which flow we should go. | ||
if (!window.walletAccount.isSignedIn()) { | ||
signedOutFlow(); | ||
} else { | ||
signedInFlow(); | ||
} | ||
} | ||
|
||
// Function that initializes the signIn button using WalletAccount | ||
function signedOutFlow() { | ||
// Displaying the signed out flow container. | ||
document.getElementById('signed-out-flow').classList.remove('d-none'); | ||
// Adding an event to a sing-in button. | ||
document.getElementById('sign-in-button').addEventListener('click', () => { | ||
window.walletAccount.requestSignIn( | ||
// The contract name that would be authorized to be called by the user's account. | ||
window.nearConfig.contractName, | ||
// This is the app name. It can be anything. | ||
'Who was the last person to say "Hi!"?', | ||
// We can also provide URLs to redirect on success and failure. | ||
// The current URL is used by default. | ||
); | ||
}); | ||
// Displaying the signed out flow container. | ||
document.getElementById('signed-out-flow').classList.remove('d-none'); | ||
// Adding an event to a sing-in button. | ||
document.getElementById('sign-in-button').addEventListener('click', () => { | ||
window.walletAccount.requestSignIn( | ||
// The contract name that would be authorized to be called by the user's account. | ||
window.nearConfig.contractName, | ||
// This is the app name. It can be anything. | ||
'Who was the last person to say "Hi!"?', | ||
// We can also provide URLs to redirect on success and failure. | ||
// The current URL is used by default. | ||
); | ||
}); | ||
} | ||
|
||
// Main function for the signed-in flow (already authorized by the wallet). | ||
function signedInFlow() { | ||
// Displaying the signed in flow container. | ||
document.getElementById('signed-in-flow').classList.remove('d-none'); | ||
// Displaying the signed in flow container. | ||
document.getElementById('signed-in-flow').classList.remove('d-none'); | ||
|
||
// Displaying current account name. | ||
document.getElementById('account-id').innerText = window.accountId; | ||
// Displaying current account name. | ||
document.getElementById('account-id').innerText = window.accountId; | ||
|
||
// Adding an event to a say-hi button. | ||
document.getElementById('say-hi').addEventListener('click', () => { | ||
// Adding an event to a say-hi button. | ||
document.getElementById('say-hi').addEventListener('click', () => { | ||
// We call say Hi and then update who said Hi last. | ||
window.contract.sayHi().then(updateWhoSaidHi); | ||
}); | ||
window.contract.sayHi().then(updateWhoSaidHi); | ||
}); | ||
|
||
// Adding an event to a sing-out button. | ||
document.getElementById('sign-out-button').addEventListener('click', () => { | ||
walletAccount.signOut(); | ||
// Forcing redirect. | ||
window.location.replace(window.location.origin + window.location.pathname); | ||
}); | ||
// Adding an event to a sing-out button. | ||
document.getElementById('sign-out-button').addEventListener('click', () => { | ||
walletAccount.signOut(); | ||
// Forcing redirect. | ||
window.location.replace(window.location.origin + window.location.pathname); | ||
}); | ||
} | ||
|
||
// Function to update who said hi | ||
function updateWhoSaidHi() { | ||
// JavaScript tip: | ||
// This is another example of how to use promises. Since this function is not async, | ||
// we can't await for `contract.whoSaidHi()`, instead we attaching a callback function | ||
// usin `.then()`. | ||
contract.whoSaidHi().then((who) => { | ||
// JavaScript tip: | ||
// This is another example of how to use promises. Since this function is not async, | ||
// we can't await for `contract.whoSaidHi()`, instead we attaching a callback function | ||
// usin `.then()`. | ||
contract.whoSaidHi().then((who) => { | ||
// If the result doesn't have a value we fallback to the text | ||
document.getElementById('who').innerText = who || "Nobody (but you can be the first)"; | ||
}); | ||
document.getElementById('who').innerText = who || 'Nobody (but you can be the first)'; | ||
}); | ||
} | ||
|
||
// Loads nearlib and this contract into window scope. | ||
window.nearInitPromise = initContract() | ||
.then(doWork) | ||
.catch(console.error); | ||
.then(doWork) | ||
.catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,4 @@ module.exports = function getConfig() { | |
} | ||
throw e; | ||
} | ||
} | ||
}; |
Oops, something went wrong.