Skip to content

Commit 93ae8f5

Browse files
authored
update solidity example: (#1104)
1 parent 139d90e commit 93ae8f5

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

packages/vm/examples/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock.json
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
pragma solidity ^0.5.10;
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
23

34
contract Greeter {
4-
55
string greeting;
66

7-
constructor(string memory _greeting) public {
7+
constructor(string memory _greeting) {
88
greeting = _greeting;
99
}
1010

@@ -15,5 +15,4 @@ contract Greeter {
1515
function greet() public view returns (string memory) {
1616
return greeting;
1717
}
18-
19-
}
18+
}

packages/vm/examples/run-solidity-contract/index.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
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'
56
import { Transaction } from '@ethereumjs/tx'
67
import VM from '../../dist'
7-
8-
const abi = require('ethereumjs-abi')
98
const solc = require('solc')
109

1110
const INITIAL_GREETING = 'Hello, World!'
@@ -21,7 +20,7 @@ function getSolcInput() {
2120
language: 'Solidity',
2221
sources: {
2322
'contracts/Greeter.sol': {
24-
content: fs.readFileSync(path.join(__dirname, 'contracts', 'Greeter.sol'), 'utf8'),
23+
content: readFileSync(join(__dirname, 'contracts', 'Greeter.sol'), 'utf8'),
2524
},
2625
// If more contracts were to be compiled, they should have their own entries here
2726
},
@@ -75,7 +74,7 @@ function getGreeterDeploymentBytecode(solcOutput: any): any {
7574
}
7675

7776
async function getAccountNonce(vm: VM, accountPrivateKey: Buffer) {
78-
const address = privateToAddress(accountPrivateKey)
77+
const address = Address.fromPrivateKey(accountPrivateKey)
7978
const account = await vm.stateManager.getAccount(address)
8079
return account.nonce
8180
}
@@ -88,12 +87,12 @@ async function deployContract(
8887
): Promise<Address> {
8988
// Contracts are deployed by sending their deployment bytecode to the address 0
9089
// 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])
9291
const txData = {
9392
value: 0,
9493
gasLimit: 2000000, // We assume that 2M is enough,
9594
gasPrice: 1,
96-
data: '0x' + deploymentBytecode + params.toString('hex'),
95+
data: '0x' + deploymentBytecode.toString('hex') + params.slice(2),
9796
nonce: await getAccountNonce(vm, senderPrivateKey),
9897
}
9998

@@ -114,13 +113,14 @@ async function setGreeting(
114113
contractAddress: Address,
115114
greeting: string,
116115
) {
117-
const params = abi.rawEncode(['string'], [greeting])
116+
const params = AbiCoder.encode(['string'], [greeting])
117+
const sigHash = new Interface(['function setGreeting(string)']).getSighash('setGreeting')
118118
const txData = {
119119
to: contractAddress,
120120
value: 0,
121121
gasLimit: 2000000, // We assume that 2M is enough,
122122
gasPrice: 1,
123-
data: '0x' + abi.methodID('setGreeting', ['string']).toString('hex') + params.toString('hex'),
123+
data: sigHash + params.slice(2),
124124
nonce: await getAccountNonce(vm, senderPrivateKey),
125125
}
126126

@@ -134,18 +134,19 @@ async function setGreeting(
134134
}
135135

136136
async function getGreeting(vm: VM, contractAddress: Address, caller: Address) {
137+
const sigHash = new Interface(['function greet()']).getSighash('greet')
137138
const greetResult = await vm.runCall({
138139
to: contractAddress,
139140
caller: caller,
140141
origin: caller, // The tx.origin is also the caller here
141-
data: abi.methodID('greet', []),
142+
data: Buffer.from(sigHash.slice(2), 'hex'),
142143
})
143144

144145
if (greetResult.execResult.exceptionError) {
145146
throw greetResult.execResult.exceptionError
146147
}
147148

148-
const results = abi.rawDecode(['string'], greetResult.execResult.returnValue)
149+
const results = AbiCoder.decode(['string'], greetResult.execResult.returnValue)
149150

150151
return results[0]
151152
}
@@ -156,7 +157,7 @@ async function main() {
156157
'hex',
157158
)
158159

159-
const accountAddress = new Address(privateToAddress(accountPk))
160+
const accountAddress = Address.fromPrivateKey(accountPk)
160161

161162
console.log('Account: ', accountAddress.toString())
162163

packages/vm/examples/run-solidity-contract/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"example": "ts-node index.ts"
66
},
77
"dependencies": {
8-
"ethereumjs-abi": "^0.6.7",
9-
"solc": "^0.5.10"
8+
"@ethersproject/abi": "^5.0.12",
9+
"solc": "^0.8.1"
1010
}
1111
}

0 commit comments

Comments
 (0)