Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
samlinuxAcademy committed Feb 29, 2024
0 parents commit e51a252
Show file tree
Hide file tree
Showing 29 changed files with 1,431 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

# DFX CANISTER ENVIRONMENT VARIABLES
DFX_VERSION='0.17.0'
DFX_NETWORK='local'
CANISTER_CANDID_PATH_backend='/Users/rbole/Sites/starterKits/ic-solidjs/.dfx/local/canisters/backend/backend.did'
CANISTER_CANDID_PATH_BACKEND='/Users/rbole/Sites/starterKits/ic-solidjs/.dfx/local/canisters/backend/backend.did'
FRONTEND_CANISTER_ID='be2us-64aaa-aaaaa-qaabq-cai'
CANISTER_ID_FRONTEND='be2us-64aaa-aaaaa-qaabq-cai'
CANISTER_ID_frontend='be2us-64aaa-aaaaa-qaabq-cai'
BACKEND_CANISTER_ID='bkyz2-fmaaa-aaaaa-qaaaq-cai'
CANISTER_ID_BACKEND='bkyz2-fmaaa-aaaaa-qaaaq-cai'
CANISTER_ID_backend='bkyz2-fmaaa-aaaaa-qaaaq-cai'
CANISTER_ID='be2us-64aaa-aaaaa-qaabq-cai'
CANISTER_CANDID_PATH='/Users/rbole/Sites/starterKits/ic-solidjs/.dfx/local/canisters/frontend/assetstorage.did'
# END DFX CANISTER ENVIRONMENT VARIABLES
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
*.dfx
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<p align="left" >
<img width="240" src="public/icAcademy.png">
</p>

# Internet Computer + SolidJS + Vite

This template is intended to make it easier for you to get started with development on the Internet Computer with SolidJS and Vite. SolidJS is a declarative JavaScript framework used for building UIs and web applications pragmatically and performatively.

It is designed to help developers create fast, efficient, and reusable web applications. SolidJS provides a comprehensive set of components that can be used to build complex web applications quickly and easily.

To clone and use the Github repository for your own purpose following the commands below.

**Note** to get a clean git repository. A tool called “digit” is used. Make sure you have it installed. If you haven't installed it yet, you can do so using the following command.

```bash
npm install -g degit
```

Summary of installation steps:
1. Check needed tools
2. Install and discuss the github repository
3. Start a local Internet Computer replica
4. Deploy frontend and backend canister to local replica
5. Test the Motoko backend sayHelloTo function


```bash
mkdir myapp && cd myapp
```
```bash
npx degit https://github.com/samlinux-development/ic-solidjs
```
```bash
npm install
```
```bash
dfx start --clean --background
```
```bash
dfx deploy
```
```bash
echo http://$(dfx canister id frontend).localhost:4943
```
Open your browser and use the following URL scheme:

- http://[canisterId].localhost:4943
- e.g. http://be2us-64aaa-aaaaa-qaabq-cai.localhost:4943

Check the sayHelloTo function with the CLI cammand below:
```bash
dfx canister call backend sayHelloTo '("Roland")'
```

## Deploying to the playground
To test your dApp under the Internet Computer's environment, you can deploy it to the Motoko Playground.

```bash
dfx deploy --playground
```
10 changes: 10 additions & 0 deletions backend/main.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Float "mo:base/Float";
actor {
public query func sayHelloTo(name : Text) : async Text {
return "Hello " # name # " 👋 ";
};

public query func bmi(height : Float, weight : Float) : async Float {
weight / (height/100 * height/100);
};
};
26 changes: 26 additions & 0 deletions dfx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"canisters": {
"backend": {
"main": "backend/main.mo",
"type": "motoko"
},

"frontend": {
"dependencies": [
"backend"
],
"source": [
"dist"
],
"type": "assets"
}
},
"defaults": {
"build": {
"args": "",
"packtool": ""
}
},
"output_env_file": ".env",
"version": 1
}
13 changes: 13 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0 maximum-scale=1" />
<title>Vite + Solid + TS</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/index.tsx"></script>
</body>
</html>
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "ic-solidjs",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --host",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"@dfinity/agent": "^1.0.1",
"@dfinity/candid": "^1.0.1",
"@dfinity/principal": "^1.0.1",
"@solidjs/router": "^0.12.4",
"solid-js": "^1.8.15"
},
"devDependencies": {
"dotenv": "^16.4.5",
"typescript": "^5.2.2",
"vite": "^5.1.4",
"vite-plugin-environment": "^1.1.3",
"vite-plugin-solid": "^2.10.1"
}
}
Binary file added public/icAcademy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.solid:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}
1 change: 1 addition & 0 deletions src/assets/solid.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { JSX } from "solid-js";
import { A } from "@solidjs/router";
function Navbar():JSX.Element {
return (
<>
<nav>
<A href="/">Home</A>
</nav>
</>
);
}

export default Navbar;
4 changes: 4 additions & 0 deletions src/declarations/backend/backend.did
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
service : {
bmi: (float64, float64) -> (float64) query;
sayHelloTo: (text) -> (text) query;
}
10 changes: 10 additions & 0 deletions src/declarations/backend/backend.did.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Principal } from '@dfinity/principal';
import type { ActorMethod } from '@dfinity/agent';
import type { IDL } from '@dfinity/candid';

export interface _SERVICE {
'bmi' : ActorMethod<[number, number], number>,
'sayHelloTo' : ActorMethod<[string], string>,
}
export declare const idlFactory: IDL.InterfaceFactory;
export declare const init: ({ IDL }: { IDL: IDL }) => IDL.Type[];
7 changes: 7 additions & 0 deletions src/declarations/backend/backend.did.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const idlFactory = ({ IDL }) => {
return IDL.Service({
'bmi' : IDL.Func([IDL.Float64, IDL.Float64], [IDL.Float64], ['query']),
'sayHelloTo' : IDL.Func([IDL.Text], [IDL.Text], ['query']),
});
};
export const init = ({ IDL }) => { return []; };
50 changes: 50 additions & 0 deletions src/declarations/backend/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type {
ActorSubclass,
HttpAgentOptions,
ActorConfig,
Agent,
} from "@dfinity/agent";
import type { Principal } from "@dfinity/principal";
import type { IDL } from "@dfinity/candid";

import { _SERVICE } from './backend.did';

export declare const idlFactory: IDL.InterfaceFactory;
export declare const canisterId: string;

export declare interface CreateActorOptions {
/**
* @see {@link Agent}
*/
agent?: Agent;
/**
* @see {@link HttpAgentOptions}
*/
agentOptions?: HttpAgentOptions;
/**
* @see {@link ActorConfig}
*/
actorOptions?: ActorConfig;
}

/**
* Intializes an {@link ActorSubclass}, configured with the provided SERVICE interface of a canister.
* @constructs {@link ActorSubClass}
* @param {string | Principal} canisterId - ID of the canister the {@link Actor} will talk to
* @param {CreateActorOptions} options - see {@link CreateActorOptions}
* @param {CreateActorOptions["agent"]} options.agent - a pre-configured agent you'd like to use. Supercedes agentOptions
* @param {CreateActorOptions["agentOptions"]} options.agentOptions - options to set up a new agent
* @see {@link HttpAgentOptions}
* @param {CreateActorOptions["actorOptions"]} options.actorOptions - options for the Actor
* @see {@link ActorConfig}
*/
export declare const createActor: (
canisterId: string | Principal,
options?: CreateActorOptions
) => ActorSubclass<_SERVICE>;

/**
* Intialized Actor using default settings, ready to talk to a canister using its candid interface
* @constructs {@link ActorSubClass}
*/
export declare const backend: ActorSubclass<_SERVICE>;
43 changes: 43 additions & 0 deletions src/declarations/backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Actor, HttpAgent } from "@dfinity/agent";

// Imports and re-exports candid interface
import { idlFactory } from "./backend.did.js";
export { idlFactory } from "./backend.did.js";

/* CANISTER_ID is replaced by webpack based on node environment
* Note: canister environment variable will be standardized as
* process.env.CANISTER_ID_<CANISTER_NAME_UPPERCASE>
* beginning in dfx 0.15.0
*/
export const canisterId =
process.env.CANISTER_ID_BACKEND ||
process.env.BACKEND_CANISTER_ID;

export const createActor = (canisterId, options = {}) => {
const agent = options.agent || new HttpAgent({ ...options.agentOptions });

if (options.agent && options.agentOptions) {
console.warn(
"Detected both agent and agentOptions passed to createActor. Ignoring agentOptions and proceeding with the provided agent."
);
}

// Fetch root key for certificate validation during development
if (process.env.DFX_NETWORK !== "ic") {
agent.fetchRootKey().catch((err) => {
console.warn(
"Unable to fetch root key. Check to ensure that your local replica is running"
);
console.error(err);
});
}

// Creates an actor with using the candid interface and the HttpAgent
return Actor.createActor(idlFactory, {
agent,
canisterId,
...options.actorOptions,
});
};

export const backend = canisterId ? createActor(canisterId) : undefined;
Loading

0 comments on commit e51a252

Please sign in to comment.