Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #91 from rnsdomains/develop
Browse files Browse the repository at this point in the history
v1.7.3
  • Loading branch information
ilanolkies authored May 29, 2020
2 parents a1749e4 + 7581aef commit 20a3648
Show file tree
Hide file tree
Showing 16 changed files with 410 additions and 68 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rsksmart/rns",
"version": "1.7.2",
"version": "1.7.3",
"description": "RIF Name Service library.",
"keywords": [
"rsk",
Expand Down
44 changes: 38 additions & 6 deletions src/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import { createRegistry, createContractAddresses } from './factories';
import RNSError, { LIBRARY_NOT_COMPOSED } from './errors';
import { getCurrentAddress } from './utils';
import { TransactionOptions } from './types/options';

export default abstract class implements Composable {
private _contractAddresses!: ContractAddresses;
Expand Down Expand Up @@ -66,16 +67,47 @@ export default abstract class implements Composable {
}

protected async estimateGasAndSendTransaction(
contractMethod: () => any,
contractMethod: any,
customOptions?: TransactionOptions,
): Promise<TransactionReceipt> {
const sender = await getCurrentAddress(this.blockchainApi);
let options: any;

const estimated = await contractMethod().estimateGas({ from: sender });
if (customOptions && customOptions.from) {
options = {
from: customOptions.from,
};
} else {
const sender = await getCurrentAddress(this.blockchainApi);

options = {
from: sender,
};
}

if (customOptions && customOptions.gas) {
options = {
...options,
gas: customOptions.gas,
};
} else {
const estimated = await contractMethod.estimateGas(options);

const gas = Math.floor(estimated * 1.1);
const gas = Math.floor(estimated * 1.1);

options = {
...options,
gas,
};
}

if (customOptions && customOptions.gasPrice) {
options = {
...options,
gasPrice: customOptions.gasPrice,
};
}

return new Promise((resolve, reject) => contractMethod()
.send({ from: sender, gas })
return new Promise((resolve, reject) => contractMethod.send(options)
.on('confirmation', (confirmations: Number, receipt: TransactionReceipt) => resolve(receipt))
.on('error', reject));
}
Expand Down
22 changes: 15 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Registrations from './registrations';
import Subdomains from './subdomains';
import Composer from './composer';
import * as utils from './utils';
import { TransactionOptions } from './types/options';


/**
Expand Down Expand Up @@ -88,15 +89,18 @@ export default class extends Composer implements RNS {
* @param domain - Domain to set resolution
* @param addr - Address to be set as the resolution of the given domain
* @param chainId - Should match one of the listed in SLIP44 (https://github.com/satoshilabs/slips/blob/master/slip-0044.md)
* @param options - Custom configs to be used when submitting the transaction
*
* @returns TransactionReceipt of the submitted tx
*/
setAddr(domain: string, addr: string, chainId?: ChainId): Promise<TransactionReceipt> {
setAddr(
domain: string, addr: string, chainId?: ChainId, options?: TransactionOptions,
): Promise<TransactionReceipt> {
if (!chainId) {
return this._resolutions.setAddr(domain, addr);
return this._resolutions.setAddr(domain, addr, options);
}

return this._resolutions.setChainAddr(domain, addr, chainId);
return this._resolutions.setChainAddr(domain, addr, chainId, options);
}

/**
Expand All @@ -109,11 +113,14 @@ export default class extends Composer implements RNS {
*
* @param domain - Domain to set resolver
* @param resolver - Address to be set as the resolver of the given domain
* @param options - Custom configs to be used when submitting the transaction
*
* @returns TransactionReceipt of the submitted tx
*/
setResolver(domain: string, resolver: string): Promise<TransactionReceipt> {
return this._resolutions.setResolver(domain, resolver);
setResolver(
domain: string, resolver: string, options?: TransactionOptions,
): Promise<TransactionReceipt> {
return this._resolutions.setResolver(domain, resolver, options);
}

/**
Expand All @@ -134,6 +141,7 @@ export default class extends Composer implements RNS {
* Set reverse resolution with the given name for the current address.
*
* @param name - Name to be set as the reverse resolution of the current address
* @param options - Custom configs to be used when submitting the transaction
*
* @throws NO_ACCOUNTS_TO_SIGN if the given blockchain api instance does not have associated accounts to sign the transaction - KB015
* @throws INVALID_DOMAIN if the given domain is empty, is not alphanumeric or if has uppercase characters - KB010
Expand All @@ -142,8 +150,8 @@ export default class extends Composer implements RNS {
*
* @returns TransactionReceipt of the submitted tx
*/
setReverse(name: string): Promise<TransactionReceipt> {
return this._resolutions.setName(name);
setReverse(name: string, options?: TransactionOptions): Promise<TransactionReceipt> {
return this._resolutions.setName(name, options);
}

/**
Expand Down
40 changes: 27 additions & 13 deletions src/resolutions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import RNSError, {
NO_ACCOUNTS_TO_SIGN, NO_SET_ADDR, INVALID_ADDRESS, INVALID_CHECKSUM_ADDRESS,
DOMAIN_NOT_EXISTS, INVALID_DOMAIN, NO_REVERSE_REGISTRAR, NO_SET_NAME_METHOD, NO_SET_CHAIN_ADDR,
} from './errors';
import { TransactionOptions } from './types/options';

/**
* Standard resolution protocols.
Expand Down Expand Up @@ -181,8 +182,12 @@ export default class extends Composer implements Resolutions {
*
* @param domain - Domain to set resolution
* @param addr - Address to be set as the resolution of the given domain
* @param options - Custom configs to be used when submitting the transaction
*
*/
async setAddr(domain: string, addr: string): Promise<TransactionReceipt> {
async setAddr(
domain: string, addr: string, options?: TransactionOptions,
): Promise<TransactionReceipt> {
await this.compose();

if (!await hasAccounts(this.blockchainApi)) {
Expand All @@ -200,9 +205,9 @@ export default class extends Composer implements Resolutions {
createAddrResolver,
);

const contractMethod = () => resolver.methods.setAddr(node, addr);
const contractMethod = resolver.methods.setAddr(node, addr);

return this.estimateGasAndSendTransaction(contractMethod);
return this.estimateGasAndSendTransaction(contractMethod, options);
}

/**
Expand All @@ -217,9 +222,13 @@ export default class extends Composer implements Resolutions {
* @param domain - Domain to set resolution
* @param addr - Address to be set as the resolution of the given domain
* @param chainId - chain identifier listed in SLIP44 (https://github.com/satoshilabs/slips/blob/master/slip-0044.md)
* @param options - Custom configs to be used when submitting the transaction
*
*
*/
async setChainAddr(domain: string, addr: string, chainId: ChainId): Promise<TransactionReceipt> {
async setChainAddr(
domain: string, addr: string, chainId: ChainId, options?: TransactionOptions,
): Promise<TransactionReceipt> {
await this.compose();

if (!await hasAccounts(this.blockchainApi)) {
Expand All @@ -237,15 +246,15 @@ export default class extends Composer implements Resolutions {
createChainAddrResolver,
);

const contractMethod = () => resolver
const contractMethod = resolver
.methods
.setChainAddr(
node,
chainId,
addr,
);

return this.estimateGasAndSendTransaction(contractMethod);
return this.estimateGasAndSendTransaction(contractMethod, options);
}

/**
Expand All @@ -258,8 +267,12 @@ export default class extends Composer implements Resolutions {
*
* @param domain - Domain to set resolver
* @param resolver - Address to be set as the resolver of the given domain
* @param options - Custom configs to be used when submitting the transaction
*
*/
async setResolver(domain: string, resolver: string): Promise<TransactionReceipt> {
async setResolver(
domain: string, resolver: string, options?: TransactionOptions,
): Promise<TransactionReceipt> {
await this.compose();

if (!await hasAccounts(this.blockchainApi)) {
Expand All @@ -275,9 +288,9 @@ export default class extends Composer implements Resolutions {

const node: string = namehash(domain);

const contractMethod = () => this._contracts.registry.methods.setResolver(node, resolver);
const contractMethod = this._contracts.registry.methods.setResolver(node, resolver);

return this.estimateGasAndSendTransaction(contractMethod);
return this.estimateGasAndSendTransaction(contractMethod, options);
}

/**
Expand All @@ -290,10 +303,11 @@ export default class extends Composer implements Resolutions {
*
* @param name - Domain to set resolver
* @param resolver - Address to be set as the resolver of the given domain
* @param options - Custom configs to be used when submitting the transaction
*
* @returns TransactionReceipt of the submitted tx
* @returns TransactionReceipt of the submitted tx
*/
async setName(name: string): Promise<TransactionReceipt> {
async setName(name: string, options?: TransactionOptions): Promise<TransactionReceipt> {
await this.compose();

if (!await hasAccounts(this.blockchainApi)) {
Expand Down Expand Up @@ -322,9 +336,9 @@ export default class extends Composer implements Resolutions {

const reverseRegistrar = createReverseRegistrar(this.blockchainApi, reverseRegistrarOwner);

const contractMethod = () => reverseRegistrar.methods.setName(name);
const contractMethod = reverseRegistrar.methods.setName(name);

return this.estimateGasAndSendTransaction(contractMethod);
return this.estimateGasAndSendTransaction(contractMethod, options);
}

/**
Expand Down
37 changes: 26 additions & 11 deletions src/subdomains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
isValidDomain, isValidTld, isValidLabel, namehash, hasAccounts, labelhash,
getCurrentAddress, isValidAddress, isValidChecksumAddress,
} from './utils';
import { TransactionOptions } from './types/options';

/**
* Set of subdomains related methods
Expand All @@ -32,16 +33,17 @@ export default class extends Composer implements Subdomains {
node: string,
label: string,
owner: string,
options?: TransactionOptions,
): Promise<TransactionReceipt> {
const contractMethod = () => this._contracts.registry
const contractMethod = this._contracts.registry
.methods
.setSubnodeOwner(
node,
labelhash(label),
owner,
);

return this.estimateGasAndSendTransaction(contractMethod);
return this.estimateGasAndSendTransaction(contractMethod, options);
}

private _validateDomainAndLabel(domain: string, label: string): void {
Expand Down Expand Up @@ -110,8 +112,13 @@ export default class extends Composer implements Subdomains {
* @param domain - Parent .rsk domain. ie: wallet.rsk
* @param label - Subdomain to register. ie: alice
* @param owner - The owner of the new subdomain
* @param options - Custom configs to be used when submitting the transaction
*
* @returns Transaction receipt
*/
async setOwner(domain: string, label: string, owner: string): Promise<TransactionReceipt> {
async setOwner(
domain: string, label: string, owner: string, options?: TransactionOptions,
): Promise<TransactionReceipt> {
await this.compose();

if (!await hasAccounts(this.blockchainApi)) {
Expand All @@ -129,7 +136,7 @@ export default class extends Composer implements Subdomains {

const node: string = namehash(`${domain}`);

return this._setSubnodeOwner(node, label, owner);
return this._setSubnodeOwner(node, label, owner, options);
}

/**
Expand All @@ -149,6 +156,7 @@ export default class extends Composer implements Subdomains {
* @param label - Subdomain to register. ie: alice
* @param owner - The owner of the new subdomain. If not provided, the address who executes the tx will be the owner
* @param addr - The address to be set as resolution of the new subdomain
* @param options - Custom configs to be used when submitting the transaction
*
* @returns Transaction receipt
*/
Expand All @@ -157,6 +165,7 @@ export default class extends Composer implements Subdomains {
label: string,
owner?: string,
addr?: string,
options?: TransactionOptions,
): Promise<TransactionReceipt> {
await this.compose();

Expand Down Expand Up @@ -184,21 +193,27 @@ export default class extends Composer implements Subdomains {
}

const node: string = namehash(`${domain}`);
const sender = await getCurrentAddress(this.blockchainApi);
let sender;

if (options && options.from) {
sender = options.from;
} else {
sender = await getCurrentAddress(this.blockchainApi);
}

if (!addr) {
return this._setSubnodeOwner(node, label, owner || sender);
return this._setSubnodeOwner(node, label, owner || sender, options);
} if (!owner || owner === sender) {
// submits just two transactions
await this._setSubnodeOwner(node, label, sender);
await this._setSubnodeOwner(node, label, sender, options);

return this._resolutions.setAddr(`${label}.${domain}`, addr);
return this._resolutions.setAddr(`${label}.${domain}`, addr, options);
}
// needs to submit three txs
await this._setSubnodeOwner(node, label, sender);
await this._setSubnodeOwner(node, label, sender, options);

await this._resolutions.setAddr(`${label}.${domain}`, addr);
await this._resolutions.setAddr(`${label}.${domain}`, addr, options);

return this._setSubnodeOwner(node, label, owner);
return this._setSubnodeOwner(node, label, owner, options);
}
}
3 changes: 2 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ChainId, NetworkId } from './enums';
import RNS, { ContractAddresses, Options, Contracts } from './rns';
import RNS, { Contracts } from './rns';
import { ContractAddresses, Options } from './options';
import { Resolutions } from './resolutions';
import { Subdomains } from './subdomains';
import { Composable } from './composable';
Expand Down
26 changes: 26 additions & 0 deletions src/types/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Contains the necessary contract addresses to run the current lib.
*/
export interface ContractAddresses {
/**
* RNS.sol address
*/
registry: string;
}

/**
* Configuration object used to run the lib if the current network is not RSK Mainnet or RSK Testnet
*/
export interface Options {
contractAddresses?: ContractAddresses,
networkId?: number
}

/**
* Configuration object used to set custom options when sending transactions
*/
export interface TransactionOptions {
gasPrice?: number,
gas?: number,
from?: string,
}
Loading

0 comments on commit 20a3648

Please sign in to comment.