Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #151 from nearprotocol/eslint
Browse files Browse the repository at this point in the history
Add eslint and fix style issues
  • Loading branch information
vgrichina authored Sep 27, 2019
2 parents 69af793 + ec100f1 commit 99ad9cb
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 221 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tmp-project
36 changes: 36 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
env:
es6: true
node: true
jest: true
extends: 'eslint:recommended'
parserOptions:
ecmaVersion: 2018
rules:
indent:
- error
- 4
linebreak-style:
- error
- unix
quotes:
- error
- single
semi:
- error
- always
no-console: off
globals:
jasmine: true
window: true
fetch: true
Headers: true
document: true
Cookies: true
nearConfig: true
nearlib: true
near: true
walletAccount: true
contract: true
testSettings: true


1 change: 1 addition & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ run_tests:
- gcloud
script:
- yarn
- yarn lint
- NODE_ENV=ci yarn test

run_tests_staging:
Expand Down
8 changes: 4 additions & 4 deletions blank_project/gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const gulp = require("gulp");
const nearUtils = require("near-shell/gulp-utils");
const gulp = require('gulp');
const nearUtils = require('near-shell/gulp-utils');

function build_wasm(done){
nearUtils.compile("./assembly/main.ts", "./out/main.wasm", done);
};
nearUtils.compile('./assembly/main.ts', './out/main.wasm', done);
}

const build = gulp.series(build_wasm);

Expand Down
106 changes: 53 additions & 53 deletions blank_project/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,59 @@
function getConfig(env) {
switch (env) {

case 'production':
case 'development':
return {
networkId: 'default',
nodeUrl: 'https://rpc.nearprotocol.com',
contractName: CONTRACT_NAME,
walletUrl: 'https://wallet.nearprotocol.com',
initialBalance: 100000000,
};
case 'staging':
return {
networkId: 'staging',
nodeUrl: 'https://staging-rpc.nearprotocol.com/',
contractName: CONTRACT_NAME,
walletUrl: 'https://near-wallet-staging.onrender.com',
initialBalance: 100000000,
};
case 'local':
return {
networkId: 'local',
nodeUrl: 'http://localhost:3030',
keyPath: `${process.env.HOME}/.near/validator_key.json`,
walletUrl: 'http://localhost:4000/wallet',
contractName: CONTRACT_NAME,
initialBalance: 100000000,
};
case 'test':
return {
networkId: 'local',
nodeUrl: 'http://localhost:3030',
contractName: CONTRACT_NAME,
masterAccount: 'test.near',
initialBalance: 100000000,
};
case 'test-remote':
case 'ci':
return {
networkId: 'shared-test',
nodeUrl: 'http://shared-test.nearprotocol.com:3030',
contractName: CONTRACT_NAME,
masterAccount: 'test.near',
initialBalance: 100000000,
};
case 'ci-staging':
return {
networkId: 'shared-test-staging',
nodeUrl: 'http://staging-shared-test.nearprotocol.com:3030',
contractName: CONTRACT_NAME,
masterAccount: 'test.near',
initialBalance: 100000000,
};
default:
throw Error(`Unconfigured environment '${env}'. Can be configured in src/config.js.`);
case 'production':
case 'development':
return {
networkId: 'default',
nodeUrl: 'https://rpc.nearprotocol.com',
contractName: CONTRACT_NAME,
walletUrl: 'https://wallet.nearprotocol.com',
initialBalance: 100000000,
};
case 'staging':
return {
networkId: 'staging',
nodeUrl: 'https://staging-rpc.nearprotocol.com/',
contractName: CONTRACT_NAME,
walletUrl: 'https://near-wallet-staging.onrender.com',
initialBalance: 100000000,
};
case 'local':
return {
networkId: 'local',
nodeUrl: 'http://localhost:3030',
keyPath: `${process.env.HOME}/.near/validator_key.json`,
walletUrl: 'http://localhost:4000/wallet',
contractName: CONTRACT_NAME,
initialBalance: 100000000,
};
case 'test':
return {
networkId: 'local',
nodeUrl: 'http://localhost:3030',
contractName: CONTRACT_NAME,
masterAccount: 'test.near',
initialBalance: 100000000,
};
case 'test-remote':
case 'ci':
return {
networkId: 'shared-test',
nodeUrl: 'http://shared-test.nearprotocol.com:3030',
contractName: CONTRACT_NAME,
masterAccount: 'test.near',
initialBalance: 100000000,
};
case 'ci-staging':
return {
networkId: 'shared-test-staging',
nodeUrl: 'http://staging-shared-test.nearprotocol.com:3030',
contractName: CONTRACT_NAME,
masterAccount: 'test.near',
initialBalance: 100000000,
};
default:
throw Error(`Unconfigured environment '${env}'. Can be configured in src/config.js.`);
}
}

Expand Down
124 changes: 62 additions & 62 deletions blank_project/src/main.js
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, {
// 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,
});
// Initializing our contract APIs by contract name and configuration.
window.contract = await near.loadContract(nearConfig.contractName, { // eslint-disable-line require-atomic-updates
// 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
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);
46 changes: 23 additions & 23 deletions blank_project/src/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// >> tests-snippet
describe("Greeter", function() {
describe('Greeter', function() {
let near;
let contract;
let accountId;
Expand All @@ -8,34 +8,34 @@ describe("Greeter", function() {

// Common setup below
beforeAll(async function() {
if (window.testSettings === undefined) {
window.testSettings = {};
}
near = await nearlib.connect(testSettings);
accountId = testSettings.accountId;
const contractName = testSettings.contractName ?
testSettings.contractName :
(new URL(window.location.href)).searchParams.get("contractName");
contract = await near.loadContract(contractName, {
if (window.testSettings === undefined) {
window.testSettings = {};
}
near = await nearlib.connect(testSettings);
accountId = testSettings.accountId;
const contractName = testSettings.contractName ?
testSettings.contractName :
(new URL(window.location.href)).searchParams.get('contractName');
contract = await near.loadContract(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: [],
sender: accountId
});
viewMethods: ['whoSaidHi'],
// Change methods can modify the state. But you don't receive the returned value when called.
changeMethods: [],
sender: accountId
});
});

// Multiple tests can be described below. Search Jasmine JS for documentation.
describe("simple", function() {
beforeAll(async function() {
describe('simple', function() {
beforeAll(async function() {
// There can be some common setup for each test.
});
});

it("get hello message", async function() {
const result = await contract.whoSaidHi();
expect(result).toBeNull();
});
});
it('get hello message', async function() {
const result = await contract.whoSaidHi();
expect(result).toBeNull();
});
});
});
// << tests-snippet
Loading

0 comments on commit 99ad9cb

Please sign in to comment.