-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from harmony-one/1country-rent-renew
1country rent renew
- Loading branch information
Showing
24 changed files
with
1,665 additions
and
959 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import axios from "axios"; | ||
|
||
import config from "../../../config"; | ||
import { formatUSDAmount } from "../utils"; | ||
|
||
const base = axios.create({ | ||
baseURL: | ||
"https://api.coingecko.com/api/v3/simple/price?ids=harmony&vs_currencies=usd", | ||
timeout: 10000, | ||
}); | ||
|
||
export const getUSDPrice = async ( | ||
onePrice: string | ||
): Promise<{ price: string | null; error: string | null }> => { | ||
try { | ||
const response = await axios.get( | ||
"https://api.coingecko.com/api/v3/simple/price?ids=harmony&vs_currencies=usd" | ||
); | ||
const usdPrice = response.data["harmony"].usd; | ||
return { price: formatUSDAmount(Number(onePrice) * usdPrice), error: null }; | ||
} catch (e) { | ||
console.log(e); | ||
return { price: null, error: "Can't retrieve USD price" }; | ||
} | ||
}; |
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,151 @@ | ||
import { ethers } from "ethers"; | ||
import web3Utils from "web3-utils"; | ||
import pino, {Logger} from "pino"; | ||
|
||
import DCv2Abi from "../contracts/DCv2"; | ||
import config from "../../../config"; | ||
|
||
const logger = pino({ | ||
name: "OneCountryBot", | ||
transport: { | ||
target: "pino-pretty", | ||
options: { | ||
colorize: true, | ||
}, | ||
}, | ||
}); | ||
|
||
export interface DomainRecord { | ||
renter: string; | ||
rentTime: number; | ||
lastPrice: { | ||
amount: string; | ||
formatted: string; | ||
}; | ||
expirationTime: number; | ||
url: string; | ||
prev: string; | ||
next: string; | ||
} | ||
|
||
export interface DomainPrice { | ||
amount: string; | ||
formatted: string; | ||
} | ||
|
||
export interface SendNameExpired { | ||
isExpired: boolean; | ||
expirationDate: number; | ||
isInGracePeriod: boolean; | ||
} | ||
|
||
const defaultProvider = new ethers.providers.JsonRpcProvider( | ||
config.country.defaultRPC | ||
); | ||
|
||
const EmptyAddress = "0x0000000000000000000000000000000000000000"; | ||
|
||
class DcClient { | ||
private logger: Logger; | ||
private provider: | ||
| ethers.providers.Web3Provider | ||
| ethers.providers.JsonRpcProvider; | ||
private contractReadOnly: ethers.Contract; | ||
|
||
constructor({ | ||
provider, | ||
}: // address, | ||
{ | ||
provider: ethers.providers.Web3Provider | ethers.providers.JsonRpcProvider; | ||
}) { | ||
|
||
if (!provider) { | ||
throw new Error("Provider is required"); | ||
} | ||
this.provider = provider; | ||
|
||
this.contractReadOnly = new ethers.Contract( | ||
config.country.contract, | ||
DCv2Abi, | ||
defaultProvider | ||
); | ||
this.logger = pino({ | ||
name: 'DcClient', | ||
transport: { | ||
target: 'pino-pretty', | ||
options: { | ||
colorize: true | ||
} | ||
} | ||
}) | ||
this.logger.info(`DC Contract ${config.country.contract} initialized`) | ||
} | ||
|
||
async duration() { | ||
return this.contractReadOnly.duration(); | ||
} | ||
|
||
async getPrice({ name }: { name: string }): Promise<DomainPrice> { | ||
const price = await this.contractReadOnly.getPrice(name); | ||
const amount = price.toString(); | ||
return { | ||
amount, | ||
formatted: web3Utils.fromWei(amount), | ||
}; | ||
} | ||
|
||
async getRecord({ name }: { name: string }): Promise<DomainRecord> { | ||
if (!name) { | ||
throw new Error("name is empty"); | ||
} | ||
let lastPrice = "0", | ||
url = "", | ||
prev = "", | ||
next = ""; | ||
|
||
const [ownerAddress, rentTime, expirationTime] = await Promise.all([ | ||
this.contractReadOnly.ownerOf(name).catch(() => ""), | ||
this.contractReadOnly.duration(), | ||
this.contractReadOnly.nameExpires(name), | ||
]); | ||
return { | ||
renter: | ||
!ownerAddress || ownerAddress === EmptyAddress ? null : ownerAddress, | ||
rentTime: rentTime.toNumber() * 1000, | ||
expirationTime: expirationTime.toNumber() * 1000, | ||
lastPrice: { | ||
amount: lastPrice, | ||
formatted: web3Utils.fromWei(lastPrice), | ||
}, | ||
url, | ||
prev, | ||
next, | ||
}; | ||
} | ||
|
||
async checkNameExpired({ name }: { name: string }): Promise<SendNameExpired> { | ||
const nameExpires = await this.contractReadOnly.nameExpires('fegloff.country'); | ||
const epochSecondsDec = parseInt(nameExpires); | ||
const expirationDate = new Date(epochSecondsDec * 1000); | ||
const currentDate = new Date(); | ||
const timeDifferenceMs = currentDate.getTime() - expirationDate.getTime(); | ||
const daysDifference = Math.abs( | ||
Math.ceil(timeDifferenceMs / (1000 * 60 * 60 * 24)) | ||
); | ||
const currentTimestamp = Math.floor(Date.now() / 1000); | ||
return { | ||
isExpired: | ||
epochSecondsDec === 0 ? false : currentTimestamp > epochSecondsDec, | ||
expirationDate: parseInt(nameExpires), | ||
isInGracePeriod: daysDifference <= 7, | ||
}; | ||
} | ||
|
||
async checkAvailable({ name }: { name: string }) { | ||
const isAvailable = await this.contractReadOnly.available(name); | ||
return isAvailable?.toString()?.toLowerCase() === "true"; | ||
} | ||
} | ||
|
||
export const dcClient = new DcClient({ provider: defaultProvider }); | ||
|
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.