Skip to content

Commit 257db32

Browse files
authored
Merge pull request #1553 from aeternity/feature/docs
Docs improvements
2 parents 6d5b7bd + 3669164 commit 257db32

File tree

18 files changed

+871
-988
lines changed

18 files changed

+871
-988
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ module.exports = {
4646
plugins: [
4747
'@typescript-eslint'
4848
],
49-
ignorePatterns: ['dist', 'es', 'src/apis'],
49+
ignorePatterns: ['dist', 'es', 'src/apis', 'docs/api'],
5050
rules: {
5151
'max-len': ['error', 100, 2, {
5252
ignoreUrls: true,

src/chain.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @example import { Chain } from '@aeternity/aepp-sdk'
2323
*/
2424

25-
import { AE_AMOUNT_FORMATS, AeAmountFormats, formatAmount } from './utils/amount-formatter'
25+
import { AE_AMOUNT_FORMATS, formatAmount } from './utils/amount-formatter'
2626
import verifyTransaction from './tx/validator'
2727
import { pause } from './utils/other'
2828
import { isNameValid, produceNameId, decode } from './tx/builder/helpers'
@@ -181,7 +181,7 @@ export async function getAccount (
181181
export async function getBalance (
182182
address: EncodedData<'ak'>,
183183
{ format = AE_AMOUNT_FORMATS.AETTOS, ...options }:
184-
{ format: AeAmountFormats } & Parameters<typeof getAccount>[1]
184+
{ format: AE_AMOUNT_FORMATS } & Parameters<typeof getAccount>[1]
185185
): Promise<string> {
186186
const { balance } = await getAccount(address, options).catch(() => ({ balance: 0n }))
187187

src/contract/ga.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export async function createMetaTx (
138138

139139
const opt = { ...onAccount.Ae.defaults, ...options }
140140
const { abiVersion } = await onAccount.getVmVersion(TX_TYPE.contractCall)
141-
const wrappedTx = wrapInEmptySignedTx(unpackTx(rawTransaction, { txType: TX_TYPE.signed }))
141+
const wrappedTx = wrapInEmptySignedTx(unpackTx<TX_TYPE.signed>(rawTransaction))
142142
const params = {
143143
...opt,
144144
tx: {

src/tx/builder/constants.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ export const NAME_BID_TIMEOUT_BLOCKS = 480 // # ~1 day
1919
// # this is the max length for a domain that requires a base fee to be paid
2020
export const NAME_MAX_LENGTH_FEE = 31
2121
export const NAME_BID_MAX_LENGTH = 12 // # this is the max length for a domain to be part of a bid
22-
export const POINTER_KEY_BY_PREFIX = {
23-
ak: 'account_pubkey',
24-
ok: 'oracle_pubkey',
25-
ct: 'contract_pubkey',
26-
ch: 'channel'
27-
} as const
22+
export enum POINTER_KEY_BY_PREFIX {
23+
ak = 'account_pubkey',
24+
ok = 'oracle_pubkey',
25+
ct = 'contract_pubkey',
26+
ch = 'channel'
27+
}
2828
// # https://github.com/aeternity/aeternity/blob/72e440b8731422e335f879a31ecbbee7ac23a1cf/apps/aecore/src/aec_governance.erl#L290
2929
// # https://github.com/aeternity/protocol/blob/master/AENS.md#protocol-fees-and-protection-times
3030
// # bid ranges:
@@ -62,15 +62,6 @@ export const NAME_BID_RANGES = mapObject({
6262
1: 5702887
6363
}, ([key, value]) => [key, new BigNumber(value).times(NAME_FEE_MULTIPLIER)])
6464

65-
// # ref: https://github.com/aeternity/aeternity/blob/72e440b8731422e335f879a31ecbbee7ac23a1cf/apps/aecore/src/aec_governance.erl#L273
66-
// # name bid timeouts
67-
export const NAME_BID_TIMEOUTS = {
68-
13: new BigNumber(0),
69-
12: new BigNumber(NAME_BID_TIMEOUT_BLOCKS), // # 480 blocks
70-
8: new BigNumber(31).times(NAME_BID_TIMEOUT_BLOCKS), // # 14880 blocks
71-
4: new BigNumber(62).times(NAME_BID_TIMEOUT_BLOCKS) // # 29760 blocks
72-
}
73-
7465
// # Tag constant for ids (type uint8)
7566
// # see https://github.com/aeternity/protocol/blob/master/serializations.md#the-id-type
7667
// # <<Tag:1/unsigned-integer-unit:8, Hash:32/binary-unit:8>>

src/tx/builder/helpers.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
PREFIX_ID_TAG,
99
NAME_BID_RANGES,
1010
NAME_FEE_BID_INCREMENT,
11-
NAME_BID_TIMEOUTS,
11+
NAME_BID_TIMEOUT_BLOCKS,
1212
NAME_MAX_LENGTH_FEE,
1313
POINTER_KEY_BY_PREFIX
1414
} from './constants'
@@ -203,8 +203,8 @@ export function isNameValid (name: string): boolean {
203203
*/
204204
export function getDefaultPointerKey (
205205
identifier: EncodedData<keyof typeof POINTER_KEY_BY_PREFIX>
206-
): typeof POINTER_KEY_BY_PREFIX[keyof typeof POINTER_KEY_BY_PREFIX] {
207-
decode(identifier as any)
206+
): POINTER_KEY_BY_PREFIX {
207+
decode(identifier)
208208
const prefix = identifier.substring(0, 2) as keyof typeof POINTER_KEY_BY_PREFIX
209209
return POINTER_KEY_BY_PREFIX[prefix]
210210
}
@@ -249,15 +249,16 @@ export function computeBidFee (
249249
* @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
250250
* @param name
251251
* @param claimHeight Auction starting height
252+
* @link https://github.com/aeternity/aeternity/blob/72e440b8731422e335f879a31ecbbee7ac23a1cf/apps/aecore/src/aec_governance.erl#L273
252253
* @return Auction end height
253254
*/
254-
export function computeAuctionEndBlock (name: AensName, claimHeight: number | string): string {
255+
export function computeAuctionEndBlock (name: AensName, claimHeight: number): number {
255256
const length = name.length - AENS_SUFFIX.length
256-
const h = (length <= 4 && NAME_BID_TIMEOUTS[4]) ||
257-
(length <= 8 && NAME_BID_TIMEOUTS[8]) ||
258-
(length <= 12 && NAME_BID_TIMEOUTS[12]) ||
259-
NAME_BID_TIMEOUTS[13]
260-
return h.plus(claimHeight).toString(10)
257+
const h = (length <= 4 && 62 * NAME_BID_TIMEOUT_BLOCKS) ||
258+
(length <= 8 && 31 * NAME_BID_TIMEOUT_BLOCKS) ||
259+
(length <= 12 && NAME_BID_TIMEOUT_BLOCKS) ||
260+
0
261+
return h + claimHeight
261262
}
262263

263264
/**

src/tx/builder/index.ts

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { AeAmountFormats } from './../../utils/amount-formatter'
21
import { EncodedData, EncodingType } from './../../utils/encoder'
32
import BigNumber from 'bignumber.js'
43
import { decode as rlpDecode, encode as rlpEncode, NestedUint8Array } from 'rlp'
@@ -10,16 +9,13 @@ import {
109
DEFAULT_FEE,
1110
FIELD_TYPES,
1211
MIN_GAS_PRICE,
13-
OBJECT_ID_TX_TYPE,
1412
RawTxObject,
1513
TxField,
1614
TxTypeSchemas,
1715
TxParamsCommon,
18-
TxType,
19-
TX_DESERIALIZATION_SCHEMA,
2016
TX_FEE_BASE_GAS,
2117
TX_FEE_OTHER_GAS,
22-
TX_SERIALIZATION_SCHEMA,
18+
TX_SCHEMA,
2319
TX_TYPE,
2420
TxSchema
2521
} from './schema'
@@ -37,7 +33,7 @@ import {
3733
} from './helpers'
3834
import { toBytes } from '../../utils/bytes'
3935
import MPTree, { MPTreeBinary } from '../../utils/mptree'
40-
import { ArgumentError, InvalidTxParamsError, SchemaNotFoundError, UnexpectedTsError } from '../../utils/errors'
36+
import { ArgumentError, InvalidTxParamsError, SchemaNotFoundError, DecodeError } from '../../utils/errors'
4137
import { isKeyOfObject } from '../../utils/other'
4238

4339
/**
@@ -223,7 +219,7 @@ function validateField (
223219
function transformParams (
224220
params: TxParamsCommon,
225221
schema: TxField[],
226-
{ denomination }: {denomination?: AeAmountFormats} = {}
222+
{ denomination }: {denomination?: AE_AMOUNT_FORMATS} = {}
227223
): any {
228224
params = schema
229225
.filter(([, t]) => t === FIELD_TYPES.amount)
@@ -255,7 +251,7 @@ function transformParams (
255251
)
256252
}
257253

258-
function getOracleRelativeTtl (params: any, txType: TxType): number {
254+
function getOracleRelativeTtl (params: any, txType: TX_TYPE): number {
259255
const ttlKeys = {
260256
[TX_TYPE.oracleRegister]: 'oracleTtl',
261257
[TX_TYPE.oracleExtend]: 'oracleTtl',
@@ -281,7 +277,7 @@ function getOracleRelativeTtl (params: any, txType: TxType): number {
281277
* @example calculateMinFee('spendTx', { gasLimit, params })
282278
*/
283279
export function calculateMinFee (
284-
txType: TxType,
280+
txType: TX_TYPE,
285281
{ params, vsn }: { params?: Object, vsn?: number }
286282
): string {
287283
const multiplier = new BigNumber(1e9) // 10^9 GAS_PRICE
@@ -309,7 +305,7 @@ export function calculateMinFee (
309305
* @param options.vsn
310306
*/
311307
function buildFee (
312-
txType: TxType,
308+
txType: TX_TYPE,
313309
{ params, multiplier, vsn }: { params: TxParamsCommon, multiplier: BigNumber, vsn?: number }
314310
): BigNumber {
315311
const { rlpEncoded: txWithOutFee } = buildTx({ ...params }, txType, { vsn })
@@ -339,7 +335,7 @@ function buildFee (
339335
*/
340336
export function calculateFee (
341337
fee: number | BigNumber | string = 0,
342-
txType: TxType,
338+
txType: TX_TYPE,
343339
{ params, showWarning = true, vsn }: {
344340
gasLimit?: number | string | BigNumber
345341
params?: TxSchema
@@ -376,7 +372,7 @@ export function validateParams (
376372

377373
interface TxOptionsRaw {
378374
excludeKeys?: string[]
379-
denomination?: AeAmountFormats
375+
denomination?: AE_AMOUNT_FORMATS
380376
}
381377
/**
382378
* Build binary transaction
@@ -442,7 +438,7 @@ export interface BuiltTx<Tx extends TxSchema, Prefix extends EncodingType> {
442438
}
443439

444440
export type TxParamsBuild = TxParamsCommon & {
445-
denomination?: AeAmountFormats
441+
denomination?: AE_AMOUNT_FORMATS
446442
}
447443
/**
448444
* Build transaction hash
@@ -461,29 +457,25 @@ export type TxParamsBuild = TxParamsCommon & {
461457
*/
462458
export function buildTx<Prefix> (
463459
params: TxParamsBuild,
464-
type: TxType,
460+
type: TX_TYPE,
465461
{ excludeKeys = [], prefix = 'tx', vsn, denomination = AE_AMOUNT_FORMATS.AETTOS }: {
466462
excludeKeys?: string[]
467463
prefix?: EncodingType
468464
vsn?: number
469-
denomination?: AeAmountFormats
465+
denomination?: AE_AMOUNT_FORMATS
470466
} = {}
471467
): Prefix extends EncodingType
472468
? BuiltTx<TxSchema, Prefix>
473469
: BuiltTx<TxSchema, 'tx'> {
474-
const schemas = TX_SERIALIZATION_SCHEMA[type]
475-
if (schemas == null) throw new UnexpectedTsError()
470+
const schemas = TX_SCHEMA[type]
476471

477472
vsn ??= Math.max(...Object.keys(schemas).map(a => +a))
478-
if (!isKeyOfObject(vsn, schemas)) throw new SchemaNotFoundError('serialization', type.toString(), vsn)
473+
if (!isKeyOfObject(vsn, schemas)) throw new SchemaNotFoundError('serialization', TX_TYPE[type], vsn)
479474

480475
const schema = schemas[vsn]
481476

482-
const tags = Object.entries(OBJECT_ID_TX_TYPE).find(([, t]) => t === type)
483-
if (tags == null) { throw new UnexpectedTsError() }
484-
const tag = tags[0]
485477
const binary = buildRawTx(
486-
{ ...params, VSN: vsn, tag },
478+
{ ...params, VSN: vsn, tag: type },
487479
schema,
488480
{ excludeKeys, denomination: params.denomination ?? denomination }
489481
).filter(e => e !== undefined)
@@ -494,12 +486,12 @@ export function buildTx<Prefix> (
494486
tx,
495487
rlpEncoded,
496488
binary,
497-
txObject: unpackRawTx<TxTypeSchemas[TxType]>(binary, schema)
489+
txObject: unpackRawTx<TxTypeSchemas[TX_TYPE]>(binary, schema)
498490
} as any
499491
}
500492

501493
export interface TxUnpacked<Tx extends TxSchema> {
502-
txType: TxType
494+
txType: TX_TYPE
503495
tx: RawTxObject<Tx>
504496
rlpEncoded: Uint8Array
505497
binary: Uint8Array | NestedUint8Array
@@ -516,7 +508,7 @@ export interface TxUnpacked<Tx extends TxSchema> {
516508
* @returns object.rlpEncoded rlp encoded transaction
517509
* @returns object.binary binary transaction
518510
*/
519-
export function unpackTx<TxType extends keyof TxTypeSchemas> (
511+
export function unpackTx<TxType extends TX_TYPE> (
520512
encodedTx: EncodedData<'tx'> | Uint8Array,
521513
{ txType, fromRlpBinary = false }:
522514
{ txType?: TxType, fromRlpBinary?: boolean } = {
@@ -525,16 +517,15 @@ export function unpackTx<TxType extends keyof TxTypeSchemas> (
525517
): TxUnpacked<TxTypeSchemas[TxType]> {
526518
const rlpEncoded = fromRlpBinary ? encodedTx as Uint8Array : decode(encodedTx as EncodedData<'tx'>)
527519
const binary = rlpDecode(rlpEncoded)
528-
const objId = readInt(binary[0] as Buffer) as unknown as keyof typeof TX_DESERIALIZATION_SCHEMA
529-
txType ??= OBJECT_ID_TX_TYPE[objId] as TxType
530-
const vsn = readInt(binary[1] as Buffer) as keyof typeof TX_DESERIALIZATION_SCHEMA[typeof objId]
531-
const schema = TX_DESERIALIZATION_SCHEMA[objId][vsn]
532-
if (schema == null) {
533-
throw new SchemaNotFoundError('deserialization', `tag ${objId}`, vsn)
534-
}
520+
const objId = +readInt(binary[0] as Buffer)
521+
if (!isKeyOfObject(objId, TX_SCHEMA)) throw new DecodeError(`Unknown transaction tag: ${objId}`)
522+
if (txType != null && txType !== objId) throw new DecodeError(`Expected transaction to have ${TX_TYPE[txType]} tag, got ${TX_TYPE[objId]} instead`)
523+
const vsn = +readInt(binary[1] as Buffer)
524+
if (!isKeyOfObject(vsn, TX_SCHEMA[objId])) throw new SchemaNotFoundError('deserialization', `tag ${objId}`, vsn)
525+
const schema = TX_SCHEMA[objId][vsn]
535526
return {
536-
txType,
537-
tx: unpackRawTx<TxTypeSchemas[typeof txType]>(binary, schema),
527+
txType: objId,
528+
tx: unpackRawTx<TxTypeSchemas[TxType]>(binary, schema),
538529
rlpEncoded,
539530
binary
540531
}
@@ -560,8 +551,8 @@ export function buildTxHash (rawTx: EncodedData<'tx'> | Uint8Array): string {
560551
* @return Contract public key
561552
*/
562553
export function buildContractIdByContractTx (contractTx: EncodedData<'tx'>): EncodedData<'ct'> {
563-
const { txType, tx } = unpackTx(contractTx, { txType: TX_TYPE.contractCreate })
564-
if (![TX_TYPE.contractCreate, TX_TYPE.gaAttach].includes(txType as any)) {
554+
const { txType, tx } = unpackTx<TX_TYPE.contractCreate | TX_TYPE.gaAttach>(contractTx)
555+
if (![TX_TYPE.contractCreate, TX_TYPE.gaAttach].includes(txType)) {
565556
throw new ArgumentError('contractCreateTx', 'a contractCreateTx or gaAttach', txType)
566557
}
567558
return buildContractId(tx.ownerId, +tx.nonce)

0 commit comments

Comments
 (0)