/
sendTransaction.ts
183 lines (162 loc) · 6.04 KB
/
sendTransaction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { Asset, MEMO_LENGTH } from '@ironfish/rust-nodejs'
import { BufferMap } from 'buffer-map'
import * as yup from 'yup'
import { Assert } from '../../../assert'
import { CurrencyUtils, YupUtils } from '../../../utils'
import { Wallet } from '../../../wallet'
import { NotEnoughFundsError } from '../../../wallet/errors'
import { RPC_ERROR_CODES, RpcValidationError } from '../../adapters/errors'
import { ApiNamespace } from '../namespaces'
import { routes } from '../router'
import { AssertHasRpcContext } from '../rpcContext'
import { getAccount } from './utils'
export type SendTransactionRequest = {
account?: string
outputs: {
publicAddress: string
amount: string
memo?: string
memoHex?: string
assetId?: string
}[]
fee?: string
feeRate?: string
expiration?: number | null
expirationDelta?: number | null
confirmations?: number | null
notes?: string[]
}
export type SendTransactionResponse = {
account: string
hash: string
transaction: string
}
export const SendTransactionRequestSchema: yup.ObjectSchema<SendTransactionRequest> = yup
.object({
account: yup.string().defined(),
outputs: yup
.array(
yup
.object({
publicAddress: yup.string().defined(),
amount: YupUtils.currency({ min: 0n }).defined(),
memo: yup.string().optional().max(MEMO_LENGTH),
memoHex: yup
.string()
.optional()
.max(MEMO_LENGTH * 2, 'Must be 32 byte hex encoded'),
assetId: yup.string().optional(),
})
.defined(),
)
.defined(),
fee: YupUtils.currency({ min: 1n }).optional(),
feeRate: YupUtils.currency({ min: 1n }).optional(),
expiration: yup.number().nullable().optional(),
expirationDelta: yup.number().nullable().optional(),
confirmations: yup.number().nullable().optional(),
notes: yup.array(yup.string().defined()).optional(),
})
.defined()
export const SendTransactionResponseSchema: yup.ObjectSchema<SendTransactionResponse> = yup
.object({
account: yup.string().defined(),
hash: yup.string().defined(),
transaction: yup.string().defined(),
})
.defined()
routes.register<typeof SendTransactionRequestSchema, SendTransactionResponse>(
`${ApiNamespace.wallet}/sendTransaction`,
SendTransactionRequestSchema,
async (request, context): Promise<void> => {
AssertHasRpcContext(request, context, 'wallet', 'assetsVerifier')
Assert.isNotNull(context.wallet.nodeClient)
const account = getAccount(context.wallet, request.data.account)
const status = await context.wallet.nodeClient.node.getStatus()
if (!status.content.blockchain.synced) {
throw new RpcValidationError(
`Your node must be synced with the Iron Fish network to send a transaction. Please try again later`,
)
}
const outputs = request.data.outputs.map((output) => {
if (output.memo && output.memoHex) {
throw new RpcValidationError('Only one of memo or memoHex may be set for each output')
}
let memo: Buffer
if (output.memo) {
memo = Buffer.from(output.memo, 'utf-8')
} else if (output.memoHex) {
memo = Buffer.from(output.memoHex, 'hex')
} else {
memo = Buffer.alloc(0)
}
return {
publicAddress: output.publicAddress,
amount: CurrencyUtils.decode(output.amount),
memo: memo,
assetId: output.assetId ? Buffer.from(output.assetId, 'hex') : Asset.nativeId(),
}
})
const params: Parameters<Wallet['send']>[0] = {
account,
outputs,
expiration: request.data.expiration ?? undefined,
expirationDelta: request.data.expirationDelta ?? undefined,
confirmations: request.data.confirmations ?? undefined,
}
const totalByAssetId = new BufferMap<bigint>()
for (const { assetId, amount } of outputs) {
const sum = totalByAssetId.get(assetId) ?? 0n
totalByAssetId.set(assetId, sum + amount)
}
if (request.data.fee) {
params.fee = CurrencyUtils.decode(request.data.fee)
totalByAssetId.set(Asset.nativeId(), params.fee)
}
if (request.data.feeRate) {
params.feeRate = CurrencyUtils.decode(request.data.feeRate)
}
// Check that the node has enough balance
for (const [assetId, sum] of totalByAssetId) {
const balance = await context.wallet.getBalance(account, assetId, {
confirmations: request.data.confirmations ?? undefined,
})
if (balance.available < sum) {
throw new RpcValidationError(
`Your balance is too low. Add funds to your account first`,
undefined,
RPC_ERROR_CODES.INSUFFICIENT_BALANCE,
)
}
}
if (request.data.notes) {
params.notes = request.data.notes.map((noteHash) => Buffer.from(noteHash, 'hex'))
}
try {
const transaction = await context.wallet.send(params)
request.end({
account: account.name,
transaction: transaction.serialize().toString('hex'),
hash: transaction.hash().toString('hex'),
})
} catch (e) {
if (e instanceof NotEnoughFundsError) {
// We are overriding the error message from the node to include verified assets in their appropriate denomination.
const assetData = context.assetsVerifier.getAssetData(e.assetId)
const renderedAmountNeeded = CurrencyUtils.render(
e.amountNeeded,
true,
e.assetId,
assetData,
)
const renderedAmount = CurrencyUtils.render(e.amount, false, e.assetId, assetData)
const message = `Insufficient funds: Needed ${renderedAmountNeeded} but have ${renderedAmount} available to spend. Please fund your account and/or wait for any pending transactions to be confirmed.'`
throw new RpcValidationError(message, 400, RPC_ERROR_CODES.INSUFFICIENT_BALANCE)
}
throw e
}
},
)