From 5e95c82ec3d9955d819b0d256a76b01098eab672 Mon Sep 17 00:00:00 2001 From: William Freudenberger Date: Tue, 18 Jul 2023 10:05:08 +0200 Subject: [PATCH] feat: add runtime api calls (#1470) --- centrifuge-js/src/CentrifugeBase.ts | 53 ++++++++++++++++++++++++----- centrifuge-js/src/utils/web3.ts | 6 ++-- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/centrifuge-js/src/CentrifugeBase.ts b/centrifuge-js/src/CentrifugeBase.ts index 62e52e968a..9acc160a0e 100644 --- a/centrifuge-js/src/CentrifugeBase.ts +++ b/centrifuge-js/src/CentrifugeBase.ts @@ -2,7 +2,7 @@ import type { JsonRpcSigner, TransactionRequest } from '@ethersproject/providers import { ApiRx } from '@polkadot/api' import { AddressOrPair, SubmittableExtrinsic } from '@polkadot/api/types' import { SignedBlock } from '@polkadot/types/interfaces' -import { DefinitionRpc, ISubmittableResult, Signer } from '@polkadot/types/types' +import { DefinitionRpc, DefinitionsCall, ISubmittableResult, Signer } from '@polkadot/types/types' import { hexToBn } from '@polkadot/util' import { sortAddresses } from '@polkadot/util-crypto' import 'isomorphic-fetch' @@ -105,8 +105,12 @@ const parachainTypes = { KSM: 'KSM', AUSD: 'AUSD', ForeignAsset: 'u32', + Staking: 'StakingCurrency', }, }, + StakingCurrency: { + _enum: ['BlockRewards'], + }, } const parachainRpcMethods: Record> = { @@ -124,32 +128,65 @@ const parachainRpcMethods: Record> = { }, rewards: { listCurrencies: { - description: 'List reward currencies', + description: + 'List all reward currencies for the given domain and account. These currencies could be used as keys for the computeReward call', params: [ + { + name: 'domain', + type: 'RewardDomain', + }, { name: 'account_id', type: 'AccountId', }, ], - type: 'u128', + type: 'Vec', }, computeReward: { - description: 'Compute reward', + description: 'Compute the claimable reward for the given triplet of domain, currency and account', params: [ + { + name: 'domain', + type: 'RewardDomain', + }, { name: 'currency_id', - type: '(RewardDomain,CurrencyId)', + type: 'CurrencyId', }, { name: 'account_id', type: 'AccountId', }, ], - type: 'u128', + type: 'Option', }, }, } +const parachainRuntimeApi: DefinitionsCall = { + PoolsApi: [ + { + // Runtime API calls must be in snake case (as defined in rust) + // However, RPCs are usually in camel case + methods: { + tranche_token_prices: parachainRpcMethods.pools.trancheTokenPrices, + }, + version: 1, + }, + ], + RewardsApi: [ + { + // Runtime API calls must be in snake case (as defined in rust) + // However, RPCs are usually in camel case + methods: { + compute_reward: parachainRpcMethods.rewards.computeReward, + list_currencies: parachainRpcMethods.rewards.listCurrencies, + }, + version: 1, + }, + ], +} + type Events = ISubmittableResult['events'] const txCompletedEvents: Record> = {} @@ -459,11 +496,11 @@ export class CentrifugeBase { } getApi() { - return getPolkadotApi(this.parachainUrl, parachainTypes, parachainRpcMethods) + return getPolkadotApi(this.parachainUrl, parachainTypes, parachainRpcMethods, parachainRuntimeApi) } getApiPromise() { - return firstValueFrom(getPolkadotApi(this.parachainUrl, parachainTypes, parachainRpcMethods)) + return firstValueFrom(getPolkadotApi(this.parachainUrl, parachainTypes, parachainRpcMethods, parachainRuntimeApi)) } getRelayChainApi() { diff --git a/centrifuge-js/src/utils/web3.ts b/centrifuge-js/src/utils/web3.ts index 60b815cabf..4759efbd11 100644 --- a/centrifuge-js/src/utils/web3.ts +++ b/centrifuge-js/src/utils/web3.ts @@ -1,12 +1,13 @@ import { ApiRx, WsProvider } from '@polkadot/api' -import { DefinitionRpc, DefinitionRpcSub } from '@polkadot/types/types' +import { DefinitionRpc, DefinitionRpcSub, DefinitionsCall } from '@polkadot/types/types' import { Observable } from 'rxjs' const cached: { [key: string]: Observable } = {} export function getPolkadotApi( wsUrl: string, types?: any, - rpc?: Record> + rpc?: Record>, + runtime?: DefinitionsCall ) { return ( cached[wsUrl] || @@ -15,6 +16,7 @@ export function getPolkadotApi( // @ts-ignore types, rpc, + runtime, })) ) }