create-near-app 5.2.0 → 5.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-near-app",
3
- "version": "5.2.0",
3
+ "version": "5.2.1",
4
4
  "description": "Quickly scaffold your dApp on NEAR Blockchain",
5
5
  "main": "index.js",
6
6
  "engines": {
@@ -1,3 +1,4 @@
1
+ // Find all our documentation at https://docs.near.org
1
2
  import { NearBindgen, near, call, view } from 'near-sdk-js';
2
3
 
3
4
  @NearBindgen({})
@@ -10,9 +11,8 @@ class HelloNear {
10
11
  }
11
12
 
12
13
  @call({}) // This method changes the state, for which it cost gas
13
- set_greeting({ message }: { message: string }): void {
14
- // Record a log permanently to the blockchain!
15
- near.log(`Saving greeting ${message}`);
16
- this.greeting = message;
14
+ set_greeting({ greeting }: { greeting: string }): void {
15
+ near.log(`Saving greeting ${greeting}`);
16
+ this.greeting = greeting;
17
17
  }
18
18
  }
@@ -1,11 +1,4 @@
1
- /*
2
- * Example smart contract written in RUST
3
- *
4
- * Learn more about writing NEAR smart contracts with Rust:
5
- * https://near-docs.io/develop/Contract
6
- *
7
- */
8
-
1
+ // Find all our documentation at https://docs.near.org
9
2
  use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
10
3
  use near_sdk::{log, near_bindgen};
11
4
 
@@ -36,7 +29,6 @@ impl Contract {
36
29
 
37
30
  // Public method - accepts a greeting, such as "howdy", and records it
38
31
  pub fn set_greeting(&mut self, message: String) {
39
- // Use env::log to record logs permanently to the blockchain!
40
32
  log!("Saving greeting {}", message);
41
33
  self.message = message;
42
34
  }
@@ -6,20 +6,21 @@ import './assets/global.css';
6
6
  import { EducationalText, SignInPrompt, SignOutButton } from './ui-components';
7
7
 
8
8
 
9
- export default function App({ isSignedIn, helloNEAR, wallet }) {
9
+ export default function App({ isSignedIn, contractId, wallet }) {
10
10
  const [valueFromBlockchain, setValueFromBlockchain] = React.useState();
11
11
 
12
12
  const [uiPleaseWait, setUiPleaseWait] = React.useState(true);
13
13
 
14
14
  // Get blockchian state once on component load
15
15
  React.useEffect(() => {
16
- helloNEAR.getGreeting()
16
+ getGreeting()
17
17
  .then(setValueFromBlockchain)
18
18
  .catch(alert)
19
19
  .finally(() => {
20
20
  setUiPleaseWait(false);
21
21
  });
22
- }, []);
22
+ }
23
+ , []);
23
24
 
24
25
  /// If user not signed-in with wallet - show prompt
25
26
  if (!isSignedIn) {
@@ -31,14 +32,21 @@ export default function App({ isSignedIn, helloNEAR, wallet }) {
31
32
  e.preventDefault();
32
33
  setUiPleaseWait(true);
33
34
  const { greetingInput } = e.target.elements;
34
- helloNEAR.setGreeting(greetingInput.value)
35
- .then(async () => {return helloNEAR.getGreeting();})
35
+
36
+ // use the wallet to send the greeting to the contract
37
+ wallet.callMethod({ method: 'set_greeting', args: { message: greetingInput.value }, contractId })
38
+ .then(async () => {return getGreeting();})
36
39
  .then(setValueFromBlockchain)
37
40
  .finally(() => {
38
41
  setUiPleaseWait(false);
39
42
  });
40
43
  }
41
44
 
45
+ function getGreeting(){
46
+ // use the wallet to query the contract's greeting
47
+ return wallet.viewMethod({ method: 'get_greeting', contractId })
48
+ }
49
+
42
50
  return (
43
51
  <>
44
52
  <SignOutButton accountId={wallet.accountId} onClick={() => wallet.signOut()}/>
@@ -64,4 +72,4 @@ export default function App({ isSignedIn, helloNEAR, wallet }) {
64
72
  </main>
65
73
  </>
66
74
  );
67
- }
75
+ }
@@ -4,22 +4,20 @@ import ReactDOM from 'react-dom';
4
4
  import App from './App';
5
5
 
6
6
  // NEAR
7
- import { HelloNEAR } from './near-interface';
8
7
  import { Wallet } from './near-wallet';
9
8
 
9
+ const CONTRACT_ADDRESS = process.env.CONTRACT_NAME
10
+
10
11
  // When creating the wallet you can optionally ask to create an access key
11
12
  // Having the key enables to call non-payable methods without interrupting the user to sign
12
- const wallet = new Wallet({ createAccessKeyFor: process.env.CONTRACT_NAME })
13
-
14
- // Abstract the logic of interacting with the contract to simplify your flow
15
- const helloNEAR = new HelloNEAR({ contractId: process.env.CONTRACT_NAME, walletToUse: wallet });
13
+ const wallet = new Wallet({ createAccessKeyFor: CONTRACT_ADDRESS })
16
14
 
17
15
  // Setup on page load
18
16
  window.onload = async () => {
19
17
  const isSignedIn = await wallet.startUp()
20
18
 
21
19
  ReactDOM.render(
22
- <App isSignedIn={isSignedIn} helloNEAR={helloNEAR} wallet={wallet} />,
20
+ <App isSignedIn={isSignedIn} contractId={CONTRACT_ADDRESS} wallet={wallet} />,
23
21
  document.getElementById('root')
24
22
  );
25
23
  }
@@ -1,13 +1,11 @@
1
1
  import 'regenerator-runtime/runtime';
2
2
  import { Wallet } from './near-wallet';
3
- import { HelloNEAR } from './near-interface';
3
+
4
+ const CONTRACT_ADDRESS = process.env.CONTRACT_NAME;
4
5
 
5
6
  // When creating the wallet you can optionally ask to create an access key
6
7
  // Having the key enables to call non-payable methods without interrupting the user to sign
7
- const wallet = new Wallet({ createAccessKeyFor: process.env.CONTRACT_NAME })
8
-
9
- // Abstract the logic of interacting with the contract to simplify your flow
10
- const helloNEAR = new HelloNEAR({ contractId: process.env.CONTRACT_NAME, walletToUse: wallet });
8
+ const wallet = new Wallet({ createAccessKeyFor: CONTRACT_ADDRESS })
11
9
 
12
10
  // Setup on page load
13
11
  window.onload = async () => {
@@ -19,47 +17,50 @@ window.onload = async () => {
19
17
  signedOutFlow();
20
18
  }
21
19
 
22
- fetchGreeting();
20
+ getGreeting();
23
21
  };
24
22
 
25
23
  // Button clicks
26
- document.querySelector('form').onsubmit = doUserAction;
24
+ document.querySelector('form').onsubmit = setGreeting;
27
25
  document.querySelector('#sign-in-button').onclick = () => { wallet.signIn(); };
28
26
  document.querySelector('#sign-out-button').onclick = () => { wallet.signOut(); };
29
27
 
30
- // Take the new greeting and send it to the contract
31
- async function doUserAction(event) {
28
+ async function setGreeting(event) {
29
+ // handle UI
32
30
  event.preventDefault();
33
31
  const { greeting } = event.target.elements;
34
32
 
35
33
  document.querySelector('#signed-in-flow main')
36
34
  .classList.add('please-wait');
37
35
 
38
- await helloNEAR.setGreeting(greeting.value);
36
+ // use the wallet to send the greeting to the Smart Contract
37
+ await wallet.callMethod({ method: 'set_greeting', args: { message: greeting.value }, contractId: CONTRACT_ADDRESS });
39
38
 
40
- // ===== Fetch the data from the blockchain =====
41
- await fetchGreeting();
42
- document.querySelector('#signed-in-flow main')
43
- .classList.remove('please-wait');
39
+ // query the new greeting
40
+ await getGreeting();
41
+
42
+ // handle UI stuff
43
+ document.querySelector('#signed-in-flow main').classList.remove('please-wait');
44
44
  }
45
45
 
46
- // Get greeting from the contract on chain
47
- async function fetchGreeting() {
48
- const currentGreeting = await helloNEAR.getGreeting();
46
+ async function getGreeting() {
47
+ // use the wallet to query the Smart Contract
48
+ const currentGreeting = await wallet.viewMethod({ method: 'get_greeting', contractId: CONTRACT_ADDRESS });
49
49
 
50
+ // handle UI stuff
50
51
  document.querySelectorAll('[data-behavior=greeting]').forEach(el => {
51
52
  el.innerText = currentGreeting;
52
53
  el.value = currentGreeting;
53
54
  });
54
55
  }
55
56
 
56
- // Display the signed-out-flow container
57
+ // UI: Display the signed-out-flow container
57
58
  function signedOutFlow() {
58
59
  document.querySelector('#signed-in-flow').style.display = 'none';
59
60
  document.querySelector('#signed-out-flow').style.display = 'block';
60
61
  }
61
62
 
62
- // Displaying the signed in flow container and fill in account-specific data
63
+ // UI: Displaying the signed in flow container and fill in account-specific data
63
64
  function signedInFlow() {
64
65
  document.querySelector('#signed-out-flow').style.display = 'none';
65
66
  document.querySelector('#signed-in-flow').style.display = 'block';
@@ -1,16 +0,0 @@
1
- /* Talking with a contract often involves transforming data, we recommend you to encapsulate that logic into a class */
2
-
3
- export class HelloNEAR {
4
- constructor({ contractId, walletToUse }) {
5
- this.contractId = contractId;
6
- this.wallet = walletToUse;
7
- }
8
-
9
- async getGreeting() {
10
- return await this.wallet.viewMethod({ contractId: this.contractId, method: 'get_greeting' });
11
- }
12
-
13
- async setGreeting(greeting) {
14
- return await this.wallet.callMethod({ contractId: this.contractId, method: 'set_greeting', args: { message: greeting } });
15
- }
16
- }