From 2f6180b0f871b3fda6fba4c2cea5ee491924a4ff Mon Sep 17 00:00:00 2001 From: aniket-engg Date: Thu, 9 Jan 2020 16:30:47 +0530 Subject: [PATCH] receive function --- remix-lib/src/execution/txHelper.js | 14 +++++++++++--- remix-lib/src/universalDapp.js | 4 ++++ remix-lib/test/txFormat.js | 15 +++++++++------ remix-lib/test/txHelper.js | 10 +++++++++- remix-tests/src/types.ts | 2 +- 5 files changed, 34 insertions(+), 11 deletions(-) diff --git a/remix-lib/src/execution/txHelper.js b/remix-lib/src/execution/txHelper.js index 630a972289..d6b9136273 100644 --- a/remix-lib/src/execution/txHelper.js +++ b/remix-lib/src/execution/txHelper.js @@ -34,7 +34,7 @@ module.exports = { }, encodeFunctionId: function (funABI) { - if (funABI.type === 'fallback') return '0x' + if (funABI.type === 'fallback' || funABI.type === 'receive') return '0x' let abi = new ethers.utils.Interface([funABI]) abi = abi.functions[funABI.name] return abi.sighash @@ -53,10 +53,10 @@ module.exports = { return -1 } // If we reach here, either a and b are both constant or both not; sort by name then - // special case for fallback and constructor + // special case for fallback, receive and constructor function if (a.type === 'function' && typeof a.name !== 'undefined') { return a.name.localeCompare(b.name) - } else if (a.type === 'constructor' || a.type === 'fallback') { + } else if (a.type === 'constructor' || a.type === 'fallback' || a.type === 'receive') { return 1 } }) @@ -124,6 +124,14 @@ module.exports = { } }, + getReceiveInterface: function (abi) { + for (let i = 0; i < abi.length; i++) { + if (abi[i].type === 'receive') { + return abi[i] + } + } + }, + /** * return the contract obj of the given @arg name. Uses last compilation result. * return null if not found diff --git a/remix-lib/src/universalDapp.js b/remix-lib/src/universalDapp.js index d6cf4eb995..681e3de429 100644 --- a/remix-lib/src/universalDapp.js +++ b/remix-lib/src/universalDapp.js @@ -253,6 +253,10 @@ module.exports = class UniversalDApp { return txHelper.getFallbackInterface(contractABI) } + getReceiveInterface (contractABI) { + return txHelper.getReceiveInterface(contractABI) + } + getInputs (funABI) { if (!funABI.inputs) { return '' diff --git a/remix-lib/test/txFormat.js b/remix-lib/test/txFormat.js index a55b792ae3..6edd32e43a 100644 --- a/remix-lib/test/txFormat.js +++ b/remix-lib/test/txFormat.js @@ -243,12 +243,13 @@ function encodeFunctionCallTest (st) { /* *********************************************************** */ -tape('test fallback function', function (t) { +tape('test fallback & receive function', function (t) { t.test('(fallback)', function (st) { - st.plan(2) - let output = compiler.compile(compilerInput(fallbackFunction)) + st.plan(3) + let output = compiler.compile(compilerInput(fallbackAndReceiveFunction)) output = JSON.parse(output) - const contract = output.contracts['test.sol']['fallbackFunctionContract'] + const contract = output.contracts['test.sol']['fallbackAndReceiveFunctionContract'] + st.equal(txHelper.encodeFunctionId(contract.abi[2]), '0x') st.equal(txHelper.encodeFunctionId(contract.abi[1]), '0x805da4ad') st.equal(txHelper.encodeFunctionId(contract.abi[0]), '0x') }) @@ -381,13 +382,15 @@ contract testContractLinkLibrary { } }` -const fallbackFunction = `pragma solidity >= 0.5.0 < 0.7.0; +const fallbackAndReceiveFunction = `pragma solidity >= 0.5.0 < 0.7.0; -contract fallbackFunctionContract { +contract fallbackAndReceiveFunctionContract { function get (uint _p, string memory _o) public { } fallback () external {} + + receive() payable external{} }` const abiEncoderV2 = `pragma experimental ABIEncoderV2; diff --git a/remix-lib/test/txHelper.js b/remix-lib/test/txHelper.js index 039ec6e83a..f46606a275 100644 --- a/remix-lib/test/txHelper.js +++ b/remix-lib/test/txHelper.js @@ -3,7 +3,7 @@ const tape = require('tape') const txHelper = require('../src/execution/txHelper') tape('getFunction', function (st) { - st.plan(5) + st.plan(6) let fn = txHelper.getFunction(JSON.parse(abi), 'o((address,uint256))') st.equal(fn.name, 'o') @@ -18,6 +18,9 @@ tape('getFunction', function (st) { fn = txHelper.getFallbackInterface(JSON.parse(abi)) st.equal(fn.type, 'fallback') + + fn = txHelper.getReceiveInterface(JSON.parse(abi)) + st.equal(fn.type, 'receive') }) const abi = `[ @@ -143,5 +146,10 @@ const abi = `[ "payable": false, "stateMutability": "nonpayable", "type": "fallback" + }, + { + "payable": true, + "stateMutability": "payable", + "type": "receive" } ]` diff --git a/remix-tests/src/types.ts b/remix-tests/src/types.ts index 0af2f50244..5b0ab611c7 100644 --- a/remix-tests/src/types.ts +++ b/remix-tests/src/types.ts @@ -150,7 +150,7 @@ export type ABIDescription = FunctionDescription | EventDescription export interface FunctionDescription { /** Type of the method. default is 'function' */ - type?: 'function' | 'constructor' | 'fallback' + type?: 'function' | 'constructor' | 'fallback' | 'receive' /** The name of the function. Constructor and fallback function never have name */ name?: string /** List of parameters of the method. Fallback function doesn’t have inputs. */