From 48e36da33726c56f3a5a073703e30112caf7845e Mon Sep 17 00:00:00 2001 From: Colin Platt Date: Wed, 21 Jun 2023 14:16:49 +0200 Subject: [PATCH] adding initial methods --- .gitmodules | 3 ++ lib/solady | 1 + remappings.txt | 3 ++ src/BrowserProvider.sol | 2 + src/Counter.sol | 14 ------- src/ethers.sol | 81 +++++++++++++++++++++++++++++++++++++++++ test/Counter.t.sol | 24 ------------ test/ethers.t.sol | 19 ++++++++++ 8 files changed, 109 insertions(+), 38 deletions(-) create mode 160000 lib/solady create mode 100644 remappings.txt create mode 100644 src/BrowserProvider.sol delete mode 100644 src/Counter.sol create mode 100644 src/ethers.sol delete mode 100644 test/Counter.t.sol create mode 100644 test/ethers.t.sol diff --git a/.gitmodules b/.gitmodules index 888d42d..3a3a0ee 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "lib/forge-std"] path = lib/forge-std url = https://github.com/foundry-rs/forge-std +[submodule "lib/solady"] + path = lib/solady + url = https://github.com/Vectorized/solady diff --git a/lib/solady b/lib/solady new file mode 160000 index 0000000..96c3e21 --- /dev/null +++ b/lib/solady @@ -0,0 +1 @@ +Subproject commit 96c3e2151f8e340742e0f9e7a48da59e6d07662b diff --git a/remappings.txt b/remappings.txt new file mode 100644 index 0000000..8ee04c3 --- /dev/null +++ b/remappings.txt @@ -0,0 +1,3 @@ +ds-test/=lib/solady/lib/ds-test/src/ +forge-std/=lib/forge-std/src/ +solady/=lib/solady/ diff --git a/src/BrowserProvider.sol b/src/BrowserProvider.sol new file mode 100644 index 0000000..1b15279 --- /dev/null +++ b/src/BrowserProvider.sol @@ -0,0 +1,2 @@ +// SPDX-License-Identifier: MIT +pragma solidity 0.8.19; \ No newline at end of file diff --git a/src/Counter.sol b/src/Counter.sol deleted file mode 100644 index aded799..0000000 --- a/src/Counter.sol +++ /dev/null @@ -1,14 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.13; - -contract Counter { - uint256 public number; - - function setNumber(uint256 newNumber) public { - number = newNumber; - } - - function increment() public { - number++; - } -} diff --git a/src/ethers.sol b/src/ethers.sol new file mode 100644 index 0000000..ec85710 --- /dev/null +++ b/src/ethers.sol @@ -0,0 +1,81 @@ +// SPDX-License-Identifier: MIT +pragma solidity 0.8.19; + +import { LibString } from "lib/solady/src/Milady.sol"; + +contract ethers_sol { + using LibString for *; + + function _request(string memory method) internal pure returns (string memory) { + return string.concat( + 'await window.ethereum.request({"method": "', + method, + '","params": []});' + ); + } + + function _request(string memory method, string memory params) internal pure returns (string memory) { + return string.concat( + 'await window.ethereum.request({"method": "', + method, + '","params": [', + params, + ']});' + ); + } + + function _request(string memory _method, string memory _params, string memory _promise) internal pure returns (string memory) { + return string.concat( + 'await window.ethereum.request({"method": "', + _method, + '","params": [', + _params, + ']}).then(', + _promise, + ');' + ); + } + + function _request(string memory _method, string memory _params, string memory _promise, string memory _catch) internal pure returns (string memory) { + return string.concat( + 'await window.ethereum.request({"method": "', + _method, + '","params": [', + _params, + ']}).then(', + _promise, + ').catch(', + _catch, + ');' + ); + } + + function eth_accounts() public pure returns (string memory) { + return _request('eth_accounts'); + } + + enum subscriptionType { logs, newHeads, newPendingTransactions, syncing } + + function _subscriptionTypeToString(subscriptionType _type) internal pure returns (string memory) { + string memory _typeString; + if(_type == subscriptionType.logs) { + _typeString = '"logs"'; + } else if(_type == subscriptionType.newHeads) { + _typeString = '"newHeads"'; + } else if(_type == subscriptionType.newPendingTransactions) { + _typeString = '"newPendingTransactions"'; + } else if(_type == subscriptionType.syncing) { + _typeString = '"syncing"'; + } + return _typeString; + } + + function eth_subscribe(subscriptionType _type) public pure returns (string memory) { + return _request('eth_subscribe', string.concat(_subscriptionTypeToString(_type), ', null')); + } + + function eth_subscribe(subscriptionType _type, address _address) public pure returns (string memory) { + return _request('eth_subscribe', string.concat(_subscriptionTypeToString(_type), ',', _address.toHexString())); + } + +} \ No newline at end of file diff --git a/test/Counter.t.sol b/test/Counter.t.sol deleted file mode 100644 index 30235e8..0000000 --- a/test/Counter.t.sol +++ /dev/null @@ -1,24 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.13; - -import "forge-std/Test.sol"; -import "../src/Counter.sol"; - -contract CounterTest is Test { - Counter public counter; - - function setUp() public { - counter = new Counter(); - counter.setNumber(0); - } - - function testIncrement() public { - counter.increment(); - assertEq(counter.number(), 1); - } - - function testSetNumber(uint256 x) public { - counter.setNumber(x); - assertEq(counter.number(), x); - } -} diff --git a/test/ethers.t.sol b/test/ethers.t.sol new file mode 100644 index 0000000..41a9a48 --- /dev/null +++ b/test/ethers.t.sol @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.13; + +import "forge-std/Test.sol"; +import "src/ethers.sol"; + +contract ethersTest is Test { + + ethers_sol public ethers; + + function setUp() public { + ethers = new ethers_sol(); + } + + function test_eth_accounts() public { + assertEq(ethers.eth_accounts(), 'await window.ethereum.request({"method": "eth_accounts","params": []});'); + } + +} \ No newline at end of file