Skip to content

Commit

Permalink
fix(wallet-mobile): Use absolute slot number when creating a TX (#3642)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljscript authored Sep 20, 2024
1 parent a50ce84 commit de98527
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('dateToEpochInfo', () => {
start: new Date('2024-05-09T21:44:51.000Z'),
end: new Date('2024-05-14T21:44:51.000Z'),
era: shelleyEraConfig,
eras: networkManagers[Chain.Network.Mainnet].eras,
}

const result = convertDateToEpoch(inputDate)
Expand All @@ -42,6 +43,7 @@ describe('dateToEpochInfo', () => {
start: new Date('2017-09-23T21:44:51.000Z'),
end: new Date('2017-09-28T21:44:51.000Z'),
era: byronEraConfig,
eras: networkManagers[Chain.Network.Mainnet].eras,
}

const result = convertDateToEpoch(inputDate)
Expand All @@ -58,6 +60,7 @@ describe('dateToEpochInfo', () => {
start: new Date('2020-07-24T21:44:51.000Z'),
end: new Date('2020-07-29T21:44:51.000Z'),
era: byronEraConfig,
eras: networkManagers[Chain.Network.Mainnet].eras,
}

const result = convertDateToEpoch(inputDate)
Expand All @@ -75,6 +78,7 @@ describe('dateToEpochInfo', () => {
start: new Date('2020-07-29T21:44:51.000Z'),
end: new Date('2020-08-03T21:44:51.000Z'),
era: shelleyEraConfig,
eras: networkManagers[Chain.Network.Mainnet].eras,
}

const result = convertDateToEpoch(inputDate)
Expand All @@ -91,6 +95,7 @@ describe('dateToEpochInfo', () => {
start: new Date('2024-05-11T01:00:00.000Z'),
end: new Date('2024-05-16T01:00:00.000Z'),
era: shelleyPreprodEraConfig,
eras: networkManagers['preprod'].eras,
}

const result = convertDateToEpoch(inputDate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function dateToEpochInfo(eras: Network.Manager['eras']) {
start: epochStart,
end: epochEnd,
era: era,
eras: eras,
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('epochProgress', () => {
expect(result).toEqual({
progress: 100,
currentSlot: 431999,
absoluteSlot: 124156799,
timeRemaining: {days: 0, hours: 0, minutes: 0, seconds: 1},
})
})
Expand All @@ -24,6 +25,7 @@ describe('epochProgress', () => {
const result = epochProgress(dateToEpochInfo(networkManagers['mainnet'].eras)(currentDate))(currentDate)

expect(result).toEqual({
absoluteSlot: 123724800,
progress: 0,
currentSlot: 0,
timeRemaining: {days: 5, hours: 0, minutes: 0, seconds: 0},
Expand All @@ -35,6 +37,7 @@ describe('epochProgress', () => {
const result = epochProgress(dateToEpochInfo(networkManagers['mainnet'].eras)(currentDate))(currentDate)

expect(result).toEqual({
absoluteSlot: 123940800,
progress: 50,
currentSlot: 216000,
timeRemaining: {days: 2, hours: 12, minutes: 0, seconds: 0},
Expand All @@ -47,6 +50,7 @@ describe('epochProgress', () => {
start: new Date('2024-05-09T21:44:51.000Z'),
end: new Date('2024-05-14T21:44:51.000Z'),
era: shelleyEraConfig,
eras: networkManagers['mainnet'].eras,
}

const currentDate = new Date('2022-01-02T00:00:01Z')
Expand All @@ -56,6 +60,7 @@ describe('epochProgress', () => {
expect(result).toEqual({
progress: 100,
currentSlot: 432000,
absoluteSlot: 49515310,
timeRemaining: {days: 0, hours: 0, minutes: 0, seconds: 0},
})
})
Expand All @@ -68,6 +73,7 @@ describe('epochProgress', () => {
const result = progressFn(currentDate)

expect(result).toEqual({
absoluteSlot: 61718400,
progress: 86.67,
currentSlot: 374400,
timeRemaining: {days: 0, hours: 16, minutes: 0, seconds: 0},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@ export function epochProgress(epochInfo: Network.EpochInfo) {
const epochStart = epochInfo.start
const epochEnd = epochInfo.end

if (date > epochEnd || date < epochStart)
let absoluteSlot = 0

for (const era of epochInfo.eras) {
if (date >= era.start && (era.end === undefined || date < era.end)) {
absoluteSlot += Math.floor((date.getTime() - era.start.getTime()) / 1e3 / era.slotInSeconds)
break
}
absoluteSlot += Math.floor(
((era.end?.getTime() ?? date.getTime()) - era.start.getTime()) / 1e3 / era.slotInSeconds,
)
}

if (date > epochEnd || date < epochStart) {
return freeze(
{
progress: 100,
currentSlot: epochInfo.era.slotsPerEpoch,
absoluteSlot,
timeRemaining: {
days: 0,
hours: 0,
Expand All @@ -20,6 +33,7 @@ export function epochProgress(epochInfo: Network.EpochInfo) {
},
true,
)
}

const progress =
Math.round(
Expand All @@ -36,13 +50,14 @@ export function epochProgress(epochInfo: Network.EpochInfo) {

return freeze(
{
progress: progress,
currentSlot: currentSlot,
progress,
currentSlot,
absoluteSlot,
timeRemaining: {
days: days,
hours: hours,
minutes: minutes,
seconds: seconds,
days,
hours,
minutes,
seconds,
},
},
true,
Expand Down
34 changes: 13 additions & 21 deletions apps/wallet-mobile/src/yoroi-wallets/cardano/cardano-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,9 @@ export const makeCardanoWallet = (networkManager: Network.Manager, implementatio
addressMode: Wallet.AddressMode
}) {
if (implementationConfig.features.staking) {
const time = await this.checkServerStatus()
.then(({serverTime}) => serverTime || Date.now())
.catch(() => Date.now())
const primaryTokenId = this.portfolioPrimaryTokenInfo.id

const absSlotNumber = new BigNumber(this.networkManager.epoch.progress(new Date(time)).currentSlot)
const absSlotNumber = await this.getAbsoluteSlotNumber()
const changeAddr = this.getAddressedChangeAddress(addressMode)
const addressedUtxos = await this.getAddressedUtxos()
const registrationStatus = this.getDelegationStatus().isRegistered
Expand Down Expand Up @@ -437,11 +434,7 @@ export const makeCardanoWallet = (networkManager: Network.Manager, implementatio
const primaryTokenId = this.portfolioPrimaryTokenInfo.id

try {
const time = await this.checkServerStatus()
.then(({serverTime}) => serverTime || Date.now())
.catch(() => Date.now())

const absSlotNumber = new BigNumber(this.networkManager.epoch.progress(new Date(time)).currentSlot)
const absSlotNumber = await this.getAbsoluteSlotNumber()
const votingPublicKey = await Promise.resolve(Buffer.from(catalystKeyHex, 'hex'))
.then((bytes) => CardanoMobile.PrivateKey.fromExtendedBytes(bytes))
.then((key) => key.toPublic())
Expand Down Expand Up @@ -528,12 +521,9 @@ export const makeCardanoWallet = (networkManager: Network.Manager, implementatio
addressMode: Wallet.AddressMode
}): Promise<YoroiUnsignedTx> {
if (implementationConfig.features.staking) {
const time = await this.checkServerStatus()
.then(({serverTime}) => serverTime || Date.now())
.catch(() => Date.now())
const primaryTokenId = this.portfolioPrimaryTokenInfo.id

const absSlotNumber = new BigNumber(this.networkManager.epoch.progress(new Date(time)).currentSlot)
const absSlotNumber = await this.getAbsoluteSlotNumber()
const changeAddr = this.getAddressedChangeAddress(addressMode)
const addressedUtxos = await this.getAddressedUtxos()
const accountState = await legacyApi.getAccountState(
Expand Down Expand Up @@ -589,11 +579,8 @@ export const makeCardanoWallet = (networkManager: Network.Manager, implementatio
votingCertificates: CardanoTypes.Certificate[]
addressMode: Wallet.AddressMode
}) {
const time = await this.checkServerStatus()
.then(({serverTime}) => serverTime || Date.now())
.catch(() => Date.now())
const primaryTokenId = this.portfolioPrimaryTokenInfo.id
const absSlotNumber = new BigNumber(this.networkManager.epoch.progress(new Date(time)).currentSlot)
const absSlotNumber = await this.getAbsoluteSlotNumber()
const changeAddr = this.getAddressedChangeAddress(addressMode)
const addressedUtxos = await this.getAddressedUtxos()

Expand Down Expand Up @@ -758,6 +745,13 @@ export const makeCardanoWallet = (networkManager: Network.Manager, implementatio
return Promise.resolve(addressedUtxos)
}

private async getAbsoluteSlotNumber() {
const time = await this.checkServerStatus()
.then(({serverTime}) => serverTime || Date.now())
.catch(() => Date.now())
return new BigNumber(this.networkManager.epoch.progress(new Date(time)).absoluteSlot)
}

async createUnsignedTx({
entries,
addressMode,
Expand All @@ -767,11 +761,9 @@ export const makeCardanoWallet = (networkManager: Network.Manager, implementatio
addressMode: Wallet.AddressMode
metadata?: Array<CardanoTypes.TxMetadata>
}) {
const time = await this.checkServerStatus()
.then(({serverTime}) => serverTime || Date.now())
.catch(() => Date.now())
const primaryTokenId = this.portfolioPrimaryTokenInfo.id
const absSlotNumber = new BigNumber(this.networkManager.epoch.progress(new Date(time)).currentSlot)
const absSlotNumber = await this.getAbsoluteSlotNumber()

const changeAddr = this.getAddressedChangeAddress(addressMode)
const addressedUtxos = await this.getAddressedUtxos()

Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/network/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ export type NetworkEpochInfo = {
start: Date
end: Date
era: NetworkEraConfig
eras: ReadonlyArray<NetworkEraConfig>
}

export type NetworkEpochProgress = {
progress: number
currentSlot: number
absoluteSlot: number
timeRemaining: {
days: number
hours: number
Expand Down

0 comments on commit de98527

Please sign in to comment.