mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-13 15:10:49 +08:00
Merge pull request #8 from sebastinez/sebastinez/validate-balance
Validate RAD balance before commit tx
This commit is contained in:
@@ -67,6 +67,10 @@ export function registrar(config: Config) {
|
||||
return new ethers.Contract(config.registrar.address, config.abi.registrar, config.provider);
|
||||
}
|
||||
|
||||
export function radToken(config: Config) {
|
||||
return new ethers.Contract(config.radToken.address, config.abi.token, config.provider);
|
||||
}
|
||||
|
||||
export async function registrationFee(config: Config) {
|
||||
return await registrar(config).registrationFeeRad();
|
||||
}
|
||||
@@ -87,7 +91,7 @@ export async function registerName(name: string, owner: string, config: Config)
|
||||
await commitAndRegister(name, owner, config);
|
||||
}
|
||||
} catch (e) {
|
||||
throw { type: Failure.TransactionFailed, message: e.message, txHash: e.txHash };
|
||||
throw { type: e.type || Failure.TransactionFailed, message: e.message, txHash: e.txHash };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,6 +99,11 @@ async function commitAndRegister(name: string, owner: string, config: Config) {
|
||||
let salt = ethers.utils.randomBytes(32);
|
||||
let minAge = (await registrar(config).minCommitmentAge()).toNumber();
|
||||
let fee = await registrationFee(config);
|
||||
// Avoids gas spent by the owner, trying to commit to a name and not having
|
||||
// enough RAD balance
|
||||
if ((await radToken(config).balanceOf(owner)).lt(fee)) {
|
||||
throw { type: Failure.InsufficientBalance, message: "Not enough RAD funds" };
|
||||
}
|
||||
|
||||
await commit(makeCommitment(name, owner, salt), fee, minAge, config);
|
||||
// Save commitment in local storage in case the user refreshes or closes
|
||||
|
||||
@@ -9,6 +9,7 @@ export interface Err {
|
||||
export enum Failure {
|
||||
TransactionFailed = 1,
|
||||
NotAuthenticated = 2,
|
||||
InsufficientBalance = 3,
|
||||
}
|
||||
|
||||
export const error = writable<Err | null>(null);
|
||||
|
||||
Reference in New Issue
Block a user