Skip to content

Commit

Permalink
Unify Account and WalletBalance interface to always include nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
lubej committed Jul 9, 2024
1 parent d16a5f3 commit 2a37deb
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Account, AccountProps } from '../Account'

const props = {
address: 'oasis1qq3xrq0urs8qcffhvmhfhz4p0mu7ewc8rscnlwxe',
balance: { available: '200', debonding: '0', delegations: '800', total: '1000' },
balance: { available: '200', debonding: '0', delegations: '800', total: '1000', nonce: '0' },
onClick: () => {},
isActive: false,
displayBalance: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('<AccountSelector />', () => {
wallets: {
oasis1qq3xrq0urs8qcffhvmhfhz4p0mu7ewc8rscnlwxe: {
address: 'oasis1qq3xrq0urs8qcffhvmhfhz4p0mu7ewc8rscnlwxe',
balance: { available: '100', debonding: '0', delegations: '0', total: '100' },
balance: { available: '100', debonding: '0', delegations: '0', total: '100', nonce: '0' },
publicKey: '00',
type: WalletType.UsbLedger,
},
Expand Down
10 changes: 5 additions & 5 deletions src/app/lib/getAccountBalanceWithFallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { call } from 'typed-redux-saga'
import { getExplorerAPIs, getOasisNic } from '../state/network/saga'
import { Account } from '../state/account/types'

function* getBalanceGRPC(address: string, { includeNonce = true } = {}) {
function* getBalanceGRPC(address: string) {
const nic = yield* call(getOasisNic)
const publicKey = yield* call(addressToPublicKey, address)
const account = yield* call([nic, nic.stakingAccount], { owner: publicKey, height: 0 })
Expand All @@ -14,19 +14,19 @@ function* getBalanceGRPC(address: string, { includeNonce = true } = {}) {
delegations: null,
debonding: null,
total: null,
...(includeNonce ? { nonce: account.general?.nonce?.toString() ?? '0' } : {}),
nonce: account.general?.nonce?.toString() ?? '0',
}
}

export function* getAccountBalanceWithFallback(address: string, { includeNonce = true } = {}) {
export function* getAccountBalanceWithFallback(address: string) {
const { getAccount } = yield* call(getExplorerAPIs)
try {
const account: Account = yield* call(getAccount, address, { includeNonce })
const account: Account = yield* call(getAccount, address)
return account
} catch (apiError: any) {
console.error('get account failed, continuing to RPC fallback.', apiError)
try {
const account: Account = yield* call(getBalanceGRPC, address, { includeNonce })
const account: Account = yield* call(getBalanceGRPC, address)
return account
} catch (rpcError) {
console.error('get account with RPC failed, continuing without updated account.', rpcError)
Expand Down
1 change: 1 addition & 0 deletions src/app/pages/AccountPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export function AccountPage() {
account.available == null || balanceDelegations == null || balanceDebondingDelegations == null
? null
: (BigInt(account.available) + balanceDelegations + balanceDebondingDelegations).toString(),
nonce: account.nonce,
}

// Restart fetching account balances if address or network changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('<ImportAccountsSelectionModal />', () => {
importAccountsActions.accountsListed([
{
address: 'oasis1qzyqaxestzlum26e2vdgvkerm6d9qgdp7gh2pxqe',
balance: { available: '0', debonding: '0', delegations: '0', total: '0' },
balance: { available: '0', debonding: '0', delegations: '0', total: '0', nonce: '0' },
path: [44, 474, 0],
pathDisplay: `m/44'/474'/0'`,
publicKey: '00',
Expand All @@ -77,7 +77,7 @@ describe('<ImportAccountsSelectionModal />', () => {
importAccountsActions.accountsListed([
{
address: 'oasis1qzyqaxestzlum26e2vdgvkerm6d9qgdp7gh2pxqe',
balance: { available: '0', debonding: '0', delegations: '0', total: '0' },
balance: { available: '0', debonding: '0', delegations: '0', total: '0', nonce: '0' },
path: [44, 474, 0],
pathDisplay: `m/44'/474'/0'`,
publicKey: '00',
Expand All @@ -86,7 +86,7 @@ describe('<ImportAccountsSelectionModal />', () => {
},
{
address: 'oasis1qqv25adrld8jjquzxzg769689lgf9jxvwgjs8tha',
balance: { available: '0', debonding: '0', delegations: '0', total: '0' },
balance: { available: '0', debonding: '0', delegations: '0', total: '0', nonce: '0' },
path: [44, 474, 1],
pathDisplay: `m/44'/474'/1'`,
publicKey: '00',
Expand Down
3 changes: 1 addition & 2 deletions src/app/state/account/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface BalanceDetails {
delegations: StringifiedBigInt | null
/** This is delayed in getAccount by 20 seconds on oasisscan and 5 seconds on oasismonitor. */
total: StringifiedBigInt | null
nonce?: StringifiedBigInt | null
nonce: StringifiedBigInt
}

export interface Allowance {
Expand All @@ -22,7 +22,6 @@ export interface Allowance {
export interface Account extends BalanceDetails {
address: string
allowances?: Allowance[]
nonce?: StringifiedBigInt
}

/* --- STATE --- */
Expand Down
8 changes: 2 additions & 6 deletions src/app/state/wallet/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,11 @@ export function* openWalletsFromLedger({ payload }: PayloadAction<OpenSelectedAc
const accounts: ImportAccountsListAccount[] = yield* select(selectSelectedAccounts)
yield* put(importAccountsActions.clear())
for (const account of accounts) {
const { nonce: _, ...balance } = account.balance!

yield* call(addWallet, {
address: account.address,
publicKey: account.publicKey,
type: WalletType.UsbLedger,
balance,
balance: account.balance!,
path: account.path,
pathDisplay: account.pathDisplay,
selectImmediately: account === accounts[0], // Select first
Expand Down Expand Up @@ -91,11 +89,9 @@ export function* openWalletFromMnemonic({ payload }: PayloadAction<OpenSelectedA
const accounts: ImportAccountsListAccount[] = yield* select(selectSelectedAccounts)
yield* put(importAccountsActions.clear())
for (const account of accounts) {
const { nonce: _, ...balance } = account.balance!

yield* call(addWallet, {
address: account.address,
balance,
balance: account.balance!,
path: account.path,
pathDisplay: account.pathDisplay,
privateKey: account.privateKey,
Expand Down
6 changes: 6 additions & 0 deletions src/utils/__fixtures__/test-inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ export const walletExtensionV0UnlockedState = {
debonding: '0',
delegations: '0',
total: '0',
nonce: '0',
},
name: 'ledger5',
path: [44, 474, 0, 0, 5],
Expand All @@ -333,6 +334,7 @@ export const walletExtensionV0UnlockedState = {
debonding: '0',
delegations: '0',
total: '0',
nonce: '0',
},
name: 'ledger1',
path: [44, 474, 0, 0, 0],
Expand All @@ -349,6 +351,7 @@ export const walletExtensionV0UnlockedState = {
debonding: '0',
delegations: '0',
total: '0',
nonce: '0',
},
name: 'Account 1',
path: [44, 474, 0],
Expand All @@ -367,6 +370,7 @@ export const walletExtensionV0UnlockedState = {
debonding: '0',
delegations: '0',
total: '0',
nonce: '0',
},
name: 'Account 2',
path: [44, 474, 1],
Expand Down Expand Up @@ -402,6 +406,7 @@ export const walletExtensionV0UnlockedState = {
debonding: '0',
delegations: '0',
total: '0',
nonce: '0',
},
name: 'ledger5-1',
path: [44, 474, 0, 0, 6],
Expand All @@ -418,6 +423,7 @@ export const walletExtensionV0UnlockedState = {
debonding: '0',
delegations: '0',
total: '0',
nonce: '0',
},
name: 'private key1',
privateKey:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ exports[`decryptWithPasswordV0 1`] = `
"available": null,
"debonding": null,
"delegations": null,
"nonce": "0",
"total": null,
},
"name": "ledger5",
Expand All @@ -66,6 +67,7 @@ exports[`decryptWithPasswordV0 1`] = `
"available": null,
"debonding": null,
"delegations": null,
"nonce": "0",
"total": null,
},
"name": "ledger1",
Expand All @@ -86,6 +88,7 @@ exports[`decryptWithPasswordV0 1`] = `
"available": null,
"debonding": null,
"delegations": null,
"nonce": "0",
"total": null,
},
"name": "Account 1",
Expand All @@ -105,6 +108,7 @@ exports[`decryptWithPasswordV0 1`] = `
"available": null,
"debonding": null,
"delegations": null,
"nonce": "0",
"total": null,
},
"name": "Account 2",
Expand All @@ -124,6 +128,7 @@ exports[`decryptWithPasswordV0 1`] = `
"available": null,
"debonding": null,
"delegations": null,
"nonce": "0",
"total": null,
},
"name": "short privatekey",
Expand All @@ -137,6 +142,7 @@ exports[`decryptWithPasswordV0 1`] = `
"available": null,
"debonding": null,
"delegations": null,
"nonce": "0",
"total": null,
},
"name": "ledger5-1",
Expand All @@ -157,6 +163,7 @@ exports[`decryptWithPasswordV0 1`] = `
"available": null,
"debonding": null,
"delegations": null,
"nonce": "0",
"total": null,
},
"name": "private key1",
Expand Down
3 changes: 3 additions & 0 deletions src/utils/walletExtensionV0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export async function decryptWithPasswordV0(
debonding: null,
delegations: null,
total: null,
nonce: '0',
},
name: acc.accountName,
}
Expand All @@ -215,6 +216,7 @@ export async function decryptWithPasswordV0(
debonding: null,
delegations: null,
total: null,
nonce: '0',
},
name: acc.accountName,
}
Expand All @@ -231,6 +233,7 @@ export async function decryptWithPasswordV0(
debonding: null,
delegations: null,
total: null,
nonce: '0',
},
name: acc.accountName,
}
Expand Down
8 changes: 4 additions & 4 deletions src/vendors/oasisscan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ export function getOasisscanAPIs(url: string | 'https://api.oasisscan.com/mainne
const operationsEntity = new OperationsEntityApi(explorerConfig)
const runtime = new RuntimeApi(explorerConfig)

async function getAccount(address: string, { includeNonce = true } = {}): Promise<Account> {
async function getAccount(address: string): Promise<Account> {
const account = await accounts.getAccount({ accountId: address })
if (!account || account.code !== 0) throw new Error('Wrong response code') // TODO
return parseAccount(account.data, { includeNonce })
return parseAccount(account.data)
}

async function getAllValidators(): Promise<Validator[]> {
Expand Down Expand Up @@ -103,7 +103,7 @@ export function getOasisscanAPIs(url: string | 'https://api.oasisscan.com/mainne
}
}

export function parseAccount(account: AccountsRow, { includeNonce = true } = {}): Account {
export function parseAccount(account: AccountsRow): Account {
return {
address: account.address,
allowances: account.allowances.map(allowance => ({
Expand All @@ -114,7 +114,7 @@ export function parseAccount(account: AccountsRow, { includeNonce = true } = {})
delegations: parseRoseStringToBaseUnitString(account.escrow),
debonding: parseRoseStringToBaseUnitString(account.debonding),
total: parseRoseStringToBaseUnitString(account.total),
...(includeNonce ? { nonce: BigInt(account.nonce ?? 0).toString() } : {}),
nonce: BigInt(account.nonce ?? 0).toString(),
}
}

Expand Down

0 comments on commit 2a37deb

Please sign in to comment.