1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

ve updates

This commit is contained in:
alexcos20 2023-02-09 11:38:57 +02:00
parent 8d6288f63b
commit abf0c9bbcc
4 changed files with 61 additions and 100 deletions

View File

@ -1,4 +1,5 @@
import { AbiItem } from 'web3-utils'
// import { AbiItem } from 'web3-utils'
import { ethers, Signer, Interface, InterfaceAbi, Contract } from 'ethers'
import veAllocateABI from '@oceanprotocol/contracts/artifacts/contracts/ve/veAllocate.sol/veAllocate.json'
import { calculateEstimatedGas, sendTx } from '../../utils'
import { SmartContractWithAddress } from '../SmartContractWithAddress'
@ -7,29 +8,26 @@ import { ReceiptOrEstimate } from '../../@types'
* Provides an interface for veOcean contract
*/
export class VeAllocate extends SmartContractWithAddress {
getDefaultAbi(): AbiItem | AbiItem[] {
return veAllocateABI.abi as AbiItem[]
getDefaultAbi() {
return veAllocateABI.abi
}
/**
* set a specific percentage of veOcean to a specific nft
* Maximum allocated percentage is 10000, so 1% is specified as 100
* @param {String} userAddress user address
* @param {String} amount Percentage used
* @param {String} nft NFT address to allocate to
* @param {String} chainId chainId of NFT
* @return {Promise<ReceiptOrEstimate>}
*/
public async setAllocation<G extends boolean = false>(
userAddress: string,
amount: string,
nft: string,
chainId: number,
estimateGas?: G
): Promise<ReceiptOrEstimate<G>> {
const estGas = await calculateEstimatedGas(
userAddress,
this.contract.methods.setAllocation,
this.contract.setAllocation,
amount,
nft,
chainId
@ -38,11 +36,10 @@ export class VeAllocate extends SmartContractWithAddress {
// Invoke function of the contract
const trxReceipt = await sendTx(
userAddress,
estGas + 1,
this.web3,
estGas + BigInt(1),
this.signer,
this.config?.gasFeeMultiplier,
this.contract.methods.setAllocation,
this.contract.setAllocation,
amount,
nft,
chainId
@ -53,22 +50,19 @@ export class VeAllocate extends SmartContractWithAddress {
/**
* set specific percetage of veOcean to multiple nfts
* Maximum allocated percentage is 10000, so 1% is specified as 100
* @param {String} userAddress user address
* @param {String[]} amount Array of percentages used
* @param {String[]} nft Array of NFT addresses
* @param {String[]} chainId Array of chainIds
* @return {Promise<ReceiptOrEstimate>}
*/
public async setBatchAllocation<G extends boolean = false>(
userAddress: string,
amount: string[],
nft: string[],
chainId: number[],
estimateGas?: G
): Promise<ReceiptOrEstimate<G>> {
const estGas = await calculateEstimatedGas(
userAddress,
this.contract.methods.setBatchAllocation,
this.contract.setBatchAllocation,
amount,
nft,
chainId
@ -77,11 +71,10 @@ export class VeAllocate extends SmartContractWithAddress {
// Invoke function of the contract
const trxReceipt = await sendTx(
userAddress,
estGas + 1,
this.web3,
estGas + BigInt(1),
this.signer,
this.config?.gasFeeMultiplier,
this.contract.methods.setBatchAllocation,
this.contract.setBatchAllocation,
amount,
nft,
chainId
@ -94,7 +87,7 @@ export class VeAllocate extends SmartContractWithAddress {
* @return {Promise<number>}
*/
public async getTotalAllocation(userAddress: string): Promise<number> {
const allocation = await this.contract.methods.getTotalAllocation(userAddress).call()
const allocation = await this.contract.getTotalAllocation(userAddress)
return allocation
}
@ -109,9 +102,7 @@ export class VeAllocate extends SmartContractWithAddress {
nft: string,
chainId: string
): Promise<number> {
const allocation = await this.contract.methods
.getveAllocation(userAddress, nft, chainId)
.call()
const allocation = await this.contract.getveAllocation(userAddress, nft, chainId)
return allocation
}
}

View File

@ -1,4 +1,5 @@
import { AbiItem } from 'web3-utils'
// import { AbiItem } from 'web3-utils'
import { ethers, InterfaceAbi, Interface } from 'ethers'
import veFeeABI from '@oceanprotocol/contracts/artifacts/contracts/ve/veFeeDistributor.vy/veFeeDistributor.json'
import { calculateEstimatedGas, sendTx } from '../../utils'
import { SmartContractWithAddress } from '../SmartContractWithAddress'
@ -7,8 +8,8 @@ import { ReceiptOrEstimate } from '../../@types'
* Provides an interface for veOcean contract
*/
export class VeFeeDistributor extends SmartContractWithAddress {
getDefaultAbi(): AbiItem | AbiItem[] {
return veFeeABI.abi as AbiItem[]
getDefaultAbi() {
return veFeeABI.abi
}
/**
@ -18,23 +19,20 @@ export class VeFeeDistributor extends SmartContractWithAddress {
may need to be called more than once to claim all available
fees. In the `Claimed` event that fires, if `claim_epoch` is
less than `max_epoch`, the account may claim again
* @param {String} userAddress user address
* @return {Promise<ReceiptOrEstimate>}
*/
public async claim<G extends boolean = false>(
userAddress: string,
estimateGas?: G
): Promise<ReceiptOrEstimate<G>> {
const estGas = await calculateEstimatedGas(userAddress, this.contract.methods.claim)
const estGas = await calculateEstimatedGas(this.contract.claim)
if (estimateGas) return <ReceiptOrEstimate<G>>estGas
// Invoke function of the contract
const trxReceipt = await sendTx(
userAddress,
estGas + 20000,
this.web3,
estGas + BigInt(20000),
this.signer,
this.config?.gasFeeMultiplier,
this.contract.methods.claim
this.contract.claim
)
return <ReceiptOrEstimate<G>>trxReceipt
}
@ -44,29 +42,22 @@ export class VeFeeDistributor extends SmartContractWithAddress {
Used to claim for many accounts at once, or to make
multiple claims for the same address when that address
has significant veOCEAN history
* @param {String} fromUserAddress user address that sends the tx
* @param {String} addresses array of addresses to claim
* @return {Promise<ReceiptOrEstimate>}
*/
public async claimMany<G extends boolean = false>(
fromUserAddress: string,
addresses: string[],
estimateGas?: G
): Promise<ReceiptOrEstimate<G>> {
const estGas = await calculateEstimatedGas(
fromUserAddress,
this.contract.methods.claim_many,
addresses
)
const estGas = await calculateEstimatedGas(this.contract.claim_many, addresses)
if (estimateGas) return <ReceiptOrEstimate<G>>estGas
// Invoke function of the contract
const trxReceipt = await sendTx(
fromUserAddress,
estGas + 20000,
this.web3,
estGas + BigInt(20000),
this.signer,
this.config?.gasFeeMultiplier,
this.contract.methods.claim_many,
this.contract.claim_many,
addresses
)
return <ReceiptOrEstimate<G>>trxReceipt

View File

@ -1,4 +1,5 @@
import { AbiItem } from 'web3-utils'
// import { AbiItem } from 'web3-utils'
import { ethers, InterfaceAbi, Interface } from 'ethers'
import veFeeEstimate from '@oceanprotocol/contracts/artifacts/contracts/ve/veFeeEstimate.vy/veFeeEstimate.json'
import { SmartContractWithAddress } from '../SmartContractWithAddress'
import { VeOcean } from './VeOcean'
@ -6,8 +7,8 @@ import { VeOcean } from './VeOcean'
* Provides an interface for veOcean contract
*/
export class VeFeeEstimate extends SmartContractWithAddress {
getDefaultAbi(): AbiItem | AbiItem[] {
return veFeeEstimate.abi as AbiItem[]
getDefaultAbi() {
return veFeeEstimate.abi
}
/**
@ -16,11 +17,8 @@ export class VeFeeEstimate extends SmartContractWithAddress {
* @return {Promise<string>}
*/
public async estimateClaim(userAddress: string): Promise<string> {
const amount = await this.contract.methods.estimateClaim(userAddress).call()
const veOcean = new VeOcean(
await this.contract.methods.voting_escrow().call(),
this.web3
)
const amount = await this.contract.estimateClaim(userAddress)
const veOcean = new VeOcean(await this.contract.voting_escrow(), this.signer)
const amountFormated = await this.unitsToAmount(await veOcean.getToken(), amount)
return amountFormated
}

View File

@ -1,4 +1,5 @@
import { AbiItem } from 'web3-utils'
// import { AbiItem } from 'web3-utils'
import { ethers, Interface, InterfaceAbi } from 'ethers'
import veOceanABI from '@oceanprotocol/contracts/artifacts/contracts/ve/veOCEAN.vy/veOCEAN.json'
import { calculateEstimatedGas, sendTx } from '../../utils'
import { SmartContractWithAddress } from '../SmartContractWithAddress'
@ -7,27 +8,24 @@ import { ReceiptOrEstimate } from '../../@types'
* Provides an interface for veOcean contract
*/
export class VeOcean extends SmartContractWithAddress {
getDefaultAbi(): AbiItem | AbiItem[] {
return veOceanABI.abi as AbiItem[]
getDefaultAbi() {
return veOceanABI.abi
}
/**
* Deposit `amount` tokens for `userAddress` and lock until `unlockTime`
* @param {String} userAddress user address
* @param {String} amount Amount of tokens to be locked
* @param {Number} unlockTime Timestamp for unlock
* @return {Promise<ReceiptOrEstimate>}
*/
public async lockTokens<G extends boolean = false>(
userAddress: string,
amount: string,
unlockTime: number,
estimateGas?: G
): Promise<ReceiptOrEstimate<G>> {
const amountFormatted = await this.amountToUnits(await this.getToken(), amount)
const estGas = await calculateEstimatedGas(
userAddress,
this.contract.methods.create_lock,
this.contract.create_lock,
amountFormatted,
unlockTime
)
@ -35,11 +33,10 @@ export class VeOcean extends SmartContractWithAddress {
// Invoke function of the contract
const trxReceipt = await sendTx(
userAddress,
estGas + 20000, // sometimes, it's not enough
this.web3,
estGas + BigInt(20000),
this.signer,
this.config?.gasFeeMultiplier,
this.contract.methods.create_lock,
this.contract.create_lock,
amountFormatted,
unlockTime
)
@ -55,15 +52,13 @@ export class VeOcean extends SmartContractWithAddress {
* @return {Promise<ReceiptOrEstimate>}
*/
public async depositFor<G extends boolean = false>(
fromUserAddress: string,
toAddress: string,
amount: string,
estimateGas?: G
): Promise<ReceiptOrEstimate<G>> {
const amountFormatted = await this.amountToUnits(await this.getToken(), amount)
const estGas = await calculateEstimatedGas(
fromUserAddress,
this.contract.methods.deposit_for,
this.contract.deposit_for,
toAddress,
amountFormatted
)
@ -71,11 +66,10 @@ export class VeOcean extends SmartContractWithAddress {
// Invoke function of the contract
const trxReceipt = await sendTx(
fromUserAddress,
estGas + 20000, // sometimes, it's not enough
this.web3,
estGas + BigInt(20000),
this.signer,
this.config?.gasFeeMultiplier,
this.contract.methods.deposit_for,
this.contract.deposit_for,
toAddress,
amountFormatted
)
@ -84,30 +78,26 @@ export class VeOcean extends SmartContractWithAddress {
/**
* Deposit `amount` additional tokens for `userAddress` without modifying the unlock time
* @param {String} userAddress user address that sends the tx
* @param {String} amount Amount of tokens to be locked
* @return {Promise<ReceiptOrEstimate>}
*/
public async increaseAmount<G extends boolean = false>(
userAddress: string,
amount: string,
estimateGas?: G
): Promise<ReceiptOrEstimate<G>> {
const amountFormatted = await this.amountToUnits(await this.getToken(), amount)
const estGas = await calculateEstimatedGas(
userAddress,
this.contract.methods.increase_amount,
this.contract.increase_amount,
amountFormatted
)
if (estimateGas) return <ReceiptOrEstimate<G>>estGas
// Invoke function of the contract
const trxReceipt = await sendTx(
userAddress,
estGas + 20000, // sometimes, it's not enough
this.web3,
estGas + BigInt(20000),
this.signer,
this.config?.gasFeeMultiplier,
this.contract.methods.increase_amount,
this.contract.increase_amount,
amountFormatted
)
return <ReceiptOrEstimate<G>>trxReceipt
@ -120,24 +110,21 @@ export class VeOcean extends SmartContractWithAddress {
* @return {Promise<ReceiptOrEstimate>}
*/
public async increaseUnlockTime<G extends boolean = false>(
userAddress: string,
unlockTime: number,
estimateGas?: G
): Promise<ReceiptOrEstimate<G>> {
const estGas = await calculateEstimatedGas(
userAddress,
this.contract.methods.increase_unlock_time,
this.contract.increase_unlock_time,
unlockTime
)
if (estimateGas) return <ReceiptOrEstimate<G>>estGas
// Invoke function of the contract
const trxReceipt = await sendTx(
userAddress,
estGas + 20000, // sometimes, it's not enough
this.web3,
estGas + BigInt(20000),
this.signer,
this.config?.gasFeeMultiplier,
this.contract.methods.increase_unlock_time,
this.contract.increase_unlock_time,
unlockTime
)
return <ReceiptOrEstimate<G>>trxReceipt
@ -145,26 +132,20 @@ export class VeOcean extends SmartContractWithAddress {
/**
* Withdraw all tokens for `userAddress`
* @param {String} userAddress user address that sends the tx
* @return {Promise<ReceiptOrEstimate>}
*/
public async withdraw<G extends boolean = false>(
userAddress: string,
estimateGas?: G
): Promise<ReceiptOrEstimate<G>> {
const estGas = await calculateEstimatedGas(
userAddress,
this.contract.methods.withdraw
)
const estGas = await calculateEstimatedGas(this.contract.withdraw)
if (estimateGas) return <ReceiptOrEstimate<G>>estGas
// Invoke function of the contract
const trxReceipt = await sendTx(
userAddress,
estGas + 1,
this.web3,
estGas + BigInt(1),
this.signer,
this.config?.gasFeeMultiplier,
this.contract.methods.withdraw
this.contract.withdraw
)
return <ReceiptOrEstimate<G>>trxReceipt
}
@ -174,7 +155,7 @@ export class VeOcean extends SmartContractWithAddress {
* @return {Promise<number>}
*/
public async getVotingPower(userAddress: string): Promise<number> {
const balance = await this.contract.methods.balanceOf(userAddress).call()
const balance = await this.contract.balanceOf(userAddress)
return balance
}
@ -183,7 +164,7 @@ export class VeOcean extends SmartContractWithAddress {
* @return {Promise<string>}
*/
public async getLockedAmount(userAddress: string): Promise<string> {
const balance = await this.contract.methods.locked(userAddress).call()
const balance = await this.contract.locked(userAddress)
const balanceFormated = await this.unitsToAmount(
await this.getToken(),
balance.amount
@ -197,7 +178,7 @@ export class VeOcean extends SmartContractWithAddress {
* @return {Promise<number>}
*/
public async lockEnd(userAddress: string): Promise<number> {
const untilLock = await this.contract.methods.locked__end(userAddress).call()
const untilLock = await this.contract.locked__end(userAddress)
return untilLock
}
@ -207,7 +188,7 @@ export class VeOcean extends SmartContractWithAddress {
public async totalSupply(): Promise<string> {
const supplyFormated = await this.unitsToAmount(
await this.getToken(),
await this.contract.methods.totalSupply().call()
await this.contract.totalSupply()
)
return supplyFormated
}
@ -216,7 +197,7 @@ export class VeOcean extends SmartContractWithAddress {
* @return {Promise<string>}
*/
public async getToken(): Promise<string> {
const tokenAddress = await this.contract.methods.token().call()
const tokenAddress = await this.contract.token()
return tokenAddress
}
}