Skip to content

Commit

Permalink
Merge pull request #164 from palladians/feat/general-types
Browse files Browse the repository at this point in the history
chore: improve mina-core api and create new pallad-core package
  • Loading branch information
mich3lang3lo authored Apr 15, 2024
2 parents 98e3f63 + e336a40 commit 1eced9d
Show file tree
Hide file tree
Showing 134 changed files with 1,338 additions and 371 deletions.
8 changes: 3 additions & 5 deletions packages/features/src/common/hooks/use-account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getSessionPersistence } from '@palladxyz/persistence'
import { useVault } from '@palladxyz/vault'
import { getPublicKey, isDelegated, useVault } from '@palladxyz/vault'
import easyMeshGradient from 'easy-mesh-gradient'
import { useMemo } from 'react'
import { useNavigate } from 'react-router-dom'
Expand All @@ -20,11 +20,11 @@ export const useAccount = () => {
const setVaultStateUninitialized = useAppStore(
(state) => state.setVaultStateUninitialized
)
const publicKey = currentWallet.credential.credential?.address as string
const fetchWallet = async () => {
await _syncWallet()
return getAccountsInfo(network, publicKey) // TODO: replace with getBalance
}
const publicKey = getPublicKey(currentWallet)
const swr = useSWR(
publicKey ? [publicKey, 'account', network] : null,
async () => await fetchWallet(),
Expand All @@ -46,9 +46,7 @@ export const useAccount = () => {
}),
[publicKey]
)
const stakeDelegated =
currentWallet.accountInfo['MINA']?.publicKey !==
currentWallet.accountInfo['MINA']?.delegate
const stakeDelegated = isDelegated(currentWallet)
const copyWalletAddress = async () => {
await navigator.clipboard.writeText(publicKey ?? '')
toast({
Expand Down
4 changes: 2 additions & 2 deletions packages/features/src/common/hooks/use-transaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useVault } from '@palladxyz/vault'
import { getPublicKey, useVault } from '@palladxyz/vault'
import useSWR from 'swr'

import { useAppStore } from '../store/app'
Expand All @@ -8,7 +8,7 @@ export const useTransaction = ({ hash }: { hash: string }) => {
const currentWallet = useVault((state) => state.getCurrentWallet())
const _syncTransactions = useVault((state) => state._syncTransactions)
const getTransaction = useVault((state) => state.getTransaction)
const publicKey = currentWallet.credential.credential?.address as string
const publicKey = getPublicKey(currentWallet)
const network = useAppStore((state) => state.network)
const syncAndGetTransaction = async () => {
await _syncTransactions(providerConfig, publicKey)
Expand Down
4 changes: 2 additions & 2 deletions packages/features/src/common/hooks/use-transactions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useVault } from '@palladxyz/vault'
import { getPublicKey, useVault } from '@palladxyz/vault'
import useSWR from 'swr'

import { useAppStore } from '../store/app'

export const useTransactions = () => {
const currentWallet = useVault((state) => state.getCurrentWallet())
const getTransactions = useVault((state) => state.getTransactions)
const publicKey = currentWallet.credential.credential?.address as string
const publicKey = getPublicKey(currentWallet)
const network = useAppStore((state) => state.network)
return useSWR(
publicKey ? [publicKey, 'transactions', network] : null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,17 @@ export const ConfirmTransactionForm = () => {
validUntil: '4294967295',
fee,
amount,
nonce: currentWallet.accountInfo['MINA'].inferredNonce, // TODO: remove hardcoded 'MINA'
nonce: currentWallet.accountInfo['MINA'].inferredNonce, // need a util for this whole `onSubmit` to remove Mina dependency
type: 'payment'
}
const constructedTx = await constructTx(
transaction,
kind === 'staking'
? Mina.TransactionKind.STAKE_DELEGATION
: Mina.TransactionKind.PAYMENT
)
const constructTxArgs = {
transaction: transaction,
transactionKind:
kind === 'staking'
? Mina.TransactionKind.STAKE_DELEGATION
: Mina.TransactionKind.PAYMENT
}
const constructedTx = await constructTx(constructTxArgs)
const getPassphrase = () =>
new Promise<Uint8Array>((resolve) =>
resolve(Buffer.from(data.spendingPassword))
Expand Down
12 changes: 12 additions & 0 deletions packages/key-management/src/util/Transactions/build-tx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Mina } from '@palladxyz/mina-core'

import { constructTransaction } from './buildMinaTx'

export type constructTxArgs = {
transaction: Mina.TransactionBody
transactionKind: Mina.TransactionKind
}

export function constructTx(args: constructTxArgs) {
return constructTransaction(args.transaction, args.transactionKind)
}
1 change: 1 addition & 0 deletions packages/key-management/src/util/Transactions/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './build-tx'
export * from './buildMinaTx'
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BorrowedTypes } from '../../'
import { BorrowedTypes } from '..'

/*
* represents the signed message - SignedLegacy<MessageBody>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BorrowedTypes } from '../../'
import { BorrowedTypes } from '..'

/**
* Represents the body of a message.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InvalidStringError, OpaqueString } from '@palladxyz/util'

import { BorrowedTypes } from '../../'
import { BorrowedTypes } from '..'

/**
* Represents the body of a transaction.
Expand Down Expand Up @@ -54,17 +54,17 @@ export type SignedTransaction =
/**
* transaction hash as base64 string
*/
export type TransactionId = OpaqueString<'TransactionId'>
export type TxId = OpaqueString<'TransactionId'>

/**
* @param {string} value transaction hash as base64 string
* @throws InvalidStringError
*/
export const TransactionId = (value: string): TransactionId => {
export const TransactionId = (value: string): TxId => {
if (!isBase64(value)) {
throw new InvalidStringError('Not a valid base64 string')
}
return value as TransactionId
return value as TxId
}

function isBase64(value: string): boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BorrowedTypes } from '../../'
import { BorrowedTypes } from '..'

export type SignedZkAppCommand =
BorrowedTypes.Signed<BorrowedTypes.ZkappCommand>
Expand Down
11 changes: 10 additions & 1 deletion packages/mina-core/src/Mina/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
export * from './types'
export * from './Account'
export * from './Address'
export * from './Block'
export * from './Fields'
export * from './Message'
export * from './Networks'
export * from './Nullifier'
export * from './Pagination'
export * from './Transaction'
export * from './ZkApp'
10 changes: 0 additions & 10 deletions packages/mina-core/src/Mina/types/index.ts

This file was deleted.

18 changes: 7 additions & 11 deletions packages/mina-core/src/Providers/UnifiedProvider.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Paginated, TransactionBody } from '../Mina'
import { AccountInfo, AccountInfoArgs } from './AccountInfoProvider'
import { TransactionBody } from '../Mina'
import { AccountInfo, AccountInfoArgs } from './account-info-provider'
import {
TransactionsByAddressesArgs,
TransactionsByIdsArgs
} from './ChainHistoryProvider'
} from './chain-history-provider'
import { DaemonStatus } from './daemon-status-provider'
import { HealthCheckResponse } from './Provider'
import { TxStatus, TxStatusArgs } from './TxStatusProvider'
import { SubmitTxArgs, SubmitTxResult } from './TxSubmitProvider'
import { TxStatus, TxStatusArgs } from './tx-status-provider'
import { SubmitTxArgs, SubmitTxResult } from './tx-submit-provider'

export type UnifiedMinaProviderConfig = {
nodeUrl: string
Expand All @@ -21,18 +21,14 @@ export interface UnifiedMinaProviderType {
// Methods related to ProviderNode
getAccountInfo(
args: AccountInfoArgs
):
| Promise<AccountInfo | undefined>
| Promise<Record<string, AccountInfo> | undefined>
): Promise<Record<string, AccountInfo> | undefined>
getTransactionStatus?(args: TxStatusArgs): Promise<TxStatus | undefined>
submitTransaction(args: SubmitTxArgs): Promise<SubmitTxResult | undefined>

// Methods related to ProviderArchive
getTransactions(
args: TransactionsByAddressesArgs
):
| Promise<Paginated<TransactionBody> | undefined>
| Promise<TransactionBody[] | undefined>
): Promise<TransactionBody[] | undefined>
getTransaction?(
args: TransactionsByIdsArgs
): Promise<TransactionBody[] | undefined>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ export interface AccountInfoProvider extends Provider {
*/
getAccountInfo: (
args: AccountInfoArgs
) => Promise<AccountInfo> | Promise<Record<string, AccountInfo>>
) => Promise<Record<string, AccountInfo>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface TransactionsByAddressesArgs {
blockRange?: Range<Mina.BlockNo>
}

export type TransactionsByIdsArgs = { ids: Mina.TransactionId[] }
export type TransactionsByIdsArgs = { ids: Mina.TxId[] }
export type PaginatedTransaction = Mina.Paginated<Mina.TransactionBody>

export interface ChainHistoryProvider extends Provider {
Expand All @@ -23,14 +23,12 @@ export interface ChainHistoryProvider extends Provider {
*/
transactionsByAddresses: (
args: TransactionsByAddressesArgs
) =>
| Promise<Mina.Paginated<Mina.TransactionBody>>
| Promise<Mina.TransactionBody[]>
) => Promise<Mina.TransactionBody[]>

/**
* Gets the transactions matching the provided hashes.
*
* @param {Mina.TransactionId[]} ids array of transaction ids
* @param {Mina.TxId[]} ids array of transaction ids
* @returns {Mina.TransactionBody[]} an array of transactions
*/
transactionsByHashes: (
Expand Down
8 changes: 4 additions & 4 deletions packages/mina-core/src/Providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from './AccountInfoProvider'
export * from './ChainHistoryProvider'
export * from './account-info-provider'
export * from './chain-history-provider'
export * from './daemon-status-provider'
export * from './Provider'
export * from './TxStatusProvider'
export * from './TxSubmitProvider'
export * from './tx-status-provider'
export * from './tx-submit-provider'
export * from './UnifiedProvider'
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BorrowedTypes } from '../../'
import { BorrowedTypes } from '../..'
import { TransactionBody, TransactionKind } from '../../Mina'
import { Provider } from '../Provider'

Expand Down
6 changes: 3 additions & 3 deletions packages/mina-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * as Mina from './Mina'
// TODO: Examine why it fails with TS4023 once this line is removed.
export * as BorrowedTypes from './borrowed-types'
export * as Mina from './Mina'
export * from './Providers'
export * from './types'
// TODO: Examine why it fails with TS4023 once this line is removed.
export { TransactionKind } from './Mina/Transaction'
2 changes: 0 additions & 2 deletions packages/mina-core/src/types.ts

This file was deleted.

82 changes: 82 additions & 0 deletions packages/pallad-core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# @palladxyz/mina-core

@palladxyz/mina-core is a TypeScript package designed for interacting with the Mina Protocol. It provides interfaces for retrieving account information, processing transaction history, submitting transactions, and provider health checks. The package also includes the core types that represent the main data structures of the Mina Protocol.

## Installation

Install the package via npm:

```bash
npm install @palladxyz/mina-core
```

## Usage

### AccountInfoProvider

The `AccountInfoProvider` interface is used to fetch account information based on a public key.

```ts
import { AccountInfoProvider } from '@palladxyz/mina-core'

async function getAccountInfo(provider: AccountInfoProvider) {
const accountInfo = await provider.getAccountInfo({ publicKey: '...' })
console.log(accountInfo)
}
```

### ChainHistoryProvider

The `ChainHistoryProvider` interface provides methods to fetch transactions either by addresses or by hashes.

```ts
import { ChainHistoryProvider } from '@palladxyz/mina-core'

async function getTransactionsByAddresses(provider: ChainHistoryProvider) {
const transactions = await provider.transactionsByAddresses({
addresses: ['...']
})
console.log(transactions)
}

async function getTransactionsByHashes(provider: ChainHistoryProvider) {
const transactions = await provider.transactionsByHashes({ ids: ['...'] })
console.log(transactions)
}
```

### TxSubmitProvider

The `TxSubmitProvider` interface allows you to submit signed transactions to the network.

```ts
import { TxSubmitProvider } from '@palladxyz/mina-core'

async function submitTx(provider: TxSubmitProvider) {
const signedTx = // Prepare your signed transaction here
await provider.submitTx({ signedTransaction: signedTx })
}
```

### Provider

All provider types extend the base Provider interface, which provides a health check method to verify if the provider is operational.

```ts
import { Provider } from '@palladxyz/mina-core'

async function checkHealth(provider: Provider) {
const healthCheckResponse = await provider.healthCheck()
console.log(
healthCheckResponse.ok ? 'Provider is healthy' : 'Provider is down'
)
}
```

### Mina Core Types

This package also includes TypeScript definitions for some of the core data structures of the Mina Protocol, which can be found under `./src/Mina/types`.

### Disclaimer

This package is still under development. Use it at your own risk and ensure to test thoroughly in your applications. Contributions and suggestions are welcomed!
34 changes: 34 additions & 0 deletions packages/pallad-core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@palladxyz/pallad-core",
"version": "0.0.1",
"type": "module",
"description": "Core Pallad Package",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"scripts": {
"build": "tsup",
"dev": "tsup --watch",
"test:unit": "vitest run",
"cleanup": "rimraf node_modules dist .turbo"
},
"dependencies": {
"@palladxyz/mina-core": "^0.0.1",
"@palladxyz/key-management": "^0.0.1",
"@palladxyz/util": "^0.0.1",
"bs58check": "^3.0.1",
"buffer": "^6.0.3",
"mina-signer": "^2.1.1",
"viem": "^2.9.16"
},
"devDependencies": {
"@palladxyz/common": "^1.0.0",
"@types/mocha": "^10.0.6",
"@types/secp256k1": "^4.0.6"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './types'
Loading

0 comments on commit 1eced9d

Please sign in to comment.