create-near-app 3.1.0 → 4.0.0-beta.3.0
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/README.md +37 -31
- package/dist/app.js +76 -0
- package/dist/checks.js +34 -0
- package/dist/make.js +137 -0
- package/dist/messages.js +56 -0
- package/dist/package-json.js +164 -0
- package/dist/tracking.js +34 -0
- package/dist/types.js +3 -0
- package/dist/user-input.js +211 -0
- package/index.js +1 -233
- package/package.json +49 -40
- package/{contracts → templates/contracts}/assemblyscript/README.md +6 -5
- package/templates/contracts/assemblyscript/as-pect.config.js +1 -0
- package/{contracts → templates/contracts}/assemblyscript/asconfig.json +0 -0
- package/{contracts → templates/contracts}/assemblyscript/assembly/__tests__/as-pect.d.ts +0 -0
- package/templates/contracts/assemblyscript/assembly/__tests__/main.spec.ts +11 -0
- package/{contracts → templates/contracts}/assemblyscript/assembly/as_types.d.ts +0 -0
- package/{contracts → templates/contracts}/assemblyscript/assembly/index.ts +5 -5
- package/{contracts → templates/contracts}/assemblyscript/assembly/tsconfig.json +0 -0
- package/templates/contracts/assemblyscript/package.json +14 -0
- package/templates/contracts/js/README.md +23 -0
- package/templates/contracts/js/babel.config.json +7 -0
- package/templates/contracts/js/package.json +18 -0
- package/templates/contracts/js/src/contract.ts +31 -0
- package/templates/contracts/js/tsconfig.json +10 -0
- package/{contracts → templates/contracts}/rust/.cargo/config +0 -0
- package/{contracts → templates/contracts}/rust/Cargo.toml +1 -1
- package/{contracts → templates/contracts}/rust/README.md +6 -8
- package/{contracts → templates/contracts}/rust/src/lib.rs +0 -0
- package/templates/frontend/react/frontend/App.js +66 -0
- package/templates/{react/frontend/assets/img → frontend/react/frontend/assets}/favicon.ico +0 -0
- package/templates/{vanilla/frontend/assets/css → frontend/react/frontend/assets}/global.css +64 -80
- package/templates/{react/frontend/assets/img → frontend/react/frontend/assets}/logo-black.svg +0 -0
- package/templates/{react/frontend/assets/img → frontend/react/frontend/assets}/logo-white.svg +0 -0
- package/templates/frontend/react/frontend/index.html +15 -0
- package/templates/frontend/react/frontend/index.js +15 -0
- package/templates/{react/frontend/assets/js/near/utils.js → frontend/react/frontend/near-api.js} +19 -19
- package/templates/frontend/react/frontend/near-config.js +61 -0
- package/templates/frontend/react/frontend/package.json +44 -0
- package/templates/frontend/react/frontend/ui-components.js +60 -0
- package/templates/{vanilla/frontend/assets/img → frontend/vanilla/frontend/assets}/favicon.ico +0 -0
- package/templates/{react/frontend/assets/css → frontend/vanilla/frontend/assets}/global.css +64 -80
- package/templates/{vanilla/frontend/assets/img → frontend/vanilla/frontend/assets}/logo-black.svg +0 -0
- package/templates/{vanilla/frontend/assets/img → frontend/vanilla/frontend/assets}/logo-white.svg +0 -0
- package/templates/frontend/vanilla/frontend/index.html +86 -0
- package/templates/frontend/vanilla/frontend/index.js +78 -0
- package/templates/{vanilla/frontend/assets/js/near/utils.js → frontend/vanilla/frontend/near-api.js} +18 -18
- package/templates/frontend/vanilla/frontend/near-config.js +62 -0
- package/templates/frontend/vanilla/frontend/package.json +18 -0
- package/templates/integration-tests/overrides/js-contract/js-tests/src/main.ava.ts +46 -0
- package/templates/integration-tests/overrides/js-contract/rust-tests/src/tests.rs +74 -0
- package/{integration-tests/js → templates/integration-tests/shared/js-tests}/ava.config.cjs +9 -9
- package/{integration-tests/js → templates/integration-tests/shared/js-tests}/package.json +5 -4
- package/templates/integration-tests/shared/js-tests/src/main.ava.ts +44 -0
- package/{integration-tests/rs → templates/integration-tests/shared/rust-tests}/Cargo.toml +1 -1
- package/{integration-tests/rs → templates/integration-tests/shared/rust-tests}/src/tests.rs +7 -5
- package/templates/{none → shared}/.gitpod.yml +2 -2
- package/templates/{vanilla → shared}/README.md +29 -30
- package/templates/{react → shared}/near.gitignore +1 -1
- package/contracts/assemblyscript/as-pect.config.js +0 -1
- package/contracts/assemblyscript/assembly/__tests__/main.spec.ts +0 -11
- package/contracts/assemblyscript/package.json +0 -13
- package/integration-tests/js/src/config.ts +0 -31
- package/integration-tests/js/src/main.ava.ts +0 -32
- package/integration-tests/ts/ava.config.cjs +0 -9
- package/integration-tests/ts/package.json +0 -16
- package/integration-tests/ts/src/main.ava.ts +0 -44
- package/templates/none/.gitattributes +0 -2
- package/templates/none/README.md +0 -101
- package/templates/none/near.gitignore +0 -27
- package/templates/none/neardev/shared-test/test.near.json +0 -1
- package/templates/none/package.json +0 -33
- package/templates/react/.gitpod.yml +0 -6
- package/templates/react/README.md +0 -102
- package/templates/react/ava.config.cjs +0 -9
- package/templates/react/frontend/App.js +0 -202
- package/templates/react/frontend/__mocks__/fileMock.js +0 -3
- package/templates/react/frontend/assets/js/near/config.js +0 -63
- package/templates/react/frontend/index.html +0 -25
- package/templates/react/frontend/index.js +0 -14
- package/templates/react/neardev/shared-test/test.near.json +0 -1
- package/templates/react/package.json +0 -55
- package/templates/vanilla/.gitattributes +0 -2
- package/templates/vanilla/.gitpod.yml +0 -6
- package/templates/vanilla/frontend/assets/js/index.js +0 -74
- package/templates/vanilla/frontend/assets/js/near/config.js +0 -63
- package/templates/vanilla/frontend/index.html +0 -103
- package/templates/vanilla/near.gitignore +0 -27
- package/templates/vanilla/neardev/shared-test/test.near.json +0 -1
- package/templates/vanilla/package.json +0 -34
- package/utils/rust-setup.js +0 -113
- package/utils/tracking.js +0 -29
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
Hello NEAR!
|
|
2
|
+
=================================
|
|
3
|
+
|
|
4
|
+
A [smart contract] written with [NEAR JavaScript SDK] for an app initialized with [create-near-app]
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Quick Start
|
|
8
|
+
===========
|
|
9
|
+
|
|
10
|
+
1. Install dependencies: `npm install`
|
|
11
|
+
2. Deploy to NEAR TestNet with a temporary dev account: `npm run deploy`
|
|
12
|
+
|
|
13
|
+
Exploring The Code
|
|
14
|
+
==================
|
|
15
|
+
|
|
16
|
+
1. The smart contract code is in `src/contract.ts`.
|
|
17
|
+
2. There are two functions to the smart contract: `get_greeting` and `set_greeting`.
|
|
18
|
+
3. Tests: Use integration-tests that `create-near-app` created for you.
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
[smart contract]: https://docs.near.org/develop/welcome
|
|
22
|
+
[NEAR JavaScript SDK]: https://github.com/near/near-sdk-js
|
|
23
|
+
[create-near-app]: https://github.com/near/create-near-app
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "hello_near",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"license": "(MIT AND Apache-2.0)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "near-sdk-js build src/contract.ts build/hello_near.wasm",
|
|
8
|
+
"deploy": "npm run build && rm -rf neardev && near dev-deploy --wasmFile build/hello_near.wasm && export $(cat ./neardev/dev-account.env) && near call $CONTRACT_NAME init --accountId $CONTRACT_NAME --deposit 1 && echo $CONTRACT_NAME",
|
|
9
|
+
"test": "echo use integration-tests"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"near-cli": "^3.4.0",
|
|
13
|
+
"near-sdk-js": "0.4.0-4"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"typescript": "^4.7.4"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { NearBindgen, NearContract, near, call, view } from 'near-sdk-js';
|
|
2
|
+
|
|
3
|
+
// The @NearBindgen decorator allows this code to compile to Base64.
|
|
4
|
+
@NearBindgen
|
|
5
|
+
class MyContract extends NearContract {
|
|
6
|
+
my_greeting: string;
|
|
7
|
+
|
|
8
|
+
constructor() {
|
|
9
|
+
//execute the NEAR Contract's constructor
|
|
10
|
+
super();
|
|
11
|
+
this.my_greeting = 'Hello';
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// @call indicates that this is a 'change method' or a function
|
|
15
|
+
// that changes state on the blockchain. Change methods cost gas.
|
|
16
|
+
// For more info -> https://docs.near.org/docs/concepts/gas
|
|
17
|
+
@call
|
|
18
|
+
set_greeting({ message }: { message: string }): void {
|
|
19
|
+
near.log(`Saving greeting ${message}`);
|
|
20
|
+
this.my_greeting = message;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// @view indicates a 'view method' or a function that returns
|
|
24
|
+
// the current values stored on the blockchain. View calls are free
|
|
25
|
+
// and do not cost gas.
|
|
26
|
+
@view
|
|
27
|
+
get_greeting(): string {
|
|
28
|
+
near.log(`The current greeting is ${this.my_greeting}`);
|
|
29
|
+
return this.my_greeting;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
File without changes
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
Hello NEAR!
|
|
2
|
+
=================================
|
|
3
3
|
|
|
4
4
|
A [smart contract] written in [Rust] for an app initialized with [create-near-app]
|
|
5
5
|
|
|
@@ -9,18 +9,16 @@ Quick Start
|
|
|
9
9
|
|
|
10
10
|
Before you compile this code, you will need to install Rust with [correct target]
|
|
11
11
|
|
|
12
|
-
|
|
13
12
|
Exploring The Code
|
|
14
13
|
==================
|
|
15
14
|
|
|
16
15
|
1. The main smart contract code lives in `src/lib.rs`.
|
|
17
|
-
2.
|
|
18
|
-
|
|
19
|
-
can see any debug info you print to the console.
|
|
16
|
+
2. There are two functions to the smart contract: `get_greeting` and `set_greeting`.
|
|
17
|
+
3. Tests: You can run smart contract tests with the `cargo test`.
|
|
20
18
|
|
|
21
19
|
|
|
22
|
-
[smart contract]: https://docs.near.org/
|
|
20
|
+
[smart contract]: https://docs.near.org/develop/welcome
|
|
23
21
|
[Rust]: https://www.rust-lang.org/
|
|
24
22
|
[create-near-app]: https://github.com/near/create-near-app
|
|
25
|
-
[correct target]: https://
|
|
23
|
+
[correct target]: https://docs.near.org/develop/prerequisites#rust-and-wasm
|
|
26
24
|
[cargo]: https://doc.rust-lang.org/book/ch01-03-hello-cargo.html
|
|
File without changes
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import 'regenerator-runtime/runtime';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
|
|
4
|
+
import './assets/global.css';
|
|
5
|
+
|
|
6
|
+
import { getGreetingFromContract, setGreetingOnContract } from './near-api';
|
|
7
|
+
import { EducationalText, SignInPrompt, SignOutButton } from './ui-components';
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
export default function App() {
|
|
11
|
+
const [valueFromBlockchain, setValueFromBlockchain] = React.useState();
|
|
12
|
+
|
|
13
|
+
const [uiPleaseWait, setUiPleaseWait] = React.useState(true);
|
|
14
|
+
|
|
15
|
+
// Get blockchian state once on component load
|
|
16
|
+
React.useEffect(() => {
|
|
17
|
+
getGreetingFromContract()
|
|
18
|
+
.then(setValueFromBlockchain)
|
|
19
|
+
.catch(alert)
|
|
20
|
+
.finally(() => {
|
|
21
|
+
setUiPleaseWait(false);
|
|
22
|
+
});
|
|
23
|
+
}, []);
|
|
24
|
+
|
|
25
|
+
/// If user not signed-in with wallet - show prompt
|
|
26
|
+
if (!window.walletConnection.isSignedIn()) {
|
|
27
|
+
// Sign-in flow will reload the page later
|
|
28
|
+
return <SignInPrompt greeting={valueFromBlockchain}/>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function changeGreeting(e) {
|
|
32
|
+
e.preventDefault();
|
|
33
|
+
setUiPleaseWait(true);
|
|
34
|
+
const { greetingInput } = e.target.elements;
|
|
35
|
+
setGreetingOnContract(greetingInput.value)
|
|
36
|
+
.then(getGreetingFromContract)
|
|
37
|
+
.then(setValueFromBlockchain)
|
|
38
|
+
.catch(alert)
|
|
39
|
+
.finally(() => {
|
|
40
|
+
setUiPleaseWait(false);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<>
|
|
46
|
+
<SignOutButton accountId={window.accountId}/>
|
|
47
|
+
<main className={uiPleaseWait ? 'please-wait' : ''}>
|
|
48
|
+
<h1>
|
|
49
|
+
The contract says: <span className="greeting">{valueFromBlockchain}</span>
|
|
50
|
+
</h1>
|
|
51
|
+
<form onSubmit={changeGreeting} className='change'>
|
|
52
|
+
<label>Change greeting:</label>
|
|
53
|
+
<div>
|
|
54
|
+
<input
|
|
55
|
+
autoComplete="off"
|
|
56
|
+
defaultValue={valueFromBlockchain}
|
|
57
|
+
id="greetingInput"
|
|
58
|
+
/>
|
|
59
|
+
<button>Save</button>
|
|
60
|
+
</div>
|
|
61
|
+
</form>
|
|
62
|
+
<EducationalText/>
|
|
63
|
+
</main>
|
|
64
|
+
</>
|
|
65
|
+
);
|
|
66
|
+
}
|
|
File without changes
|
|
@@ -27,17 +27,75 @@ body {
|
|
|
27
27
|
main {
|
|
28
28
|
margin: 0 auto;
|
|
29
29
|
max-width: 26em;
|
|
30
|
-
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
main.please-wait {
|
|
33
|
+
opacity: 0.5;
|
|
34
|
+
cursor: wait;
|
|
31
35
|
}
|
|
32
36
|
|
|
33
37
|
h1 {
|
|
34
|
-
background-image: url(
|
|
38
|
+
background-image: url(./logo-black.svg);
|
|
35
39
|
background-position: center 1em;
|
|
36
40
|
background-repeat: no-repeat;
|
|
37
41
|
background-size: auto 1.5em;
|
|
38
42
|
margin-top: 0;
|
|
39
|
-
padding: 3.5em 0 0
|
|
43
|
+
padding: 3.5em 0 0;
|
|
40
44
|
text-align: center;
|
|
45
|
+
font-size: 1.5em;
|
|
46
|
+
}
|
|
47
|
+
.greeting {
|
|
48
|
+
color: var(--secondary);
|
|
49
|
+
text-decoration: underline;
|
|
50
|
+
}
|
|
51
|
+
h2 {
|
|
52
|
+
text-align: center;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.please-wait .change {
|
|
56
|
+
pointer-events: none;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
ul.information {
|
|
60
|
+
margin: 2em 0 2em 0;
|
|
61
|
+
padding: 0;
|
|
62
|
+
text-align: left;
|
|
63
|
+
font-size: 0.8em;
|
|
64
|
+
|
|
65
|
+
}
|
|
66
|
+
.information li:first-child {
|
|
67
|
+
border-top: 1px solid var(--light-gray);
|
|
68
|
+
}
|
|
69
|
+
.information li {
|
|
70
|
+
padding: 0.5em 0;
|
|
71
|
+
border-bottom: 1px solid var(--light-gray);
|
|
72
|
+
list-style: none;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
.change {
|
|
76
|
+
display: flex;
|
|
77
|
+
flex-direction: column;
|
|
78
|
+
align-content: stretch;
|
|
79
|
+
justify-content: space-evenly;
|
|
80
|
+
align-items: stretch;
|
|
81
|
+
font-size: 1em;
|
|
82
|
+
border: 2px solid var(--light-gray);
|
|
83
|
+
padding: 0.5em;
|
|
84
|
+
}
|
|
85
|
+
.change > div {
|
|
86
|
+
display: flex;
|
|
87
|
+
align-content: stretch;
|
|
88
|
+
justify-content: space-evenly;
|
|
89
|
+
align-items: stretch;
|
|
90
|
+
}
|
|
91
|
+
.change input {
|
|
92
|
+
flex: 1;
|
|
93
|
+
}
|
|
94
|
+
.change label {
|
|
95
|
+
display: block;
|
|
96
|
+
text-align: left;
|
|
97
|
+
margin-right: 10px;
|
|
98
|
+
padding-bottom: 0.5em;
|
|
41
99
|
}
|
|
42
100
|
|
|
43
101
|
a,
|
|
@@ -73,57 +131,14 @@ button {
|
|
|
73
131
|
button:hover, button:focus {
|
|
74
132
|
box-shadow: 0 0 10em rgba(255, 255, 255, 0.2) inset;
|
|
75
133
|
}
|
|
76
|
-
button:active {
|
|
77
|
-
box-shadow: 0 0 10em rgba(0, 0, 0, 0.1) inset;
|
|
78
|
-
}
|
|
79
|
-
button.link {
|
|
80
|
-
background: none;
|
|
81
|
-
border: none;
|
|
82
|
-
box-shadow: none;
|
|
83
|
-
display: inline;
|
|
84
|
-
}
|
|
85
|
-
[disabled] button, button[disabled] {
|
|
86
|
-
box-shadow: none;
|
|
87
|
-
background-color: var(--light-gray);
|
|
88
|
-
color: gray;
|
|
89
|
-
cursor: not-allowed;
|
|
90
|
-
transform: none;
|
|
91
|
-
}
|
|
92
|
-
[disabled] button {
|
|
93
|
-
text-indent: -900em;
|
|
94
|
-
width: 2em;
|
|
95
|
-
position: relative;
|
|
96
|
-
}
|
|
97
|
-
[disabled] button:after {
|
|
98
|
-
content: " ";
|
|
99
|
-
display: block;
|
|
100
|
-
width: 0.8em;
|
|
101
|
-
height: 0.8em;
|
|
102
|
-
border-radius: 50%;
|
|
103
|
-
border: 2px solid #fff;
|
|
104
|
-
border-color: var(--fg) transparent var(--fg) transparent;
|
|
105
|
-
animation: loader 1.2s linear infinite;
|
|
106
|
-
position: absolute;
|
|
107
|
-
top: 0.45em;
|
|
108
|
-
right: 0.5em;
|
|
109
|
-
}
|
|
110
|
-
@keyframes loader {
|
|
111
|
-
0% { transform: rotate(0deg) }
|
|
112
|
-
100% { transform: rotate(360deg) }
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
fieldset {
|
|
116
|
-
border: none;
|
|
117
|
-
padding: 2em 0;
|
|
118
|
-
}
|
|
119
134
|
|
|
120
135
|
input {
|
|
121
|
-
background-color: var(--
|
|
136
|
+
background-color: var(--light-gray);
|
|
122
137
|
border: none;
|
|
123
138
|
border-radius: 5px 0 0 5px;
|
|
124
139
|
caret-color: var(--primary);
|
|
125
140
|
color: inherit;
|
|
126
|
-
padding: 0.25em
|
|
141
|
+
padding: 0.25em 0.5em;
|
|
127
142
|
}
|
|
128
143
|
input::selection {
|
|
129
144
|
background-color: var(--secondary);
|
|
@@ -141,37 +156,6 @@ li {
|
|
|
141
156
|
padding-bottom: 1em;
|
|
142
157
|
}
|
|
143
158
|
|
|
144
|
-
aside {
|
|
145
|
-
animation: notify ease-in-out 10s;
|
|
146
|
-
background-color: var(--shadow);
|
|
147
|
-
border-radius: 5px;
|
|
148
|
-
bottom: 0;
|
|
149
|
-
font-size: 0.8em;
|
|
150
|
-
margin: 1em;
|
|
151
|
-
padding: 1em;
|
|
152
|
-
position: fixed;
|
|
153
|
-
transform: translateY(10em);
|
|
154
|
-
right: 0;
|
|
155
|
-
}
|
|
156
|
-
aside footer {
|
|
157
|
-
display: flex;
|
|
158
|
-
font-size: 0.9em;
|
|
159
|
-
justify-content: space-between;
|
|
160
|
-
margin-top: 0.5em;
|
|
161
|
-
}
|
|
162
|
-
aside footer *:first-child {
|
|
163
|
-
color: var(--success);
|
|
164
|
-
}
|
|
165
|
-
aside footer *:last-child {
|
|
166
|
-
color: var(--gray);
|
|
167
|
-
}
|
|
168
|
-
@keyframes notify {
|
|
169
|
-
0% { transform: translateY(10em) }
|
|
170
|
-
5% { transform: translateY(0) }
|
|
171
|
-
95% { transform: translateY(0) }
|
|
172
|
-
100% { transform: translateY(10em) }
|
|
173
|
-
}
|
|
174
|
-
|
|
175
159
|
@media (prefers-color-scheme: dark) {
|
|
176
160
|
html {
|
|
177
161
|
--bg: #1e1e1e;
|
|
@@ -181,7 +165,7 @@ aside footer *:last-child {
|
|
|
181
165
|
--light-gray: #444;
|
|
182
166
|
}
|
|
183
167
|
h1 {
|
|
184
|
-
background-image: url(
|
|
168
|
+
background-image: url(./logo-white.svg);
|
|
185
169
|
}
|
|
186
170
|
input:focus {
|
|
187
171
|
box-shadow: 0 0 10em rgba(255, 255, 255, 0.02) inset;
|
package/templates/{react/frontend/assets/img → frontend/react/frontend/assets}/logo-black.svg
RENAMED
|
File without changes
|
package/templates/{react/frontend/assets/img → frontend/react/frontend/assets}/logo-white.svg
RENAMED
|
File without changes
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8" />
|
|
5
|
+
<link rel="icon" href="assets/favicon.ico" />
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
7
|
+
<link rel="apple-touch-icon" href="assets/favicon.ico" />
|
|
8
|
+
<title>Welcome to NEAR with React</title>
|
|
9
|
+
</head>
|
|
10
|
+
<body>
|
|
11
|
+
<noscript>You need to enable JavaScript to run this app.</noscript>
|
|
12
|
+
<div id="root"></div>
|
|
13
|
+
<script src="./index.js" type="module"></script>
|
|
14
|
+
</body>
|
|
15
|
+
</html>
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { createRoot } from 'react-dom/client';
|
|
3
|
+
import App from './App';
|
|
4
|
+
import { initContract } from './near-api';
|
|
5
|
+
|
|
6
|
+
const reactRoot = createRoot(document.querySelector('#root'));
|
|
7
|
+
|
|
8
|
+
window.nearInitPromise = initContract()
|
|
9
|
+
.then(() => {
|
|
10
|
+
reactRoot.render(<App />);
|
|
11
|
+
})
|
|
12
|
+
.catch(e => {
|
|
13
|
+
reactRoot.render(<div style={{color: 'red'}}>Error: <code>{e.message}</code></div>);
|
|
14
|
+
console.error(e);
|
|
15
|
+
});
|
package/templates/{react/frontend/assets/js/near/utils.js → frontend/react/frontend/near-api.js}
RENAMED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import { connect, Contract, keyStores, WalletConnection } from 'near-api-js'
|
|
2
|
-
import getConfig from './config'
|
|
1
|
+
import { connect, Contract, keyStores, WalletConnection } from 'near-api-js';
|
|
2
|
+
import { getConfig } from './near-config';
|
|
3
3
|
|
|
4
|
-
const nearConfig = getConfig(process.env.NODE_ENV || 'development')
|
|
4
|
+
const nearConfig = getConfig(process.env.NODE_ENV || 'development');
|
|
5
5
|
|
|
6
6
|
// Initialize contract & set global variables
|
|
7
7
|
export async function initContract() {
|
|
8
8
|
// Initialize connection to the NEAR testnet
|
|
9
|
-
const near = await connect(Object.assign({ deps: { keyStore: new keyStores.BrowserLocalStorageKeyStore() } }, nearConfig))
|
|
9
|
+
const near = await connect(Object.assign({ deps: { keyStore: new keyStores.BrowserLocalStorageKeyStore() } }, nearConfig));
|
|
10
10
|
|
|
11
11
|
// Initializing Wallet based Account. It can work with NEAR testnet wallet that
|
|
12
12
|
// is hosted at https://wallet.testnet.near.org
|
|
13
|
-
window.walletConnection = new WalletConnection(near)
|
|
13
|
+
window.walletConnection = new WalletConnection(near);
|
|
14
14
|
|
|
15
15
|
// Getting the Account ID. If still unauthorized, it's just empty string
|
|
16
|
-
window.accountId = window.walletConnection.getAccountId()
|
|
16
|
+
window.accountId = window.walletConnection.getAccountId();
|
|
17
17
|
|
|
18
18
|
// Initializing our contract APIs by contract name and configuration
|
|
19
19
|
window.contract = await new Contract(window.walletConnection.account(), nearConfig.contractName, {
|
|
@@ -21,31 +21,31 @@ export async function initContract() {
|
|
|
21
21
|
viewMethods: ['get_greeting'],
|
|
22
22
|
// Change methods can modify the state. But you don't receive the returned value when called.
|
|
23
23
|
changeMethods: ['set_greeting'],
|
|
24
|
-
})
|
|
24
|
+
});
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
export function
|
|
28
|
-
window.walletConnection.signOut()
|
|
27
|
+
export function signOutNearWallet() {
|
|
28
|
+
window.walletConnection.signOut();
|
|
29
29
|
// reload page
|
|
30
|
-
window.location.replace(window.location.origin + window.location.pathname)
|
|
30
|
+
window.location.replace(window.location.origin + window.location.pathname);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
export function
|
|
33
|
+
export function signInWithNearWallet() {
|
|
34
34
|
// Allow the current app to make calls to the specified contract on the
|
|
35
35
|
// user's behalf.
|
|
36
36
|
// This works by creating a new access key for the user's account and storing
|
|
37
37
|
// the private key in localStorage.
|
|
38
|
-
window.walletConnection.requestSignIn(nearConfig.contractName)
|
|
38
|
+
window.walletConnection.requestSignIn(nearConfig.contractName);
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
export async function
|
|
41
|
+
export async function setGreetingOnContract(message) {
|
|
42
42
|
let response = await window.contract.set_greeting({
|
|
43
|
-
args:{message: message}
|
|
44
|
-
})
|
|
45
|
-
return response
|
|
43
|
+
args: { message: message }
|
|
44
|
+
});
|
|
45
|
+
return response;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
export async function
|
|
49
|
-
let greeting = await window.contract.get_greeting()
|
|
50
|
-
return greeting
|
|
48
|
+
export async function getGreetingFromContract() {
|
|
49
|
+
let greeting = await window.contract.get_greeting();
|
|
50
|
+
return greeting;
|
|
51
51
|
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const CONTRACT_NAME = process.env.CONTRACT_NAME;
|
|
2
|
+
|
|
3
|
+
export function getConfig(env) {
|
|
4
|
+
switch (env) {
|
|
5
|
+
|
|
6
|
+
case 'production':
|
|
7
|
+
case 'mainnet':
|
|
8
|
+
return {
|
|
9
|
+
networkId: 'mainnet',
|
|
10
|
+
nodeUrl: 'https://rpc.mainnet.near.org',
|
|
11
|
+
contractName: CONTRACT_NAME,
|
|
12
|
+
walletUrl: 'https://wallet.near.org',
|
|
13
|
+
helperUrl: 'https://helper.mainnet.near.org',
|
|
14
|
+
explorerUrl: 'https://explorer.mainnet.near.org',
|
|
15
|
+
};
|
|
16
|
+
case 'development':
|
|
17
|
+
case 'testnet':
|
|
18
|
+
return {
|
|
19
|
+
networkId: 'testnet',
|
|
20
|
+
nodeUrl: 'https://rpc.testnet.near.org',
|
|
21
|
+
contractName: CONTRACT_NAME,
|
|
22
|
+
walletUrl: 'https://wallet.testnet.near.org',
|
|
23
|
+
helperUrl: 'https://helper.testnet.near.org',
|
|
24
|
+
explorerUrl: 'https://explorer.testnet.near.org',
|
|
25
|
+
};
|
|
26
|
+
case 'betanet':
|
|
27
|
+
return {
|
|
28
|
+
networkId: 'betanet',
|
|
29
|
+
nodeUrl: 'https://rpc.betanet.near.org',
|
|
30
|
+
contractName: CONTRACT_NAME,
|
|
31
|
+
walletUrl: 'https://wallet.betanet.near.org',
|
|
32
|
+
helperUrl: 'https://helper.betanet.near.org',
|
|
33
|
+
explorerUrl: 'https://explorer.betanet.near.org',
|
|
34
|
+
};
|
|
35
|
+
case 'local':
|
|
36
|
+
return {
|
|
37
|
+
networkId: 'local',
|
|
38
|
+
nodeUrl: 'http://localhost:3030',
|
|
39
|
+
keyPath: `${process.env.HOME}/.near/validator_key.json`,
|
|
40
|
+
walletUrl: 'http://localhost:4000/wallet',
|
|
41
|
+
contractName: CONTRACT_NAME,
|
|
42
|
+
};
|
|
43
|
+
case 'test':
|
|
44
|
+
case 'ci':
|
|
45
|
+
return {
|
|
46
|
+
networkId: 'shared-test',
|
|
47
|
+
nodeUrl: 'https://rpc.ci-testnet.near.org',
|
|
48
|
+
contractName: CONTRACT_NAME,
|
|
49
|
+
masterAccount: 'test.near',
|
|
50
|
+
};
|
|
51
|
+
case 'ci-betanet':
|
|
52
|
+
return {
|
|
53
|
+
networkId: 'shared-test-staging',
|
|
54
|
+
nodeUrl: 'https://rpc.ci-betanet.near.org',
|
|
55
|
+
contractName: CONTRACT_NAME,
|
|
56
|
+
masterAccount: 'test.near',
|
|
57
|
+
};
|
|
58
|
+
default:
|
|
59
|
+
throw Error(`Unconfigured environment '${env}'. Can be configured in src/config.js.`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "my-near-app",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"license": "(MIT AND Apache-2.0)",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"start": "echo The app is starting! && env-cmd -f ../contract/neardev/dev-account.env parcel index.html --open",
|
|
7
|
+
"build": "parcel build index.html --public-url ./"
|
|
8
|
+
},
|
|
9
|
+
"devDependencies": {
|
|
10
|
+
"@babel/core": "^7.18.2",
|
|
11
|
+
"@babel/preset-env": "^7.18.2",
|
|
12
|
+
"@babel/preset-react": "^7.17.12",
|
|
13
|
+
"@types/node": "^18.6.2",
|
|
14
|
+
"ava": "^4.2.0",
|
|
15
|
+
"react-test-renderer": "^18.1.0",
|
|
16
|
+
"ts-node": "^10.8.0",
|
|
17
|
+
"typescript": "^4.7.2",
|
|
18
|
+
"nodemon": "^2.0.16",
|
|
19
|
+
"parcel": "^2.6.0",
|
|
20
|
+
"process": "^0.11.10",
|
|
21
|
+
"env-cmd": "^10.1.0"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"near-api-js": "^0.44.2",
|
|
25
|
+
"react": "^18.1.0",
|
|
26
|
+
"react-dom": "^18.1.0",
|
|
27
|
+
"regenerator-runtime": "^0.13.9"
|
|
28
|
+
},
|
|
29
|
+
"resolutions": {
|
|
30
|
+
"@babel/preset-env": "7.13.8"
|
|
31
|
+
},
|
|
32
|
+
"browserslist": {
|
|
33
|
+
"production": [
|
|
34
|
+
">0.2%",
|
|
35
|
+
"not dead",
|
|
36
|
+
"not op_mini all"
|
|
37
|
+
],
|
|
38
|
+
"development": [
|
|
39
|
+
"last 1 chrome version",
|
|
40
|
+
"last 1 firefox version",
|
|
41
|
+
"last 1 safari version"
|
|
42
|
+
]
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { signInWithNearWallet, signOutNearWallet } from './near-api';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
|
|
4
|
+
export function SignInPrompt({greeting}) {
|
|
5
|
+
return (
|
|
6
|
+
<main>
|
|
7
|
+
<h1>
|
|
8
|
+
The contract says: <span className="greeting">{greeting}</span>
|
|
9
|
+
</h1>
|
|
10
|
+
<h3>
|
|
11
|
+
Welcome to NEAR!
|
|
12
|
+
</h3>
|
|
13
|
+
<p>
|
|
14
|
+
Your contract is storing a greeting message in the NEAR blockchain. To
|
|
15
|
+
change it you need to sign in using the NEAR Wallet. It is very simple,
|
|
16
|
+
just use the button below.
|
|
17
|
+
</p>
|
|
18
|
+
<p>
|
|
19
|
+
Do not worry, this app runs in the test network ("testnet"). It works
|
|
20
|
+
just like the main network ("mainnet"), but using NEAR Tokens that are
|
|
21
|
+
only for testing!
|
|
22
|
+
</p>
|
|
23
|
+
<br/>
|
|
24
|
+
<p style={{ textAlign: 'center' }}>
|
|
25
|
+
<button onClick={signInWithNearWallet}>Sign in with NEAR Wallet</button>
|
|
26
|
+
</p>
|
|
27
|
+
</main>
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function SignOutButton({accountId}) {
|
|
32
|
+
return (
|
|
33
|
+
<button style={{ float: 'right' }} onClick={signOutNearWallet}>
|
|
34
|
+
Sign out {accountId}
|
|
35
|
+
</button>
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function EducationalText() {
|
|
40
|
+
return (
|
|
41
|
+
<>
|
|
42
|
+
<p>
|
|
43
|
+
Look at that! A Hello World app! This greeting is stored on the NEAR blockchain. Check it out:
|
|
44
|
+
</p>
|
|
45
|
+
<ol>
|
|
46
|
+
<li>
|
|
47
|
+
Look in <code>src/App.js</code> and <code>src/utils.js</code> – you'll see <code>get_greeting</code> and <code>set_greeting</code> being called on <code>contract</code>. What's this?
|
|
48
|
+
</li>
|
|
49
|
+
<li>
|
|
50
|
+
Ultimately, this <code>contract</code> code is defined in <code>assembly/main.ts</code> – this is the source code for your <a target="_blank" rel="noreferrer" href="https://docs.near.org/docs/develop/contracts/overview">smart contract</a>.</li>
|
|
51
|
+
<li>
|
|
52
|
+
When you run <code>npm run dev</code>, the code in <code>assembly/main.ts</code> gets deployed to the NEAR testnet. You can see how this happens by looking in <code>package.json</code> at the <code>scripts</code> section to find the <code>dev</code> command.</li>
|
|
53
|
+
</ol>
|
|
54
|
+
<hr />
|
|
55
|
+
<p>
|
|
56
|
+
To keep learning, check out <a target="_blank" rel="noreferrer" href="https://docs.near.org">the NEAR docs</a> or look through some <a target="_blank" rel="noreferrer" href="https://examples.near.org">example apps</a>.
|
|
57
|
+
</p>
|
|
58
|
+
</>
|
|
59
|
+
);
|
|
60
|
+
}
|
package/templates/{vanilla/frontend/assets/img → frontend/vanilla/frontend/assets}/favicon.ico
RENAMED
|
File without changes
|