Skip to content

Commit

Permalink
generate function for asking id
Browse files Browse the repository at this point in the history
  • Loading branch information
icerove committed Oct 20, 2020
1 parent 4a48d16 commit fe5a618
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ exports.clean = async function () {
};

exports.deploy = async function (options) {
await eventtracking.askForId(options);
console.log(
`Starting deployment. Account id: ${options.accountId}, node: ${options.nodeUrl}, helper: ${options.helperUrl}, file: ${options.wasmFile}`);

Expand Down Expand Up @@ -82,6 +83,7 @@ const openUrl = async function(url) {

exports.login = async function (options) {
await eventtracking.askForConsentIfNeeded(options);
await eventtracking.askForId(options);
if (!options.walletUrl) {
console.log('Log in is not needed on this environment. Please use appropriate master account for shell operations.');
await eventtracking.track(eventtracking.EVENT_ID_LOGIN_END, { success: true, login_is_not_needed: true }, options);
Expand Down Expand Up @@ -176,6 +178,7 @@ exports.login = async function (options) {
};

exports.viewAccount = async function (options) {
await eventtracking.askForId(options);
let near = await connect(options);
let account = await near.account(options.accountId);
let state = await account.state();
Expand All @@ -187,7 +190,7 @@ exports.viewAccount = async function (options) {
};

exports.deleteAccount = async function (options) {

await eventtracking.askForId(options);
console.log(
`Deleting account. Account id: ${options.accountId}, node: ${options.nodeUrl}, helper: ${options.helperUrl}, beneficiary: ${options.beneficiaryId}`);
const near = await connect(options);
Expand All @@ -198,6 +201,7 @@ exports.deleteAccount = async function (options) {
};

exports.keys = async function (options) {
await eventtracking.askForId(options);
let near = await connect(options);
let account = await near.account(options.accountId);
let accessKeys = await account.getAccessKeys();
Expand All @@ -206,6 +210,7 @@ exports.keys = async function (options) {
};

exports.sendMoney = async function (options) {
await eventtracking.askForId(options);
console.log(`Sending ${options.amount} NEAR to ${options.receiver} from ${options.sender}`);
const near = await connect(options);
const account = await near.account(options.sender);
Expand All @@ -214,6 +219,7 @@ exports.sendMoney = async function (options) {
};

exports.stake = async function (options) {
await eventtracking.askForId(options);
console.log(`Staking ${options.amount} (${utils.format.parseNearAmount(options.amount)}) on ${options.accountId} with public key = ${qs.unescape(options.stakingKey)}.`);
const near = await connect(options);
const account = await near.account(options.accountId);
Expand Down
50 changes: 49 additions & 1 deletion utils/eventtracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const getEventTrackingConsent = async () => {
const answer = await new Promise((resolve) => {
rl.question(
chalk`We would like to collect data on near-cli usage to improve developer experience.` +
chalk` We will never send private information. We only collect which commands are run via an anonymous identifier.` +
chalk` We will never send private information. We only collect which commands are run with attributes.` +
chalk`{bold.yellow Would you like to opt in (y/n)? }`,
async (consentToEventTracking) => {
if (consentToEventTracking.toLowerCase() == 'y') {
Expand All @@ -109,6 +109,53 @@ const getEventTrackingConsent = async () => {
}
};

const getIdTrackingConsent = async () => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
try {
for (let attempts = 0; attempts < 10; attempts++) {
const answer = await new Promise((resolve) => {
rl.question(
chalk`We would like to help with your development journey with NEAR.` +
chalk` We will ask you to expose your account Id while using command. ` +
chalk`{bold.yellow Would you like to expose the account Id (y/n)? }`,
async (consentToEventTracking) => {
if (consentToEventTracking.toLowerCase() == 'y') {
resolve(true);
} else if (
consentToEventTracking.toLowerCase() == 'n'
) {
resolve(false);
}
resolve(undefined);
}
);
});
if (answer !== undefined) {
return answer;
}
}
return false; // If they can't figure it out in this many attempts, just opt out
} finally {
rl.close();
}
};

const askForId = async (options) => {
const answer = await getIdTrackingConsent();
if(answer){
const shellSettings = settings.getShellSettings();
const id = isGitPod() ? getGitPodUserHash() : shellSettings[TRACKING_SESSION_ID_KEY];
await Promise.all([
mixpanel.alias(options.accountId, id),
mixpanel.people.set({account_id: options.accountId})
]);

}
};

const askForConsentIfNeeded = async (options) => {
const shellSettings = settings.getShellSettings();
// if the appropriate option is not in settings, ask now and save settings.
Expand All @@ -135,6 +182,7 @@ module.exports = {
track,
askForConsentIfNeeded,
trackDeployedContract,
askForId,
// Some of the event ids are auto-generated runtime with the naming convention event_id_shell_{command}_start

EVENT_ID_CREATE_ACCOUNT_END: 'event_id_shell_create-account_end',
Expand Down

0 comments on commit fe5a618

Please sign in to comment.