Skip to content

Commit

Permalink
fix: remove src folder
Browse files Browse the repository at this point in the history
  • Loading branch information
donoso-eth committed Jun 23, 2023
1 parent ed71e47 commit d76c7cc
Show file tree
Hide file tree
Showing 45 changed files with 66 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ coverage
typechain

test/contracts/types
src/logs/
logs/

.eslintcache

Expand Down
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ PRIVATE_KEY="" # optional: only needed if you wish to create a task from the CLI

## Write a Web3 Function

- Go to `src/web3-functions/my-web3-function`
- Go to `web3-functions/my-web3-function`
- Write your Web3 Function logic within the `Web3Function.onRun` function.
- Example:
```typescript
Expand Down Expand Up @@ -127,11 +127,11 @@ Web3Function.onRun(async (context: Web3FunctionContext) => {
- `--debug` Show Runtime debug messages
- `--chain-id=[number]` Specify the chainId to be used for your Web3 Function (default: `5` for Goerli)
- Example:<br/> `npx w3f test src/web3-functions/oracle/index.ts --show-logs`
- Example:<br/> `npx w3f test web3-functions/oracle/index.ts --show-logs`
- Output:
```
Web3Function Build result:
Schema: src/web3-functions/oracle/schema.json
Schema: web3-functions/oracle/schema.json
Built file: /Users/chuahsonglin/Documents/GitHub/Gelato/backend/js-resolver-template/.tmp/index.js
File size: 1.63mb
Build time: 91.34ms
Expand Down Expand Up @@ -162,13 +162,13 @@ Web3Function Runtime stats:
### Writing unit test for your web3 function
- Define your tests in `src/test/hellow-world.test.ts`
- Define your tests in `test/hellow-world.test.ts`
- Use `yarn test` command to run unit test suite.
You can fork a network in your unit test.
RPC methods of provider can be found in [Foundry's Anvil docs](https://book.getfoundry.sh/reference/anvil/)
Example: [`src/test/advertising-board.test.ts`](./src/test/advertising-board.test.ts)
Example: [`test/advertising-board.test.ts`](./test/advertising-board.test.ts)
```ts
import { AnvilServer } from "./utils/anvil-server";
Expand Down Expand Up @@ -221,7 +221,7 @@ Web3Function.onRun(async (context: Web3FunctionContext) => {
```
```
npx w3f test src/web3-functions/oracle/index.ts --logs
npx w3f test web3-functions/oracle/index.ts --logs
```
## Use State / Storage
Expand Down Expand Up @@ -262,7 +262,7 @@ Web3Function.onRun(async (context: Web3FunctionContext) => {
```
Test storage execution:<br/>
`npx w3f test src/web3-functions/storage/index.ts --logs`
`npx w3f test web3-functions/storage/index.ts --logs`
You will see your updated key/values:
```
Expand All @@ -288,14 +288,14 @@ if (!coingeckoApi)
```
3. Test your Web3 Function using secrets:<br/>
`npx w3f test src/web3-functions/secrets/index.ts --logs`
`npx w3f test web3-functions/secrets/index.ts --logs`
## Deploy your Web3Function on IPFS
Use `npx w3f deploy FILEPATH` command to deploy your web3 function.
Example:<br/>
`npx w3f deploy src/web3-functions/oracle/index.ts`
`npx w3f deploy web3-functions/oracle/index.ts`
The deployer will output your Web3Function IPFS CID, that you can use to create your task:
```
Expand Down Expand Up @@ -351,10 +351,10 @@ Task created, taskId: 0x8438933eb9c6e4632d984b4db1e7672082d367b900e536f86295b2e2
Fetch price data from Coingecko API to update your on-chain Oracle
Source: [`src/web3-functions/oracle/index.ts`](./src/web3-functions/oracle/index.ts)
Source: [`web3-functions/oracle/index.ts`](./web3-functions/oracle/index.ts)
Run:<br/>
`npx w3f test src/web3-functions/oracle/index.ts --logs`
`npx w3f test web3-functions/oracle/index.ts --logs`
Create task: <br/>
`yarn create-task:oracle`
Expand All @@ -364,10 +364,10 @@ Create task: <br/>
Listen to smart contract events and use storage context to maintain your execution state.
Source: [`src/web3-functions/event-listener/index.ts`](./src/web3-functions/event-listener/index.ts)
Source: [`web3-functions/event-listener/index.ts`](./web3-functions/event-listener/index.ts)
Run:<br/>
`npx w3f test src/web3-functions/event-listener/index.ts --logs`
`npx w3f test web3-functions/event-listener/index.ts --logs`
Create task: <br/>
`yarn create-task:event`
Expand All @@ -376,10 +376,10 @@ Create task: <br/>
Fetch data from a private API to update your on-chain Oracle
Source: [`src/web3-functions/secrets/index.ts`](./src/web3-functions/secrets/index.ts)
Source: [`web3-functions/secrets/index.ts`](./web3-functions/secrets/index.ts)
Run:<br/>
`npx w3f test src/web3-functions/secrets/index.ts --logs`
`npx w3f test web3-functions/secrets/index.ts --logs`
Create task: <br/>
`yarn create-task:secrets`
Expand All @@ -388,10 +388,10 @@ Create task: <br/>
Fetch a random quote from an API and post it on chain.
Source: [`src/web3-functions/advertising-board/index.ts`](./src/web3-functions/advertising-board/index.ts)
Source: [`web3-functions/advertising-board/index.ts`](./web3-functions/advertising-board/index.ts)
Run:<br/>
`npx w3f test src/web3-functions/advertising-board/index.ts`
`npx w3f test web3-functions/advertising-board/index.ts`
Create task: <br/>
`yarn create-task:ad-board`
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Config } from "@jest/types";

const config: Config.InitialOptions = {
roots: ["<rootDir>/src/"],
roots: ["<rootDir>"],
preset: "ts-jest",
testEnvironment: "node",
};
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
"format:check": "prettier --check '*/**/*.{js,json,md,ts}'",
"lint": "eslint --cache .",
"test": "jest",
"create-task:event": "ts-node src/scripts/create-task-event-listener.ts",
"create-task:oracle": "ts-node src/scripts/create-task-oracle.ts",
"create-task:secrets": "ts-node src/scripts/create-task-with-secrets.ts",
"create-task:ad-board": "ts-node src/scripts/create-task-ad-board.ts"
"create-task:event": "ts-node scripts/create-task-event-listener.ts",
"create-task:oracle": "ts-node scripts/create-task-oracle.ts",
"create-task:secrets": "ts-node scripts/create-task-with-secrets.ts",
"create-task:ad-board": "ts-node scripts/create-task-ad-board.ts"
},
"keywords": [],
"author": "",
Expand All @@ -37,7 +37,7 @@
},
"dependencies": {
"@gelatonetwork/automate-sdk": "^2.7.0-beta",
"@gelatonetwork/web3-functions-sdk": "^2.0.3",
"@gelatonetwork/web3-functions-sdk": "^2.0.7",
"dotenv": "^16.0.3",
"ethers": "^5.7.1",
"ky": "^0.32.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const main = async () => {
console.log("Deploying Web3Function on IPFS...");

const web3FunctionPath = path.join(
"src",
"web3-functions",
"advertising-board",
"index.ts"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const main = async () => {
// Deploy Web3Function on IPFS
console.log("Deploying Web3Function on IPFS...");
const web3FunctionPath = path.join(
"src",
"web3-functions",
"event-listener",
"index.ts"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const main = async () => {
// Deploy Web3Function on IPFS
console.log("Deploying Web3Function on IPFS...");
const web3FunctionPath = path.join(
"src",
"web3-functions",
"oracle",
"index.ts"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const pk = process.env.PRIVATE_KEY;
if (!process.env.PROVIDER_URLS) throw new Error("Missing env PROVIDER_URLS");
const providerUrl = process.env.PROVIDER_URLS.split(",")[0];

const w3fRootDir = path.join("src", "web3-functions");
const w3fRootDir = path.join( "web3-functions");
const w3fName = "secrets";

const main = async () => {
Expand All @@ -25,7 +25,6 @@ const main = async () => {
// Deploy Web3Function on IPFS
console.log("Deploying Web3Function on IPFS...");
const web3FunctionPath = path.join(
"src",
"web3-functions",
"secrets",
"index.ts"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { runWeb3Function } from "./utils";
import { AnvilServer } from "./utils/anvil-server";

const w3fName = "advertising-board";
const w3fRootDir = path.join("src", "web3-functions");
const w3fRootDir = path.join("web3-functions");
const w3fPath = path.join(w3fRootDir, w3fName, "index.ts");

describe("Advertising Board Web3 Function test", () => {
Expand All @@ -21,6 +21,8 @@ describe("Advertising Board Web3 Function test", () => {
const { secrets } = Web3FunctionLoader.load(w3fName, w3fRootDir);
const gasPrice = (await goerliFork.provider.getGasPrice()).toString();



context = {
secrets,
storage: {},
Expand All @@ -42,8 +44,10 @@ describe("Advertising Board Web3 Function test", () => {
// mock storage state of "lastPost"
context.storage = { lastPost: blockTime.toString() };


const res = await runWeb3Function(w3fPath, context, [goerliFork.provider]);


expect(res.result.canExec).toEqual(false);
});

Expand Down
2 changes: 1 addition & 1 deletion src/test/hello-world.test.ts → test/hello-world.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { runWeb3Function } from "./utils";
import { ethers } from "ethers";

const w3fName = "hello-world";
const w3fRootDir = path.join("src", "web3-functions");
const w3fRootDir = path.join("web3-functions");
const w3fPath = path.join(w3fRootDir, w3fName, "index.ts");

describe("My Web3 Function test", () => {
Expand Down
File renamed without changes.
14 changes: 12 additions & 2 deletions src/test/utils/index.ts → test/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import {
Web3FunctionContextData,
MultiChainProviderConfig,
Web3FunctionRunnerOptions,
} from "@gelatonetwork/web3-functions-sdk";
import { Web3FunctionRunner } from "@gelatonetwork/web3-functions-sdk/runtime";
import { Web3FunctionBuilder } from "@gelatonetwork/web3-functions-sdk/builder";
import { ethers } from "ethers";


export const MAX_RPC_LIMIT = 100;
export const MAX_DOWNLOAD_LIMIT = 10 * 1024 * 1024;
export const MAX_UPLOAD_LIMIT = 5 * 1024 * 1024;
export const MAX_REQUEST_LIMIT = 100;

export const runWeb3Function = async (
web3FunctionPath: string,
context: Web3FunctionContextData,
Expand All @@ -21,14 +28,17 @@ export const runWeb3Function = async (
const runner = new Web3FunctionRunner(false);
const runtime: "docker" | "thread" = "thread";
const memory = buildRes.schema.memory;
const rpcLimit = 100;
const rpcLimit = MAX_RPC_LIMIT
const timeout = buildRes.schema.timeout * 1000;
const version = buildRes.schema.web3FunctionVersion;

const options = {
const options:Web3FunctionRunnerOptions = {
runtime,
showLogs: true,
memory,
downloadLimit:MAX_DOWNLOAD_LIMIT,
uploadLimit:MAX_UPLOAD_LIMIT,
requestLimit:MAX_REQUEST_LIMIT,
rpcLimit,
timeout,
};
Expand Down
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
"resolveJsonModule": true,
"typeRoots": ["./node_modules/@types"],
"outDir": "dist",
"rootDir": "src",
"declaration": true,
"useUnknownInCatchVariables": false
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "jest.config.ts"],
"include": ["scritps", "web3-functions", "jest.config.ts"],
"exclude": ["node_modules"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
import { utils } from "ethers";
import ky from "ky"; // we recommend using ky as axios doesn't support fetch by default


const AD_BOARD_ABI = [
"function postMessage(string)",
"function viewMessage(address)",
Expand All @@ -24,6 +25,8 @@ Web3Function.onRun(async (context: Web3FunctionContext) => {
const nextPostTime = lastPost + 3600; // 1h
const timestamp = (await provider.getBlock("latest")).timestamp;



if (timestamp < nextPostTime) {
return { canExec: false, message: `Time not elapsed` };
}
Expand All @@ -42,6 +45,8 @@ Web3Function.onRun(async (context: Web3FunctionContext) => {
return { canExec: false, message: `QuoteApi call failed` };
}

console.log(47)

await storage.set("lastPost", timestamp.toString());

return {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ return (async () => {

Dependencies that are used in `onRun.js` should be imported in the web3 function `index.ts` file, not in `onRun.js`.

In `/src/web3-functions/private/index.ts`:
In `/web3-functions/private/index.ts`:

```ts
// import dependencies used in onRun.js
Expand All @@ -30,7 +30,7 @@ import ky from "ky";

The dependencies `ky` and `ethers` are used in `onRun.js`. They will be passed into `onRun.js`

In `/src/web3-functions/private/index.ts`:
In `/web3-functions/private/index.ts`:

```ts
const onRunFunction = new Function("context", "ky", "ethers", onRunScript);
Expand All @@ -42,7 +42,7 @@ In `onRun.js`, you can use the dependencies as if they are already imported.

Web3 function context which includes, `secrets`, `userArgs`, `multiChainProvider` can be accessed normally in `onRun.js` as `context` is passed as arguments.

In `/src/web3-functions/private/index.ts`:
In `/web3-functions/private/index.ts`:

```ts
const onRunFunction = new Function("context", "ky", "ethers", onRunScript);
Expand All @@ -52,7 +52,7 @@ const onRunFunction = new Function("context", "ky", "ethers", onRunScript);

Results returned in `onRun.js` will be bubbled.

In `/src/web3-functions/private/onRun.js`:
In `/web3-functions/private/onRun.js`:

```ts
return {
Expand All @@ -78,15 +78,15 @@ return {

Example:

In `/src/web3-functions/private/schema.json`
In `/web3-functions/private/schema.json`

```json
"userArgs": {
"args": "string"
}
```

In `/src/web3-functions/private/userArgs.json`
In `/web3-functions/private/userArgs.json`

```json
{
Expand All @@ -102,4 +102,4 @@ Create a `.env` file with secrets:
GIST_ID=0c58ee8ce55bc7af5f42a2d75c27433c
```

Run `$ npx w3f test ./src/web3-functions/private/index.ts --logs --debug`
Run `$ npx w3f test ./web3-functions/private/index.ts --logs --debug`
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit d76c7cc

Please sign in to comment.