This repository has been archived by the owner on Jul 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added LL platform types and logic (#1166)
* added ledgerLiveAPI hook * added serializers, converters and platform types * fixed tx serializer * fixed types * added bitcoin tx * feat(Platform): added SignedTransaction types * added paraswap -> eth dep Co-authored-by: Rémi Jarasson (Ext) <[email protected]>
- Loading branch information
Showing
8 changed files
with
330 additions
and
0 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
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,42 @@ | ||
// @flow | ||
import { useEffect, useRef, useCallback } from "react"; | ||
import { | ||
JSONRPCServer, | ||
JSONRPCServerAndClient, | ||
JSONRPCClient, | ||
} from "json-rpc-2.0"; | ||
|
||
type LedgerLiveAPIHandlers = {}; | ||
|
||
type SendFunc = (request: any) => void; | ||
|
||
export const useJSONRPCServer = ( | ||
handlers: LedgerLiveAPIHandlers, | ||
send: SendFunc | ||
) => { | ||
const serverRef: { current: null | typeof JSONRPCServerAndClient } = useRef( | ||
null | ||
); | ||
|
||
useEffect(() => { | ||
const server = new JSONRPCServerAndClient( | ||
new JSONRPCServer(), | ||
new JSONRPCClient(send) | ||
); | ||
|
||
const methodIds = Object.keys(handlers); | ||
methodIds.forEach((methodId: string) => { | ||
server.addMethod(methodId, handlers[methodId]); | ||
}); | ||
|
||
serverRef.current = server; | ||
}, [send, handlers]); | ||
|
||
const receive = useCallback(async (request: any) => { | ||
if (serverRef.current) { | ||
await serverRef.current.receiveAndSend(request); | ||
} | ||
}, []); | ||
|
||
return [receive]; | ||
}; |
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,35 @@ | ||
// @flow | ||
|
||
import type { Account, CryptoCurrency } from "../types"; | ||
import type { PlatformAccount, PlatformCurrency } from "./types"; | ||
|
||
export function accountToPlatformAccount(account: Account): PlatformAccount { | ||
return { | ||
id: account.id, | ||
name: account.name, | ||
address: account.freshAddress, | ||
currency: account.currency.id, | ||
balance: account.balance, | ||
spendableBalance: account.spendableBalance, | ||
blockHeight: account.blockHeight, | ||
lastSyncDate: account.lastSyncDate, | ||
}; | ||
} | ||
|
||
export function currencyToPlatformCurrency( | ||
currency: CryptoCurrency | ||
): PlatformCurrency { | ||
return { | ||
type: currency.type, | ||
id: currency.id, | ||
ticker: currency.ticker, | ||
name: currency.name, | ||
family: currency.family, | ||
color: currency.color, | ||
units: currency.units.map((unit) => ({ | ||
name: unit.name, | ||
code: unit.code, | ||
magnitude: unit.magnitude, | ||
})), | ||
}; | ||
} |
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,39 @@ | ||
// @flow | ||
import type { SignedOperationRaw } from "../types"; | ||
|
||
export type RawPlatformAccount = { | ||
id: string, | ||
name: string, | ||
address: string, | ||
currency: string, | ||
balance: string, | ||
spendableBalance: string, | ||
blockHeight: number, | ||
lastSyncDate: string, | ||
}; | ||
|
||
export interface RawPlatformTransactionCommon { | ||
amount: string; | ||
recipient: string; | ||
} | ||
|
||
export interface RawPlatformEthereumTransaction | ||
extends RawPlatformTransactionCommon { | ||
family: "ethereum"; | ||
nonce: ?number; | ||
data: ?string; | ||
gasPrice: ?string; | ||
gasLimit: ?string; | ||
} | ||
|
||
export interface RawPlatformBitcoinTransaction | ||
extends RawPlatformTransactionCommon { | ||
family: "bitcoin"; | ||
feePerByte: ?string; | ||
} | ||
|
||
export type RawPlatformTransaction = | ||
| RawPlatformEthereumTransaction | ||
| RawPlatformBitcoinTransaction; | ||
|
||
export type RawPlatformSignedTransaction = SignedOperationRaw; |
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,151 @@ | ||
// @flow | ||
|
||
import { fromSignedOperationRaw, toSignedOperationRaw } from "../transaction"; | ||
|
||
import type { | ||
RawPlatformAccount, | ||
RawPlatformTransaction, | ||
RawPlatformEthereumTransaction, | ||
RawPlatformBitcoinTransaction, | ||
RawPlatformSignedTransaction, | ||
} from "./rawTypes"; | ||
import type { | ||
PlatformAccount, | ||
PlatformTransaction, | ||
PlatformEthereumTransaction, | ||
PlatformBitcoinTransaction, | ||
PlatformSignedTransaction, | ||
} from "./types"; | ||
|
||
import { BigNumber } from "bignumber.js"; | ||
|
||
export function serializePlatformAccount( | ||
account: PlatformAccount | ||
): RawPlatformAccount { | ||
return { | ||
id: account.id, | ||
name: account.name, | ||
address: account.address, | ||
currency: account.currency, | ||
balance: account.balance.toString(), | ||
spendableBalance: account.spendableBalance.toString(), | ||
blockHeight: account.blockHeight, | ||
lastSyncDate: account.lastSyncDate.toString(), | ||
}; | ||
} | ||
|
||
export function deserializePlatformAccount( | ||
rawAccount: RawPlatformAccount | ||
): PlatformAccount { | ||
return { | ||
id: rawAccount.id, | ||
name: rawAccount.name, | ||
address: rawAccount.address, | ||
currency: rawAccount.currency, | ||
balance: new BigNumber(rawAccount.balance), | ||
spendableBalance: new BigNumber(rawAccount.spendableBalance), | ||
blockHeight: rawAccount.blockHeight, | ||
lastSyncDate: new Date(rawAccount.lastSyncDate), | ||
}; | ||
} | ||
|
||
export function serializePlatformEthereumTransaction( | ||
transaction: PlatformEthereumTransaction | ||
): RawPlatformEthereumTransaction { | ||
return { | ||
family: transaction.family, | ||
amount: transaction.amount.toString(), | ||
recipient: transaction.recipient, | ||
nonce: transaction.nonce, | ||
data: transaction.data ? transaction.data.toString() : undefined, | ||
gasPrice: transaction.gasPrice | ||
? transaction.gasPrice.toString() | ||
: undefined, | ||
gasLimit: transaction.gasLimit | ||
? transaction.gasLimit.toString() | ||
: undefined, | ||
}; | ||
} | ||
|
||
export function deserializePlatformEthereumTransaction( | ||
rawTransaction: RawPlatformEthereumTransaction | ||
): PlatformEthereumTransaction { | ||
return { | ||
family: rawTransaction.family, | ||
amount: new BigNumber(rawTransaction.amount), | ||
recipient: rawTransaction.recipient, | ||
nonce: rawTransaction.nonce, | ||
data: rawTransaction.data ? Buffer.from(rawTransaction.data) : undefined, | ||
gasPrice: rawTransaction.gasPrice | ||
? new BigNumber(rawTransaction.gasPrice) | ||
: undefined, | ||
gasLimit: rawTransaction.gasLimit | ||
? new BigNumber(rawTransaction.gasLimit) | ||
: undefined, | ||
}; | ||
} | ||
|
||
export function serializePlatformBitcoinTransaction( | ||
transaction: PlatformBitcoinTransaction | ||
): RawPlatformBitcoinTransaction { | ||
return { | ||
family: transaction.family, | ||
amount: transaction.amount.toString(), | ||
recipient: transaction.recipient, | ||
feePerByte: transaction.feePerByte | ||
? transaction.feePerByte.toString() | ||
: undefined, | ||
}; | ||
} | ||
|
||
export function deserializePlatformBitcoinTransaction( | ||
rawTransaction: RawPlatformBitcoinTransaction | ||
): PlatformBitcoinTransaction { | ||
return { | ||
family: rawTransaction.family, | ||
amount: new BigNumber(rawTransaction.amount), | ||
recipient: rawTransaction.recipient, | ||
feePerByte: rawTransaction.feePerByte | ||
? new BigNumber(rawTransaction.feePerByte) | ||
: undefined, | ||
}; | ||
} | ||
|
||
export function serializePlatformTransaction( | ||
transaction: PlatformTransaction | ||
): RawPlatformTransaction { | ||
switch (transaction.family) { | ||
case "ethereum": | ||
return serializePlatformEthereumTransaction(transaction); | ||
case "bitcoin": | ||
return serializePlatformBitcoinTransaction(transaction); | ||
default: | ||
throw new Error(`Can't serialize ${transaction.family} transactions`); | ||
} | ||
} | ||
|
||
export function deserializePlatformTransaction( | ||
rawTransaction: RawPlatformTransaction | ||
): PlatformTransaction { | ||
switch (rawTransaction.family) { | ||
case "ethereum": | ||
return deserializePlatformEthereumTransaction(rawTransaction); | ||
case "bitcoin": | ||
return deserializePlatformBitcoinTransaction(rawTransaction); | ||
default: | ||
throw new Error(`Can't deserialize transaction: family not supported`); | ||
} | ||
} | ||
|
||
export function serializePlatformSignedTransaction( | ||
signedTransaction: PlatformSignedTransaction | ||
): RawPlatformSignedTransaction { | ||
return toSignedOperationRaw(signedTransaction, true); | ||
} | ||
|
||
export function deserializePlatformSignedTransaction( | ||
rawSignedTransaction: RawPlatformSignedTransaction, | ||
accountId: string | ||
): PlatformSignedTransaction { | ||
return fromSignedOperationRaw(rawSignedTransaction, accountId); | ||
} |
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,56 @@ | ||
// @flow | ||
|
||
import type { BigNumber } from "bignumber.js"; | ||
|
||
import type { SignedOperation } from "../types"; | ||
|
||
export type PlatformAccount = { | ||
id: string, | ||
name: string, | ||
address: string, | ||
currency: string, | ||
balance: BigNumber, | ||
spendableBalance: BigNumber, | ||
blockHeight: number, | ||
lastSyncDate: Date, | ||
}; | ||
|
||
export type PlatformUnit = { | ||
name: string, | ||
code: string, | ||
magnitude: number, | ||
}; | ||
|
||
export type PlatformCurrency = { | ||
type: string, | ||
color: string, | ||
ticker: string, | ||
id: string, | ||
name: string, | ||
family: string, | ||
units: PlatformUnit[], | ||
}; | ||
|
||
export interface PlatformTransactionCommon { | ||
amount: BigNumber; | ||
recipient: string; | ||
} | ||
|
||
export interface PlatformEthereumTransaction extends PlatformTransactionCommon { | ||
family: "ethereum"; | ||
nonce: ?number; | ||
data: ?Buffer; | ||
gasPrice: ?BigNumber; | ||
gasLimit: ?BigNumber; | ||
} | ||
|
||
export interface PlatformBitcoinTransaction extends PlatformTransactionCommon { | ||
family: "bitcoin"; | ||
feePerByte: ?BigNumber; | ||
} | ||
|
||
export type PlatformTransaction = | ||
| PlatformEthereumTransaction | ||
| PlatformBitcoinTransaction; | ||
|
||
export type PlatformSignedTransaction = SignedOperation; |
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
c62f6c7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: