Skip to content

Commit

Permalink
feat: elrond integration
Browse files Browse the repository at this point in the history
  • Loading branch information
JunichiSugiura committed Mar 24, 2023
1 parent d924352 commit db2b58e
Show file tree
Hide file tree
Showing 26 changed files with 371 additions and 55 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-apes-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/wallet-api-core": minor
---

Support Elrond family
1 change: 1 addition & 0 deletions packages/core/src/families/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const FAMILIES = [
"polkadot",
"stellar",
"tron",
"elrond",
] as const;

export const schemaFamilies = z.enum(FAMILIES);
42 changes: 42 additions & 0 deletions packages/core/src/families/elrond/serializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import BigNumber from "bignumber.js";
import type { ElrondTransaction, RawElrondTransaction } from "./types";

export function serializeElrondTransaction({
family,
mode,
amount,
recipient,
fees,
data,
gasLimit,
}: ElrondTransaction): RawElrondTransaction {
return {
family,
amount: amount.toString(),
recipient,
mode,
fees: fees ? fees.toString() : undefined,
data: data ? data.toString("hex") : undefined,
gasLimit: gasLimit.toString(),
};
}

export function deserializeElrondTransaction({
family,
mode,
amount,
recipient,
fees,
data,
gasLimit,
}: RawElrondTransaction): ElrondTransaction {
return {
family,
mode,
amount: new BigNumber(amount),
recipient,
fees: fees ? new BigNumber(fees) : undefined,
data: data ? Buffer.from(data, "hex") : undefined,
gasLimit: new BigNumber(gasLimit),
};
}
22 changes: 22 additions & 0 deletions packages/core/src/families/elrond/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type BigNumber from "bignumber.js";
import type { z } from "zod";
import type { TransactionCommon } from "../index";
import type { schemaRawElrondTransaction } from "./validation";

export type ElrondTransactionMode =
| "send"
| "delegate"
| "reDelegateRewards"
| "unDelegate"
| "claimRewards"
| "withdraw";

export interface ElrondTransaction extends TransactionCommon {
readonly family: RawElrondTransaction["family"];
mode: ElrondTransactionMode;
data?: Buffer;
fees: BigNumber | null | undefined;
gasLimit: BigNumber;
}

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

export const schemaElrondTransactionMode = z.enum([
"send",
"delegate",
"reDelegateRewards",
"unDelegate",
"claimRewards",
"withdraw",
]);

export const schemaRawElrondTransaction = schemaTransactionCommon.extend({
family: z.literal(schemaFamilies.enum.elrond),
mode: schemaElrondTransactionMode,
fees: z.string().optional().nullable(),
data: z.string().optional(),
gasLimit: z.string(),
});
2 changes: 1 addition & 1 deletion packages/core/src/families/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export * from "./ripple/types";
export * from "./stellar/types";
export * from "./tezos/types";
export * from "./tron/types";

export * from "./elrond/types";
export * from "./common";
export * from "./serializer";
export * from "./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 @@ -38,6 +38,10 @@ import {
deserializeTronTransaction,
serializeTronTransaction,
} from "./tron/serializer";
import {
deserializeElrondTransaction,
serializeElrondTransaction,
} from "./elrond/serializer";

import type { RawTransaction, Transaction } from "./types";

Expand Down Expand Up @@ -70,6 +74,8 @@ export function serializeTransaction(transaction: Transaction): RawTransaction {
return serializeStellarTransaction(transaction);
case "tron":
return serializeTronTransaction(transaction);
case "elrond":
return serializeElrondTransaction(transaction);
default: {
const exhaustiveCheck: never = transaction; // https://www.typescriptlang.org/docs/handbook/2/narrowing.html#exhaustiveness-checking
return exhaustiveCheck;
Expand Down Expand Up @@ -109,6 +115,8 @@ export function deserializeTransaction(
return deserializeStellarTransaction(rawTransaction);
case "tron":
return deserializeTronTransaction(rawTransaction);
case "elrond":
return deserializeElrondTransaction(rawTransaction);
default: {
const exhaustiveCheck: never = rawTransaction; // https://www.typescriptlang.org/docs/handbook/2/narrowing.html#exhaustiveness-checking
return exhaustiveCheck;
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/families/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { RippleTransaction } from "./ripple/types";
import type { StellarTransaction } from "./stellar/types";
import type { TezosTransaction } from "./tezos/types";
import type { TronTransaction } from "./tron/types";
import type { ElrondTransaction } from "./elrond/types";
import type { schemaRawTransaction } from "./validation";

/**
Expand Down Expand Up @@ -63,4 +64,5 @@ export type Transaction =
| TezosTransaction
| PolkadotTransaction
| StellarTransaction
| TronTransaction;
| TronTransaction
| ElrondTransaction;
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,6 +9,7 @@ import { schemaRawRippleTransaction } from "./ripple/validation";
import { schemaRawStellarTransaction } from "./stellar/validation";
import { schemaRawTezosTransaction } from "./tezos/validation";
import { schemaRawTronTransaction } from "./tron/validation";
import { schemaRawElrondTransaction } from "./elrond/validation";

export const schemaRawTransaction = z.discriminatedUnion("family", [
schemaRawAlgorandTransaction,
Expand All @@ -21,4 +22,5 @@ export const schemaRawTransaction = z.discriminatedUnion("family", [
schemaRawStellarTransaction,
schemaRawTezosTransaction,
schemaRawTronTransaction,
schemaRawElrondTransaction,
]);
88 changes: 88 additions & 0 deletions packages/core/tests/serializers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {
TezosTransaction,
Transaction,
TronTransaction,
ElrondTransaction,
RawElrondTransaction,
} from "../src";

const date = new Date();
Expand Down Expand Up @@ -522,6 +524,52 @@ describe("serializers.ts", () => {
});
});

describe("elrond", () => {
const family = schemaFamilies.enum.elrond;

it("should succeed to serialize a elrond transaction with resource and duration", () => {
const transaction: ElrondTransaction = {
family,
mode: "send",
amount: new BigNumber(100),
recipient: "recipient",
fees: new BigNumber(0),
};
const serializedTransaction = serializeTransaction(transaction);

expect(serializedTransaction).toEqual({
family,
mode: "send",
fees: "0",
amount: "100",
recipient: "recipient",
gasLimit: undefined,
data: undefined,
});
});

it("should succeed to serialize a elrond transaction without resource and duration", () => {
const transaction: ElrondTransaction = {
family,
mode: "send",
amount: new BigNumber(100),
recipient: "recipient",
fees: new BigNumber(0),
};
const serializedTransaction = serializeTransaction(transaction);

expect(serializedTransaction).toEqual({
family,
mode: "send",
amount: "100",
recipient: "recipient",
fees: "0",
gasLimit: undefined,
data: undefined,
});
});
});

describe("deserializeTransaction", () => {
describe("ethereum", () => {
const family = schemaFamilies.enum.ethereum;
Expand Down Expand Up @@ -979,6 +1027,46 @@ describe("serializers.ts", () => {
});
});

describe("elrond", () => {
const family = schemaFamilies.enum.elrond;

it("should succeed to deserialize a elrond transaction with resource and duration", () => {
const serializedTransaction: RawElrondTransaction = {
family,
mode: "send",
amount: "100",
recipient: "recipient",
};

const transaction = deserializeTransaction(serializedTransaction);

expect(transaction).toEqual({
family,
mode: "send",
amount: new BigNumber(100),
recipient: "recipient",
});
});

it("should succeed to deserialize a elrond transaction without resource and duration", () => {
const serializedTransaction: RawElrondTransaction = {
family,
mode: "send",
amount: "100",
recipient: "recipient",
};

const transaction = deserializeTransaction(serializedTransaction);

expect(transaction).toEqual({
family,
mode: "send",
amount: new BigNumber(100),
recipient: "recipient",
});
});
});

describe("Serialize -> Deserialize flow", () => {
describe("Account", () => {
it("should not alter account", () => {
Expand Down

0 comments on commit db2b58e

Please sign in to comment.