Skip to content

Commit 412f2c6

Browse files
committed
Create new wallets from templates
1 parent 6433e2a commit 412f2c6

File tree

3 files changed

+48
-26
lines changed

3 files changed

+48
-26
lines changed

wallets/client/components/forms.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,12 @@ function WalletProtocolSelector ({ wallet }) {
123123
function WalletProtocolForm ({ wallet }) {
124124
const sendRecvParam = useSendRecvParam()
125125
const protocolParam = useWalletProtocolParam()
126+
const router = useRouter()
127+
const isUserWallet = wallet.__typename === 'UserWallet'
126128

127129
const send = sendRecvParam === 'send'
128130
let protocol = wallet.protocols.find(p => p.name === protocolParam && p.send === send)
129-
if (!protocol && wallet.__typename === 'UserWallet') {
131+
if (!protocol && isUserWallet) {
130132
protocol = wallet.template.protocols.find(p => p.name === protocolParam && p.send === send)
131133
}
132134
if (!protocol) {
@@ -142,9 +144,15 @@ function WalletProtocolForm ({ wallet }) {
142144
const { fields, initial, schema } = useProtocolForm(protocol)
143145

144146
const onSubmit = useCallback(async values => {
145-
await upsertWalletProtocol(values)
146-
toaster.success('wallet saved')
147-
}, [upsertWalletProtocol, toaster])
147+
const wallet = await upsertWalletProtocol(values)
148+
if (isUserWallet) {
149+
toaster.success('wallet saved')
150+
return
151+
}
152+
// we just created a new user wallet from a template
153+
router.replace(`/wallets/${wallet.id}/${sendRecvParam}`, null, { shallow: true })
154+
toaster.success('wallet attached', { persistOnNavigate: true })
155+
}, [upsertWalletProtocol, toaster, isUserWallet, router])
148156

149157
return (
150158
<Form
@@ -156,7 +164,7 @@ function WalletProtocolForm ({ wallet }) {
156164
{fields.map(field => <WalletProtocolFormField key={field.name} {...field} />)}
157165
<div className='d-flex justify-content-end'>
158166
<CancelButton>cancel</CancelButton>
159-
<SubmitButton variant='primary'>save</SubmitButton>
167+
<SubmitButton variant='primary'>{isUserWallet ? 'save' : 'attach'}</SubmitButton>
160168
</div>
161169
</Form>
162170
)

wallets/client/hooks/query.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ export function useWalletProtocolMutation (wallet, protocol) {
5252

5353
const encrypted = await encryptConfig(values)
5454

55-
const { data } = await mutate({
56-
variables: {
57-
// TODO(wallet-v2): use template id if no wallet id is provided
58-
walletId: wallet.id,
59-
...encrypted
60-
}
61-
})
62-
63-
return data
55+
const variables = encrypted
56+
if (wallet.__typename === 'WalletTemplate') {
57+
variables.templateId = wallet.id
58+
} else {
59+
variables.walletId = wallet.id
60+
}
61+
62+
const { data } = await mutate({ variables })
63+
return Object.values(data)[0]
6464
}, [mutate, encryptConfig])
6565
}
6666

wallets/server/resolvers/protocol.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,19 @@ function upsertWalletProtocol (protocol) {
8080

8181
const dataFragment = toFragment(args)
8282

83-
if (walletId) {
84-
const [userWallet] = await models.$transaction([
85-
models.userWallet.update({
83+
return await models.$transaction(
84+
async tx => {
85+
if (templateId) {
86+
const { id: newWalletId } = await tx.userWallet.create({
87+
data: {
88+
templateId: Number(templateId),
89+
userId: me.id
90+
}
91+
})
92+
walletId = newWalletId
93+
}
94+
95+
const userWallet = await tx.userWallet.update({
8696
where: {
8797
id: Number(walletId),
8898
// this makes sure that users can only update their own wallets
@@ -91,32 +101,39 @@ function upsertWalletProtocol (protocol) {
91101
},
92102
data: {
93103
protocols: {
94-
update: {
104+
upsert: {
95105
where: {
96106
ProtocolWallet_walletId_send_protocol_key: {
97107
walletId: Number(walletId),
98108
send: protocol.send,
99109
protocol: protocol.name
100110
}
101111
},
102-
data: {
112+
update: {
103113
[relation]: {
104114
update: dataFragment
105115
}
116+
},
117+
create: {
118+
send: protocol.send,
119+
protocol: protocol.name,
120+
[relation]: {
121+
create: dataFragment
122+
}
106123
}
107124
}
108125
}
109126
},
110127
include: {
111128
protocols: true
112129
}
113-
}),
130+
})
114131
// XXX Prisma seems to run the vault update AFTER the update of the table that points to it
115132
// which means our trigger to set the jsonb column in the ProtocolWallet table does not see
116133
// the updated vault entry.
117134
// To fix this, we run a protocol wallet update to force the trigger to run again.
118135
// TODO(wallet-v2): fix this in a better way?
119-
models.protocolWallet.update({
136+
tx.protocolWallet.update({
120137
where: {
121138
ProtocolWallet_walletId_send_protocol_key: {
122139
walletId: Number(walletId),
@@ -132,11 +149,8 @@ function upsertWalletProtocol (protocol) {
132149
}
133150
}
134151
})
135-
])
136-
137-
return mapUserWalletResolveTypes(userWallet)
138-
}
139152

140-
// TODO(wallet-v2): create new wallet from template
153+
return mapUserWalletResolveTypes(userWallet)
154+
})
141155
}
142156
}

0 commit comments

Comments
 (0)