From db5013c1053f4fc89d2142248c3a9524c9549b04 Mon Sep 17 00:00:00 2001 From: Micha Vie Date: Thu, 30 Nov 2023 00:09:33 +0100 Subject: [PATCH] update artcpa app --- src/extensions/artcpaclub/src/config.ts | 13 +++++-- src/extensions/artcpaclub/src/contracts.tsx | 6 +++ .../artcpaclub/src/esdt/EsdtTab.tsx | 15 +++++-- .../artcpaclub/src/esdt/_Staker.tsx | 8 ++-- .../artcpaclub/src/esdt/_Unstaker.tsx | 34 ++++++++++++++-- .../src/previews/StakeEsdtActionPreview.tsx | 39 +++++++++++++++++++ .../src/previews/UnstakeEsdtActionPreview.tsx | 35 +++++++++++++++++ 7 files changed, 135 insertions(+), 15 deletions(-) create mode 100644 src/extensions/artcpaclub/src/previews/StakeEsdtActionPreview.tsx create mode 100644 src/extensions/artcpaclub/src/previews/UnstakeEsdtActionPreview.tsx diff --git a/src/extensions/artcpaclub/src/config.ts b/src/extensions/artcpaclub/src/config.ts index 11e3e9d..cccdb80 100644 --- a/src/extensions/artcpaclub/src/config.ts +++ b/src/extensions/artcpaclub/src/config.ts @@ -1,6 +1,13 @@ +import { Network } from '../../../shared/types' + export const Config = { - Urls: { - ApiBase: 'https://api.artcpaclub.com/api', - Marketplace: 'https://marketplace.artcpaclub.com', + ApiBaseUrl: (network: Network) => { + if (network === 'devnet') return 'https://devnet-api.artcpaclub.com/api' + return 'https://api.artcpaclub.com/api' + }, + + MarketplaceUrl: (network: Network) => { + if (network === 'devnet') return 'https://devnet-marketplace.artcpaclub.com' + return 'https://marketplace.artcpaclub.com' }, } diff --git a/src/extensions/artcpaclub/src/contracts.tsx b/src/extensions/artcpaclub/src/contracts.tsx index 378416e..9e3769b 100644 --- a/src/extensions/artcpaclub/src/contracts.tsx +++ b/src/extensions/artcpaclub/src/contracts.tsx @@ -1,3 +1,7 @@ +import React from 'react' +import { ProposalAction } from '@peerme/core-ts' +import { StakeEsdtActionPreview } from './previews/StakeEsdtActionPreview' +import { UnstakeEsdtActionPreview } from './previews/UnstakeEsdtActionPreview' import { Network, ExtensionScInfo, ExtensionConfig } from '../../../shared/types' const getContractAddress = (network: Network) => { @@ -10,9 +14,11 @@ export const Contracts = (config: ExtensionConfig): ExtensionScInfo => ({ UserStake: { Address: getContractAddress(config.network), Endpoint: 'userStake', + ActionPreview: (action: ProposalAction) => , }, UserUnstake: { Address: getContractAddress(config.network), Endpoint: 'userUnstake', + ActionPreview: (action: ProposalAction) => , }, }) diff --git a/src/extensions/artcpaclub/src/esdt/EsdtTab.tsx b/src/extensions/artcpaclub/src/esdt/EsdtTab.tsx index 9d58b7c..e2511de 100644 --- a/src/extensions/artcpaclub/src/esdt/EsdtTab.tsx +++ b/src/extensions/artcpaclub/src/esdt/EsdtTab.tsx @@ -26,7 +26,7 @@ export function EsdtTab() { useEffect(() => { if (!poolId) return - fetch(Config.Urls.ApiBase + '/tokenstaking/' + poolId).then(async (res) => { + fetch(Config.ApiBaseUrl(app.config.network) + '/tokenstaking/' + poolId).then(async (res) => { const data = await res.json() setSelectedPool(data) }) @@ -57,14 +57,21 @@ export function EsdtTab() { ) : ( <>
-

{selectedPool.title}

- Token: {selectedPool.stake_token_id} +

{selectedPool.title}

+
    +
  • + Stake Token: {selectedPool.stake_token_id} +
  • +
  • + Reward Token: {selectedPool.reward_token_id} +
  • +
<_Staker pool={selectedPool} className="mb-4" /> diff --git a/src/extensions/artcpaclub/src/esdt/_Staker.tsx b/src/extensions/artcpaclub/src/esdt/_Staker.tsx index 7836903..0fb0c3e 100644 --- a/src/extensions/artcpaclub/src/esdt/_Staker.tsx +++ b/src/extensions/artcpaclub/src/esdt/_Staker.tsx @@ -14,7 +14,7 @@ type Props = { export function _Staker(props: Props) { const app = useApp() const [payment, setPayment] = useState(null) - const isSubmitDisabled = !payment + const isSubmitDisabled = !payment || payment.amountAsBigInteger.isZero() const handleSubmit = (e: SyntheticEvent) => { e.preventDefault() @@ -37,13 +37,13 @@ export function _Staker(props: Props) { setPayment(val)} - tokenIdWhitelist={[props.pool.stake_token_id]} + tokenIdWhitelist={['EGLD', props.pool.stake_token_id]} className="mb-8" /> diff --git a/src/extensions/artcpaclub/src/esdt/_Unstaker.tsx b/src/extensions/artcpaclub/src/esdt/_Unstaker.tsx index c4ded84..2734871 100644 --- a/src/extensions/artcpaclub/src/esdt/_Unstaker.tsx +++ b/src/extensions/artcpaclub/src/esdt/_Unstaker.tsx @@ -1,7 +1,8 @@ import { EsdtPool } from '../types' -import { Button } from '@peerme/web-ui' import { Contracts } from '../contracts' -import React, { SyntheticEvent } from 'react' +import { Button, Input } from '@peerme/web-ui' +import { sanitizeNumeric } from '@peerme/core-ts' +import React, { SyntheticEvent, useState } from 'react' import { useApp } from '../../../../shared/hooks/useApp' import { AppSection } from '../../../../shared/ui/elements' @@ -12,6 +13,7 @@ type Props = { export function _Unstaker(props: Props) { const app = useApp() + const [amount, setAmount] = useState('0') const handleSubmit = (e: SyntheticEvent) => { e.preventDefault() @@ -28,9 +30,33 @@ export function _Unstaker(props: Props) { return (
-

Work in progress.

+ +
+ setAmount(sanitizeNumeric(val))} + className="mb-2" + autoComplete="off" + /> + {/* TODO: implement once SC data available {+amount !== +scAmount && ( +
+ +
+ )} */} +
diff --git a/src/extensions/artcpaclub/src/previews/StakeEsdtActionPreview.tsx b/src/extensions/artcpaclub/src/previews/StakeEsdtActionPreview.tsx new file mode 100644 index 0000000..b9f61ad --- /dev/null +++ b/src/extensions/artcpaclub/src/previews/StakeEsdtActionPreview.tsx @@ -0,0 +1,39 @@ +import { Config } from '../config' +import { EsdtPool } from '../types' +import React, { useEffect, useState } from 'react' +import { TokenTransfer } from '@multiversx/sdk-core/out' +import { ExtensionConfig } from '../../../../shared/types' +import { ActionPreviewHighlight } from '../../../../shared/ui/elements' +import { ProposalAction, toFormattedTokenPayment, toTokenPaymentFromProposalPayment } from '@peerme/core-ts' + +type Props = { + action: ProposalAction + config: ExtensionConfig +} + +export function StakeEsdtActionPreview(props: Props) { + const poolId = props.action.arguments[0] as number + const [selectedPool, setSelectedPool] = useState(null) + + const displayablePayments = + props.action.payments.length > 0 + ? props.action.payments + .map((payment) => toFormattedTokenPayment(toTokenPaymentFromProposalPayment(payment))) + .join(', ') + : toFormattedTokenPayment(TokenTransfer.egldFromBigInteger(props.action.value)) + + useEffect(() => { + if (!poolId) return + fetch(Config.ApiBaseUrl(props.config.network) + '/tokenstaking/' + poolId).then(async (res) => { + const data = await res.json() + setSelectedPool(data) + }) + }, [poolId]) + + return ( + + stake {displayablePayments}{' '} + {selectedPool ? `in pool ${selectedPool.title}` : `in pool with id ${poolId}`}. + + ) +} diff --git a/src/extensions/artcpaclub/src/previews/UnstakeEsdtActionPreview.tsx b/src/extensions/artcpaclub/src/previews/UnstakeEsdtActionPreview.tsx new file mode 100644 index 0000000..8b310aa --- /dev/null +++ b/src/extensions/artcpaclub/src/previews/UnstakeEsdtActionPreview.tsx @@ -0,0 +1,35 @@ +import { Config } from '../config' +import { EsdtPool } from '../types' +import { ProposalAction } from '@peerme/core-ts' +import React, { useEffect, useState } from 'react' +import { ExtensionConfig } from '../../../../shared/types' +import { ActionPreviewHighlight } from '../../../../shared/ui/elements' + +type Props = { + action: ProposalAction + config: ExtensionConfig +} + +export function UnstakeEsdtActionPreview(props: Props) { + const poolId = props.action.arguments[0] as number + const optAmount = props.action.arguments[1] as number | null + const [selectedPool, setSelectedPool] = useState(null) + + useEffect(() => { + if (!poolId) return + fetch(Config.ApiBaseUrl(props.config.network) + '/tokenstaking/' + poolId).then(async (res) => { + const data = await res.json() + setSelectedPool(data) + }) + }, [poolId]) + + return ( + + unstake{' '} + + {optAmount ? optAmount : 'all'} {selectedPool ? selectedPool.stake_token_id : ''} + {' '} + {selectedPool ? `from pool ${selectedPool.title}` : `from pool with id ${poolId}`}. + + ) +}