diff --git a/karma-esm.conf.js b/karma-esm.conf.js index 0b5a4a2e0..ba46eede6 100644 --- a/karma-esm.conf.js +++ b/karma-esm.conf.js @@ -43,12 +43,13 @@ module.exports = function(config) { ], } }, - /* client: { + args: [ process.env.FAUCET_PRIVATEKEY ], + /* mocha: { - grep: 'utf', + grep: 'Test WebSocketProvider', } + */ } - */ }) } diff --git a/karma-umd.conf.js b/karma-umd.conf.js index cefcb0aa3..fc3ee4fe0 100644 --- a/karma-umd.conf.js +++ b/karma-umd.conf.js @@ -44,12 +44,13 @@ module.exports = function(config) { ] } }, - /* client: { + args: [ process.env.FAUCET_PRIVATEKEY ], + /* mocha: { grep: 'Test WebSocketProvider', } + */ } - */ }) } diff --git a/package.json b/package.json index 7f8bf746b..4ef3679ae 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,7 @@ "flatworm": "0.0.2-beta.7", "jison": "0.4.18", "karma": "^6.4.1", - "karma-chrome-launcher": "3.1.0", + "karma-chrome-launcher": "3.1.1", "karma-mocha": "2.0.1", "libnpmpublish": "3.0.1", "mocha": "^9.1.3", diff --git a/packages/tests/src.ts/test-contract-interface.ts b/packages/tests/src.ts/test-contract-interface.ts index 614c2331e..738fb6d2b 100644 --- a/packages/tests/src.ts/test-contract-interface.ts +++ b/packages/tests/src.ts/test-contract-interface.ts @@ -634,9 +634,9 @@ describe("Test ParamType Parser", function() { }); describe('Test EIP-838 Error Codes', function() { - const addr = "0xbd0B4B009a76CA97766360F04f75e05A3E449f1E"; + const addr = "0x9FC52a97e59aeea064D9c24a383B70e8475b3e0B"; it("testError1", async function () { - const provider = new ethers.providers.InfuraProvider("ropsten", "49a0efa3aaee4fd99797bfa94d8ce2f1"); + const provider = new ethers.providers.InfuraProvider("goerli", "49a0efa3aaee4fd99797bfa94d8ce2f1"); const contract = new ethers.Contract(addr, [ "function testError1(bool pass, address addr, uint256 value) pure returns (bool)", "function testError2(bool pass, bytes data) pure returns (bool)", @@ -666,7 +666,8 @@ describe("Additional test cases", function() { const iface = new ethers.utils.Interface([ "function test(address foo) view returns (bool)" ]); - const tx = iface.encodeFunctionData("test", [ "c1912fee45d61c87cc5ea59dae31190fffff232d" ]); - console.log(tx); + //const tx = + iface.encodeFunctionData("test", [ "c1912fee45d61c87cc5ea59dae31190fffff232d" ]); + //console.log(tx); }); }); diff --git a/packages/tests/src.ts/test-contract.json b/packages/tests/src.ts/test-contract.json index 37ae109ce..f86e73823 100644 --- a/packages/tests/src.ts/test-contract.json +++ b/packages/tests/src.ts/test-contract.json @@ -1,5 +1,6 @@ { - "deployer": "0xE78e48deF8A432Ed0e9287d5678496E759e8A564", + "_updated": "I redployed this to goerli on 2022-10-19", + "deployer": "0x75544911a6f2e69ceea374f3f7e5ea9c987ece09", "source": "pragma solidity ^0.4.20;\npragma experimental ABIEncoderV2;\n\ncontract TestContract {\n struct TestStruct {\n address p0;\n uint256 p1;\n }\n\n struct TestStructParent {\n address p0;\n uint256 p1;\n TestStruct child;\n }\n\n event Test(address p0, uint256 p2);\n event TestP0(address indexed p0, uint256 p2);\n event TestP0P1(address indexed p0, uint256 indexed p2);\n\n event TestAnon(address p0, uint256 p2) anonymous;\n event TestAnonP0(address indexed p0, uint256 p2) anonymous;\n event TestAnonP0P1(address indexed p0, uint256 indexed p2) anonymous;\n\n event TestIndexedString(string indexed p2, uint256 p1);\n\n event TestV2(TestStruct indexed p0, TestStruct p1);\n event TestV2Nested(TestStructParent indexed p0, TestStructParent p1);\n\n /*\n event TestV2Array(TestStruct indexed p0[2], TestStruct p1[2]);\n event TestV2NestedArray(TestStructParent indexed p0[2], TestStructParent p1[2]);\n\n event TestV2DynamicArray(TestStruct indexed p0[], TestStruct p1[]);\n event TestV2NestedDynamicArray(TestStructParent indexed p0[], TestStructParent p1[]);\n */\n\n event TestHash(string name, bytes32 hash);\n\n function testEvents(address p0, uint256 p1, string p2) public {\n\n Test(p0, p1);\n TestP0(p0, p1);\n TestP0P1(p0, p1);\n\n TestAnon(p0, p1);\n TestAnonP0(p0, p1);\n TestAnonP0P1(p0, p1);\n\n TestIndexedString(p2, p1);\n\n TestStruct memory testStruct;\n testStruct.p0 = p0;\n testStruct.p1 = p1;\n\n TestStructParent memory testStructParent;\n testStructParent.p0 = address(uint160(p0) + 1);\n testStructParent.p1 = p1 + 1;\n testStructParent.child = testStruct;\n\n TestV2(testStruct, testStruct);\n TestV2Nested(testStructParent, testStructParent);\n\n TestHash(\"TestStructKeccak256\", keccak256(testStruct));\n TestHash(\"TestStructParentKeccak256\", keccak256(testStructParent));\n }\n\n function testV2(TestStructParent p0) public pure returns (TestStructParent result) {\n p0.p0 = address(uint160(p0.p0) + 0xf0);\n p0.p1 += 0xf0;\n p0.child.p0 = address(uint160(p0.child.p0) + 0x0f);\n p0.child.p1 += 0x0f;\n\n return p0;\n }\n\n function testSingleResult(uint32 p0) public pure returns (uint32 r0) {\n r0 = p0 + 1;\n }\n\n function testMultiResult(uint32 p0) public pure returns (uint32 r0, uint32 r1) {\n r0 = p0 + 1;\n r1 = p0 + 2;\n }\n}\n", "optimize": 1, "timestamp": 1528239748494, @@ -14,7 +15,7 @@ "interface": "[{\"constant\":true,\"inputs\":[{\"name\":\"p0\",\"type\":\"uint32\"}],\"name\":\"testSingleResult\",\"outputs\":[{\"name\":\"r0\",\"type\":\"uint32\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"p0\",\"type\":\"uint32\"}],\"name\":\"testMultiResult\",\"outputs\":[{\"name\":\"r0\",\"type\":\"uint32\"},{\"name\":\"r1\",\"type\":\"uint32\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"components\":[{\"name\":\"p0\",\"type\":\"address\"},{\"name\":\"p1\",\"type\":\"uint256\"},{\"components\":[{\"name\":\"p0\",\"type\":\"address\"},{\"name\":\"p1\",\"type\":\"uint256\"}],\"name\":\"child\",\"type\":\"tuple\"}],\"name\":\"p0\",\"type\":\"tuple\"}],\"name\":\"testV2\",\"outputs\":[{\"components\":[{\"name\":\"p0\",\"type\":\"address\"},{\"name\":\"p1\",\"type\":\"uint256\"},{\"components\":[{\"name\":\"p0\",\"type\":\"address\"},{\"name\":\"p1\",\"type\":\"uint256\"}],\"name\":\"child\",\"type\":\"tuple\"}],\"name\":\"result\",\"type\":\"tuple\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"p0\",\"type\":\"address\"},{\"name\":\"p1\",\"type\":\"uint256\"},{\"name\":\"p2\",\"type\":\"string\"}],\"name\":\"testEvents\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"p0\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"p2\",\"type\":\"uint256\"}],\"name\":\"Test\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"p0\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"p2\",\"type\":\"uint256\"}],\"name\":\"TestP0\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"p0\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"p2\",\"type\":\"uint256\"}],\"name\":\"TestP0P1\",\"type\":\"event\"},{\"anonymous\":true,\"inputs\":[{\"indexed\":false,\"name\":\"p0\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"p2\",\"type\":\"uint256\"}],\"name\":\"TestAnon\",\"type\":\"event\"},{\"anonymous\":true,\"inputs\":[{\"indexed\":true,\"name\":\"p0\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"p2\",\"type\":\"uint256\"}],\"name\":\"TestAnonP0\",\"type\":\"event\"},{\"anonymous\":true,\"inputs\":[{\"indexed\":true,\"name\":\"p0\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"p2\",\"type\":\"uint256\"}],\"name\":\"TestAnonP0P1\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"p2\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"p1\",\"type\":\"uint256\"}],\"name\":\"TestIndexedString\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"components\":[{\"name\":\"p0\",\"type\":\"address\"},{\"name\":\"p1\",\"type\":\"uint256\"}],\"indexed\":true,\"name\":\"p0\",\"type\":\"tuple\"},{\"components\":[{\"name\":\"p0\",\"type\":\"address\"},{\"name\":\"p1\",\"type\":\"uint256\"}],\"indexed\":false,\"name\":\"p1\",\"type\":\"tuple\"}],\"name\":\"TestV2\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"components\":[{\"name\":\"p0\",\"type\":\"address\"},{\"name\":\"p1\",\"type\":\"uint256\"},{\"components\":[{\"name\":\"p0\",\"type\":\"address\"},{\"name\":\"p1\",\"type\":\"uint256\"}],\"name\":\"child\",\"type\":\"tuple\"}],\"indexed\":true,\"name\":\"p0\",\"type\":\"tuple\"},{\"components\":[{\"name\":\"p0\",\"type\":\"address\"},{\"name\":\"p1\",\"type\":\"uint256\"},{\"components\":[{\"name\":\"p0\",\"type\":\"address\"},{\"name\":\"p1\",\"type\":\"uint256\"}],\"name\":\"child\",\"type\":\"tuple\"}],\"indexed\":false,\"name\":\"p1\",\"type\":\"tuple\"}],\"name\":\"TestV2Nested\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"name\",\"type\":\"string\"},{\"indexed\":false,\"name\":\"hash\",\"type\":\"bytes32\"}],\"name\":\"TestHash\",\"type\":\"event\"}]", "runtimeBytecode": "0x6060604052600436106100615763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663132414eb811461006657806361096af31461009a57806370e4a0e6146100c6578063babf8901146100f1575b600080fd5b341561007157600080fd5b61008461007f36600461063d565b610111565b60405161009191906107b5565b60405180910390f35b34156100a557600080fd5b6100b86100b336600461063d565b610117565b6040516100919291906107c3565b34156100d157600080fd5b6100e46100df366004610617565b610124565b604051610091919061078b565b34156100fc57600080fd5b61010f61010a3660046105b2565b61017b565b005b60010190565b6001810191600290910190565b61012c610453565b8151600160a060020a0360f09182011683526020830181815101905250604082015151600f018260400151600160a060020a039091169052600f60408301516020018181510190525090919050565b610183610479565b61018b610453565b7f4ba54330f8090ac87ed0a0b470fd9c8a6bbec84bfe94f4fa7776d1dc27da9f9c85856040516101bc92919061073b565b60405180910390a184600160a060020a03167f02d0f5cbd3582d62980b4541ea596241400b46b70e62e8f5d4191b59e5256b79856040516101fd91906107a7565b60405180910390a28385600160a060020a03167f259f1a317c656f6d04907e133c9444121c489db10cc9ae0cb8e5089fb24579bd60405160405180910390a3848460405161024c92919061073b565b60405180910390a084600160a060020a03168460405161026c91906107a7565b60405180910390a18385600160a060020a031660405160405180910390a2826040518082805190602001908083835b602083106102ba5780518252601f19909201916020918201910161029b565b6001836020036101000a038019825116818451161790925250505091909101925060409150505180910390207f64c167146cf1edcaf437640ce9016042b5d80b35064b04ad29af4b8b1420db908560405161031591906107a7565b60405180910390a2600160a060020a03808616835260208084018690526001808801909216835290850190820152604080820183905282907fe85f025e05a85adc87cd1791754b085143ee32261f66e8b6118e8229b494dcdb9082905161037c9190610799565b60405180910390a2807fe0f519dd634b43e6774158c4064aeaeb81762b6577a8c8b8c65b53053fd915fc826040516103b4919061078b565b60405180910390a27fd8b63a613757c85c5a7bc41a52505a0a5d424f9c1a0cdb6d86c53af2f392d8188260405190815260200160405180910390206040516103fc9190610756565b60405180910390a17fd8b63a613757c85c5a7bc41a52505a0a5d424f9c1a0cdb6d86c53af2f392d818816040519081526020016040518091039020604051610444919061077b565b60405180910390a15050505050565b60806040519081016040908152600080835260208301528101610474610479565b905290565b604080519081016040526000808252602082015290565b600061049c8235610831565b9392505050565b6000601f82018390126104b557600080fd5b81356104c86104c382610809565b6107de565b915080825260208301602083018583830111156104e457600080fd5b6104ef838284610849565b50505092915050565b60006080828403121561050a57600080fd5b61051460606107de565b905060006105228484610490565b82525060206105338484830161059a565b602083015250604061054784828501610553565b60408301525092915050565b60006040828403121561056557600080fd5b61056f60406107de565b9050600061057d8484610490565b825250602061058e8484830161059a565b60208301525092915050565b600061049c823561083d565b600061049c8235610840565b6000806000606084860312156105c757600080fd5b60006105d38686610490565b93505060206105e48682870161059a565b925050604084013567ffffffffffffffff81111561060157600080fd5b61060d868287016104a3565b9150509250925092565b60006080828403121561062957600080fd5b600061063584846104f8565b949350505050565b60006020828403121561064f57600080fd5b600061063584846105a6565b61066481610831565b82525050565b6106648161083d565b601381527f546573745374727563744b656363616b32353600000000000000000000000000602082015260400190565b601981527f54657374537472756374506172656e744b656363616b32353600000000000000602082015260400190565b6080820181516106e3848261065b565b5060208201516106f6602085018261066a565b506040820151610709604085018261070f565b50505050565b60408201815161071f848261065b565b506020820151610709602085018261066a565b61066481610840565b60408101610749828561065b565b61049c602083018461066a565b6040808252810161076681610673565b9050610775602083018461066a565b92915050565b60408082528101610766816106a3565b6080810161077582846106d3565b60408101610775828461070f565b60208101610775828461066a565b602081016107758284610732565b604081016107d18285610732565b61049c6020830184610732565b6000604051905081810181811067ffffffffffffffff8211171561080157600080fd5b604052919050565b600067ffffffffffffffff82111561082057600080fd5b506020601f91909101601f19160190565b600160a060020a031690565b90565b63ffffffff1690565b828183375060009101525600a265627a7a72305820738426ffc61b45a331eec053537d02526e13fd71949a31290da38ec04e43f1f46c6578706572696d656e74616cf50037", "transactionHash": "0x19d1edeaa4f9b40cd0f08e8896bba04d1e74b97ddf4dabaa36e852512edf189d", - "contractAddress": "0x94d28bB6cd09f0488039a1870FbDB496Bc6ce98B", + "contractAddress": "0x63c5bd7ef280f150aca761a5e9a922959eb26732", "blockHash": "0xa5d83b96fb8002004d31ab6a5e20c48f6928169dae8bc70257b115f950e9d39c", "blockNumber": 2411314 -} \ No newline at end of file +} diff --git a/packages/tests/src.ts/test-contract.ts b/packages/tests/src.ts/test-contract.ts index 501b36c8d..538851b84 100644 --- a/packages/tests/src.ts/test-contract.ts +++ b/packages/tests/src.ts/test-contract.ts @@ -4,9 +4,11 @@ import assert from "assert"; import { ethers } from "ethers"; +import { sendTransaction } from "./utils" + import contractData from "./test-contract.json"; -const provider = new ethers.providers.InfuraProvider("rinkeby", "49a0efa3aaee4fd99797bfa94d8ce2f1"); +const provider = new ethers.providers.InfuraProvider("goerli", "49a0efa3aaee4fd99797bfa94d8ce2f1"); //const provider = ethers.getDefaultProvider("rinkeby"); const TIMEOUT_PERIOD = 120000; @@ -94,8 +96,12 @@ async function TestContractEvents() { }); }); - const data = await ethers.utils.fetchJson('https://api.ethers.io/api/v1/?action=triggerTest&address=' + contract.address); - console.log('*** Triggered Transaction Hash: ' + data.hash); + const hash = await sendTransaction({ + to: "0x63c5bd7ef280f150aca761a5e9a922959eb26732", + data: "0xbabf890100000000000000000000000006b5955a67d827cdf91823e3bb8f069e6c89c1d600000000000000000000000000000000000000000000000000000000000000420000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000a48656c6c6f576f726c6400000000000000000000000000000000000000000000" + }); + + console.log('*** Triggered Transaction Hash: ' + hash); return running; } diff --git a/packages/tests/src.ts/test-providers.ts b/packages/tests/src.ts/test-providers.ts index 2069d1c21..45844f586 100644 --- a/packages/tests/src.ts/test-providers.ts +++ b/packages/tests/src.ts/test-providers.ts @@ -6,6 +6,8 @@ import assert from "assert"; import { ethers } from "ethers"; +import { fundAddress, returnFunds } from "./utils"; + const bnify = ethers.BigNumber.from; type TestCases = { @@ -712,27 +714,16 @@ testFunctions.push({ }); describe("Test Provider Methods", function() { - let fundReceipt: Promise = null; - - const provider = new ethers.providers.InfuraProvider("goerli", getApiKeys("goerli").infura); - - let faucetWallet: null | ethers.Wallet; - - try { - faucetWallet = new ethers.Wallet(process.env.FAUCET_PRIVATEKEY, provider); - } catch (error) { - console.log("ERROR getting faucet", error); - } + let fundReceipt: Promise = null; before(async function() { this.timeout(300000); // Get some ether from the faucet //const funder = await ethers.utils.fetchJson(`https:/\/api.ethers.io/api/v1/?action=fundAccount&address=${ fundWallet.address.toLowerCase() }`); - const tx = await faucetWallet.sendTransaction({ to: fundWallet.address, value: "314159265358979323" }); - fundReceipt = tx.wait(); //provider.waitForTransaction(funder.hash); - fundReceipt.then((receipt) => { + fundReceipt = fundAddress(fundWallet.address).then((hash) => { console.log(`*** Funded: ${ fundWallet.address }`); + return hash; }); }); @@ -743,17 +734,9 @@ describe("Test Provider Methods", function() { await fundReceipt; // Refund all unused ether to the faucet - const provider = new ethers.providers.InfuraProvider("goerli", getApiKeys("goerli").infura); - const gasPrice = await provider.getGasPrice(); - const balance = await provider.getBalance(fundWallet.address); - const tx = await fundWallet.connect(provider).sendTransaction({ - to: faucetWallet.address, - gasLimit: 21000, - gasPrice: gasPrice, - value: balance.sub(gasPrice.mul(21000)) - }); + const hash = await returnFunds(fundWallet); - console.log(`*** Sweep Transaction:`, tx.hash); + console.log(`*** Sweep Transaction:`, hash); }); providerFunctions.forEach(({ name, networks, create}) => { diff --git a/packages/tests/src.ts/utils.ts b/packages/tests/src.ts/utils.ts index 2f3bd85c5..34eb917ee 100644 --- a/packages/tests/src.ts/utils.ts +++ b/packages/tests/src.ts/utils.ts @@ -61,6 +61,75 @@ function equals(a: any, b: any): boolean { return a === b; } +function getWallet(): ethers.Wallet { + const provider = new ethers.providers.InfuraProvider("goerli", "49a0efa3aaee4fd99797bfa94d8ce2f1"); + + let key: null | string = null; + + // browser + if (key == null) { + try { + if (typeof window !== "undefined") { + key = (window).__karma__.config.args[0]; + if (typeof(key) !== "string") { key = null; } + } + } catch (error) { } + } + + // node.js + if (key == null) { + try { + key = process.env.FAUCET_PRIVATEKEY; + if (typeof(key) !== "string") { key = null; } + } catch (error) { } + } + + if (key == null) { + throw new Error("could not find faucet private key"); + } + + return new ethers.Wallet(key, provider); +} + +export async function fundAddress(address: string): Promise { + try { + const faucetWallet = getWallet(); + const tx = await faucetWallet.sendTransaction({ + to: address, + value: "314159265358979323" + }); + return tx.wait().then((resp) => resp.transactionHash); + } catch (error) { + console.log("ERROR getting faucet", error); + throw error; + } +} + +export async function returnFunds(wallet: ethers.Wallet): Promise { + + const faucet = getWallet(); + + const provider = faucet.provider; + + // Refund all unused ether to the faucet + const gasPrice = await provider.getGasPrice(); + const balance = await provider.getBalance(wallet.address); + const tx = await wallet.connect(provider).sendTransaction({ + to: faucet.address, + gasLimit: 21000, + gasPrice: gasPrice, + value: balance.sub(gasPrice.mul(21000)) + }); + + return tx.hash; +} + +export async function sendTransaction(txObj: ethers.providers.TransactionRequest): Promise { + const wallet = getWallet(); + const tx = await wallet.sendTransaction(txObj); + return tx.hash; +} + export { randomBytes, randomHexString,