Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: status message typo & outdated error #19

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
18 changes: 18 additions & 0 deletions packages/client/src/getTransferType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ConnectorLib, Transfer } from './types'

export function getTransferType (transfer: Transfer): ConnectorLib {
// TODO: find a way to `require(transfer.type)`
try {
switch (transfer.type) {
case '@near-eth/nep141-erc20/natural-erc20/sendToNear':
return require('@near-eth/nep141-erc20/dist/natural-erc20/sendToNear')
case '@near-eth/nep141-erc20/bridged-nep141/sendToEthereum':
return require('@near-eth/nep141-erc20/dist/bridged-nep141/sendToEthereum')
default:
throw new Error(`Unregistered library for transfer with type=${transfer.type}`)
}
} catch (depLoadError) {
console.error(depLoadError)
throw new Error(`Can't find library for transfer with type=${transfer.type}`)
}
}
37 changes: 36 additions & 1 deletion packages/client/src/i18nHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FAILED } from './statuses'
import { Transfer, Step } from './types'
import { Localizations, Transfer, Step } from './types'
import { getTransferType } from './getTransferType'

export function stepsFor (
transfer: Transfer,
Expand All @@ -15,3 +16,37 @@ export function stepsFor (
: i <= completed ? 'completed' : 'pending'
}))
}

export function localizedError (
transfer: Transfer,
errorKey: string
): string {
const type = getTransferType(transfer)
const i18n = type.i18n

// get user's locale from browser
const locale = navigator.language.replace('-', '_')

// get available & fallback locale from given i18n
const availableLocales = Object.keys(i18n)
const fallback = availableLocales[0] as string
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably fall back to something that matches the first part of locale here. So if no es_MX but there is an es_ES, use es_ES.


// check if i18n includes localizations for browser's locale;
// fall back to fallback if not
let localized = i18n[locale]
if (localized === undefined) {
localized = i18n[fallback] as Localizations
}

// check if preferred localization contains error for given errorKey;
// fall back to fallback's error message if not
let error = localized.errors(transfer)[errorKey]
if (error === undefined) {
error = (i18n[fallback] as Localizations).errors(transfer)[errorKey]
}

if (error !== undefined) return error

console.error(`No defined error for errorKey=${errorKey} for locale=${locale} or fallback=${fallback}`)
return errorKey
}
19 changes: 1 addition & 18 deletions packages/client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
import * as storage from './storage'
import * as status from './statuses'
import {
ConnectorLib,
Transfer,
DecoratedTransfer,
Step,
UnsavedTransfer
} from './types'
import { getTransferType } from './getTransferType'

export { onChange } from './storage'
export { setEthProvider, setNearConnection } from './utils'

function getTransferType (transfer: Transfer): ConnectorLib {
// TODO: find a way to `require(transfer.type)`
try {
switch (transfer.type) {
case '@near-eth/nep141-erc20/natural-erc20/sendToNear':
return require('@near-eth/nep141-erc20/dist/natural-erc20/sendToNear')
case '@near-eth/nep141-erc20/bridged-nep141/sendToEthereum':
return require('@near-eth/nep141-erc20/dist/bridged-nep141/sendToEthereum')
default:
throw new Error(`Unregistered library for transfer with type=${transfer.type}`)
}
} catch (depLoadError) {
console.error(depLoadError)
throw new Error(`Can't find library for transfer with type=${transfer.type}`)
}
}

/**
* Return a list of transfers
*
Expand Down
3 changes: 2 additions & 1 deletion packages/client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ export type DecoratedTransfer = Transfer & {
callToAction?: string
}

interface Localizations {
export interface Localizations {
steps: (t: Transfer) => Step[]
callToAction: (t: Transfer) => string | null
statusMessage: (t: Transfer) => string
errors: (t: Transfer) => { [key: string]: string }
}

export interface ConnectorLib {
Expand Down
19 changes: 11 additions & 8 deletions packages/nep141-erc20/src/natural-erc20/sendToNear/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Web3 from 'web3'
import { track, get } from '@near-eth/client'
import { parseRpcError } from 'near-api-js/lib/utils/rpc_errors'
import { utils } from 'near-api-js'
import { stepsFor } from '@near-eth/client/dist/i18nHelpers'
import { localizedError, stepsFor } from '@near-eth/client/dist/i18nHelpers'
import * as status from '@near-eth/client/dist/statuses'
import { getEthProvider, getNearAccount, formatLargeNum } from '@near-eth/client/dist/utils'
import getName from '../getName'
Expand Down Expand Up @@ -81,7 +81,7 @@ export const i18n = {
}
switch (transfer.completedStep) {
case null: return 'Approving transfer'
case APPROVE: return 'Transfering to NEAR'
case APPROVE: return 'Transferring to NEAR'
case LOCK: return `Confirming transfer ${transfer.completedConfirmations + 1} of ${transfer.neededConfirmations}`
case SYNC: return 'Depositing in NEAR'
case MINT: return 'Transfer complete'
Expand All @@ -96,7 +96,12 @@ export const i18n = {
case SYNC: return 'Deposit'
default: return null
}
}
},
errors: transfer => ({
cannotCheckStatus: `Don't know how to checkStatus for transfer ${transfer.id}`,
conflictingTransfer: 'Another transfer is already in progress, please complete the "Transfer" step and try again',
noActionDefined: `Don't know how to act on transfer: ${transfer.id}`
})
}
}

Expand All @@ -110,7 +115,7 @@ export function act (transfer) {
case APPROVE: return lock(transfer)
case LOCK: return checkSync(transfer)
case SYNC: return mint(transfer)
default: throw new Error(`Don't know how to act on transfer: ${transfer.id}`)
default: throw new Error(localizedError(transfer, 'noActionDefined'))
}
}

Expand All @@ -124,7 +129,7 @@ export function checkStatus (transfer) {
case APPROVE: return checkLock(transfer)
case LOCK: return checkSync(transfer)
case SYNC: return checkMint(transfer)
default: throw new Error(`Don't know how to checkStatus for transfer ${transfer.id}`)
default: throw new Error(localizedError(transfer, 'cannotCheckStatus'))
}
}

Expand Down Expand Up @@ -192,9 +197,7 @@ export async function initiate ({
(!completedStep || completedStep === APPROVE)
})
if (conflictingTransfer) {
throw new Error(
'Another transfer is already in progress, please complete the "Lock" step and try again'
)
throw new Error(localizedError(transferDraft, 'conflictingTransfer'))
}

// TODO: move to core 'decorate'; get both from contracts
Expand Down