/
solana.ts
167 lines (153 loc) · 5.56 KB
/
solana.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
import { Result, WriteCommand } from '@chainlink/gauntlet-core'
import {
Transaction,
BpfLoader,
BPF_LOADER_PROGRAM_ID,
Keypair,
LAMPORTS_PER_SOL,
PublicKey,
TransactionSignature,
TransactionInstruction,
sendAndConfirmRawTransaction,
} from '@solana/web3.js'
import { withProvider, withWallet, withNetwork } from '../middlewares'
import { TransactionResponse } from '../types'
import { ProgramError, parseIdlErrors, Idl, Program, AnchorProvider } from '@project-serum/anchor'
import { SolanaWallet } from '../wallet'
import { logger } from '@chainlink/gauntlet-core/dist/utils'
import { makeTx } from '../../lib/utils'
export default abstract class SolanaCommand extends WriteCommand<TransactionResponse> {
wallet: SolanaWallet
provider: AnchorProvider
program: Program
abstract execute: () => Promise<Result<TransactionResponse>>
makeRawTransaction: (signer: PublicKey) => Promise<TransactionInstruction[]>
buildCommand?: (flags, args) => Promise<SolanaCommand>
beforeExecute?: (signer: PublicKey) => Promise<void>
afterExecute = async (response: Result<TransactionResponse>): Promise<void> => {
logger.success(`Execution finished at transaction: ${response.responses[0].tx.hash}`)
}
constructor(flags, args) {
super(flags, args)
this.use(withNetwork, withWallet, withProvider)
}
static lamportsToSol = (lamports: number) => lamports / LAMPORTS_PER_SOL
loadProgram = (idl: Idl, address: string): Program<Idl> => {
const program = new Program(idl, address, this.provider)
return program
}
wrapResponse = (hash: string, address: string, states?: Record<string, string>): TransactionResponse => ({
hash: hash,
address: address,
states,
wait: async (hash) => {
const success = !(await this.provider.connection.confirmTransaction(hash)).value.err
return { success }
},
})
wrapInspectResponse = (success: boolean, address: string, states?: Record<string, string>): TransactionResponse => ({
hash: '',
address,
states,
wait: async () => ({ success }),
})
deploy = async (bytecode: Buffer | Uint8Array | Array<number>, programId: Keypair): Promise<TransactionResponse> => {
const success = await BpfLoader.load(
this.provider.connection,
this.wallet.payer,
programId,
bytecode,
BPF_LOADER_PROGRAM_ID,
)
return {
hash: '',
address: programId.publicKey.toString(),
wait: async (hash) => ({
success: success,
}),
}
}
signAndSendRawTx = async (
rawTxs: TransactionInstruction[],
extraSigners?: Keypair[],
overrides: {
units?: number
price?: number
} = {},
): Promise<TransactionSignature> => {
const { blockhash, lastValidBlockHeight } = await this.provider.connection.getLatestBlockhash()
if (overrides.units) logger.info(`Sending transaction with custom unit limit: ${overrides.units}`)
if (overrides.price) logger.info(`Sending transaction with custom unit price: ${overrides.price}`)
const tx = makeTx(
rawTxs,
{
blockhash,
lastValidBlockHeight,
feePayer: this.wallet.publicKey,
},
overrides,
)
if (extraSigners) {
tx.sign(...extraSigners)
}
const signedTx = await this.wallet.signTransaction(tx)
logger.loading('Sending tx...')
return await sendAndConfirmRawTransaction(this.provider.connection, signedTx.serialize())
}
sendTxWithIDL = (sendAction: (...args: any) => Promise<TransactionSignature>, idl: Idl) => async (
...args
): Promise<TransactionSignature> => {
try {
return await sendAction(...args)
} catch (e) {
// Translate IDL error
const idlErrors = parseIdlErrors(idl)
let translatedErr = ProgramError.parse(e, idlErrors)
if (translatedErr === null) {
throw e
}
throw translatedErr
}
}
simulateTx = async (signer: PublicKey, txInstructions: TransactionInstruction[], feePayer?: PublicKey) => {
try {
const { blockhash, lastValidBlockHeight } = await this.provider.connection.getLatestBlockhash()
const tx = makeTx(txInstructions, {
feePayer: feePayer || signer,
blockhash,
lastValidBlockHeight,
})
// simulating through connection allows to skip signing tx (useful when using Ledger device)
const { value: simulationResponse } = await this.provider.connection.simulateTransaction(tx)
if (simulationResponse.err) {
throw new Error(JSON.stringify({ error: simulationResponse.err, logs: simulationResponse.logs }))
}
logger.success(`Tx simulation succeeded: ${simulationResponse.unitsConsumed} units consumed.`)
return simulationResponse.unitsConsumed
} catch (e) {
const parsedError = JSON.parse(e.message)
const errorCode = parsedError.error.InstructionError ? parsedError.error.InstructionError[1].Custom : -1
// Insufficient funds error
if (errorCode == 1 && parsedError.logs.includes('Program log: Error: Insufficient funds')) {
logger.error('Feed has insufficient funds for transfer')
// Other errors
} else {
logger.error(`Tx simulation failed: ${e.message}`)
}
throw e
}
}
sendTx = async (tx: Transaction, signers: Keypair[], idl: Idl): Promise<TransactionSignature> => {
try {
return await this.provider.sendAndConfirm(tx, signers)
} catch (err) {
// Translate IDL error
const idlErrors = parseIdlErrors(idl)
let translatedErr = ProgramError.parse(err, idlErrors)
if (translatedErr === null) {
throw err
}
throw translatedErr
}
}
}