-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[XRP] Expose functions for Alpaca (#7160)
* feat: separate core logic from bridge logic Signed-off-by: Stéphane Prohaszka <[email protected]> * feat: move files to correct sub dir Signed-off-by: Stéphane Prohaszka <[email protected]> * feat: reorganize module Signed-off-by: Stéphane Prohaszka <[email protected]> * feat: update Polkadot Alpaca api and add consistency with Ripple Signed-off-by: Stéphane Prohaszka <[email protected]> * fix: missing export Signed-off-by: Stéphane Prohaszka <[email protected]> * feat: returns operations content Signed-off-by: Stéphane Prohaszka <[email protected]> * fix: unimported Signed-off-by: Stéphane Prohaszka <[email protected]> * chore: cleanup Signed-off-by: Stéphane Prohaszka <[email protected]> * chore: changeset Signed-off-by: Stéphane Prohaszka <[email protected]> * fix: xrp dataset import Signed-off-by: Stéphane Prohaszka <[email protected]> * feat: add unit test to core Signed-off-by: Stéphane Prohaszka <[email protected]> * feat: add more unit test Signed-off-by: Stéphane Prohaszka <[email protected]> --------- Signed-off-by: Stéphane Prohaszka <[email protected]>
- Loading branch information
1 parent
f6cea5f
commit b14d37d
Showing
65 changed files
with
1,170 additions
and
392 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@ledgerhq/coin-polkadot": minor | ||
"@ledgerhq/coin-xrp": minor | ||
"@ledgerhq/live-common": minor | ||
--- | ||
|
||
Expose API on CoinModule (Xrp and Polkadot) so they can be used in a dedicated service |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { PolkadotConfig, setCoinConfig } from "../config"; | ||
import { | ||
broadcast, | ||
craftEstimationTransaction, | ||
craftTransaction, | ||
defaultExtrinsicArg, | ||
estimateFees, | ||
getBalance, | ||
listOperations, | ||
type Operation, | ||
} from "../logic"; | ||
|
||
export type Api = { | ||
broadcast: (tx: string) => Promise<string>; | ||
combine: (tx: string, signature: string, pubkey: string) => string; | ||
craftTransaction: ( | ||
address: string, | ||
transaction: { | ||
recipient: string; | ||
amount: bigint; | ||
fee: bigint; | ||
}, | ||
) => Promise<string>; | ||
estimateFees: (addr: string, amount: bigint) => Promise<bigint>; | ||
getBalance: (address: string) => Promise<bigint>; | ||
listOperations: (address: string, blockHeight: number) => Promise<Operation[]>; | ||
}; | ||
export function createApi(config: PolkadotConfig): Api { | ||
setCoinConfig(() => ({ ...config, status: { type: "active" } })); | ||
|
||
return { | ||
broadcast, | ||
combine: () => { | ||
throw new Error("UnsupportedMethod"); | ||
}, | ||
craftTransaction: craft, | ||
estimateFees: estimate, | ||
getBalance, | ||
listOperations, | ||
}; | ||
} | ||
|
||
async function craft( | ||
address: string, | ||
transaction: { | ||
recipient: string; | ||
amount: bigint; | ||
}, | ||
): Promise<string> { | ||
const extrinsicArg = defaultExtrinsicArg(transaction.amount, transaction.recipient); | ||
//TODO: Retrieve correctly the nonce via a call to the node `await api.rpc.system.accountNextIndex(address)` | ||
const nonce = -1; | ||
const tx = await craftTransaction(address, nonce, extrinsicArg); | ||
const extrinsic = tx.registry.createType("Extrinsic", tx.unsigned, { | ||
version: tx.unsigned.version, | ||
}); | ||
return extrinsic.toHex(); | ||
} | ||
|
||
async function estimate(addr: string, amount: bigint): Promise<bigint> { | ||
const tx = await craftEstimationTransaction(addr, amount); | ||
return estimateFees(tx); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import polkadotAPI from "../network"; | ||
import { loadPolkadotCrypto } from "./polkadot-crypto"; | ||
|
||
export default async function broadcast(signature: string): Promise<string> { | ||
export async function broadcast(signature: string): Promise<string> { | ||
await loadPolkadotCrypto(); | ||
return await polkadotAPI.submitExtrinsic(signature); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import network from "../network"; | ||
|
||
export async function getBalance(addr: string): Promise<bigint> { | ||
const balances = await network.getBalances(addr); | ||
return BigInt(balances.balance.toString()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
libs/coin-modules/coin-polkadot/src/logic/listOperations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import network from "../network"; | ||
import { PolkadotOperation } from "../types"; | ||
|
||
export type Operation = { | ||
hash: string; | ||
address: string; | ||
type: string; | ||
value: bigint; | ||
fee: bigint; | ||
blockHeight: number; | ||
senders: string[]; | ||
recipients: string[]; | ||
date: Date; | ||
transactionSequenceNumber: number; | ||
}; | ||
|
||
export async function listOperations(addr: string): Promise<Operation[]> { | ||
//The accountId is used to map Operations to Live types. | ||
const fakeAccountId = ""; | ||
const operations = await network.getOperations(fakeAccountId, addr); | ||
|
||
return operations.map(convertToCoreOperation(addr)); | ||
} | ||
|
||
const convertToCoreOperation = (address: string) => (operation: PolkadotOperation) => { | ||
const { | ||
hash, | ||
type, | ||
value, | ||
fee, | ||
blockHeight, | ||
senders, | ||
recipients, | ||
date, | ||
transactionSequenceNumber, | ||
} = operation; | ||
return { | ||
hash, | ||
address, | ||
type, | ||
value: BigInt(value.toString()), | ||
fee: BigInt(fee.toString()), | ||
blockHeight: blockHeight ?? 0, | ||
senders, | ||
recipients, | ||
date, | ||
transactionSequenceNumber: transactionSequenceNumber ?? 0, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ | ||
/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */ | ||
// `workerThreads: true` is required for validating object with `bigint` values | ||
module.exports = { | ||
preset: "ts-jest", | ||
testEnvironment: "node", | ||
testPathIgnorePatterns: ["lib/", "lib-es/"], | ||
testPathIgnorePatterns: ["lib/", "lib-es/", ".*\\.integ\\.test\\.[tj]s"], | ||
workerThreads: true, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */ | ||
module.exports = { | ||
preset: "ts-jest", | ||
testEnvironment: "node", | ||
testRegex: ".integ.test.ts$", | ||
testPathIgnorePatterns: ["lib/", "lib-es/"], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.