Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds test for token payment flow and soroban helpers #947

Merged
merged 4 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions @shared/helpers/soroban/__tests__/server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
Memo,
MemoType,
Operation,
Server,
Transaction,
TransactionBuilder,
} from "soroban-client";
import { simulateTx } from "../server";

export const FUTURENET_DETAILS = {
network: "FUTURENET",
networkUrl: "https://horizon-futurenet.stellar.org",
networkPassphrase: "Test SDF Future Network ; October 2022",
};

describe("Soroban Helpers - ", () => {
describe("simulateTx", () => {
const TEST_XDR =
"AAAAAgAAAACM6IR9GHiRoVVAO78JJNksy2fKDQNs2jBn8bacsRLcrAAAAGQAALDTAAAAmQAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAGAAAAAAAAAACAAAAEgAAAAGvUaDMj6075hfTiVH7DPAwLD7vh/GD+dlkZfp6o9gqdgAAAA8AAAAGc3ltYm9sAAAAAAAAAAAAAAAAAAA=";
const testTx = TransactionBuilder.fromXDR(
TEST_XDR,
FUTURENET_DETAILS.networkPassphrase,
) as Transaction<Memo<MemoType>, Operation[]>;
const mockSimResult = {
auth: [],
xdr: "AAAAAwAAAAA=",
};
const mockSim = jest.fn(
(_tx: Transaction<Memo<MemoType>, Operation[]>) => ({
results: [mockSimResult],
}),
);
const mockServer = ({
simulateTransaction: mockSim,
} as any) as Server;

test("should take tx/server and return a native type according to the generic type argument", async () => {
const result = await simulateTx<number>(testTx, mockServer);
expect(typeof result).toEqual("number");
});
});
});
47 changes: 46 additions & 1 deletion extension/src/helpers/__tests__/stellar.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { getTransactionInfo, truncatedPublicKey } from "../stellar";
import BigNumber from "bignumber.js";
import {
getTransactionInfo,
truncatedPublicKey,
stroopToXlm,
xlmToStroop,
} from "../stellar";
import * as urls from "../urls";

describe("truncatedPublicKey", () => {
Expand Down Expand Up @@ -46,3 +52,42 @@ describe("getTransactionInfo", () => {
}
});
});

describe("stroopToXlm", () => {
test("should convert a raw string representing stroops into the equivalent value in lumens", () => {
const stroops = "10000001";
const lumens = stroopToXlm(stroops);

expect(lumens).toEqual(new BigNumber(Number(stroops) / 1e7));
});

test("should convert a raw number representing stroops into the equivalent value in lumens", () => {
const stroops = 10000001;
const lumens = stroopToXlm(stroops);

expect(lumens).toEqual(new BigNumber(Number(stroops) / 1e7));
});

test("should convert a BigNumber representing stroops into the equivalent value in lumens", () => {
const stroops = new BigNumber("10000001");
const lumens = stroopToXlm(stroops);

expect(lumens).toEqual(stroops.dividedBy(1e7));
});
});

describe("xlmToStroop", () => {
test("should convert a raw string representing a value in lumens to its equivalent value in stroops", () => {
const lumens = "11";
const stroops = xlmToStroop(lumens);

expect(stroops).toEqual(new BigNumber(Math.round(Number(lumens) * 1e7)));
});

test("should convert a BigNumber representing a value in lumens to its equivalent value in stroops", () => {
const lumens = new BigNumber("11");
const stroops = xlmToStroop(lumens);

expect(stroops).toEqual(lumens.times(1e7));
});
});
65 changes: 59 additions & 6 deletions extension/src/popup/__testHelpers__/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import { Provider } from "react-redux";
import * as SorobanClient from "soroban-client";
import BigNumber from "bignumber.js";
import { createMemoryHistory } from "history";
import {
Expand All @@ -8,7 +9,8 @@ import {
getDefaultMiddleware,
} from "@reduxjs/toolkit";
import { APPLICATION_STATE } from "@shared/constants/applicationState";
import { ActionStatus, Balances } from "@shared/api/types";
import { Balances } from "@shared/api/types";
import { FUTURENET_NETWORK_DETAILS } from "@shared/constants/stellar";

import { isSerializable } from "helpers/stellar";
import { reducer as auth } from "popup/ducks/accountServices";
Expand All @@ -17,7 +19,11 @@ import {
reducer as transactionSubmission,
initialState as transactionSubmissionInitialState,
} from "popup/ducks/transactionSubmission";
import { initialState as sorobanInitialState } from "popup/ducks/soroban";
import { reducer as soroban } from "popup/ducks/soroban";
import { SorobanContext } from "../SorobanContext";

const publicKey = "GA4UFF2WJM7KHHG4R5D5D2MZQ6FWMDOSVITVF7C5OLD5NFP6RBBW2FGV";

const rootReducer = combineReducers({
auth,
Expand All @@ -41,6 +47,35 @@ const makeDummyStore = (state: any) =>
],
});

const MockSorobanProvider = ({
children,
pubKey,
}: {
children: React.ReactNode;
pubKey: string;
}) => {
const server = new SorobanClient.Server(
FUTURENET_NETWORK_DETAILS.networkUrl,
{
allowHttp: FUTURENET_NETWORK_DETAILS.networkUrl.startsWith("http://"),
},
);

const newTxBuilder = async (fee = SorobanClient.BASE_FEE) => {
const sourceAccount = new SorobanClient.Account(pubKey, "0");
return new SorobanClient.TransactionBuilder(sourceAccount, {
fee,
networkPassphrase: FUTURENET_NETWORK_DETAILS.networkPassphrase,
});
};

return (
<SorobanContext.Provider value={{ server, newTxBuilder }}>
{children}
</SorobanContext.Provider>
);
};

export const Wrapper: React.FunctionComponent<any> = ({
children,
state,
Expand All @@ -63,14 +98,13 @@ export const Wrapper: React.FunctionComponent<any> = ({
applicationState: APPLICATION_STATE.MNEMONIC_PHRASE_CONFIRMED,
},
transactionSubmission: transactionSubmissionInitialState,
soroban: {
getTokenBalancesStatus: ActionStatus.IDLE,
tokenBalances: [],
},
soroban: sorobanInitialState,
...state,
})}
>
{children}
<MockSorobanProvider pubKey={publicKey}>
{children}
</MockSorobanProvider>
</Provider>
</Router>
</>
Expand Down Expand Up @@ -99,6 +133,25 @@ export const mockBalances = {
subentryCount: 1,
};

export const mockTokenBalances = {
tokenBalances: [
{
contractId: "CCXVDIGMR6WTXZQX2OEVD6YM6AYCYPXPQ7YYH6OZMRS7U6VD3AVHNGBJ",
decimals: 0,
name: "Demo Token",
symbol: "DT",
total: new BigNumber(10),
},
],
};

export const mockTokenBalance = {
balance: 10,
decimals: 0,
name: "Demo Token",
symbol: "DT",
};

export const mockAccounts = [
{
hardwareWalletType: "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export const AccountAssets = ({
<ScamAssetIcon isScamAsset={isScamAsset} />
</div>
<div className="AccountAssets__copy-right">
<div className="asset-amount">
<div className="asset-amount" data-testid="asset-amount">
{formatAmount(amountVal)} <span>{amountUnit}</span>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion extension/src/popup/ducks/soroban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const getTokenBalances = createAsyncThunk<
}
});

const initialState = {
export const initialState = {
getTokenBalancesStatus: ActionStatus.IDLE,
tokenBalances: [] as TokenBalances,
};
Expand Down
Loading