-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move invites vuex state to composable
- Loading branch information
Showing
11 changed files
with
140 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { | ||
AE_AMOUNT_FORMATS, | ||
AeSdk, | ||
Encoded, | ||
Node, | ||
} from '@aeternity/aepp-sdk'; | ||
import type { IInvite } from '@/types'; | ||
import { STORAGE_KEYS } from '@/constants'; | ||
import { tg } from '@/popup/plugins/i18n'; | ||
import { getAccountFromSecret } from '@/protocols/aeternity/helpers'; | ||
import migrateInvitesVuexToComposable from '@/migrations/006-invites-vuex-to-composable'; | ||
import { useStorageRef } from './storageRef'; | ||
import { useModals } from './modals'; | ||
import { useNetworks } from './networks'; | ||
|
||
const invites = useStorageRef<IInvite[]>( | ||
[], | ||
STORAGE_KEYS.invites, | ||
{ | ||
migrations: [ | ||
migrateInvitesVuexToComposable, | ||
], | ||
}, | ||
); | ||
|
||
export function useInvites() { | ||
const { activeNetwork } = useNetworks(); | ||
const { openDefaultModal } = useModals(); | ||
|
||
function addInvite(secretKey: Buffer) { | ||
invites.value.unshift({ | ||
secretKey: secretKey.toJSON(), | ||
createdAt: Date.now(), | ||
}); | ||
} | ||
|
||
function removeInvite(secretKey: Buffer) { | ||
invites.value = invites.value.filter((invite) => invite.secretKey !== secretKey); | ||
} | ||
|
||
async function claimInvite({ | ||
secretKey, | ||
recipientId, | ||
amount = '0', | ||
isMax = false, | ||
}: { secretKey: Buffer; recipientId: Encoded.AccountAddress; amount?: string; isMax: boolean }) { | ||
const aeSdk = new AeSdk({ | ||
nodes: [{ | ||
name: activeNetwork.value.name, | ||
instance: new Node(activeNetwork.value.protocols.aeternity.nodeUrl), | ||
}], | ||
accounts: [getAccountFromSecret(secretKey)], | ||
}); | ||
if (!isMax) { | ||
await aeSdk.spend( | ||
amount, | ||
recipientId, | ||
// @ts-ignore | ||
{ denomination: AE_AMOUNT_FORMATS.AE }, | ||
); | ||
} else { | ||
await aeSdk.transferFunds( | ||
1, // Decimal percentage, 1 = 100% | ||
recipientId, | ||
{ verify: false }, | ||
); | ||
} | ||
} | ||
|
||
async function handleInsufficientBalanceError(error: Error, isInviteError = false) { | ||
if ( | ||
(!isInviteError && !error.message.includes('is not enough to execute')) | ||
|| (isInviteError && !error.message.includes('Transaction build error')) | ||
) { | ||
return false; | ||
} | ||
|
||
await openDefaultModal({ | ||
msg: (isInviteError) | ||
? tg('pages.invite.insufficient-invite-balance') | ||
: tg('pages.invite.insufficient-balance'), | ||
}); | ||
return true; | ||
} | ||
|
||
return { | ||
invites, | ||
addInvite, | ||
removeInvite, | ||
claimInvite, | ||
handleInsufficientBalanceError, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { IInvite, Migration } from '@/types'; | ||
import { collectVuexState } from './migrationHelpers'; | ||
|
||
const migration: Migration<IInvite[]> = async (restoredValue: IInvite[]) => { | ||
if (!restoredValue?.length) { | ||
const invites = (await collectVuexState())?.invites?.invites; | ||
if (invites?.length) { | ||
return invites; | ||
} | ||
} | ||
return restoredValue; | ||
}; | ||
|
||
export default migration; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters