Skip to content

Commit

Permalink
Add support for Xahau (alt XRPL Protocol) @ ripple family
Browse files Browse the repository at this point in the history
  • Loading branch information
WietseWind committed Apr 19, 2024
1 parent 55a322c commit a3203dd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
26 changes: 15 additions & 11 deletions libs/ledger-live-common/src/families/ripple/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ import { NEW_ACCOUNT_ERROR_MESSAGE } from "../bridge/js";

// TODO: Pick different RPC endpoint if on another XRPL Protocol based chain

const defaultEndpoint = () => getEnv("API_RIPPLE_RPC");

export const connectionTimeout = 30 * 1000; // default connectionTimeout is 2s and make the specs bot failed

const rippleUnit = getCryptoCurrencyById("ripple").units[0];

export const parseAPIValue = (value: string): BigNumber => parseCurrencyUnit(rippleUnit, value);

export const submit = async (signature: string): Promise<any> => {
export const submit = async (signature: string, endpoint?: string): Promise<any> => {
console.log('submit', signature, window, endpoint)
const res = await network({
method: "POST",
url: `${defaultEndpoint()}`,
url: endpoint || getEnv("API_RIPPLE_RPC"),
data: {
method: "submit",
params: [
Expand Down Expand Up @@ -49,11 +48,13 @@ type AccountInfo = {
export const getAccountInfo = async (
recipient: string,
current?: boolean,
endpoint?: string,
): Promise<AccountInfo> => {
console.log('getAccountInfo', recipient, current, endpoint)
const res = async () => {
const res = await network({
method: "POST",
url: `${defaultEndpoint()}`,
url: endpoint || getEnv("API_RIPPLE_RPC"),
data: {
method: "account_info",
params: [
Expand All @@ -79,11 +80,12 @@ export const getAccountInfo = async (
});
};

export const getServerInfo = async (endpointConfig?: string | null | undefined): Promise<any> => {
export const getServerInfo = async (endpoint?: string): Promise<any> => {
console.log('getServerInfo', endpoint)
const res = async () => {
const res = await network({
method: "POST",
url: endpointConfig ?? `${defaultEndpoint()}`,
url: endpoint || getEnv("API_RIPPLE_RPC"),
data: {
method: "server_info",
params: [
Expand All @@ -106,11 +108,12 @@ export const getServerInfo = async (endpointConfig?: string | null | undefined):
});
};

export const getTransactions = async (address: string, options: any | undefined): Promise<any> => {
export const getTransactions = async (address: string, options: any | undefined, endpoint?: string): Promise<any> => {
console.log('getTransactions', address, options, endpoint)
const res = async () => {
const res = await network({
method: "POST",
url: `${defaultEndpoint()}`,
url: endpoint || getEnv("API_RIPPLE_RPC"),
data: {
method: "account_tx",
params: [
Expand All @@ -134,11 +137,12 @@ export const getTransactions = async (address: string, options: any | undefined)
});
};

export default async function getLedgerIndex(): Promise<number> {
export default async function getLedgerIndex(endpoint?: string): Promise<number> {
console.log('getLedgerIndex', endpoint)
const res = async () => {
const ledgerResponse = await network({
method: "POST",
url: `${defaultEndpoint()}`,
url: endpoint || getEnv("API_RIPPLE_RPC"),
data: {
method: "ledger",
params: [
Expand Down
15 changes: 11 additions & 4 deletions libs/ledger-live-common/src/families/ripple/js-synchronization.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import BigNumber from "bignumber.js";
import { getAccountInfo, getServerInfo, getTransactions } from "./api";
import { GetAccountShape, makeScanAccounts, makeSync, mergeOps } from "../../bridge/jsHelpers";
import { AccountShapeInfo, GetAccountShape, makeScanAccounts, makeSync, mergeOps } from "../../bridge/jsHelpers";
import { encodeOperationId } from "../../operation";
import { Account, Operation } from "@ledgerhq/types-live";
import { encodeAccountId } from "../../account";
import { NEW_ACCOUNT_ERROR_MESSAGE } from "./bridge/js";
import { getEnv } from "@ledgerhq/live-env";
import { TxXRPL } from "./types.api";

const getEndpoint = (info: AccountShapeInfo) => {
return info.currency.id.toUpperCase() === 'XAHAU'
? getEnv('API_XAHAU_RPC')
: getEnv('API_RIPPLE_RPC')
}

const txToOperation =
(accountId: string, address: string) =>
({
Expand Down Expand Up @@ -67,7 +74,7 @@ const getAccountShape: GetAccountShape = async (info): Promise<Partial<Account>>
xpubOrAddress: address,
derivationMode,
});
const accountInfo = await getAccountInfo(address);
const accountInfo = await getAccountInfo(address, undefined, getEndpoint(info));

if (!accountInfo || accountInfo.error === NEW_ACCOUNT_ERROR_MESSAGE) {
return {
Expand All @@ -81,7 +88,7 @@ const getAccountShape: GetAccountShape = async (info): Promise<Partial<Account>>
};
}

const serverInfo = await getServerInfo();
const serverInfo = await getServerInfo(getEndpoint(info));

const oldOperations = initialAccount?.operations || [];
const startAt = oldOperations.length ? (oldOperations[0].blockHeight || 0) + 1 : 0;
Expand All @@ -99,7 +106,7 @@ const getAccountShape: GetAccountShape = async (info): Promise<Partial<Account>>
minLedgerVersion,
),
ledger_index_max: maxLedgerVersion,
})) || [];
}, getEndpoint(info))) || [];

const newOperations = filterOperations(newTransactions, accountId, address);

Expand Down

0 comments on commit a3203dd

Please sign in to comment.