Skip to content

Commit

Permalink
feat: add declarations xrc
Browse files Browse the repository at this point in the history
  • Loading branch information
wirapratamaz committed May 7, 2024
1 parent 3ca94b1 commit ccb1bf7
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/declarations/xrc/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type {
ActorSubclass,
HttpAgentOptions,
ActorConfig,
Agent,
} from "@dfinity/agent";
import type { Principal } from "@dfinity/principal";
import type { IDL } from "@dfinity/candid";

import { _SERVICE } from "./xrc.did";

export declare const idlFactory: IDL.InterfaceFactory;
export declare const canisterId: string;

export declare interface CreateActorOptions {
/**
* @see {@link Agent}
*/
agent?: Agent;
/**
* @see {@link HttpAgentOptions}
*/
agentOptions?: HttpAgentOptions;
/**
* @see {@link ActorConfig}
*/
actorOptions?: ActorConfig;
}

/**
* Intializes an {@link ActorSubclass}, configured with the provided SERVICE interface of a canister.
* @constructs {@link ActorSubClass}
* @param {string | Principal} canisterId - ID of the canister the {@link Actor} will talk to
* @param {CreateActorOptions} options - see {@link CreateActorOptions}
* @param {CreateActorOptions["agent"]} options.agent - a pre-configured agent you'd like to use. Supercedes agentOptions
* @param {CreateActorOptions["agentOptions"]} options.agentOptions - options to set up a new agent
* @see {@link HttpAgentOptions}
* @param {CreateActorOptions["actorOptions"]} options.actorOptions - options for the Actor
* @see {@link ActorConfig}
*/
export declare const createActor: (
canisterId: string | Principal,
options?: CreateActorOptions
) => ActorSubclass<_SERVICE>;

/**
* Intialized Actor using default settings, ready to talk to a canister using its candid interface
* @constructs {@link ActorSubClass}
*/
export declare const xrc: ActorSubclass<_SERVICE>;
37 changes: 37 additions & 0 deletions src/declarations/xrc/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Actor, HttpAgent } from "@dfinity/agent";

// Imports and re-exports candid interface
import { idlFactory } from "./xrc.did.js";
export { idlFactory } from "./xrc.did.js";

// CANISTER_ID is replaced by webpack based on node environment
export const canisterId = process.env.XRC_CANISTER_ID;

export const createActor = (canisterId, options = {}) => {
const agent = options.agent || new HttpAgent({ ...options.agentOptions });

if (options.agent && options.agentOptions) {
console.warn(
"Detected both agent and agentOptions passed to createActor. Ignoring agentOptions and proceeding with the provided agent."
);
}

// Fetch root key for certificate validation during development
if (process.env.DFX_NETWORK !== "ic") {
agent.fetchRootKey().catch((err) => {
console.warn(
"Unable to fetch root key. Check to ensure that your local replica is running"
);
console.error(err);
});
}

// Creates an actor with using the candid interface and the HttpAgent
return Actor.createActor(idlFactory, {
agent,
canisterId,
...options.actorOptions,
});
};

export const xrc = createActor(canisterId);
78 changes: 78 additions & 0 deletions src/declarations/xrc/xrc.did
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
type AssetClass = variant { Cryptocurrency; FiatCurrency };

type Asset = record {
symbol : text;
class : AssetClass;
};

// The parameters for the `get_exchange_rate` API call.
type GetExchangeRateRequest = record {
base_asset : Asset;
quote_asset : Asset;
// An optional timestamp to get the rate for a specific time period.
timestamp : opt nat64;
};

type ExchangeRateMetadata = record {
decimals : nat32;
base_asset_num_received_rates : nat64;
base_asset_num_queried_sources : nat64;
quote_asset_num_received_rates : nat64;
quote_asset_num_queried_sources : nat64;
standard_deviation : nat64;
};

type ExchangeRate = record {
base_asset : Asset;
quote_asset : Asset;
timestamp : nat64;
rate : nat64;
metadata : ExchangeRateMetadata;
};

type ExchangeRateError = variant {
// Returned when the base asset rates are not found from the exchanges HTTP outcalls.
CryptoBaseAssetNotFound : null;
// Returned when the quote asset rates are not found from the exchanges HTTP outcalls.
CryptoQuoteAssetNotFound : null;
// Returned when the stablecoin rates are not found from the exchanges HTTP outcalls needed for computing a crypto/fiat pair.
StablecoinRateNotFound : null;
// Returned when there are not enough stablecoin rates to determine the forex/USDT rate.
StablecoinRateTooFewRates : null;
// Returned when the stablecoin rate is zero.
StablecoinRateZeroRate : null;
// Returned when a rate for the provided forex asset could not be found at the provided timestamp.
ForexInvalidTimestamp : null;
// Returned when the forex base asset is found.
ForexBaseAssetNotFound : null;
// Returned when the forex quote asset is found.
ForexQuoteAssetNotFound : null;
// Returned when neither forex asset is found.
ForexAssetsNotFound : null;
// Returned when the caller is not the CMC and there are too many active requests.
RateLimited : null;
// Returned when the caller does not send enough cycles to make a request.
NotEnoughCycles : null;
// Returned when the canister fails to accept enough cycles.
FailedToAcceptCycles : null;
/// Returned if too many collected rates deviate substantially.
InconsistentRatesReceived : null;
// Until candid bug is fixed, new errors after launch will be placed here.
Other : record {
// The identifier for the error that occurred.
code : nat32;
// A description of the error that occurred.
description : text;
};
};

type GetExchangeRateResult = variant {
// Successfully retrieved the exchange rate from the cache or API calls.
Ok : ExchangeRate;
// Failed to retrieve the exchange rate due to invalid API calls, invalid timestamp, etc.
Err : opt ExchangeRateError;
};

service : {
"get_exchange_rate" : (GetExchangeRateRequest) -> (GetExchangeRateResult);
};
52 changes: 52 additions & 0 deletions src/declarations/xrc/xrc.did.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { Principal } from "@dfinity/principal";
import type { ActorMethod } from "@dfinity/agent";

export interface Asset {
class: AssetClass;
symbol: string;
}
export type AssetClass = { Cryptocurrency: null } | { FiatCurrency: null };
export interface ExchangeRate {
metadata: ExchangeRateMetadata;
rate: bigint;
timestamp: bigint;
quote_asset: Asset;
base_asset: Asset;
}
export type ExchangeRateError =
| { CryptoQuoteAssetNotFound: null }
| { FailedToAcceptCycles: null }
| { ForexBaseAssetNotFound: null }
| { CryptoBaseAssetNotFound: null }
| { StablecoinRateTooFewRates: null }
| { ForexAssetsNotFound: null }
| { InconsistentRatesReceived: null }
| { RateLimited: null }
| { StablecoinRateZeroRate: null }
| { Other: { code: number; description: string } }
| { ForexInvalidTimestamp: null }
| { NotEnoughCycles: null }
| { ForexQuoteAssetNotFound: null }
| { StablecoinRateNotFound: null };
export interface ExchangeRateMetadata {
decimals: number;
quote_asset_num_received_rates: bigint;
base_asset_num_received_rates: bigint;
base_asset_num_queried_sources: bigint;
standard_deviation: bigint;
quote_asset_num_queried_sources: bigint;
}
export interface GetExchangeRateRequest {
timestamp: [] | [bigint];
quote_asset: Asset;
base_asset: Asset;
}
export type GetExchangeRateResult =
| { Ok: ExchangeRate }
| { Err: [] | [ExchangeRateError] };
export interface _SERVICE {
get_exchange_rate: ActorMethod<
[GetExchangeRateRequest],
GetExchangeRateResult
>;
}
57 changes: 57 additions & 0 deletions src/declarations/xrc/xrc.did.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
export const idlFactory = ({ IDL }) => {
const AssetClass = IDL.Variant({
Cryptocurrency: IDL.Null,
FiatCurrency: IDL.Null,
});
const Asset = IDL.Record({ class: AssetClass, symbol: IDL.Text });
const GetExchangeRateRequest = IDL.Record({
timestamp: IDL.Opt(IDL.Nat64),
quote_asset: Asset,
base_asset: Asset,
});
const ExchangeRateMetadata = IDL.Record({
decimals: IDL.Nat32,
quote_asset_num_received_rates: IDL.Nat64,
base_asset_num_received_rates: IDL.Nat64,
base_asset_num_queried_sources: IDL.Nat64,
standard_deviation: IDL.Nat64,
quote_asset_num_queried_sources: IDL.Nat64,
});
const ExchangeRate = IDL.Record({
metadata: ExchangeRateMetadata,
rate: IDL.Nat64,
timestamp: IDL.Nat64,
quote_asset: Asset,
base_asset: Asset,
});
const ExchangeRateError = IDL.Variant({
CryptoQuoteAssetNotFound: IDL.Null,
FailedToAcceptCycles: IDL.Null,
ForexBaseAssetNotFound: IDL.Null,
CryptoBaseAssetNotFound: IDL.Null,
StablecoinRateTooFewRates: IDL.Null,
ForexAssetsNotFound: IDL.Null,
InconsistentRatesReceived: IDL.Null,
RateLimited: IDL.Null,
StablecoinRateZeroRate: IDL.Null,
Other: IDL.Record({ code: IDL.Nat32, description: IDL.Text }),
ForexInvalidTimestamp: IDL.Null,
NotEnoughCycles: IDL.Null,
ForexQuoteAssetNotFound: IDL.Null,
StablecoinRateNotFound: IDL.Null,
});
const GetExchangeRateResult = IDL.Variant({
Ok: ExchangeRate,
Err: IDL.Opt(ExchangeRateError),
});
return IDL.Service({
get_exchange_rate: IDL.Func(
[GetExchangeRateRequest],
[GetExchangeRateResult],
[]
),
});
};
export const init = ({ IDL }) => {
return [];
};

0 comments on commit ccb1bf7

Please sign in to comment.