Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PlatformAPI] GetDeposits / Fix GetDepositOffers #50

Merged
merged 1 commit into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions examples/platformvm/getAllDepositOffers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { Avalanche } from "caminojs/index"
import {
KeyChain,
PlatformVMAPI,
GetAllDepositOffersResponse
} from "caminojs/apis/platformvm"
import { KeyChain, PlatformVMAPI } from "caminojs/apis/platformvm"
import { ExamplesConfig } from "../common/examplesConfig"
import { DefaultLocalGenesisPrivateKey, PrivateKeyPrefix } from "caminojs/utils"

Expand Down Expand Up @@ -33,9 +29,9 @@ const main = async (): Promise<any> => {
await InitAvalanche()

const active = true
const response: GetAllDepositOffersResponse =
await pchain.getAllDepositOffers(active)
console.log(response.depositOffers)
const depositOffers = await pchain.getAllDepositOffers(active)
console.log(depositOffers)
console.log("start: ", depositOffers[0].start.toNumber())
}

main()
46 changes: 46 additions & 0 deletions examples/platformvm/getDeposits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Avalanche } from "caminojs/index"
import {
GetUTXOsResponse,
KeyChain,
PlatformVMAPI,
UTXOSet
} from "caminojs/apis/platformvm"
import { ExamplesConfig } from "../common/examplesConfig"
import { DefaultLocalGenesisPrivateKey, PrivateKeyPrefix } from "caminojs/utils"

const config: ExamplesConfig = require("../common/examplesConfig.json")
const avalanche: Avalanche = new Avalanche(
config.host,
config.port,
config.protocol,
config.networkID
)

const privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}`

let pchain: PlatformVMAPI
let pKeychain: KeyChain
let pAddressStrings: string[]

const InitAvalanche = async () => {
await avalanche.fetchNetworkSettings()
pchain = avalanche.PChain()
pKeychain = pchain.keyChain()
pKeychain.importKey(privKey)
pAddressStrings = pchain.keyChain().getAddressStrings()
}

const main = async (): Promise<any> => {
await InitAvalanche()

const platformVMUTXOResponse: GetUTXOsResponse = await pchain.getUTXOs(
pAddressStrings
)
const utxoSet: UTXOSet = platformVMUTXOResponse.utxos
const txIDs = utxoSet.getLockedTxIDs()

const deposits = await pchain.getDeposits(txIDs.depositIDs)
console.log(deposits)
}

main()
58 changes: 53 additions & 5 deletions src/apis/platformvm/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ import {
DelegationFeeError
} from "../../utils/errors"
import {
APIDeposit,
BalanceDict,
DepositOffer,
GetCurrentValidatorsParams,
GetPendingValidatorsParams,
GetRewardUTXOsParams,
Expand Down Expand Up @@ -73,7 +75,9 @@ import {
GetClaimablesParams,
GetClaimablesResponse,
GetAllDepositOffersParams,
GetAllDepositOffersResponse
GetAllDepositOffersResponse,
GetDepositsParams,
GetDepositsResponse
} from "./interfaces"
import { TransferableInput } from "./inputs"
import { TransferableOutput } from "./outputs"
Expand Down Expand Up @@ -586,17 +590,61 @@ export class PlatformVMAPI extends JRPCAPI {
*
* @returns Promise for a list containing deposit offers.
*/
getAllDepositOffers = async (
active: boolean
): Promise<GetAllDepositOffersResponse> => {
getAllDepositOffers = async (active: boolean): Promise<DepositOffer[]> => {
const params: GetAllDepositOffersParams = {
active
}
const response: RequestResponseData = await this.callMethod(
"platform.getAllDepositOffers",
params
)
return response.data.result as GetAllDepositOffersResponse

const offers: GetAllDepositOffersResponse = response.data.result
return offers.depositOffers.map((offer) => {
return {
id: offer.id,
interestRateNominator: new BN(offer.interestRateNominator),
start: new BN(offer.start),
end: new BN(offer.end),
minAmount: new BN(offer.minAmount),
minDuration: offer.minDuration,
maxDuration: offer.maxDuration,
unlockPeriodDuration: offer.unlockPeriodDuration,
noRewardsPeriodDuration: offer.noRewardsPeriodDuration,
memo: offer.memo,
flags: new BN(offer.flags)
} as DepositOffer
})
}

/**
* Returns deposits coressponding to requested txIDs.
*
* @param depositTxIDs A list of txIDs (cb58) to request deposits for.
*
* @returns Promise for a list containing deposits.
*/
getDeposits = async (depositTxIDs: string[]): Promise<APIDeposit[]> => {
const params: GetDepositsParams = {
depositTxIDs
}
const response: RequestResponseData = await this.callMethod(
"platform.getDeposits",
params
)

const deposits: GetDepositsResponse = response.data.result
return deposits.deposits.map((deposit) => {
return {
depositTxID: deposit.depositTxID,
depositOfferID: deposit.depositOfferID,
unlockedAmount: new BN(deposit.unlockedAmount),
claimedRewardAmount: new BN(deposit.claimedRewardAmount),
start: new BN(deposit.start),
duration: deposit.duration,
amount: new BN(deposit.amount)
} as APIDeposit
})
}

/**
Expand Down
42 changes: 30 additions & 12 deletions src/apis/platformvm/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,18 +259,36 @@ export interface GetAllDepositOffersResponse {
depositOffers: DepositOffer[]
}

interface DepositOffer {
ID: string
InterestRateNominator: number
Start: number
End: number
MinAmount: number
MinDuration: number
MaxDuration: number
UnlockPeriodDuration: number
NoRewardsPeriodDuration: number
Memo: string
Flags: number
export interface DepositOffer {
id: string
interestRateNominator: BN
start: BN
end: BN
minAmount: BN
minDuration: number
maxDuration: number
unlockPeriodDuration: number
noRewardsPeriodDuration: number
memo: string
flags: BN
}

export interface GetDepositsParams {
depositTxIDs: string[]
}

export interface GetDepositsResponse {
deposits: APIDeposit[]
}

export interface APIDeposit {
depositTxID: string
depositOfferID: string
unlockedAmount: BN
claimedRewardAmount: BN
start: BN
duration: number
amount: BN
}

export interface GetMaxStakeAmountParams {
Expand Down