Skip to content
This repository was archived by the owner on Jun 23, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions templates/01-hello-username/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ async function initContract() {
window.walletAccount = new nearlib.WalletAccount(window.near);

// 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, {
if (window.walletAccount.getAccountId()) {
//Creating account object
account = await near.account(await window.walletAccount.getAccountId());
// Initializing our contract APIs by contract name and configuration.
window.contract = new nearlib.Contract(account, 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,
});
});
}
}

// Using initialized contract
Expand All @@ -41,7 +41,7 @@ async function doWork() {
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.
// Adding an event to a sign-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.
Expand All @@ -60,15 +60,15 @@ function signedInFlow() {
document.getElementById('signed-in-flow').classList.remove('d-none');

// Displaying current account name.
document.getElementById('account-id').innerText = window.accountId;
document.getElementById('account-id').innerText = window.walletAccount.getAccountId();

// 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);
});

// Adding an event to a sing-out button.
// Adding an event to a sign-out button.
document.getElementById('sign-out-button').addEventListener('click', () => {
walletAccount.signOut();
// Forcing redirect.
Expand Down
18 changes: 13 additions & 5 deletions templates/02-counter/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ async function connect() {
// Initializing connection to the NEAR node.
window.near = await nearlib.connect(Object.assign(nearConfig, { deps: { keyStore: new nearlib.keyStores.BrowserLocalStorageKeyStore() }}));

// Needed to access wallet login
// 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 our contract APIs by contract name and configuration.
window.contract = await near.loadContract(nearConfig.contractName, {
// Getting the Account ID. If unauthorized yet, it's just empty string.
if (window.walletAccount.getAccountId()) {
//Creating account object
account = await near.account(await window.walletAccount.getAccountId());
// Initializing our contract APIs by contract name and configuration.
window.contract = new nearlib.Contract(account, 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: ["getCounter"],
// Change methods can modify the state. But you don't receive the returned value when called.
changeMethods: ["incrementCounter", "decrementCounter"],
sender: window.walletAccount.getAccountId()
});
});
}
}

function updateUI() {
Expand Down
18 changes: 10 additions & 8 deletions templates/04-guest-book/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,18 @@ async function init() {
window.walletAccount = new nearlib.WalletAccount(window.near);

// Getting the Account ID. If unauthorized yet, it's just empty string.
accountId = walletAccount.getAccountId();

// Initializing the contract.
// For now we need to specify method names from the contract manually.
// It also takes the Account ID which it would use for signing transactions.
contract = await near.loadContract(nearConfig.contractName, {
if (window.walletAccount.getAccountId()) {
//Creating account object
account = await near.account(await window.walletAccount.getAccountId());
// Initializing our contract APIs by contract name and configuration.
window.contract = new nearlib.Contract(account, 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: ["getMessages"],
// Change methods can modify the state. But you don't receive the returned value when called.
changeMethods: ["addMessage"],
sender: accountId,
});
});
}

// Enable wallet link now that config is available
$('a.wallet').removeClass('disabled').attr('href', nearConfig.walletUrl);
Expand Down
18 changes: 13 additions & 5 deletions templates/05-token-contract/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ async function connect() {
// Initializing connection to the NEAR node.
window.near = await nearlib.connect(Object.assign(nearConfig, { deps: { keyStore: new nearlib.keyStores.BrowserLocalStorageKeyStore() }}));

// Needed to access wallet login
// 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 our contract APIs by contract name and configuration.
window.contract = await near.loadContract(nearConfig.contractName, {
// Getting the Account ID. If unauthorized yet, it's just empty string.
if (window.walletAccount.getAccountId()) {
//Creating account object
account = await near.account(await window.walletAccount.getAccountId());
// Initializing our contract APIs by contract name and configuration.
window.contract = new nearlib.Contract(account, 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: ["totalSupply", "balanceOf", "allowance"],
// Change methods can modify the state. But you don't receive the returned value when called.
changeMethods: ["init", "transfer", "approve", "transferFrom"],
sender: window.walletAccount.getAccountId()
});
});
}
}

function updateUI() {
Expand Down