Skip to content

Commit e7774ea

Browse files
Nana-ECshemnon
andauthored
Support 'from' in 'eth_call' (0.4) (#383)
Cherry pick #375 When a from value is in the eth_call params, set it to the senderId. Signed-off-by: Danno Ferrin <[email protected]> Co-authored-by: Danno Ferrin <[email protected]>
1 parent 8cdc9cf commit e7774ea

File tree

4 files changed

+91
-12
lines changed

4 files changed

+91
-12
lines changed

packages/relay/src/lib/clients/sdkClient.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ export class SDKClient {
194194
.setEthereumData(transactionBuffer), callerName);
195195
}
196196

197-
async submitContractCallQuery(to: string, data: string, gas: number, callerName: string): Promise<ContractFunctionResult> {
197+
async submitContractCallQuery(to: string, data: string, gas: number, from: string, callerName: string): Promise<ContractFunctionResult> {
198198
const contract = SDKClient.prune0x(to);
199199
const contractId = contract.startsWith("00000000000")
200200
? ContractId.fromSolidityAddress(contract)
@@ -209,6 +209,10 @@ export class SDKClient {
209209
contractCallQuery.setFunctionParameters(Buffer.from(SDKClient.prune0x(data), 'hex'));
210210
}
211211

212+
if (from) {
213+
contractCallQuery.setSenderAccountId(AccountId.fromEvmAddress(0,0, from))
214+
}
215+
212216
if (this.clientMain.operatorAccountId !== null) {
213217
contractCallQuery
214218
.setPaymentTransactionId(TransactionId.generate(this.clientMain.operatorAccountId));

packages/relay/src/lib/eth.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -738,10 +738,10 @@ export class EthImpl implements Eth {
738738
gas = call.gas;
739739
}
740740
}
741-
741+
742742
// Execute the call and get the response
743-
this.logger.debug('Making eth_call on contract %o with gas %d and call data "%s"', call.to, gas, call.data);
744-
const contractCallResponse = await this.sdkClient.submitContractCallQuery(call.to, call.data, gas, EthImpl.ethCall);
743+
this.logger.debug('Making eth_call on contract %o with gas %d and call data "%s" from "%s"', call.to, gas, call.data, call.from);
744+
const contractCallResponse = await this.sdkClient.submitContractCallQuery(call.to, call.data, gas, call.from, EthImpl.ethCall);
745745

746746
// FIXME Is this right? Maybe so?
747747
return EthImpl.prepend0x(Buffer.from(contractCallResponse.asBytes()).toString('hex'));

packages/relay/tests/lib/eth.spec.ts

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import pino from 'pino';
3737
import { Block, Transaction } from '../../src/lib/model';
3838
import constants from '../../src/lib/constants';
3939
import { SDKClient } from '../../src/lib/clients';
40-
import { TextEncoder } from 'util';
4140
const logger = pino();
4241
const registry = new Registry();
4342
const Relay = new RelayImpl(logger, registry);
@@ -115,6 +114,7 @@ describe('Eth calls using MirrorNode', async function () {
115114
const gasUsed2 = 800000;
116115
const maxGasLimit = 250000;
117116
const maxGasLimitHex = EthImpl.numberTo0x(maxGasLimit);
117+
const contractCallData = "0xef641f44"
118118
const firstTransactionTimestampSeconds = '1653077547';
119119
const firstTransactionTimestampSecondsHex = EthImpl.numberTo0x(Number(firstTransactionTimestampSeconds));
120120
const contractAddress1 = '0x000000000000000000000000000000000000055f';
@@ -1318,6 +1318,81 @@ describe('Eth calls using MirrorNode', async function () {
13181318
expect(error.message).to.equal('Error encountered estimating the gas price');
13191319
}
13201320
});
1321+
1322+
describe('eth_call', async function () {
1323+
it('eth_call with no gas', async function () {
1324+
sdkClientStub.submitContractCallQuery.returns({
1325+
asBytes: function () {
1326+
return Uint8Array.of(0)
1327+
}
1328+
}
1329+
);
1330+
1331+
const result = await ethImpl.call({
1332+
"from": contractAddress1,
1333+
"to": contractAddress2,
1334+
"data": contractCallData,
1335+
}, 'latest')
1336+
1337+
sinon.assert.calledWith(sdkClientStub.submitContractCallQuery, contractAddress2, contractCallData, 400_000, contractAddress1, 'eth_call');
1338+
expect(result).to.equal("0x00")
1339+
});
1340+
1341+
it('eth_call with no data', async function () {
1342+
sdkClientStub.submitContractCallQuery.returns({
1343+
asBytes: function () {
1344+
return Uint8Array.of(0)
1345+
}
1346+
}
1347+
);
1348+
1349+
var result = await ethImpl.call({
1350+
"from": contractAddress1,
1351+
"to": contractAddress2,
1352+
"gas": maxGasLimitHex
1353+
}, 'latest')
1354+
1355+
sinon.assert.calledWith(sdkClientStub.submitContractCallQuery, contractAddress2, undefined, maxGasLimit, contractAddress1, 'eth_call');
1356+
expect(result).to.equal("0x00")
1357+
});
1358+
1359+
it('eth_call with no from address', async function () {
1360+
sdkClientStub.submitContractCallQuery.returns({
1361+
asBytes: function () {
1362+
return Uint8Array.of(0)
1363+
}
1364+
}
1365+
);
1366+
1367+
const result = await ethImpl.call({
1368+
"to": contractAddress2,
1369+
"data": contractCallData,
1370+
"gas": maxGasLimitHex
1371+
}, 'latest')
1372+
1373+
sinon.assert.calledWith(sdkClientStub.submitContractCallQuery, contractAddress2, contractCallData, maxGasLimit, undefined, 'eth_call');
1374+
expect(result).to.equal("0x00")
1375+
});
1376+
1377+
it('eth_call with all fields', async function () {
1378+
sdkClientStub.submitContractCallQuery.returns({
1379+
asBytes: function () {
1380+
return Uint8Array.of(0)
1381+
}
1382+
}
1383+
);
1384+
1385+
const result = await ethImpl.call({
1386+
"from": contractAddress1,
1387+
"to": contractAddress2,
1388+
"data": contractCallData,
1389+
"gas": maxGasLimitHex
1390+
}, 'latest')
1391+
1392+
sinon.assert.calledWith(sdkClientStub.submitContractCallQuery, contractAddress2, contractCallData, maxGasLimit, contractAddress1, 'eth_call');
1393+
expect(result).to.equal("0x00")
1394+
});
1395+
});
13211396
});
13221397

13231398
describe('Eth', async function () {

packages/server/tests/acceptance/erc20.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {Utils} from '../helpers/utils';
3232

3333
describe('ERC20 Acceptance Tests', async function () {
3434
this.timeout(240 * 1000); // 240 seconds
35-
const {servicesNode, relay, logger} = global;
35+
const {servicesNode, relay} = global;
3636

3737
// cached entities
3838
const accounts: AliasAccount[] = [];
@@ -175,7 +175,8 @@ describe('ERC20 Acceptance Tests', async function () {
175175
if (testTitles[i].testName !== HTS) {
176176
describe('when the spender has enough allowance', function () {
177177
before(async function () {
178-
await contract.connect(tokenOwnerWallet).approve(spender, initialSupply);
178+
const tx = await contract.connect(tokenOwnerWallet).approve(spender, initialSupply);
179+
await tx.wait();
179180
});
180181

181182
describe('when the token owner has enough balance', function () {
@@ -190,6 +191,7 @@ describe('ERC20 Acceptance Tests', async function () {
190191

191192
it('transfers the requested amount', async function () {
192193
tx = await contract.connect(spenderWallet).transferFrom(tokenOwner, to, initialSupply);
194+
await tx.wait();
193195
const ownerBalance = await contract.balanceOf(tokenOwner);
194196
const toBalance = await contract.balanceOf(to);
195197
expect(ownerBalance.toString()).to.be.equal('0');
@@ -336,7 +338,7 @@ describe('ERC20 Acceptance Tests', async function () {
336338
const receipt = await relay.provider.getTransactionReceipt(contract.deployTransaction.hash);
337339
contract = new ethers.Contract(receipt.to, contractJson.abi, accounts[0].wallet);
338340
return contract;
339-
};
341+
}
340342

341343
const createHTS = async(tokenName, symbol, adminAccount, initialSupply, abi, associatedAccounts) => {
342344
const htsResult = await servicesNode.createHTS({
@@ -356,8 +358,6 @@ describe('ERC20 Acceptance Tests', async function () {
356358
// Setup initial balance of token owner account
357359
await servicesNode.transferHTSToken(accounts[0].accountId, htsResult.receipt.tokenId, initialSupply, htsResult.client);
358360
const evmAddress = Utils.idToEvmAddress(htsResult.receipt.tokenId.toString());
359-
const contract = new ethers.Contract(evmAddress, abi, accounts[0].wallet);
360-
361-
return contract;
361+
return new ethers.Contract(evmAddress, abi, accounts[0].wallet);
362362
};
363-
});
363+
});

0 commit comments

Comments
 (0)