diff --git a/src/near/config.ts b/src/near/config.ts index 78b0f43..20eaafd 100644 --- a/src/near/config.ts +++ b/src/near/config.ts @@ -1,5 +1,5 @@ export const CONTRACT_NAME = - process.env.CONTRACT_NAME || 'dev-1642588138566-86232214910457' + process.env.CONTRACT_NAME || 'dev-1644330473949-94120853225128' export enum NETWORK_TYPE { PRODUCTION = 'production', diff --git a/src/near/initContract.ts b/src/near/initContract.ts index 009ef38..63210f6 100644 --- a/src/near/initContract.ts +++ b/src/near/initContract.ts @@ -1,52 +1,6 @@ import * as nearAPI from 'near-api-js' import { getConfig } from './config' - -interface ContractType extends nearAPI.Contract { - deposit: ( - { amount: string, live: boolean }: Record, - gas?: string, - deposit?: string - ) => void - borrow: ( - { amount: string, short: boolean }: Record, - gas?: string, - deposit?: string - ) => void - new: ( - { owner_id: string }: Record, - gas?: string, - deposit?: string - ) => void - /* - * This function exists to allow withdraw of deposits, either from - * a user's SolvencyPool deposit, or LivePool (borrowing) position - * Hence, the first boolean parameter (for indicating which pool), - * last boolean parameter indicates the currency being withdrawn. - */ - renege: ( - // @ts-ignore Todo: find a solution for this weird duplicate alert - { amount: string, sp: boolean, qd: boolean }: Record, - gas?: string, - deposit?: string - ) => void - /* - * Close out caller's borrowing position by paying - * off all pledge's own debt with own collateral - */ - fold: ( - { short: boolean }: Record, - gas?: string, - deposit?: string - ) => void - /* - * Call this function to attempt liquidating a single Pledge - */ - clip: ( - { account: string }: Record, - gas?: string, - deposit?: string - ) => void -} +import { ContractType } from 'src/near/types' export interface NearContextType { contract: ContractType | null @@ -96,13 +50,9 @@ export const initContract = async (): Promise => { nearConfig.contractName, { // View methods are read-only – they don't modify the state, but usually return some value - viewMethods: ['get_pledge'], + viewMethods: ['get_pledge', 'get_pool_stats'], // Change methods can modify the state, but you don't receive the returned value when called - changeMethods: ['renege', 'borrow', 'deposit', 'fold', 'clip', 'new'], - // Sender is the account ID to initialize transactions. - // getAccountId() will return empty string if user is still unauthorized - // Todo: check if sender is needed - // sender: walletConnection.getAccountId(), + changeMethods: ['renege', 'borrow', 'deposit', 'fold', 'new'], } ) // @ts-ignore - Todo: verify whats the problem with Contract ts type diff --git a/src/near/nearContext.tsx b/src/near/nearContext.tsx index 1ec80ac..20e7176 100644 --- a/src/near/nearContext.tsx +++ b/src/near/nearContext.tsx @@ -1,13 +1,8 @@ -// connect to NEAR import React, { createContext, useState, useEffect } from 'react' import { initContract } from './initContract' -// import { CONTRACT_NAME } from 'src/near/config' import { NearContextType } from './initContract' -// import { connect, keyStores, WalletConnection, Contract } from 'near-api-js' -// import { getConfig } from './config' - export const NearContext = createContext({ contract: null, currentUser: null, @@ -31,24 +26,6 @@ export const NearProvider = ({ const initNear = async () => { const { contract, currentUser, walletConnection, nearConfig } = await initContract() - // const nearConfig = getConfig(process.env.NODE_ENV || 'testnet') - // - // // connect to NEAR - // const updatedConfig = { - // ...nearConfig, - // keyStore: new keyStores.BrowserLocalStorageKeyStore(), - // headers: {}, - // } - // const near = await connect(updatedConfig) - // - // const wallet: WalletConnection = await new WalletConnection(near, `quid`) - // - // const account = await wallet.account() - // - // const contract = await new Contract(account, updatedConfig.contractName, { - // viewMethods: ['get_greeting'], - // changeMethods: ['set_greeting'], - // }) setNearState({ contract, currentUser, walletConnection, nearConfig }) } diff --git a/src/near/types.ts b/src/near/types.ts new file mode 100644 index 0000000..46916ba --- /dev/null +++ b/src/near/types.ts @@ -0,0 +1,88 @@ +import * as nearAPI from 'near-api-js' + +export interface PledgeType { + debit: string + s_debit: string + credit: string + s_credit: string + quid_sp: string + near_sp: string +} + +export interface PoolStatsType { + blood_credit: string + blood_debit: string + froze_long_credit: string + froze_long_debit: string + froze_short_credit: string + froze_short_debit: string + live_short_credit: string + live_short_debit: string + live_long_credit: string + live_long_debit: string + dead_short_credit: string + dead_short_debit: string + dead_long_credit: string + dead_long_debit: string +} + +export interface ContractType extends nearAPI.Contract { + /* + * Change Methods + */ + deposit: ( + // Live to false means depositing on Solvency Pool and true means you can borrow against the collateral + { qd_amt: string, live: boolean }: Record, + gas?: string, + deposit?: string + ) => void + borrow: ( + { amount: string, short: boolean }: Record, + gas?: string, + deposit?: string + ) => void + /* + * This function exists to allow withdraw of deposits, either from + * a user's SolvencyPool deposit, or LivePool (borrowing) position + * Hence, the first boolean parameter (for indicating which pool), + * last boolean parameter indicates the currency being withdrawn. + */ + renege: ( + // @ts-ignore Todo: find a solution for this weird duplicate alert + { amount: string, sp: boolean, qd: boolean }: Record, + gas?: string, + deposit?: string + ) => void + /* + * Close out caller's borrowing position by paying + * off all pledge's own debt with own collateral + */ + fold: ( + { short: boolean }: Record, + gas?: string, + deposit?: string + ) => void + /* + * Call this function to attempt liquidating a single Pledge + */ + // clip: ( + // { account: string }: Record, + // gas?: string, + // deposit?: string + // ) => void + /* + * View Methods + */ + // Get Pledge for one Account + get_pledge: ( + { account: string }: Record, + gas?: string, + deposit?: string + ) => Promise + // Get Pool Stats + get_pool_stats: ( + {}: Record, + gas?: string, + deposit?: string + ) => Promise +} diff --git a/src/pages/stake.tsx b/src/pages/stake.tsx index 5788de6..2609653 100644 --- a/src/pages/stake.tsx +++ b/src/pages/stake.tsx @@ -1,13 +1,33 @@ import * as React from 'react' import Head from 'next/head' -import { Box, Text, Tabs, Tab, TextInput, Button } from 'grommet' import { utils } from 'near-api-js' +import { Box, Text, Tabs, Tab, TextInput, Button } from 'grommet' import { NearContext } from 'src/near/nearContext' +import { PledgeType, PoolStatsType } from 'src/near/types' const Stake: React.FC = () => { + const { contract, currentUser } = React.useContext(NearContext) const [depositAmnt, setDepositAmnt] = React.useState(null) - const { contract } = React.useContext(NearContext) + const [withdrawAmnt, setWithdrawAmnt] = React.useState(null) + const [pledged, setPledged] = React.useState() + const [stats, setStats] = React.useState() + + const getNewPledgedAmnt = async () => { + const newPledges = await contract?.get_pledge({ + account: currentUser?.accountId, + }) + setPledged(newPledges) + } + const getStats = async () => { + const newStats = await contract?.get_pool_stats({}) + setStats(newStats) + } + + React.useEffect(() => { + getNewPledgedAmnt() + getStats() + }, [contract]) return ( <> @@ -30,15 +50,16 @@ const Stake: React.FC = () => { size="xxlarge" margin={{ bottom: 'medium', top: 'large' }} > - Lend your Nears or qUids + Stake on qUid Solvency Pool + {/* Deposit and Withdraw Box*/} { if (depositAmnt) { contract?.deposit( { - amount: utils.format.parseNearAmount(depositAmnt), + qd_amt: '0', live: false, }, '300000000000000', utils.format.parseNearAmount(depositAmnt) || undefined ) - // contract?.borrow( - // { - // amount: utils.format.parseNearAmount(depositAmnt), - // short: false, - // }, - // '300000000000000', - // utils.format.parseNearAmount(depositAmnt) || - // utils.format.parseNearAmount('1') || - // undefined - // ) } }} /> @@ -119,10 +130,97 @@ const Stake: React.FC = () => { title="Withdraw" color="linear-gradient(90deg, rgb(50.588% 99.608% 91.373%) 0%, rgb(53.456% 96.078% 91.912%) 6.25%, rgb(56.324% 92.549% 92.451%) 12.5%, rgb(59.191% 89.02% 92.99%) 18.75%, rgb(62.059% 85.49% 93.529%) 25%, rgb(64.926% 81.961% 94.069%) 31.25%, rgb(67.794% 78.431% 94.608%) 37.5%, rgb(70.662% 74.902% 95.147%) 43.75%, rgb(73.529% 71.373% 95.686%) 50%, rgb(76.397% 67.843% 96.225%) 56.25%, rgb(79.265% 64.314% 96.765%) 62.5%, rgb(82.132% 60.784% 97.304%) 68.75%, rgb(85% 57.255% 97.843%) 75%, rgb(87.868% 53.725% 98.382%) 81.25%, rgb(90.735% 50.196% 98.922%) 87.5%, rgb(93.603% 46.667% 99.461%) 93.75%, rgb(96.471% 43.137% 100%) 100% )" > - Coming soon... +
+ + + { + setWithdrawAmnt(e?.target?.value) + }} + /> + + Near + + +
+