Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rahul/ifl 1603 make asset response consistent across all endpoints #4272

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
84e4811
new burn asset response with asset object
patnir Sep 7, 2023
6ba92da
updating tests
patnir Sep 7, 2023
dfdf840
moving asset to a types file to be reused in mint and burn
patnir Sep 8, 2023
a9fd8f8
updating test
patnir Sep 8, 2023
d659113
adding two different construct rpc methods
patnir Sep 12, 2023
de11199
updating RcpAccountAssetBalanceDeltaSchema
patnir Sep 12, 2023
de6602e
updating get account transactions
patnir Sep 12, 2023
2ee8aff
deprecating asset variables in balance endpoints
patnir Sep 12, 2023
067e459
removing asset object when id doesn't guarantee asset object is found
patnir Sep 12, 2023
bc00c67
removing optional asset object
patnir Sep 12, 2023
d9251c6
removing unused variable
patnir Sep 12, 2023
862744a
removing construct functions
patnir Sep 12, 2023
dee7827
removing optional asset object
patnir Sep 12, 2023
416e7dd
deprecating asset name fields in note, mint, and burn
patnir Sep 13, 2023
009d009
normalizing rpc asset object for all endpointS
patnir Sep 13, 2023
3fdcbb1
fixing tests
patnir Sep 13, 2023
e06918b
using asset value object
patnir Sep 13, 2023
8153ee0
adding back GetAssetResponse
patnir Sep 13, 2023
9371644
reverting client ts changes
patnir Sep 13, 2023
804d84b
show n/a if supply is not available
patnir Sep 13, 2023
d12c57e
Merge branch 'staging' into rahul/ifl-1603-spike-field-update-method-…
patnir Sep 13, 2023
7bb9d34
chain get asset uses same asset model
patnir Sep 13, 2023
acfc85c
using Assert.isNotUndefined(accountAsset)
patnir Sep 13, 2023
425e638
deprecating the status field on the asset
patnir Sep 13, 2023
0e00ba2
adding comment
patnir Sep 13, 2023
b4bdc00
removing expect
patnir Sep 13, 2023
cc80b3f
Resetti9ng fixture
patnir Sep 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ironfish-cli/src/commands/chain/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class Asset extends IronfishCommand {
this.log(`Metadata: ${BufferUtils.toHuman(Buffer.from(data.content.metadata, 'hex'))}`)
this.log(`Creator: ${data.content.creator}`)
this.log(`Owner: ${data.content.owner}`)
this.log(`Supply: ${data.content.supply}`)
this.log(`Supply: ${data.content.supply ?? 'N/A'}`)
this.log(`Identifier: ${data.content.id}`)
this.log(`Transaction Created: ${data.content.createdTransactionHash}`)
}
Expand Down
55 changes: 33 additions & 22 deletions ironfish/src/rpc/routes/chain/getAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,19 @@
import { ASSET_ID_LENGTH } from '@ironfish/rust-nodejs'
import * as yup from 'yup'
import { Assert } from '../../../assert'
import { AssetValue } from '../../../blockchain/database/assetValue'
import { FullNode } from '../../../node'
import { CurrencyUtils } from '../../../utils'
import { AssetStatus } from '../../../wallet'
import { NotFoundError, ValidationError } from '../../adapters'
import { RpcAsset, RpcAssetSchema } from '../../types'
import { ApiNamespace, routes } from '../router'

export type GetAssetRequest = {
id: string
}

export type GetAssetResponse = {
createdTransactionHash: string
id: string
metadata: string
name: string
nonce: number
creator: string
owner: string
supply: string
}
export type GetAssetResponse = RpcAsset

export const GetAssetRequestSchema: yup.ObjectSchema<GetAssetRequest> = yup
.object()
Expand All @@ -31,18 +25,33 @@ export const GetAssetRequestSchema: yup.ObjectSchema<GetAssetRequest> = yup
})
.defined()

export const GetAssetResponse: yup.ObjectSchema<GetAssetResponse> = yup
.object({
createdTransactionHash: yup.string().defined(),
id: yup.string().defined(),
metadata: yup.string().defined(),
name: yup.string().defined(),
nonce: yup.number().defined(),
creator: yup.string().defined(),
owner: yup.string().defined(),
supply: yup.string().defined(),
})
.defined()
export const GetAssetResponse: yup.ObjectSchema<GetAssetResponse> = RpcAssetSchema.defined()

/**
* Note: This logic will be deprecated when we move the field `status` from the Asset response object. The status field has
* more to do with the transaction than the asset itself.
*
* @param node: FullNode
* @param asset: AssetValue
* @returns Promise<AssetStatus>
*/
async function getAssetStatus(node: FullNode, asset: AssetValue): Promise<AssetStatus> {
const blockHash = await node.chain.getBlockHashByTransactionHash(asset.createdTransactionHash)
if (!blockHash) {
return AssetStatus.UNKNOWN
}

const blockHeader = await node.chain.getHeader(blockHash)

if (!blockHeader) {
return AssetStatus.UNKNOWN
}

return blockHeader.sequence + node.chain.config.get('confirmations') <
node.chain.head.sequence
? AssetStatus.CONFIRMED
: AssetStatus.UNCONFIRMED
}

routes.register<typeof GetAssetRequestSchema, GetAssetResponse>(
`${ApiNamespace.chain}/getAsset`,
Expand Down Expand Up @@ -72,6 +81,8 @@ routes.register<typeof GetAssetRequestSchema, GetAssetResponse>(
creator: asset.creator.toString('hex'),
owner: asset.owner.toString('hex'),
supply: CurrencyUtils.encode(asset.supply),
status: await getAssetStatus(node, asset),
verification: node.assetsVerifier.verify(asset.id),
})
},
)
9 changes: 9 additions & 0 deletions ironfish/src/rpc/routes/chain/getTransactionStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,27 @@ import { ApiNamespace, routes } from '../router'

interface Note {
assetId: string
/**
* @deprecated Please use getAsset endpoint to get this information
*/
assetName: string
hash: string
value: string
memo: string
}
interface Mint {
assetId: string
/**
* @deprecated Please use getAsset endpoint to get this information
*/
assetName: string
value: string
}
interface Burn {
assetId: string
/**
* @deprecated Please use getAsset endpoint to get this information
*/
assetName: string
value: string
}
Expand Down
18 changes: 18 additions & 0 deletions ironfish/src/rpc/routes/wallet/burnAsset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { Asset } from '@ironfish/rust-nodejs'
import { Assert } from '../../../assert'
import {
useAccountFixture,
useMinerBlockFixture,
Expand Down Expand Up @@ -74,6 +75,10 @@ describe('Route wallet/burnAsset', () => {
})
jest.spyOn(wallet, 'burn').mockResolvedValueOnce(burnTransaction)

const accountAsset = await account.getAsset(assetId)

Assert.isNotUndefined(accountAsset)

const response = await routeTest.client.wallet.burnAsset({
account: account.name,
assetId: assetId.toString('hex'),
Expand All @@ -82,6 +87,19 @@ describe('Route wallet/burnAsset', () => {
})

expect(response.content).toEqual({
asset: {
id: asset.id().toString('hex'),
metadata: asset.metadata().toString('hex'),
name: asset.name().toString('hex'),
creator: asset.creator().toString('hex'),
nonce: accountAsset.nonce ?? null,
owner: accountAsset.owner.toString('hex') ?? '',
status: await node.wallet.getAssetStatus(account, accountAsset, {
confirmations: 0,
}),
verification: node.assetsVerifier.verify(asset.id()),
createdTransactionHash: accountAsset.createdTransactionHash.toString('hex') ?? null,
},
assetId: asset.id().toString('hex'),
name: asset.name().toString('hex'),
hash: burnTransaction.hash().toString('hex'),
Expand Down
22 changes: 22 additions & 0 deletions ironfish/src/rpc/routes/wallet/burnAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import * as yup from 'yup'
import { Assert } from '../../../assert'
import { CurrencyUtils, YupUtils } from '../../../utils'
import { RpcAsset, RpcAssetSchema } from '../../types'
import { ApiNamespace, routes } from '../router'
import { getAccount } from './utils'

Expand All @@ -18,8 +19,15 @@ export interface BurnAssetRequest {
}

export interface BurnAssetResponse {
asset: RpcAsset
/**
* @deprecated Please use `asset.id` instead
*/
assetId: string
hash: string
/**
* @deprecated Please use `asset.name` instead
*/
name: string
value: string
}
Expand All @@ -38,6 +46,7 @@ export const BurnAssetRequestSchema: yup.ObjectSchema<BurnAssetRequest> = yup

export const BurnAssetResponseSchema: yup.ObjectSchema<BurnAssetResponse> = yup
.object({
asset: RpcAssetSchema.required(),
assetId: yup.string().required(),
hash: yup.string().required(),
name: yup.string().required(),
Expand Down Expand Up @@ -71,6 +80,19 @@ routes.register<typeof BurnAssetRequestSchema, BurnAssetResponse>(
const burn = transaction.burns[0]

request.end({
asset: {
patnir marked this conversation as resolved.
Show resolved Hide resolved
id: asset.id.toString('hex'),
metadata: asset.metadata.toString('hex'),
name: asset.name.toString('hex'),
nonce: asset.nonce,
creator: asset.creator.toString('hex'),
owner: asset.owner.toString('hex'),
verification: node.assetsVerifier.verify(asset.id),
status: await node.wallet.getAssetStatus(account, asset, {
confirmations: request.data.confirmations,
}),
createdTransactionHash: asset.createdTransactionHash.toString('hex'),
},
assetId: burn.assetId.toString('hex'),
hash: transaction.hash().toString('hex'),
name: asset.name.toString('hex'),
Expand Down
21 changes: 8 additions & 13 deletions ironfish/src/rpc/routes/wallet/getAccountTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import * as yup from 'yup'
import { TransactionStatus, TransactionType } from '../../../wallet'
import { RpcSpend, RpcSpendSchema } from '../chain'
import { ApiNamespace, routes } from '../router'
import { RpcWalletNote, RpcWalletNoteSchema } from './types'
import {
RcpAccountAssetBalanceDelta,
RcpAccountAssetBalanceDeltaSchema,
RpcWalletNote,
RpcWalletNoteSchema,
} from './types'
import {
getAccount,
getAccountDecryptedNotes,
Expand Down Expand Up @@ -35,7 +40,7 @@ export type GetAccountTransactionResponse = {
burnsCount: number
timestamp: number
submittedSequence: number
assetBalanceDeltas: Array<{ assetId: string; assetName: string; delta: string }>
assetBalanceDeltas: RcpAccountAssetBalanceDelta[]
notes: RpcWalletNote[]
Comment on lines -38 to 44
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using an already defined object

spends: RpcSpend[]
} | null
Expand Down Expand Up @@ -69,17 +74,7 @@ export const GetAccountTransactionResponseSchema: yup.ObjectSchema<GetAccountTra
burnsCount: yup.number().defined(),
timestamp: yup.number().defined(),
submittedSequence: yup.number().defined(),
assetBalanceDeltas: yup
.array(
yup
.object({
assetId: yup.string().defined(),
assetName: yup.string().defined(),
delta: yup.string().defined(),
})
.defined(),
)
.defined(),
assetBalanceDeltas: yup.array(RcpAccountAssetBalanceDeltaSchema).defined(),
notes: yup.array(RpcWalletNoteSchema).defined(),
spends: yup.array(RpcSpendSchema).defined(),
})
Expand Down
21 changes: 8 additions & 13 deletions ironfish/src/rpc/routes/wallet/getAccountTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import { TransactionValue } from '../../../wallet/walletdb/transactionValue'
import { RpcRequest } from '../../request'
import { RpcSpend, RpcSpendSchema } from '../chain'
import { ApiNamespace, routes } from '../router'
import { RpcWalletNote, RpcWalletNoteSchema } from './types'
import {
RcpAccountAssetBalanceDelta,
RcpAccountAssetBalanceDeltaSchema,
RpcWalletNote,
RpcWalletNoteSchema,
} from './types'
import {
getAccount,
getAccountDecryptedNotes,
Expand Down Expand Up @@ -43,7 +48,7 @@ export type GetAccountTransactionsResponse = {
expiration: number
timestamp: number
submittedSequence: number
assetBalanceDeltas: Array<{ assetId: string; assetName: string; delta: string }>
assetBalanceDeltas: RcpAccountAssetBalanceDelta[]
Comment on lines -46 to +51
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using an already defined object

notes?: RpcWalletNote[]
spends?: RpcSpend[]
}
Expand Down Expand Up @@ -79,17 +84,7 @@ export const GetAccountTransactionsResponseSchema: yup.ObjectSchema<GetAccountTr
expiration: yup.number().defined(),
timestamp: yup.number().defined(),
submittedSequence: yup.number().defined(),
assetBalanceDeltas: yup
.array(
yup
.object({
assetId: yup.string().defined(),
assetName: yup.string().defined(),
delta: yup.string().defined(),
})
.defined(),
)
.defined(),
assetBalanceDeltas: yup.array(RcpAccountAssetBalanceDeltaSchema).defined(),
notes: yup.array(RpcWalletNoteSchema).defined(),
spends: yup.array(RpcSpendSchema).defined(),
})
Expand Down
13 changes: 10 additions & 3 deletions ironfish/src/rpc/routes/wallet/getAsset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import '../../../testUtilities/matchers'
import { Asset } from '@ironfish/rust-nodejs'
import { Assert } from '../../../assert'
import { FullNode } from '../../../node'
import { Block, Transaction } from '../../../primitives'
import {
Expand Down Expand Up @@ -111,6 +112,11 @@ describe('Route chain.getAsset', () => {
id: asset.id().toString('hex'),
account: account.name,
})

const accountAsset = await account.getAsset(asset.id())

Assert.isNotUndefined(accountAsset)

expect(response.content).toEqual({
createdTransactionHash: pendingMint.hash().toString('hex'),
creator: account.publicAddress,
Expand All @@ -119,9 +125,10 @@ describe('Route chain.getAsset', () => {
metadata: asset.metadata().toString('hex'),
name: asset.name().toString('hex'),
nonce: asset.nonce(),
status: AssetStatus.PENDING,
supply: null,
verification: { status: 'unknown' },
status: await node.wallet.getAssetStatus(account, accountAsset, {
confirmations: 0,
}),
verification: node.assetsVerifier.verify(asset.id()),
})
})

Expand Down
36 changes: 5 additions & 31 deletions ironfish/src/rpc/routes/wallet/getAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { ASSET_ID_LENGTH } from '@ironfish/rust-nodejs'
import * as yup from 'yup'
import { AssetVerification } from '../../../assets'
import { CurrencyUtils } from '../../../utils'
import { NotFoundError, ValidationError } from '../../adapters'
import { RpcAsset, RpcAssetSchema } from '../../types'
import { ApiNamespace, routes } from '../router'
import { getAccount } from './utils'

Expand All @@ -15,19 +15,7 @@ export type GetWalletAssetRequest = {
id: string
}

export type GetWalletAssetResponse = {
createdTransactionHash: string
creator: string
owner: string
id: string
metadata: string
name: string
nonce: number
status: string
verification: AssetVerification
// Populated for assets the account owns
supply: string | null
}
export type GetWalletAssetResponse = RpcAsset

export const GetWalletAssetRequestSchema: yup.ObjectSchema<GetWalletAssetRequest> = yup
.object()
Expand All @@ -38,22 +26,8 @@ export const GetWalletAssetRequestSchema: yup.ObjectSchema<GetWalletAssetRequest
})
.defined()

export const GetWalletAssetResponse: yup.ObjectSchema<GetWalletAssetResponse> = yup
.object({
createdTransactionHash: yup.string().defined(),
creator: yup.string().defined(),
owner: yup.string().defined(),
id: yup.string().defined(),
metadata: yup.string().defined(),
name: yup.string().defined(),
nonce: yup.number().defined(),
status: yup.string().defined(),
verification: yup
.object({ status: yup.string().oneOf(['verified', 'unverified', 'unknown']).defined() })
.defined(),
supply: yup.string().nullable().defined(),
})
.defined()
export const GetWalletAssetResponse: yup.ObjectSchema<GetWalletAssetResponse> =
RpcAssetSchema.defined()

routes.register<typeof GetWalletAssetRequestSchema, GetWalletAssetResponse>(
`${ApiNamespace.wallet}/getAsset`,
Expand Down Expand Up @@ -84,7 +58,7 @@ routes.register<typeof GetWalletAssetRequestSchema, GetWalletAssetResponse>(
status: await node.wallet.getAssetStatus(account, asset, {
confirmations: request.data.confirmations,
}),
supply: asset.supply ? CurrencyUtils.encode(asset.supply) : null,
supply: asset.supply ? CurrencyUtils.encode(asset.supply) : undefined,
verification: node.assetsVerifier.verify(asset.id),
})
},
Expand Down
Loading