-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathoutput.ts
More file actions
162 lines (137 loc) · 4.36 KB
/
output.ts
File metadata and controls
162 lines (137 loc) · 4.36 KB
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
import { type AbiItem } from './types'
import { logger } from '../lib'
export interface HardHatCompiledOutput {
contractName: string
sourceName: string
/** The Ethereum Contract ABI. If empty, it is represented as an empty array. */
abi: readonly AbiItem[]
bytecode: string
deployedBytecode: string
}
export interface RemixCompiledOutput {
data: {
bytecode: BytecodeObject
deployedByteCode: BytecodeObject
}
/** The Ethereum Contract ABI. If empty, it is represented as an empty array. */
abi: readonly AbiItem[]
}
export interface FoundryCompiledOutput {
abi: readonly AbiItem[]
bytecode: {
object: string
sourceMap: string
linkReferences: Record<string, Record<string, Array<{ start: number, length: number }>>>
}
deployedBytecode: {
object: string
sourceMap: string
linkReferences: Record<string, Record<string, Array<{ start: number, length: number }>>>
}
metadata: string
ir: string
irOptimized: string
storageLayout: any
evm: {
assembly: string
bytecode: {
object: string
sourceMap: string
linkReferences: Record<string, Record<string, Array<{ start: number, length: number }>>>
}
deployedBytecode: {
object: string
sourceMap: string
linkReferences: Record<string, Record<string, Array<{ start: number, length: number }>>>
}
methodIdentifiers: Record<string, string>
gasEstimates: any
}
ewasm: any
}
interface GasEstimate {
confidence: number
maxFeePerGas: number
maxPriorityFeePerGas: number
price: number
}
export interface GasEstimateOutput {
low: GasEstimate
medium: GasEstimate
high: GasEstimate
}
export interface CompiledJSONOutput {
name?: string // contract name
path?: string // local path of the contract
contractType: number // 0: null, 1: hardhat output, 2: remix output, 3: foundry output
hardhatOutput?: HardHatCompiledOutput
remixOutput?: RemixCompiledOutput
foundryOutput?: FoundryCompiledOutput
}
export const getAbi = (output: CompiledJSONOutput): any => {
if (output.contractType === 0) return []
if (output.contractType === 1) return output.hardhatOutput?.abi
if (output.contractType === 2) return output.remixOutput?.abi
if (output.contractType === 3) return output.foundryOutput?.abi
return []
}
export const getByteCode = (
output: CompiledJSONOutput
): string | undefined => {
if (output.contractType === 0) return ''
if (output.contractType === 1) {
const bytecode = output.hardhatOutput?.bytecode
if (!bytecode) return undefined
// Ensure 0x prefix for Hardhat format
return bytecode.startsWith('0x') ? bytecode : `0x${bytecode}`
}
if (output.contractType === 2) {
// Remix format
const bytecode = output.remixOutput?.data.bytecode.object
if (!bytecode) {
logger.log('Remix bytecode is undefined or null')
return undefined
}
logger.log(`Original Remix bytecode: ${bytecode.substring(0, 20)}...`)
logger.log(`Bytecode starts with 0x: ${bytecode.startsWith('0x')}`)
// Ensure 0x prefix for Remix format
const result = bytecode.startsWith('0x') ? bytecode : `0x${bytecode}`
logger.log(`Final bytecode: ${result.substring(0, 20)}...`)
return result
}
if (output.contractType === 3) {
// Foundry format
const bytecode = output.foundryOutput?.bytecode.object
if (!bytecode) {
logger.log('Foundry bytecode is undefined or null')
return undefined
}
logger.log(`Original Foundry bytecode: ${bytecode.substring(0, 20)}...`)
logger.log(`Bytecode starts with 0x: ${bytecode.startsWith('0x')}`)
// Ensure 0x prefix for Foundry format
const result = bytecode.startsWith('0x') ? bytecode : `0x${bytecode}`
logger.log(`Final Foundry bytecode: ${result.substring(0, 20)}...`)
return result
}
return undefined
}
export interface BytecodeObject {
/** The bytecode as a hex string. */
object: string
/** Opcodes list */
opcodes: string
/** The source mapping as a string. See the source mapping definition. */
sourceMap: string
/** If given, this is an unlinked object. */
linkReferences?: Record<string, Record<string, Array<{ start: number, length: number }>>>
}
export interface Fees {
maxFeePerGas: bigint
maxPriorityFeePerGas?: bigint
}
export interface FeeHistory {
oldestBlock: number
reward: string[][]
baseFeePerGas: string[]
gasUsedRatio: number[]
}