From fb6b07f5a92a672b7d58e211332d0598aeb3190c Mon Sep 17 00:00:00 2001 From: Chad Ostrowski <221614+chadoh@users.noreply.github.com> Date: Fri, 28 Jan 2022 21:59:36 -0500 Subject: [PATCH] feat: add fully-typed contract wrapper Demonstrate a way to wrap a contract's interface with a fully-typed TypeScript interface by avoiding `naj.Contract` and using `viewFunction` and `functionCall` instead. --- template.json | 1 + template/src/components/App/App.tsx | 7 +++ template/src/contracts/guest-book.ts | 67 ++++++++++++++++++++++++++++ template/src/utils/near.ts | 42 +++++++++-------- 4 files changed, 99 insertions(+), 18 deletions(-) create mode 100644 template/src/contracts/guest-book.ts diff --git a/template.json b/template.json index 0a3b8b5..a4c7e5f 100644 --- a/template.json +++ b/template.json @@ -13,6 +13,7 @@ "dependencies": { "buffer": "^6.0.3", "near-api-js": "0.44.2", + "near-units": "^0.1.9", "web-vitals": "^2.1.0" }, "eslintConfig": { diff --git a/template/src/components/App/App.tsx b/template/src/components/App/App.tsx index 7cf15a2..70ea7b0 100644 --- a/template/src/components/App/App.tsx +++ b/template/src/components/App/App.tsx @@ -1,7 +1,14 @@ import React from "react"; import { Nav } from "../Nav"; +import * as GuestBook from "../../contracts/guest-book"; export function App() { + React.useEffect(() => { + // this is showing an overly-simple way to use the GuestBook import by just + // logging the result of the call to `getMessages` + GuestBook.getMessages().then(console.log); + }, []); + return ( <>