This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into PE-4423-Remove-Extra-Network-Join-Code
- Loading branch information
Showing
15 changed files
with
538 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
const { buyRecordSchema } = require('./buyRecord'); | ||
const { auctionBidSchema } = require('./auction'); | ||
const { increaseUndernameCountSchema } = require('./undernames'); | ||
|
||
module.exports = { | ||
auctionBidSchema, | ||
buyRecordSchema, | ||
increaseUndernameCountSchema, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const increaseUndernameCountSchema = { | ||
$id: '#/definitions/increaseUndernameCount', | ||
type: 'object', | ||
properties: { | ||
function: { | ||
type: 'string', | ||
const: 'increaseUndernameCount', | ||
}, | ||
name: { | ||
type: 'string', | ||
pattern: '^([a-zA-Z0-9][a-zA-Z0-9-]{0,49}[a-zA-Z0-9]|[a-zA-Z0-9]{1})$', | ||
}, | ||
qty: { | ||
type: 'number', | ||
minimum: 1, | ||
maximum: 9990, // should be updated with contants "DEFAULT_UNDERNAME_COUNT" and "MAX_ALLOWED_UNDERNAMES" | ||
}, | ||
}, | ||
required: ['name', 'qty'], | ||
additionalProperties: false, | ||
}; | ||
|
||
module.exports = { | ||
increaseUndernameCountSchema, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { | ||
ARNS_NAME_DOES_NOT_EXIST_MESSAGE, | ||
INSUFFICIENT_FUNDS_MESSAGE, | ||
INVALID_INPUT_MESSAGE, | ||
MAX_ALLOWED_UNDERNAMES, | ||
MAX_UNDERNAME_MESSAGE, | ||
SECONDS_IN_GRACE_PERIOD, | ||
} from '../../constants'; | ||
import { ContractResult, IOState, PstAction } from '../../types'; | ||
import { | ||
calculateProRatedUndernameCost, | ||
walletHasSufficientBalance, | ||
} from '../../utilities'; | ||
import { validateIncreaseUndernameCount } from '../../validations.mjs'; | ||
|
||
declare const ContractError; | ||
declare const SmartWeave: any; | ||
|
||
export class IncreaseUndernameCount { | ||
function = 'increaseUndernameCount'; | ||
name: string; | ||
qty: number; | ||
|
||
constructor(input: any) { | ||
// validate using ajv validator | ||
if (!validateIncreaseUndernameCount(input)) { | ||
throw new ContractError( | ||
`${INVALID_INPUT_MESSAGE} for ${this.function}: ${( | ||
validateIncreaseUndernameCount as any | ||
).errors | ||
.map((e) => { | ||
const key = e.instancePath.replace('/', ''); | ||
const value = input[key]; | ||
return `${key} ('${value}') ${e.message}`; | ||
}) | ||
.join(', ')}`, | ||
); | ||
} | ||
const { name, qty } = input; | ||
this.name = name.trim().toLowerCase(); | ||
this.qty = qty; | ||
} | ||
} | ||
// Increases the lease time for an existing record | ||
export const increaseUndernameCount = async ( | ||
state: IOState, | ||
{ caller, input }: PstAction, | ||
): Promise<ContractResult> => { | ||
const { name, qty } = new IncreaseUndernameCount(input); | ||
|
||
const { balances } = state; | ||
|
||
const record = state.records[name]; | ||
const currentBlockTime = +SmartWeave.block.timestamp; | ||
const undernameCost = calculateProRatedUndernameCost( | ||
qty, | ||
currentBlockTime, | ||
record.type, | ||
record?.endTimestamp, | ||
); | ||
|
||
if (qty + record.undernames > MAX_ALLOWED_UNDERNAMES) { | ||
throw new ContractError(MAX_UNDERNAME_MESSAGE); | ||
} | ||
// Check if the user has enough tokens to increase the undername count | ||
if (!walletHasSufficientBalance(balances, caller, undernameCost)) { | ||
throw new ContractError( | ||
`${INSUFFICIENT_FUNDS_MESSAGE}: caller has ${balances[ | ||
caller | ||
].toLocaleString()} but needs to have ${undernameCost.toLocaleString()} to pay for this undername increase of ${qty} for ${name}.`, | ||
); | ||
} | ||
// check if record exists | ||
if (!record) { | ||
throw new ContractError(ARNS_NAME_DOES_NOT_EXIST_MESSAGE); | ||
} | ||
// This name's lease has expired and cannot be extended | ||
if (record.endTimestamp + SECONDS_IN_GRACE_PERIOD <= currentBlockTime) { | ||
throw new ContractError( | ||
`This name has expired and must renewed before its undername support can be extended.`, | ||
); | ||
} | ||
// TODO: move cost to protocol balance | ||
state.records[name].undernames += qty; | ||
state.balances[caller] -= undernameCost; | ||
|
||
return { state }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.