1
1
import assert from 'assert'
2
- import * as path from 'path'
3
- import * as fs from 'fs'
4
- import { Account , Address , BN , privateToAddress , bufferToHex } from 'ethereumjs-util'
2
+ import { join } from 'path'
3
+ import { readFileSync } from 'fs'
4
+ import { defaultAbiCoder as AbiCoder , Interface } from '@ethersproject/abi'
5
+ import { Account , Address , BN } from 'ethereumjs-util'
5
6
import { Transaction } from '@ethereumjs/tx'
6
7
import VM from '../../dist'
7
-
8
- const abi = require ( 'ethereumjs-abi' )
9
8
const solc = require ( 'solc' )
10
9
11
10
const INITIAL_GREETING = 'Hello, World!'
@@ -21,7 +20,7 @@ function getSolcInput() {
21
20
language : 'Solidity' ,
22
21
sources : {
23
22
'contracts/Greeter.sol' : {
24
- content : fs . readFileSync ( path . join ( __dirname , 'contracts' , 'Greeter.sol' ) , 'utf8' ) ,
23
+ content : readFileSync ( join ( __dirname , 'contracts' , 'Greeter.sol' ) , 'utf8' ) ,
25
24
} ,
26
25
// If more contracts were to be compiled, they should have their own entries here
27
26
} ,
@@ -75,7 +74,7 @@ function getGreeterDeploymentBytecode(solcOutput: any): any {
75
74
}
76
75
77
76
async function getAccountNonce ( vm : VM , accountPrivateKey : Buffer ) {
78
- const address = privateToAddress ( accountPrivateKey )
77
+ const address = Address . fromPrivateKey ( accountPrivateKey )
79
78
const account = await vm . stateManager . getAccount ( address )
80
79
return account . nonce
81
80
}
@@ -88,12 +87,12 @@ async function deployContract(
88
87
) : Promise < Address > {
89
88
// Contracts are deployed by sending their deployment bytecode to the address 0
90
89
// The contract params should be abi-encoded and appended to the deployment bytecode.
91
- const params = abi . rawEncode ( [ 'string' ] , [ greeting ] )
90
+ const params = AbiCoder . encode ( [ 'string' ] , [ greeting ] )
92
91
const txData = {
93
92
value : 0 ,
94
93
gasLimit : 2000000 , // We assume that 2M is enough,
95
94
gasPrice : 1 ,
96
- data : '0x' + deploymentBytecode + params . toString ( 'hex' ) ,
95
+ data : '0x' + deploymentBytecode . toString ( 'hex' ) + params . slice ( 2 ) ,
97
96
nonce : await getAccountNonce ( vm , senderPrivateKey ) ,
98
97
}
99
98
@@ -114,13 +113,14 @@ async function setGreeting(
114
113
contractAddress : Address ,
115
114
greeting : string ,
116
115
) {
117
- const params = abi . rawEncode ( [ 'string' ] , [ greeting ] )
116
+ const params = AbiCoder . encode ( [ 'string' ] , [ greeting ] )
117
+ const sigHash = new Interface ( [ 'function setGreeting(string)' ] ) . getSighash ( 'setGreeting' )
118
118
const txData = {
119
119
to : contractAddress ,
120
120
value : 0 ,
121
121
gasLimit : 2000000 , // We assume that 2M is enough,
122
122
gasPrice : 1 ,
123
- data : '0x' + abi . methodID ( 'setGreeting' , [ 'string' ] ) . toString ( 'hex' ) + params . toString ( 'hex' ) ,
123
+ data : sigHash + params . slice ( 2 ) ,
124
124
nonce : await getAccountNonce ( vm , senderPrivateKey ) ,
125
125
}
126
126
@@ -134,18 +134,19 @@ async function setGreeting(
134
134
}
135
135
136
136
async function getGreeting ( vm : VM , contractAddress : Address , caller : Address ) {
137
+ const sigHash = new Interface ( [ 'function greet()' ] ) . getSighash ( 'greet' )
137
138
const greetResult = await vm . runCall ( {
138
139
to : contractAddress ,
139
140
caller : caller ,
140
141
origin : caller , // The tx.origin is also the caller here
141
- data : abi . methodID ( 'greet' , [ ] ) ,
142
+ data : Buffer . from ( sigHash . slice ( 2 ) , 'hex' ) ,
142
143
} )
143
144
144
145
if ( greetResult . execResult . exceptionError ) {
145
146
throw greetResult . execResult . exceptionError
146
147
}
147
148
148
- const results = abi . rawDecode ( [ 'string' ] , greetResult . execResult . returnValue )
149
+ const results = AbiCoder . decode ( [ 'string' ] , greetResult . execResult . returnValue )
149
150
150
151
return results [ 0 ]
151
152
}
@@ -156,7 +157,7 @@ async function main() {
156
157
'hex' ,
157
158
)
158
159
159
- const accountAddress = new Address ( privateToAddress ( accountPk ) )
160
+ const accountAddress = Address . fromPrivateKey ( accountPk )
160
161
161
162
console . log ( 'Account: ' , accountAddress . toString ( ) )
162
163
0 commit comments