Skip to content
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
2 changes: 1 addition & 1 deletion packages/tron-wallet-snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/internal-snaps.git"
},
"source": {
"shasum": "IlS8YzpfJun/5aMb6cYAIzKBcn3x9BjOMVYJUE9zFzo=",
"shasum": "glaFBmbkBLS+Kyce06loy6aDIpNwrx6Zyn2uxz0RUrI=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
86 changes: 45 additions & 41 deletions packages/tron-wallet-snap/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,70 +50,74 @@ export const FALLBACK_CREATE_ACCOUNT_FEE_SUN = 100_000;
*/
export const FALLBACK_CREATE_NEW_ACCOUNT_FEE_SUN = 1_000_000;

export enum Network {
Mainnet = 'tron:728126428',
Nile = 'tron:3448148188',
Shasta = 'tron:2494104990',
}
export const Network = {
Mainnet: 'tron:728126428',
Nile: 'tron:3448148188',
Shasta: 'tron:2494104990',
} as const;

export type Network = (typeof Network)[keyof typeof Network];

export enum KnownCaip19Id {
TrxMainnet = `${Network.Mainnet}/slip44:195`,
TrxNile = `${Network.Nile}/slip44:195`,
TrxShasta = `${Network.Shasta}/slip44:195`,
export const KnownCaip19Id = {
TrxMainnet: `${Network.Mainnet}/slip44:195`,
TrxNile: `${Network.Nile}/slip44:195`,
TrxShasta: `${Network.Shasta}/slip44:195`,

UsdtMainnet = `${Network.Mainnet}/trc20:TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t`,
UsdtMainnet: `${Network.Mainnet}/trc20:TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t`,

/**
* Staked Tron
*/
TrxStakedForBandwidthMainnet = `${Network.Mainnet}/slip44:195-staked-for-bandwidth`,
TrxStakedForBandwidthNile = `${Network.Nile}/slip44:195-staked-for-bandwidth`,
TrxStakedForBandwidthShasta = `${Network.Shasta}/slip44:195-staked-for-bandwidth`,
TrxStakedForBandwidthMainnet: `${Network.Mainnet}/slip44:195-staked-for-bandwidth`,
TrxStakedForBandwidthNile: `${Network.Nile}/slip44:195-staked-for-bandwidth`,
TrxStakedForBandwidthShasta: `${Network.Shasta}/slip44:195-staked-for-bandwidth`,

TrxStakedForEnergyMainnet = `${Network.Mainnet}/slip44:195-staked-for-energy`,
TrxStakedForEnergyNile = `${Network.Nile}/slip44:195-staked-for-energy`,
TrxStakedForEnergyShasta = `${Network.Shasta}/slip44:195-staked-for-energy`,
TrxStakedForEnergyMainnet: `${Network.Mainnet}/slip44:195-staked-for-energy`,
TrxStakedForEnergyNile: `${Network.Nile}/slip44:195-staked-for-energy`,
TrxStakedForEnergyShasta: `${Network.Shasta}/slip44:195-staked-for-energy`,

/**
* TRX Ready for Withdrawal (unstaked TRX that has completed the withdrawal period)
*/
TrxReadyForWithdrawalMainnet = `${Network.Mainnet}/slip44:195-ready-for-withdrawal`,
TrxReadyForWithdrawalNile = `${Network.Nile}/slip44:195-ready-for-withdrawal`,
TrxReadyForWithdrawalShasta = `${Network.Shasta}/slip44:195-ready-for-withdrawal`,
TrxReadyForWithdrawalMainnet: `${Network.Mainnet}/slip44:195-ready-for-withdrawal`,
TrxReadyForWithdrawalNile: `${Network.Nile}/slip44:195-ready-for-withdrawal`,
TrxReadyForWithdrawalShasta: `${Network.Shasta}/slip44:195-ready-for-withdrawal`,

/**
* Staking Rewards TRX
*/
TrxStakingRewardsMainnet = `${Network.Mainnet}/slip44:195-staking-rewards`,
TrxStakingRewardsNile = `${Network.Nile}/slip44:195-staking-rewards`,
TrxStakingRewardsShasta = `${Network.Shasta}/slip44:195-staking-rewards`,
TrxStakingRewardsMainnet: `${Network.Mainnet}/slip44:195-staking-rewards`,
TrxStakingRewardsNile: `${Network.Nile}/slip44:195-staking-rewards`,
TrxStakingRewardsShasta: `${Network.Shasta}/slip44:195-staking-rewards`,

/**
* TRX in Lock Period (unstaked but waiting for lock period to end)
*/
TrxInLockPeriodMainnet = `${Network.Mainnet}/slip44:195-in-lock-period`,
TrxInLockPeriodNile = `${Network.Nile}/slip44:195-in-lock-period`,
TrxInLockPeriodShasta = `${Network.Shasta}/slip44:195-in-lock-period`,
TrxInLockPeriodMainnet: `${Network.Mainnet}/slip44:195-in-lock-period`,
TrxInLockPeriodNile: `${Network.Nile}/slip44:195-in-lock-period`,
TrxInLockPeriodShasta: `${Network.Shasta}/slip44:195-in-lock-period`,

/**
* Tron Resource Assets
*/
EnergyMainnet = `${Network.Mainnet}/slip44:energy`,
EnergyNile = `${Network.Nile}/slip44:energy`,
EnergyShasta = `${Network.Shasta}/slip44:energy`,

MaximumEnergyMainnet = `${Network.Mainnet}/slip44:maximum-energy`,
MaximumEnergyNile = `${Network.Nile}/slip44:maximum-energy`,
MaximumEnergyShasta = `${Network.Shasta}/slip44:maximum-energy`,

BandwidthMainnet = `${Network.Mainnet}/slip44:bandwidth`,
BandwidthNile = `${Network.Nile}/slip44:bandwidth`,
BandwidthShasta = `${Network.Shasta}/slip44:bandwidth`,

MaximumBandwidthMainnet = `${Network.Mainnet}/slip44:maximum-bandwidth`,
MaximumBandwidthNile = `${Network.Nile}/slip44:maximum-bandwidth`,
MaximumBandwidthShasta = `${Network.Shasta}/slip44:maximum-bandwidth`,
}
EnergyMainnet: `${Network.Mainnet}/slip44:energy`,
EnergyNile: `${Network.Nile}/slip44:energy`,
EnergyShasta: `${Network.Shasta}/slip44:energy`,

MaximumEnergyMainnet: `${Network.Mainnet}/slip44:maximum-energy`,
MaximumEnergyNile: `${Network.Nile}/slip44:maximum-energy`,
MaximumEnergyShasta: `${Network.Shasta}/slip44:maximum-energy`,

BandwidthMainnet: `${Network.Mainnet}/slip44:bandwidth`,
BandwidthNile: `${Network.Nile}/slip44:bandwidth`,
BandwidthShasta: `${Network.Shasta}/slip44:bandwidth`,

MaximumBandwidthMainnet: `${Network.Mainnet}/slip44:maximum-bandwidth`,
MaximumBandwidthNile: `${Network.Nile}/slip44:maximum-bandwidth`,
MaximumBandwidthShasta: `${Network.Shasta}/slip44:maximum-bandwidth`,
} as const;

export type KnownCaip19Id = (typeof KnownCaip19Id)[keyof typeof KnownCaip19Id];

export const TRX_METADATA = {
fungible: true as const,
Expand Down
27 changes: 14 additions & 13 deletions packages/tron-wallet-snap/src/entities/assets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { KnownCaip19Id, Network } from '../constants';
import { KnownCaip19Id } from '../constants';
import type { Network } from '../constants';
import type {
InLockPeriodCaipAssetType,
NativeCaipAssetType,
Expand Down Expand Up @@ -41,22 +42,22 @@ export type InLockPeriodAsset = BaseAsset & {

export type ResourceAsset = BaseAsset & {
assetType:
| KnownCaip19Id.EnergyMainnet
| KnownCaip19Id.EnergyNile
| KnownCaip19Id.EnergyShasta
| KnownCaip19Id.BandwidthMainnet
| KnownCaip19Id.BandwidthNile
| KnownCaip19Id.BandwidthShasta;
| typeof KnownCaip19Id.EnergyMainnet
| typeof KnownCaip19Id.EnergyNile
| typeof KnownCaip19Id.EnergyShasta
| typeof KnownCaip19Id.BandwidthMainnet
| typeof KnownCaip19Id.BandwidthNile
| typeof KnownCaip19Id.BandwidthShasta;
};

export type MaximumResourceAsset = BaseAsset & {
assetType:
| KnownCaip19Id.MaximumEnergyMainnet
| KnownCaip19Id.MaximumEnergyNile
| KnownCaip19Id.MaximumEnergyShasta
| KnownCaip19Id.MaximumBandwidthMainnet
| KnownCaip19Id.MaximumBandwidthNile
| KnownCaip19Id.MaximumBandwidthShasta;
| typeof KnownCaip19Id.MaximumEnergyMainnet
| typeof KnownCaip19Id.MaximumEnergyNile
| typeof KnownCaip19Id.MaximumEnergyShasta
| typeof KnownCaip19Id.MaximumBandwidthMainnet
| typeof KnownCaip19Id.MaximumBandwidthNile
| typeof KnownCaip19Id.MaximumBandwidthShasta;
};

export type TokenAsset = BaseAsset & {
Expand Down
51 changes: 28 additions & 23 deletions packages/tron-wallet-snap/src/handlers/clientRequest/types.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
export enum ClientRequestMethod {
SignAndSendTransaction = 'signAndSendTransaction',
export const ClientRequestMethod = {
SignAndSendTransaction: 'signAndSendTransaction',
/**
* Unified non-EVM Send
*/
ConfirmSend = 'confirmSend',
ComputeFee = 'computeFee',
OnAddressInput = 'onAddressInput',
OnAmountInput = 'onAmountInput',
ConfirmSend: 'confirmSend',
ComputeFee: 'computeFee',
OnAddressInput: 'onAddressInput',
OnAmountInput: 'onAmountInput',
/**
* Staking + Unstaking
*/
ComputeStakeFee = 'computeStakeFee',
OnStakeAmountInput = 'onStakeAmountInput',
ConfirmStake = 'confirmStake',
OnUnstakeAmountInput = 'onUnstakeAmountInput',
ConfirmUnstake = 'confirmUnstake',
ClaimUnstakedTrx = 'claimUnstakedTrx',
ClaimTrxStakingRewards = 'claimTrxStakingRewards',
ComputeStakeFee: 'computeStakeFee',
OnStakeAmountInput: 'onStakeAmountInput',
ConfirmStake: 'confirmStake',
OnUnstakeAmountInput: 'onUnstakeAmountInput',
ConfirmUnstake: 'confirmUnstake',
ClaimUnstakedTrx: 'claimUnstakedTrx',
ClaimTrxStakingRewards: 'claimTrxStakingRewards',
/**
* Sign Rewards Message
*/
SignRewardsMessage = 'signRewardsMessage',
SignRewardsMessage: 'signRewardsMessage',
/**
* Sign Proof of Ownership
*/
SignProofOfOwnership = 'signProofOfOwnership',
}
SignProofOfOwnership: 'signProofOfOwnership',
} as const;

export enum SendErrorCodes {
// eslint-disable-next-line @typescript-eslint/no-shadow
Required = 'Required',
Invalid = 'Invalid',
InsufficientBalance = 'InsufficientBalance',
InsufficientBalanceToCoverFee = 'InsufficientBalanceToCoverFee',
}
export type ClientRequestMethod =
(typeof ClientRequestMethod)[keyof typeof ClientRequestMethod];

export const SendErrorCodes = {
Required: 'Required',
Invalid: 'Invalid',
InsufficientBalance: 'InsufficientBalance',
InsufficientBalanceToCoverFee: 'InsufficientBalanceToCoverFee',
} as const;

export type SendErrorCodes =
(typeof SendErrorCodes)[keyof typeof SendErrorCodes];
34 changes: 20 additions & 14 deletions packages/tron-wallet-snap/src/handlers/cronjob/cronjob.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,26 @@ import { ConfirmTransactionRequest } from '../../ui/confirmation/views/ConfirmTr
import { CONFIRM_TRANSACTION_INTERFACE_NAME } from '../../ui/confirmation/views/ConfirmTransactionRequest/types';
import type { ConfirmTransactionRequestContext } from '../../ui/confirmation/views/ConfirmTransactionRequest/types';

export enum CronjobMethod {
ContinuouslySynchronizeSelectedAccounts = 'onSynchronizeSelectedAccountsCronjob',
}

export enum BackgroundEventMethod {
SynchronizeSelectedAccounts = 'onSynchronizeSelectedAccounts',
SynchronizeAccounts = 'onSynchronizeAccounts',
SynchronizeAccount = 'onSynchronizeAccount',
SynchronizeAccountTransactions = 'onSynchronizeAccountTransactions',
RefreshConfirmationPrices = 'refreshConfirmationPrices',
RefreshConfirmationSend = 'refreshConfirmationSend',
RefreshSignTransaction = 'refreshSignTransaction',
TrackTransaction = 'onTrackTransaction',
}
export const CronjobMethod = {
ContinuouslySynchronizeSelectedAccounts:
'onSynchronizeSelectedAccountsCronjob',
} as const;

export type CronjobMethod = (typeof CronjobMethod)[keyof typeof CronjobMethod];

export const BackgroundEventMethod = {
SynchronizeSelectedAccounts: 'onSynchronizeSelectedAccounts',
SynchronizeAccounts: 'onSynchronizeAccounts',
SynchronizeAccount: 'onSynchronizeAccount',
SynchronizeAccountTransactions: 'onSynchronizeAccountTransactions',
RefreshConfirmationPrices: 'refreshConfirmationPrices',
RefreshConfirmationSend: 'refreshConfirmationSend',
RefreshSignTransaction: 'refreshSignTransaction',
TrackTransaction: 'onTrackTransaction',
} as const;

export type BackgroundEventMethod =
(typeof BackgroundEventMethod)[keyof typeof BackgroundEventMethod];

export class CronHandler {
readonly #logger: Logger;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/**
* Enum for Tron Multichain API methods that are handled via submitRequest
*/
export enum TronMultichainMethod {
SignMessage = 'signMessage',
SignTransaction = 'signTransaction',
}
export const TronMultichainMethod = {
SignMessage: 'signMessage',
SignTransaction: 'signTransaction',
} as const;

export type TronMultichainMethod =
(typeof TronMultichainMethod)[keyof typeof TronMultichainMethod];

/**
* Error codes for Tron Multichain API
Expand Down
22 changes: 14 additions & 8 deletions packages/tron-wallet-snap/src/handlers/rpc/types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
export enum RpcRequestMethods {
OnStart = 'onStart',
OnInstall = 'onInstall',
OnUpdate = 'onUpdate',
}
export const RpcRequestMethods = {
OnStart: 'onStart',
OnInstall: 'onInstall',
OnUpdate: 'onUpdate',
} as const;

export type RpcRequestMethods =
(typeof RpcRequestMethods)[keyof typeof RpcRequestMethods];

/**
* Methods specific to the test dapp,
* to allow specific flows for manual testing.
*/
export enum TestDappRpcRequestMethod {
ComputeFee = 'computeFee',
}
export const TestDappRpcRequestMethod = {
ComputeFee: 'computeFee',
} as const;

export type TestDappRpcRequestMethod =
(typeof TestDappRpcRequestMethod)[keyof typeof TestDappRpcRequestMethod];
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,8 @@ describe('AssetsService', () => {
trc20Balances,
);

const trc20AssetId = `${String(Network.Mainnet)}/trc20:TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t`;
const trc20AssetId =
`${String(Network.Mainnet)}/trc20:TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t` as const;
mockPriceApiClient.getMultipleSpotPrices.mockResolvedValue(
createSpotPrices({
[trc20AssetId]: { id: trc20AssetId, price: 1.0 },
Expand Down Expand Up @@ -558,7 +559,8 @@ describe('AssetsService', () => {
trc20Balances,
);

const trc20AssetId = `${String(Network.Mainnet)}/trc20:TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t`;
const trc20AssetId =
`${String(Network.Mainnet)}/trc20:TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t` as const;
mockPriceApiClient.getMultipleSpotPrices.mockResolvedValue(
createSpotPrices({
[trc20AssetId]: { id: trc20AssetId, price: 1.0 },
Expand Down Expand Up @@ -1564,7 +1566,8 @@ describe('AssetsService', () => {

it('correctly updates non-essential assets with zero amounts', async () => {
await withAssetsService(async ({ assetsService, mockState }) => {
const trc20AssetId = `${Network.Mainnet}/trc20:TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t`;
const trc20AssetId =
`${Network.Mainnet}/trc20:TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t` as const;
const assets: AssetEntity[] = [
{
assetType: KnownCaip19Id.TrxMainnet,
Expand Down Expand Up @@ -1613,7 +1616,8 @@ describe('AssetsService', () => {
it('updates stale non-essential assets balance to 0 if missed from the latest snapshot', async () => {
await withAssetsService(
async ({ assetsService, mockState, mockAssetsRepository }) => {
const trc20AssetId = `${Network.Mainnet}/trc20:TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t`;
const trc20AssetId =
`${Network.Mainnet}/trc20:TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t` as const;
const savedAssets: AssetEntity[] = [
{
assetType: KnownCaip19Id.TrxMainnet as NativeCaipAssetType,
Expand Down Expand Up @@ -2038,7 +2042,8 @@ describe('AssetsService', () => {
it('adds TRC20 token to the asset list when it updates from 0 to >0', async () => {
await withAssetsService(
async ({ assetsService, mockState, mockAssetsRepository }) => {
const trc20AssetId = `${Network.Mainnet}/trc20:TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t`;
const trc20AssetId =
`${Network.Mainnet}/trc20:TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t` as const;

const savedAssets: AssetEntity[] = [
{
Expand Down Expand Up @@ -2117,7 +2122,8 @@ describe('AssetsService', () => {
it('handles multiple assets updating from 0 to >0 simultaneously', async () => {
await withAssetsService(
async ({ assetsService, mockState, mockAssetsRepository }) => {
const trc20AssetId = `${Network.Mainnet}/trc20:TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t`;
const trc20AssetId =
`${Network.Mainnet}/trc20:TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t` as const;

const savedAssets: AssetEntity[] = [
{
Expand Down Expand Up @@ -2509,7 +2515,8 @@ describe('AssetsService', () => {
it('updates TRC20 token balance when it decreases but remains >0', async () => {
await withAssetsService(
async ({ assetsService, mockState, mockAssetsRepository }) => {
const trc20AssetId = `${Network.Mainnet}/trc20:TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t`;
const trc20AssetId =
`${Network.Mainnet}/trc20:TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t` as const;

const savedAssets: AssetEntity[] = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ describe('ConfirmationHandler', () => {
it('sets iconUrl on each fee asset via getIconUrlForKnownAsset', async () => {
await withConfirmationHandler(
async ({ handler, mockFeeCalculatorService }) => {
const feeAssetType = `${Network.Mainnet}/slip44:195`;
const feeAssetType = `${Network.Mainnet}/slip44:195` as const;
const feesWithIcon: ComputeFeeResult = [
{
type: FeeType.Base,
Expand Down
Loading