-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
46f0f99
commit ba7a41b
Showing
8 changed files
with
141 additions
and
12 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
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
114 changes: 114 additions & 0 deletions
114
libs/ledger-live-common/src/families/evm/bridge/mock.ts
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,114 @@ | ||
import { BigNumber } from "bignumber.js"; | ||
import { NotEnoughBalance, RecipientRequired } from "@ledgerhq/errors"; | ||
import type { Transaction } from "@ledgerhq/coin-evm/types/index"; | ||
import type { AccountBridge, CurrencyBridge } from "@ledgerhq/types-live"; | ||
import { getMainAccount } from "../../../account"; | ||
import { | ||
scanAccounts, | ||
signOperation, | ||
broadcast, | ||
sync, | ||
makeAccountBridgeReceive, | ||
} from "../../../bridge/mockHelpers"; | ||
import { defaultUpdateTransaction } from "@ledgerhq/coin-framework/bridge/jsHelpers"; | ||
import { getGasLimit } from "@ledgerhq/coin-evm/logic"; | ||
import { getTypedTransaction } from "@ledgerhq/coin-evm/transaction"; | ||
const receive = makeAccountBridgeReceive(); | ||
|
||
const defaultGetFees = (_a, t: any) => (t.gasPrice || new BigNumber(0)).times(getGasLimit(t)); | ||
|
||
const createTransaction = (): Transaction => ({ | ||
family: "evm", | ||
mode: "send", | ||
amount: new BigNumber(10000000000), | ||
nonce: 0, | ||
recipient: "", | ||
gasPrice: new BigNumber(10000000000), | ||
gasLimit: new BigNumber(21000), | ||
chainId: 2222, | ||
useAllAmount: false, | ||
subAccountId: null, | ||
}); | ||
|
||
const estimateMaxSpendable = ({ account, parentAccount, transaction }) => { | ||
const mainAccount = getMainAccount(account, parentAccount); | ||
const estimatedFees = parentAccount | ||
? new BigNumber(0) | ||
: transaction | ||
? defaultGetFees(mainAccount, transaction) | ||
: new BigNumber(1000000000000); | ||
return Promise.resolve(BigNumber.max(0, account.balance.minus(estimatedFees))); | ||
}; | ||
|
||
const getTransactionStatus = (a, t) => { | ||
const errors: { | ||
amount?: Error; | ||
recipient?: Error; | ||
} = {}; | ||
const warnings: { | ||
feeTooHigh?: Error; | ||
gasLimit?: Error; | ||
} = {}; | ||
const tokenAccount = !t.subAccountId | ||
? null | ||
: a.subAccounts && a.subAccounts.find(ta => ta.id === t.subAccountId); | ||
const account = tokenAccount || a; | ||
const useAllAmount = !!t.useAllAmount; | ||
const estimatedFees = defaultGetFees(a, t); | ||
const totalSpent = useAllAmount | ||
? account.balance | ||
: tokenAccount | ||
? new BigNumber(t.amount) | ||
: new BigNumber(t.amount).plus(estimatedFees); | ||
const amount = useAllAmount | ||
? tokenAccount | ||
? new BigNumber(t.amount) | ||
: account.balance.minus(estimatedFees) | ||
: new BigNumber(t.amount); | ||
|
||
// Fill up transaction errors... | ||
if (totalSpent.gt(account.balance)) { | ||
errors.amount = new NotEnoughBalance(); | ||
} | ||
if (!t.recipient) { | ||
errors.recipient = new RecipientRequired(""); | ||
} | ||
return Promise.resolve({ | ||
errors, | ||
warnings, | ||
estimatedFees, | ||
amount, | ||
totalSpent, | ||
}); | ||
}; | ||
|
||
const prepareTransaction = async (_a, t) => { | ||
const typedTransaction = getTypedTransaction(t, { | ||
gasPrice: new BigNumber(50), | ||
maxFeePerGas: new BigNumber(50), | ||
maxPriorityFeePerGas: new BigNumber(50), | ||
nextBaseFee: new BigNumber(50), | ||
}); | ||
return typedTransaction; | ||
}; | ||
|
||
const accountBridge: AccountBridge<Transaction> = { | ||
createTransaction, | ||
updateTransaction: defaultUpdateTransaction, | ||
getTransactionStatus, | ||
estimateMaxSpendable, | ||
prepareTransaction, | ||
sync, | ||
receive, | ||
signOperation, | ||
broadcast, | ||
}; | ||
const currencyBridge: CurrencyBridge = { | ||
preload: () => Promise.resolve({}), | ||
hydrate: () => {}, | ||
scanAccounts, | ||
}; | ||
export default { | ||
currencyBridge, | ||
accountBridge, | ||
}; |
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