Skip to content

Commit

Permalink
refactor(wallet-mobile): drop legacy network stage 1
Browse files Browse the repository at this point in the history
  • Loading branch information
stackchain committed Sep 12, 2024
1 parent 71a19d3 commit d0e27e3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 31 deletions.
22 changes: 16 additions & 6 deletions apps/wallet-mobile/src/yoroi-wallets/cardano/cardano-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const makeCardanoWallet = (
const appApi = AppApi.appApiMaker({baseUrl: networkManager.legacyApiBaseUrl})

// legacy
const {NETWORK_CONFIG, PRIMARY_TOKEN} = constants
const {PRIMARY_TOKEN} = constants

return class CardanoWallet implements YoroiWallet {
readonly id: string
Expand Down Expand Up @@ -433,7 +433,7 @@ export const makeCardanoWallet = (
},
)

return yoroiUnsignedTx({unsignedTx, networkConfig: NETWORK_CONFIG, addressedUtxos, primaryTokenId})
return yoroiUnsignedTx({unsignedTx, networkManager, addressedUtxos, primaryTokenId, keyDeposit})
}

throwLoggedError('createDelegationTx staking not supported')
Expand Down Expand Up @@ -520,10 +520,11 @@ export const makeCardanoWallet = (
return {
votingRegTx: await yoroiUnsignedTx({
unsignedTx,
networkConfig: NETWORK_CONFIG,
networkManager: this.networkManager,
votingRegistration,
addressedUtxos,
primaryTokenId,
keyDeposit,
}),
}
} catch (e) {
Expand Down Expand Up @@ -587,9 +588,10 @@ export const makeCardanoWallet = (

return yoroiUnsignedTx({
unsignedTx: withdrawalTx,
networkConfig: NETWORK_CONFIG,
networkManager: this.networkManager,
addressedUtxos,
primaryTokenId,
keyDeposit,
})
}

Expand Down Expand Up @@ -634,11 +636,12 @@ export const makeCardanoWallet = (

return yoroiUnsignedTx({
unsignedTx,
networkConfig: NETWORK_CONFIG,
networkManager: this.networkManager,
addressedUtxos,
entries: [],
governance: true,
primaryTokenId,
keyDeposit,
})
} catch (e) {
if (e instanceof NotEnoughMoneyToSendError || e instanceof NoOutputsError) throw e
Expand Down Expand Up @@ -820,7 +823,14 @@ export const makeCardanoWallet = (
{metadata},
)

return yoroiUnsignedTx({unsignedTx, networkConfig: NETWORK_CONFIG, addressedUtxos, entries, primaryTokenId})
return yoroiUnsignedTx({
unsignedTx,
networkManager: this.networkManager,
addressedUtxos,
entries,
primaryTokenId,
keyDeposit,
})
} catch (e) {
if (e instanceof NotEnoughMoneyToSendError || e instanceof NoOutputsError) throw e
throwLoggedError(new App.Errors.LibraryError((e as Error).message))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import {Change, Datum, MultiTokenValue} from '@emurgo/yoroi-lib/dist/internals/models'
import {Balance} from '@yoroi/types'
import {Balance, Network} from '@yoroi/types'

import {YoroiEntry, YoroiMetadata, YoroiUnsignedTx, YoroiVoting} from '../../types'
import {Amounts, asQuantity, Entries, Quantities} from '../../utils'
import {Cardano, CardanoMobile} from '../../wallets'
import {CardanoHaskellShelleyNetwork} from '../networks'
import {CardanoTypes} from '../types'

export const yoroiUnsignedTx = async ({
unsignedTx,
networkConfig,
networkManager,
votingRegistration,
addressedUtxos,
entries,
primaryTokenId,
governance,
keyDeposit,
}: {
unsignedTx: CardanoTypes.UnsignedTx
networkConfig: CardanoHaskellShelleyNetwork
networkManager: Network.Manager
votingRegistration?: VotingRegistration
addressedUtxos: CardanoTypes.CardanoAddressedUtxo[]
entries?: YoroiEntry[]
primaryTokenId: string
governance?: boolean
keyDeposit: string
}) => {
const fee = toAmounts(unsignedTx.fee.values)
const change = await toEntriesFromChange(unsignedTx.change)
Expand All @@ -43,14 +44,20 @@ export const yoroiUnsignedTx = async ({
: undefined,
registrations:
unsignedTx.registrations.length > 0
? await Staking.toRegistrations({registrations: unsignedTx.registrations, networkConfig, primaryTokenId})
? await Staking.toRegistrations({
registrations: unsignedTx.registrations,
networkManager,
primaryTokenId,
keyDeposit,
})
: undefined,
deregistrations:
unsignedTx.deregistrations.length > 0
? await Staking.toDeregistrations({
deregistrations: unsignedTx.deregistrations,
networkConfig,
networkManager,
primaryTokenId,
keyDeposit,
})
: undefined,
delegations:
Expand Down Expand Up @@ -145,56 +152,50 @@ const Staking = {
return result
},

toDeregistrations: ({
toDeregistrations: async ({
deregistrations,
networkConfig: {CHAIN_NETWORK_ID, KEY_DEPOSIT},
networkManager,
primaryTokenId,
keyDeposit,
}: {
deregistrations: CardanoTypes.UnsignedTx['deregistrations']
networkConfig: CardanoHaskellShelleyNetwork
networkManager: Network.Manager
primaryTokenId: string
keyDeposit: string
}): Promise<YoroiEntry[]> =>
Promise.all(
deregistrations.map(async (deregistration) => {
const address = await deregistration
.stakeCredential()
.then((stakeCredential) =>
CardanoMobile.RewardAddress.new(
Number(CHAIN_NETWORK_ID) /* API NETWORK_ID is equivalent to CHAIN_NETWORK_ID here */,
stakeCredential,
),
)
.then((stakeCredential) => CardanoMobile.RewardAddress.new(networkManager.chainId, stakeCredential))
.then((rewardAddress) => rewardAddress.toAddress())
.then((address) => address.toBytes())
.then((bytes) => Buffer.from(bytes).toString('hex'))
return {address, amounts: {[primaryTokenId]: asQuantity(KEY_DEPOSIT)}}
return {address, amounts: {[primaryTokenId]: asQuantity(keyDeposit)}}
}),
),

toRegistrations: async ({
registrations,
networkConfig: {CHAIN_NETWORK_ID, KEY_DEPOSIT},
networkManager,
primaryTokenId,
keyDeposit,
}: {
registrations: CardanoTypes.UnsignedTx['registrations']
networkConfig: CardanoHaskellShelleyNetwork
networkManager: Network.Manager
primaryTokenId: string
keyDeposit: string
}): Promise<YoroiEntry[]> => {
return Promise.all(
registrations.map(async (registration) => {
const address: string = await registration
.stakeCredential()
.then((stakeCredential) =>
CardanoMobile.RewardAddress.new(
Number(CHAIN_NETWORK_ID) /* API NETWORK_ID is equivalent to CHAIN_NETWORK_ID here */,
stakeCredential,
),
)
.then((stakeCredential) => CardanoMobile.RewardAddress.new(networkManager.chainId, stakeCredential))
.then((rewardAddress) => rewardAddress.toAddress())
.then((address) => address.toBytes())
.then((bytes) => Buffer.from(bytes).toString('hex'))

return {address, amounts: {[primaryTokenId]: asQuantity(KEY_DEPOSIT)}}
return {address, amounts: {[primaryTokenId]: asQuantity(keyDeposit)}}
}),
)
},
Expand Down

0 comments on commit d0e27e3

Please sign in to comment.