Skip to content

Commit

Permalink
feat(tx builder): provide default name fee
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Feb 11, 2022
1 parent eea92be commit 18e4bab
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
14 changes: 2 additions & 12 deletions src/ae/aens.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,10 @@
*/

import { salt } from '../utils/crypto'
import {
commitmentHash, ensureNameValid, getMinimumNameFee, isAuctionName
} from '../tx/builder/helpers'
import { commitmentHash, ensureNameValid, isAuctionName } from '../tx/builder/helpers'
import Ae from './'
import { CLIENT_TTL, NAME_FEE, NAME_TTL } from '../tx/builder/schema'
import {
InsufficientNameFeeError,
IllegalArgumentError
} from '../utils/errors'
import { IllegalArgumentError } from '../utils/errors'

/**
* Revoke a name
Expand Down Expand Up @@ -240,11 +235,6 @@ async function claim (name, salt, options) {
ensureNameValid(name)
const opt = { ...this.Ae.defaults, ...options }

const minNameFee = getMinimumNameFee(name)
if (opt.nameFee !== this.Ae.defaults.nameFee && minNameFee.gt(opt.nameFee)) {
throw new InsufficientNameFeeError(opt.nameFee, minNameFee)
}
opt.nameFee = opt.nameFee !== this.Ae.defaults.nameFee ? opt.nameFee : minNameFee
const claimTx = await this.nameClaimTx({
...opt,
accountId: await this.address(opt),
Expand Down
20 changes: 19 additions & 1 deletion src/tx/builder/field-types.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { writeId, readId, isNameValid, produceNameId, ensureNameValid } from './helpers'
import {
writeId, readId, isNameValid, produceNameId, ensureNameValid, getMinimumNameFee, readInt, writeInt
} from './helpers'
import { InsufficientNameFeeError } from '../../utils/errors'

export class Field {
static serialize (value) {
Expand Down Expand Up @@ -30,3 +33,18 @@ export class NameId extends Field {
return readId(value)
}
}

export class NameFee extends Field {
static serialize (value, { name }) {
const minNameFee = getMinimumNameFee(name)
value ??= minNameFee
if (minNameFee.gt(value)) {
throw new InsufficientNameFeeError(value, minNameFee)
}
return writeInt(value)
}

static deserialize (value) {
return readInt(value)
}
}
4 changes: 2 additions & 2 deletions src/tx/builder/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// # https://github.com/aeternity/protocol/blob/master/serializations.md#binary-serialization

import BigNumber from 'bignumber.js'
import { Name, NameId } from './field-types'
import { Name, NameId, NameFee } from './field-types'

export const VSN = 1
export const VSN_2 = 2
Expand Down Expand Up @@ -476,7 +476,7 @@ const NAME_CLAIM_TX_2 = [
TX_FIELD('nonce', FIELD_TYPES.int),
TX_FIELD('name', Name),
TX_FIELD('nameSalt', FIELD_TYPES.int),
TX_FIELD('nameFee', FIELD_TYPES.amount),
TX_FIELD('nameFee', NameFee),
TX_FIELD('fee', FIELD_TYPES.int),
TX_FIELD('ttl', FIELD_TYPES.int)
]
Expand Down

0 comments on commit 18e4bab

Please sign in to comment.