Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into feat/mercury-mvp
Browse files Browse the repository at this point in the history
  • Loading branch information
aristidesstaffieri committed Jan 9, 2024
2 parents 47c5452 + 57309b3 commit 4a7b525
Show file tree
Hide file tree
Showing 175 changed files with 7,165 additions and 3,437 deletions.
26 changes: 25 additions & 1 deletion @shared/api/helpers/stellarSdkServer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Horizon } from "stellar-sdk";
import { FeeBumpTransaction, Horizon, Transaction } from "stellar-sdk";

export const getIsAllowHttp = (networkUrl: string) =>
!networkUrl.includes("https");
Expand All @@ -7,3 +7,27 @@ export const stellarSdkServer = (networkUrl: string) =>
new Horizon.Server(networkUrl, {
allowHttp: getIsAllowHttp(networkUrl),
});

export const submitTx = async ({
server,
tx,
}: {
server: Horizon.Server;
tx: Transaction | FeeBumpTransaction;
}): Promise<any> => {
let submittedTx;

try {
submittedTx = await server.submitTransaction(tx);
} catch (e) {
if (e.response.status === 504) {
// in case of 504, keep retrying this tx until submission succeeds or we get a different error
// https://developers.stellar.org/api/errors/http-status-codes/horizon-specific/timeout
// https://developers.stellar.org/docs/encyclopedia/error-handling
return submitTx({ server, tx });
}
throw e;
}

return submittedTx;
};
122 changes: 121 additions & 1 deletion @shared/api/internal.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { SorobanRpc, Networks } from "stellar-sdk";
import { SorobanRpc, Networks, Horizon } from "stellar-sdk";
import BigNumber from "bignumber.js";
import { INDEXER_URL } from "@shared/constants/mercury";
import {
Account,
AccountBalancesInterface,
AccountHistoryInterface,
BalanceToMigrate,
Balances,
HorizonOperation,
MigratableAccount,
MigratedAccount,
Settings,
} from "./types";
import {
Expand Down Expand Up @@ -215,6 +218,21 @@ export const getMnemonicPhrase = async (): Promise<{
return response;
};

export const getMigratedMnemonicPhrase = async (): Promise<{
mnemonicPhrase: string;
}> => {
let response = { mnemonicPhrase: "" };

try {
response = await sendMessageToBackground({
type: SERVICE_TYPES.GET_MIGRATED_MNEMONIC_PHRASE,
});
} catch (e) {
console.error(e);
}
return response;
};

export const confirmMnemonicPhrase = async (
mnemonicPhraseToConfirm: string,
): Promise<{
Expand All @@ -237,6 +255,26 @@ export const confirmMnemonicPhrase = async (
return response;
};

export const confirmMigratedMnemonicPhrase = async (
mnemonicPhraseToConfirm: string,
): Promise<{
isCorrectPhrase: boolean;
}> => {
let response = {
isCorrectPhrase: false,
};

try {
response = await sendMessageToBackground({
mnemonicPhraseToConfirm,
type: SERVICE_TYPES.CONFIRM_MIGRATED_MNEMONIC_PHRASE,
});
} catch (e) {
console.error(e);
}
return response;
};

export const recoverAccount = async (
password: string,
recoverMnemonic: string,
Expand Down Expand Up @@ -297,6 +335,88 @@ export const confirmPassword = async (
return response;
};

export const getAccountInfo = async ({
publicKey,
networkDetails,
}: {
publicKey: string;
networkDetails: NetworkDetails;
}) => {
const { networkUrl } = networkDetails;

const server = new Horizon.Server(networkUrl);

let account;
let signerArr = { records: [] as Horizon.ServerApi.AccountRecord[] };

try {
account = await server.loadAccount(publicKey);
signerArr = await server.accounts().forSigner(publicKey).call();
} catch (e) {
console.error(e);
}

return {
account,
isSigner: signerArr.records.length > 1,
};
};

export const getMigratableAccounts = async () => {
let migratableAccounts: MigratableAccount[] = [];

try {
({ migratableAccounts } = await sendMessageToBackground({
type: SERVICE_TYPES.GET_MIGRATABLE_ACCOUNTS,
}));
} catch (e) {
console.error(e);
}

return { migratableAccounts };
};

export const migrateAccounts = async ({
balancesToMigrate,
isMergeSelected,
recommendedFee,
}: {
balancesToMigrate: BalanceToMigrate[];
isMergeSelected: boolean;
recommendedFee: string;
}): Promise<{
publicKey: string;
migratedAccounts: Array<MigratedAccount>;
allAccounts: Array<Account>;
hasPrivateKey: boolean;
error: string;
}> => {
let publicKey = "";
let migratedAccounts = [] as Array<MigratedAccount>;
let allAccounts = [] as Array<Account>;
let hasPrivateKey = false;
let error = "";

try {
({
migratedAccounts,
allAccounts,
publicKey,
hasPrivateKey,
error,
} = await sendMessageToBackground({
balancesToMigrate,
isMergeSelected,
recommendedFee,
type: SERVICE_TYPES.MIGRATE_ACCOUNTS,
}));
} catch (e) {
console.error(e);
}

return { migratedAccounts, allAccounts, publicKey, hasPrivateKey, error };
};

export const getAccountIndexerBalances = async (
publicKey: string,
networkDetails: NetworkDetails,
Expand Down
2 changes: 1 addition & 1 deletion @shared/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"@stellar/wallet-sdk": "v0.11.0-beta.1",
"bignumber.js": "^9.1.1",
"prettier": "^2.0.5",
"stellar-sdk": "11.0.0-beta.5",
"stellar-sdk": "^11.1.0",
"typescript": "~3.7.2",
"webextension-polyfill": "^0.10.0"
},
Expand Down
21 changes: 21 additions & 0 deletions @shared/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export interface UserInfo {
publicKey: string;
}

export type MigratableAccount = Account & { keyIdIndex: number };

export interface Response {
error: string;
messagedId: number;
Expand Down Expand Up @@ -52,6 +54,7 @@ export interface Response {
sorobanRpcUrl: string;
networksList: NetworkDetails[];
allAccounts: Array<Account>;
migratedAccounts: MigratedAccount[];
accountName: string;
assetCode: string;
assetCanonical: string;
Expand All @@ -72,6 +75,10 @@ export interface Response {
isAllowed: boolean;
userInfo: UserInfo;
allowList: string[];
migratableAccounts: MigratableAccount[];
balancesToMigrate: BalanceToMigrate[];
isMergeSelected: boolean;
recommendedFee: string;
}

export interface BlockedDomains {
Expand Down Expand Up @@ -191,6 +198,20 @@ export interface ErrorMessage {
response?: Horizon.HorizonApi.ErrorResponseData.TransactionFailed;
}

export interface BalanceToMigrate {
publicKey: string;
name: string;
minBalance: string;
xlmBalance: string;
trustlineBalances: Horizon.HorizonApi.BalanceLine[];
keyIdIndex: number;
}

export type MigratedAccount = BalanceToMigrate & {
newPublicKey: string;
isMigrated: boolean;
};

declare global {
interface Window {
freighter: boolean;
Expand Down
2 changes: 1 addition & 1 deletion @shared/constants/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"prettier": "@stellar/prettier-config",
"version": "1.0.0",
"dependencies": {
"stellar-sdk": "^11.0.0-beta.4",
"stellar-sdk": "^11.1.0",
"typescript": "~3.7.2"
},
"devDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions @shared/constants/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export enum SERVICE_TYPES {
UPDATE_ACCOUNT_NAME = "UPDATE_ACCOUNT_NAME",
GET_MNEMONIC_PHRASE = "GET_MNEMONIC_PHRASE",
CONFIRM_MNEMONIC_PHRASE = "CONFIRM_MNEMONIC_PHRASE",
CONFIRM_MIGRATED_MNEMONIC_PHRASE = "CONFIRM_MIGRATED_MNEMONIC_PHRASE",
RECOVER_ACCOUNT = "RECOVER_ACCOUNT",
CONFIRM_PASSWORD = "CONFIRM_PASSWORD",
REJECT_ACCESS = "REJECT_ACCESS",
Expand Down Expand Up @@ -41,6 +42,9 @@ export enum SERVICE_TYPES {
ADD_TOKEN_ID = "ADD_TOKEN_ID",
GET_TOKEN_IDS = "GET_TOKEN_IDS",
REMOVE_TOKEN_ID = "REMOVE_TOKEN_ID",
GET_MIGRATABLE_ACCOUNTS = "GET_MIGRATABLE_ACCOUNTS",
GET_MIGRATED_MNEMONIC_PHRASE = "GET_MIGRATED_MNEMONIC_PHRASE",
MIGRATE_ACCOUNTS = "MIGRATE_ACCOUNTS",
}

export enum EXTERNAL_SERVICE_TYPES {
Expand Down
2 changes: 2 additions & 0 deletions @shared/constants/stellar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,5 @@ export const DEFAULT_NETWORKS: Array<NetworkDetails> = [
MAINNET_NETWORK_DETAILS,
TESTNET_NETWORK_DETAILS,
];

export const BASE_RESERVE = 0.5 as const;
44 changes: 44 additions & 0 deletions @shared/helpers/migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { BigNumber } from "bignumber.js";

export const getMigrationFeeAmount = ({
recommendedFee,
trustlineBalancesLength,
isMergeSelected,
}: {
recommendedFee: string;

trustlineBalancesLength: number;
isMergeSelected: boolean;
}) => {
/* the number of operations needs to complete the migration:
- 1 op to send the balance. This is always required
- For trustline balance(s), 1 op each to send them
- If we're merging, 1 op each to remove trustlines
- Plus one more tx to merge
*/
const opCount =
1 +
(trustlineBalancesLength * (isMergeSelected ? 2 : 1) || 0) +
(isMergeSelected ? 1 : 0);

return new BigNumber(recommendedFee).times(opCount);
};

export const calculateSenderMinBalance = ({
minBalance,
recommendedFee,
trustlineBalancesLength,
isMergeSelected,
}: {
minBalance: string;
recommendedFee: string;
trustlineBalancesLength: number;
isMergeSelected: boolean;
}) =>
new BigNumber(minBalance).plus(
getMigrationFeeAmount({
recommendedFee,
trustlineBalancesLength,
isMergeSelected,
}),
);
3 changes: 2 additions & 1 deletion @shared/helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"prettier": "@stellar/prettier-config",
"version": "1.0.0",
"dependencies": {
"stellar-sdk": "11.0.0-beta.5",
"bignumber.js": "^9.1.1",
"stellar-sdk": "^11.1.0",
"typescript": "~3.7.2"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions config/jest/setupTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ process.env.INDEXER_URL = "http://localhost:3002/api/v1";

jest.mock("helpers/metrics", () => ({
registerHandler: () => {},
emitMetric: () => {},
}));

/* eslint-disable react/no-array-index-key */
Expand Down
36 changes: 36 additions & 0 deletions extension/CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
# Release notes - Wallets - Freighter-5.11.0

### Bug

Fix memo display in SignTransaction

### Story

\[Automated Tests\] Add tests for a Swap payment

\[Automated Tests\] Managing a trustline

\[TEST\] Add tests for different memos in SignTransaction

Disable account migration

# Release notes - Wallets - Freighter-5.10.0

### Bug

Freighter's testnet Soroban migration hit runtime error on new installs

\[Layout\] UI fixes

### Story

Add full screen support

Key migration

# Release notes - Wallets - Freighter-5.9.0

### Story

Upgrade stellar-sdk for Protocol 20

# Release notes - Wallets - Freighter-5.8.0

### Bug
Expand Down
Loading

0 comments on commit 4a7b525

Please sign in to comment.