Skip to content

Commit

Permalink
feat: react-dapp-v2 updated for Tezos
Browse files Browse the repository at this point in the history
Updated for Tezos:
 - fixed RPC calls
 - fixed modals for better user experience
 - added examples for contract calls, delegation, and more
 - added balance and token metadata
  • Loading branch information
dianasavvatina committed Sep 5, 2024
1 parent cbe3cbb commit ea3b504
Show file tree
Hide file tree
Showing 12 changed files with 3,349 additions and 2,822 deletions.
10 changes: 7 additions & 3 deletions advanced/dapps/react-dapp-v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
"scripts": {
"dev": "next dev -p 3000",
"build": "next build",
"clean": "rm -rf dist",
"start": "next start",
"lint": "next lint",
"prettier": "prettier --check '**/*.{js,ts,jsx,tsx}'",
"prettier:write": "prettier --write '**/*.{js,ts,jsx,tsx}'"
},
"dependencies": {
"@airgap/beacon-types": "^4.2.2",
"@celo/wallet-base": "^5.1.1",
"@ethereumjs/tx": "^3.5.0",
"@kadena/client": "^0.5.0",
Expand All @@ -20,6 +22,8 @@
"@multiversx/sdk-wallet": "4.2.0",
"@polkadot/util-crypto": "^10.1.2",
"@solana/web3.js": "^1.36.0",
"@taquito/rpc": "^20.0.1",
"@taquito/taquito": "^20.0.1",
"@walletconnect/encoding": "^1.0.1",
"@walletconnect/sign-client": "2.15.2",
"@walletconnect/types": "2.15.2",
Expand Down Expand Up @@ -52,14 +56,14 @@
"@types/pino": "^7.0.5",
"@types/prop-types": "^15.7.4",
"@types/qr-image": "^3.2.5",
"@types/react": "18.0.15",
"@types/react-dom": "18.0.6",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@types/styled-components": "^5.1.25",
"eslint": "8.21.0",
"eslint-config-next": "12.2.4",
"eslint-plugin-package-json": "^0.13.1",
"jsonc-eslint-parser": "^2.4.0",
"prettier": "^2.8.8",
"typescript": "^4.7.4"
"typescript": "^5.5.4"
}
}
6 changes: 4 additions & 2 deletions advanced/dapps/react-dapp-v2/src/chains/tezos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ export const TezosChainData: ChainsMap = {
mainnet: {
name: "Tezos",
id: "tezos:mainnet",
rpc: ["https://mainnet.api.tez.ie"],
rpc: ["https://rpc.tzbeta.net"],
indexer: "https://api.tzkt.io/v1",
slip44: 1729,
testnet: false,
},
testnet: {
name: "Tezos Testnet",
id: "tezos:testnet",
rpc: ["https://ghostnet.ecadinfra.com"],
rpc: ["https://rpc.ghostnet.teztnets.com"],
indexer: "https://api.ghostnet.tzkt.io/v1",
slip44: 1729,
testnet: true,
},
Expand Down
14 changes: 13 additions & 1 deletion advanced/dapps/react-dapp-v2/src/components/Asset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getChainMetadata } from "../chains";
const xdaiLogo = getChainMetadata("eip155:100").logo;
const maticLogo = getChainMetadata("eip155:137").logo;
const kadenaLogo = getChainMetadata("kadena:testnet04").logo;
const tezosLogo = getChainMetadata("tezos:testnet").logo;

const SAsset = styled.div`
width: 100%;
Expand Down Expand Up @@ -48,11 +49,22 @@ function getAssetIcon(asset: AssetData): JSX.Element {
return <Icon src={maticLogo} />;
case "kda":
return <Icon src={kadenaLogo} />;
case "xtz":
return <Icon src={tezosLogo} />;
default:
return <Icon src={"/assets/eth20.svg"} />;
}
}

function getDecimals(asset: AssetData) : number {
switch (asset?.symbol?.toLowerCase()) {
case "xtz":
return 6;
default:
return 18;
}
}

interface AssetProps {
asset: AssetData;
}
Expand All @@ -67,7 +79,7 @@ const Asset = (props: AssetProps) => {
</SAssetLeft>
<SAssetRight>
<SAssetBalance>
{fromWad(asset.balance || "0")} {asset.symbol}
{fromWad(asset.balance || "0", getDecimals(asset))} {asset.symbol}
</SAssetBalance>
</SAssetRight>
</SAsset>
Expand Down
10 changes: 10 additions & 0 deletions advanced/dapps/react-dapp-v2/src/components/Blockchain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ const Blockchain: FC<PropsWithChildren<BlockchainProps>> = (
balances,
actions,
} = props;
const [showDescription, setShowDescription] = React.useState(false);
const [hoveredDescription, setHoveredDescription] = React.useState<string | null>(null);

if (!Object.keys(chainData).length) return null;

const chain = getBlockchainDisplayData(chainId, chainData);
Expand Down Expand Up @@ -181,12 +184,19 @@ const Blockchain: FC<PropsWithChildren<BlockchainProps>> = (
left
rgb={chain.meta.rgb}
onClick={() => action.callback(chainId, address)}
onMouseEnter={() => {
setShowDescription(true);
setHoveredDescription(JSON.stringify(action.description, null, 2));
}}
>
{action.method}
</SAction>
))}
</SFullWidthContainer>
) : null}
{showDescription && hoveredDescription && (
<pre style={{ textAlign: 'left' }}>{hoveredDescription}</pre>
)}
</>
)}
</SBlockchainChildrenContainer>
Expand Down
78 changes: 78 additions & 0 deletions advanced/dapps/react-dapp-v2/src/constants/default.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { getAppMetadata } from "@walletconnect/utils";
import {
PartialTezosDelegationOperation,
PartialTezosOriginationOperation as PartialTezosOriginationOperationOriginal,
PartialTezosTransactionOperation,
TezosOperationType }
from "@airgap/beacon-types";
import { ScriptedContracts } from "@taquito/rpc";

if (!process.env.NEXT_PUBLIC_PROJECT_ID)
throw new Error("`NEXT_PUBLIC_PROJECT_ID` env variable is missing.");
Expand Down Expand Up @@ -244,12 +251,83 @@ export enum DEFAULT_TRON_EVENTS {}
/**
* TEZOS
*/
// Can be removed when the fix for Origination is released:
// https://github.com/airgap-it/beacon-sdk/pull/806
interface PartialTezosOriginationOperation
extends Omit<PartialTezosOriginationOperationOriginal, "script"> {
script: ScriptedContracts;
}

export enum DEFAULT_TEZOS_METHODS {
TEZOS_GET_ACCOUNTS = "tezos_getAccounts",
TEZOS_SEND = "tezos_send",
TEZOS_SIGN = "tezos_sign",
}

export enum TEZOS_ACTIONS {
TEZOS_GET_ACCOUNTS = "tezos_getAccounts",
TEZOS_SEND = "tezos_send",
TEZOS_SEND_TRANSACTION = "tezos_send:transaction",
TEZOS_SEND_ORGINATION = "tezos_send:origination",
TEZOS_SEND_CONTRACT_CALL = "tezos_send:contract_call",
TEZOS_SEND_DELEGATION = "tezos_send:delegation",
TEZOS_SEND_UNDELEGATION = "tezos_send:undelegation",
TEZOS_SIGN = "tezos_sign",
}

const tezosTransactionOperation: PartialTezosTransactionOperation = {
kind: TezosOperationType.TRANSACTION,
destination: "$(peerAddress)",
amount: "10"
};

const tezosOriginationOperation: PartialTezosOriginationOperation = {
kind: TezosOperationType.ORIGINATION,
balance: '1',
script: { // This contract adds the parameter to the storage value
code: [
{ prim: "parameter", args: [{ prim: "int" }] },
{ prim: "storage", args: [{ prim: "int" }] },
{ prim: "code",
args: [[
{ prim: "DUP" }, // Duplicate the parameter (parameter is pushed onto the stack)
{ prim: "CAR" }, // Access the parameter from the stack (parameter is on top)
{ prim: "DIP", args: [[{ prim: "CDR" }]] }, // Access the storage value (storage is on the stack)
{ prim: "ADD" }, // Add the parameter to the storage value
{ prim: "NIL", args: [{ prim: "operation" }] }, // Create an empty list of operations
{ prim: "PAIR" } // Pair the updated storage with the empty list of operations
]]
}
],
storage: { int: "10" }
}
};

const tezosContractCallOperation: PartialTezosTransactionOperation = {
kind: TezosOperationType.TRANSACTION,
destination: "$(contractAddress)",
amount: "0",
parameters: { entrypoint: "default", value: { int: "20" } } // Add 20 to the current storage value
};

const tezosDelegationOperation: PartialTezosDelegationOperation = {
kind: TezosOperationType.DELEGATION,
delegate: "tz3ZmB8oWUmi8YZXgeRpgAcPnEMD8VgUa4Ve" // Tezos Foundation Ghost Baker. Cannot delegate to ourself as that would block undelegation
};

const tezosUndelegationOperation: PartialTezosDelegationOperation = {
kind: TezosOperationType.DELEGATION
};

// Assign the specific types to the DEFAULT_TEZOS_KINDS object
export const DEFAULT_TEZOS_KINDS = {
"tezos_send:transaction": tezosTransactionOperation,
"tezos_send:origination": tezosOriginationOperation,
"tezos_send:contract_call": tezosContractCallOperation,
"tezos_send:delegation": tezosDelegationOperation,
"tezos_send:undelegation": tezosUndelegationOperation,
};

export enum DEFAULT_TEZOS_EVENTS {}

export const DEFAULT_GITHUB_REPO_URL =
Expand Down
Loading

0 comments on commit ea3b504

Please sign in to comment.