Skip to content

Commit

Permalink
feat: integrate filecoin family
Browse files Browse the repository at this point in the history
  • Loading branch information
Chelsea Sanders committed Mar 17, 2023
1 parent d924352 commit a44a66a
Show file tree
Hide file tree
Showing 26 changed files with 414 additions and 54 deletions.
6 changes: 6 additions & 0 deletions .changeset/tiny-dolls-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@ledgerhq/wallet-api-core": minor
"wallet-api-website": minor
---

Integrate Filecoin family
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ lib/
lib-es/

# eslint cache
.eslintcache
.eslintcache

# ide
.idea
7 changes: 7 additions & 0 deletions packages/core/src/families/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@ export const FAMILIES = [
"cosmos",
"ripple",
"cosmos",
"filecoin",
"tezos",
"polkadot",
"stellar",
"tron",
] as const;

export const schemaFamilies = z.enum(FAMILIES);

export const FEES_STRATEGY = ["slow", "medium", "fast", "custom"] as const;

export const schemaFeesStrategy = z.enum(FEES_STRATEGY).optional().nullish();

export type FeesStrategy = z.infer<typeof schemaFeesStrategy>;
70 changes: 70 additions & 0 deletions packages/core/src/families/filecoin/serializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import BigNumber from "bignumber.js";
import type { FilecoinTransaction, RawFilecoinTransaction } from "./types";

export function serializeFilecoinTransaction({
amount,
data = undefined,
family,
feesStrategy = null,
method,
nonce,
params = undefined,
gasLimit,
gasFeeCap,
gasPremium,
recipient,
subAccountId = undefined,
useAllAmount = undefined,
version,
}: FilecoinTransaction): RawFilecoinTransaction {
return {
amount: amount.toString(),
data: data ? data.toString("hex") : undefined,
family,
feesStrategy,
gasLimit: gasLimit.toNumber(),
gasFeeCap: gasFeeCap.toString(),
gasPremium: gasPremium.toString(),
method,
nonce,
params,
recipient,
subAccountId,
useAllAmount: useAllAmount || undefined,
version,
};
}

export function deserializeFilecoinTransaction({
amount,
data = undefined,
family,
feesStrategy = null,
method,
nonce,
params = undefined,
gasLimit,
gasFeeCap,
gasPremium,
recipient,
subAccountId = undefined,
useAllAmount = undefined,
version,
}: RawFilecoinTransaction): FilecoinTransaction {
return {
amount: new BigNumber(amount),
data: data ? Buffer.from(data, "hex") : undefined,
family,
feesStrategy,
gasLimit: new BigNumber(gasLimit),
gasFeeCap: new BigNumber(gasFeeCap),
gasPremium: new BigNumber(gasPremium),
nonce,
method,
params,
recipient,
subAccountId,
useAllAmount,
version,
};
}
23 changes: 23 additions & 0 deletions packages/core/src/families/filecoin/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type BigNumber from "bignumber.js";
import type { z } from "zod";
import type { FeesStrategy, TransactionCommon } from "../index";
import type { schemaRawFilecoinTransaction } from "./validation";

export interface FilecoinTransaction extends TransactionCommon {
readonly family: RawFilecoinTransaction["family"];
useAllAmount?: boolean;
nonce: number;
data?: Buffer;
method: number;
version: number;
params?: string;
gasLimit: BigNumber;
gasFeeCap: BigNumber;
gasPremium: BigNumber;
subAccountId?: string | null;
feesStrategy: FeesStrategy;
}

export type RawFilecoinTransaction = z.infer<
typeof schemaRawFilecoinTransaction
>;
21 changes: 21 additions & 0 deletions packages/core/src/families/filecoin/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { z } from "zod";
import {
schemaFamilies,
schemaFeesStrategy,
schemaTransactionCommon,
} from "../common";

export const schemaRawFilecoinTransaction = schemaTransactionCommon.extend({
data: z.string().optional(),
family: z.literal(schemaFamilies.enum.filecoin),
feesStrategy: schemaFeesStrategy,
gasLimit: z.number(),
gasFeeCap: z.string(),
gasPremium: z.string(),
method: z.number(),
nonce: z.number(),
params: z.string().optional(),
subAccountId: z.string().optional().nullish(),
useAllAmount: z.boolean().optional(),
version: z.number(),
});
1 change: 1 addition & 0 deletions packages/core/src/families/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from "./bitcoin/types";
export * from "./cosmos/types";
export * from "./crypto_org/types";
export * from "./ethereum/types";
export * from "./filecoin/types";
export * from "./polkadot/types";
export * from "./ripple/types";
export * from "./stellar/types";
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/families/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import {
deserializeEthereumTransaction,
serializeEthereumTransaction,
} from "./ethereum/serializer";
import {
deserializeFilecoinTransaction,
serializeFilecoinTransaction,
} from "./filecoin/serializer";
import {
deserializePolkadotTransaction,
serializePolkadotTransaction,
Expand Down Expand Up @@ -62,6 +66,8 @@ export function serializeTransaction(transaction: Transaction): RawTransaction {
return serializeRippleTransaction(transaction);
case "cosmos":
return serializeCosmosTransaction(transaction);
case "filecoin":
return serializeFilecoinTransaction(transaction);
case "tezos":
return serializeTezosTransaction(transaction);
case "polkadot":
Expand Down Expand Up @@ -101,6 +107,8 @@ export function deserializeTransaction(
return deserializeRippleTransaction(rawTransaction);
case "cosmos":
return deserializeCosmosTransaction(rawTransaction);
case "filecoin":
return deserializeFilecoinTransaction(rawTransaction);
case "tezos":
return deserializeTezosTransaction(rawTransaction);
case "polkadot":
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/families/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { schemaFamilies } from "./common";
import type { CosmosTransaction } from "./cosmos/types";
import type { CryptoOrgTransaction } from "./crypto_org/types";
import type { EthereumTransaction } from "./ethereum/types";
import type { FilecoinTransaction } from "./filecoin/types";
import type { PolkadotTransaction } from "./polkadot/types";
import type { RippleTransaction } from "./ripple/types";
import type { StellarTransaction } from "./stellar/types";
Expand Down Expand Up @@ -58,6 +59,7 @@ export type Transaction =
| BitcoinTransaction
| AlgorandTransaction
| CryptoOrgTransaction
| FilecoinTransaction
| RippleTransaction
| CosmosTransaction
| TezosTransaction
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/families/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import { schemaRawRippleTransaction } from "./ripple/validation";
import { schemaRawStellarTransaction } from "./stellar/validation";
import { schemaRawTezosTransaction } from "./tezos/validation";
import { schemaRawTronTransaction } from "./tron/validation";
import { schemaRawFilecoinTransaction } from "./filecoin/validation";

export const schemaRawTransaction = z.discriminatedUnion("family", [
schemaRawAlgorandTransaction,
schemaRawBitcoinTransaction,
schemaRawCosmosTransaction,
schemaRawCryptoOrgTransaction,
schemaRawEthereumTransaction,
schemaRawFilecoinTransaction,
schemaRawPolkadotTransaction,
schemaRawRippleTransaction,
schemaRawStellarTransaction,
Expand Down

0 comments on commit a44a66a

Please sign in to comment.