diff --git a/Documentation/Usage.md b/Documentation/Usage.md index b030ec8f0..d622ad339 100755 --- a/Documentation/Usage.md +++ b/Documentation/Usage.md @@ -359,8 +359,8 @@ let result = try! transaction.call() ##### Other Transaction Types By default a `legacy` transaction will be created which is compatible across all chains, regardless of which fork. -To create one of the new transaction types introduced with the `london` fork you will need to set some additonal parameters -in the `TransactionOptions` object. Note you should only try to send one of tehse new types of transactions if you are on a chain +To create one of the new transaction types introduced with the `london` fork you will need to set some additional parameters +in the `TransactionOptions` object. Note you should only try to send one of these new types of transactions if you are on a chain that supports them. To send an EIP-2930 style transacton with an access list you need to set the transaction type, and the access list, @@ -385,10 +385,10 @@ To send an EIP-1559 style transaction you set the transaction type, and the new (you may also send an AccessList with an EIP-1559 transaction) When sending an EIP-1559 transaction, the older `gasPrice` parameter is ignored. ```swift options.type = .eip1559 -options.maxFeePerGas = .manual(...) // the maximum price per unit of gas, inclusive of baseFee and tip -options.maxPriorityFeePerGas = .manual(...) // the tip to be paid to the miner, per unit of gas +options.maxFeePerGas = .automatic // the maximum price per unit of gas, inclusive of baseFee and tip +options.maxPriorityFeePerGas = .automatic // the 'tip' to be paid to the miner, per unit of gas ``` -Note there is a new `Oracle` object available that can be used to assist with estimating the new gas fees +Note: There is a new `Oracle` object available that can be used to assist with estimating the new gas fees if you wish to set them manually. ### Chain state diff --git a/Sources/web3swift/Web3/Web3+MutatingTransaction.swift b/Sources/web3swift/Web3/Web3+MutatingTransaction.swift index af8409307..6d2f8362f 100755 --- a/Sources/web3swift/Web3/Web3+MutatingTransaction.swift +++ b/Sources/web3swift/Web3/Web3+MutatingTransaction.swift @@ -73,6 +73,8 @@ public class WriteTransaction: ReadTransaction { optionsForGasEstimation.value = mergedOptions.value optionsForGasEstimation.gasLimit = mergedOptions.gasLimit optionsForGasEstimation.callOnBlock = mergedOptions.callOnBlock + optionsForGasEstimation.type = mergedOptions.type + optionsForGasEstimation.accessList = mergedOptions.accessList // assemble promise for gasLimit var gasEstimatePromise: Promise? = nil @@ -102,20 +104,75 @@ public class WriteTransaction: ReadTransaction { getNoncePromise = Promise.value(nonce) } - // assemble promise for gasPrice - var gasPricePromise: Promise? = nil - guard let gasPricePolicy = mergedOptions.gasPrice else { - seal.reject(Web3Error.inputError(desc: "No gasPrice policy provided")) - return - } - switch gasPricePolicy { - case .automatic, .withMargin: - gasPricePromise = self.web3.eth.getGasPricePromise() - case .manual(let gasPrice): - gasPricePromise = Promise.value(gasPrice) + // determine gas costing, taking transaction type into account + let oracle = Web3.Oracle(self.web3, percentiles: [75]) + let finalGasPrice: BigUInt? // legacy gas model + let finalGasFee: BigUInt? // EIP-1559 gas model + let finalTipFee: BigUInt? // EIP-1559 gas model + + if mergedOptions.type == nil || mergedOptions.type != .eip1559 { // legacy Gas + // set unused gas parameters to nil + finalGasFee = nil + finalTipFee = nil + + // determine the (legacy) gas price + guard let gasPricePolicy = mergedOptions.gasPrice else { + seal.reject(Web3Error.inputError(desc: "No gasPrice policy provided")) + return + } + switch gasPricePolicy { + case .automatic, .withMargin: + let percentiles = oracle.gasPriceLegacyPercentiles + guard !percentiles.isEmpty else { + throw Web3Error.processingError(desc: "Failed to fetch gas price") + } + finalGasPrice = percentiles[0] + case .manual(let gasPrice): + finalGasPrice = gasPrice + } + } else { // else new gas fees (EIP-1559) + // set unused gas parametes to nil + finalGasPrice = nil + + // determine the tip + guard let maxPriorityFeePerGasPolicy = mergedOptions.maxPriorityFeePerGas else { + seal.reject(Web3Error.inputError(desc: "No maxPriorityFeePerGas policy provided")) + return + } + switch maxPriorityFeePerGasPolicy { + case .automatic: + let percentiles = oracle.tipFeePercentiles + guard !percentiles.isEmpty else { + throw Web3Error.processingError(desc: "Failed to fetch maxPriorityFeePerGas data") + } + finalTipFee = percentiles[0] + case .manual(let maxPriorityFeePerGas): + finalTipFee = maxPriorityFeePerGas + } + + // determine the baseFee, and calculate the maxFeePerGas + guard let maxFeePerGasPolicy = mergedOptions.maxFeePerGas else { + seal.reject(Web3Error.inputError(desc: "No maxFeePerGas policy provided")) + return + } + switch maxFeePerGasPolicy { + case .automatic: + let percentiles = oracle.baseFeePercentiles + guard !percentiles.isEmpty else { + throw Web3Error.processingError(desc: "Failed to fetch baseFee data") + } + guard let tipFee = finalTipFee else { + throw Web3Error.processingError(desc: "Missing tip value") + } + finalGasFee = percentiles[0] + tipFee + case .manual(let maxFeePerGas): + finalGasFee = maxFeePerGas + } } - var promisesToFulfill: [Promise] = [getNoncePromise!, gasPricePromise!, gasEstimatePromise!] - when(resolved: getNoncePromise!, gasEstimatePromise!, gasPricePromise!).map(on: queue, { (results: [PromiseResult]) throws -> EthereumTransaction in + + // wait for promises to resolve + var promisesToFulfill: [Promise] = [getNoncePromise!, gasEstimatePromise!] + when(resolved: getNoncePromise!, gasEstimatePromise!).map(on: queue, { (results: [PromiseResult]) throws -> EthereumTransaction in promisesToFulfill.removeAll() guard case .fulfilled(let nonce) = results[0] else { @@ -124,17 +181,25 @@ public class WriteTransaction: ReadTransaction { guard case .fulfilled(let gasEstimate) = results[1] else { throw Web3Error.processingError(desc: "Failed to fetch gas estimate") } - guard case .fulfilled(let gasPrice) = results[2] else { - throw Web3Error.processingError(desc: "Failed to fetch gas price") - } - - let estimate = mergedOptions.resolveGasLimit(gasEstimate) - let finalGasPrice = mergedOptions.resolveGasPrice(gasPrice) var finalOptions = TransactionOptions() + finalOptions.type = mergedOptions.type finalOptions.nonce = .manual(nonce) - finalOptions.gasLimit = .manual(estimate) - finalOptions.gasPrice = .manual(finalGasPrice) + finalOptions.gasLimit = .manual(mergedOptions.resolveGasLimit(gasEstimate)) + finalOptions.accessList = mergedOptions.accessList + + // set the finalized gas parameters + if let gasPrice = finalGasPrice { + finalOptions.gasPrice = .manual(mergedOptions.resolveGasPrice(gasPrice)) + } + + if let tipFee = finalTipFee { + finalOptions.maxPriorityFeePerGas = .manual(mergedOptions.resolveMaxPriorityFeePerGas(tipFee)) + } + + if let gasFee = finalGasFee { + finalOptions.maxFeePerGas = .manual(mergedOptions.resolveMaxFeePerGas(gasFee)) + } assembledTransaction.applyOptions(finalOptions) diff --git a/Tests/web3swiftTests/localTests/ABIEncoderSoliditySha3Test.swift b/Tests/web3swiftTests/localTests/ABIEncoderSoliditySha3Test.swift index a6ceda15d..5d59d356e 100644 --- a/Tests/web3swiftTests/localTests/ABIEncoderSoliditySha3Test.swift +++ b/Tests/web3swiftTests/localTests/ABIEncoderSoliditySha3Test.swift @@ -10,7 +10,7 @@ import Foundation import XCTest @testable import web3swift -class ABIEncoderSoliditySha3Test: XCTestCase { +class ABIEncoderSoliditySha3Test: LocalTestCase { func test_soliditySha3() throws { var hex = try ABIEncoder.soliditySha3(true).toHexString().addHexPrefix() diff --git a/Tests/web3swiftTests/localTests/DataConversionTests.swift b/Tests/web3swiftTests/localTests/DataConversionTests.swift index 224f1db0a..a0f43dc29 100644 --- a/Tests/web3swiftTests/localTests/DataConversionTests.swift +++ b/Tests/web3swiftTests/localTests/DataConversionTests.swift @@ -16,7 +16,7 @@ import XCTest // Some base58 test vectors pulled from: https://tools.ietf.org/id/draft-msporny-base58-01.html // note that one of the return values is incorrect in the reference above, it is corrected here -class DataConversionTests: XCTestCase { +class DataConversionTests: LocalTestCase { // test an empty input for the base58 decoder & decoder func testBase58() throws { let vector = "" diff --git a/Tests/web3swiftTests/localTests/EIP1559BlockTests.swift b/Tests/web3swiftTests/localTests/EIP1559BlockTests.swift index 6ee30db02..e60ef23b9 100644 --- a/Tests/web3swiftTests/localTests/EIP1559BlockTests.swift +++ b/Tests/web3swiftTests/localTests/EIP1559BlockTests.swift @@ -4,7 +4,7 @@ import BigInt @testable import web3swift -class EIP1559BlockTests: XCTestCase { +class EIP1559BlockTests: LocalTestCase { let uselessBlockPart = ( number: BigUInt(12_965_000), hash: Data(fromHex: "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46")!, // "hash": diff --git a/Tests/web3swiftTests/localTests/EIP712Tests.swift b/Tests/web3swiftTests/localTests/EIP712Tests.swift index a3192e959..f5a323d8f 100644 --- a/Tests/web3swiftTests/localTests/EIP712Tests.swift +++ b/Tests/web3swiftTests/localTests/EIP712Tests.swift @@ -1,15 +1,15 @@ import XCTest @testable import web3swift -class EIP712Tests: XCTestCase { +class EIP712Tests: LocalTestCase { func testWithoutChainId() throws { - + let to = EthereumAddress("0x3F06bAAdA68bB997daB03d91DBD0B73e196c5A4d")! - + let value = EIP712.UInt256(0) - + let amountLinen = EIP712.UInt256("0001000000000000000")// - + let function = ABI.Element.Function( name: "approveAndMint", inputs: [ @@ -18,22 +18,22 @@ class EIP712Tests: XCTestCase { outputs: [.init(name: "", type: .bool)], constant: false, payable: false) - + let object = ABI.Element.function(function) - + let safeTxData = object.encodeParameters([ EthereumAddress("0x41B5844f4680a8C38fBb695b7F9CFd1F64474a72")! as AnyObject, amountLinen as AnyObject ])! - + let operation: EIP712.UInt8 = 1 - + let safeTxGas = EIP712.UInt256(250000) - + let baseGas = EIP712.UInt256(60000) - + let gasPrice = EIP712.UInt256("20000000000") - + let gasToken = EthereumAddress("0x0000000000000000000000000000000000000000")! print(1) let refundReceiver = EthereumAddress("0x7c07D32e18D6495eFDC487A32F8D20daFBa53A5e")! @@ -76,13 +76,13 @@ class EIP712Tests: XCTestCase { } func testWithChainId() throws { - + let to = EthereumAddress("0x3F06bAAdA68bB997daB03d91DBD0B73e196c5A4d")! - + let value = EIP712.UInt256(0) - + let amount = EIP712.UInt256("0001000000000000000") - + let function = ABI.Element.Function( name: "approveAndMint", inputs: [ @@ -91,28 +91,28 @@ class EIP712Tests: XCTestCase { outputs: [.init(name: "", type: .bool)], constant: false, payable: false) - + let object = ABI.Element.function(function) - + let safeTxData = object.encodeParameters([ EthereumAddress("0x41B5844f4680a8C38fBb695b7F9CFd1F64474a72")! as AnyObject, amount as AnyObject ])! - + let operation: EIP712.UInt8 = 1 - + let safeTxGas = EIP712.UInt256(250000) - + let baseGas = EIP712.UInt256(60000) - + let gasPrice = EIP712.UInt256("20000000000") - + let gasToken = EthereumAddress("0x0000000000000000000000000000000000000000")! - + let refundReceiver = EthereumAddress("0x7c07D32e18D6495eFDC487A32F8D20daFBa53A5e")! - + let nonce: EIP712.UInt256 = .init(0) - + let safeTX = SafeTx( to: to, value: value, @@ -124,16 +124,16 @@ class EIP712Tests: XCTestCase { gasToken: gasToken, refundReceiver: refundReceiver, nonce: nonce) - + let mnemonic = "normal dune pole key case cradle unfold require tornado mercy hospital buyer" let keystore = try! BIP32Keystore(mnemonics: mnemonic, password: "", mnemonicsPassword: "")! - + let verifyingContract = EthereumAddress("0x76106814dc6150b0fe510fbda4d2d877ac221270")! - + let account = keystore.addresses?[0] let password = "" let chainId: EIP712.UInt256? = EIP712.UInt256(42) - + let signature = try Web3Signer.signEIP712( safeTx: safeTX, keystore: keystore, @@ -141,8 +141,7 @@ class EIP712Tests: XCTestCase { account: account!, password: password, chainId: chainId) - + XCTAssertEqual(signature.toHexString(), "f1f423cb23efad5035d4fb95c19cfcd46d4091f2bd924680b88c4f9edfa1fb3a4ce5fc5d169f354e3b464f45a425ed3f6203af06afbacdc5c8224a300ce9e6b21b") } } - diff --git a/Tests/web3swiftTests/localTests/LocalTestCase.swift b/Tests/web3swiftTests/localTests/LocalTestCase.swift new file mode 100644 index 000000000..96aa687ba --- /dev/null +++ b/Tests/web3swiftTests/localTests/LocalTestCase.swift @@ -0,0 +1,44 @@ +import Foundation +import XCTest +import BigInt + +import web3swift + +// SuperClass that all local tests should be derived from +// while this class does show up in the navigator, it has no associated tests +class LocalTestCase: XCTestCase { + static let url = URL.init(string: "http://127.0.0.1:8545")! + let ganache = try! Web3.new(LocalTestCase.url) + static var isSetUp = false + + override class func setUp() { + super.setUp() + + // check to see if we need to run the one-time setup + if isSetUp { return } + isSetUp = true + + let web3 = try! Web3.new(LocalTestCase.url) + + let block = try! web3.eth.getBlockNumber() + if block >= 25 { return } + + print("\n ****** Preloading Ganache (\(25 - block) blocks) *****\n") + + let allAddresses = try! web3.eth.getAccounts() + let sendToAddress = allAddresses[0] + let contract = web3.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2) + let value = Web3.Utils.parseToBigUInt("1.0", units: .eth) + + let from = allAddresses[0] + let writeTX = contract!.write("fallback")! + writeTX.transactionOptions.from = from + writeTX.transactionOptions.value = value + writeTX.transactionOptions.gasLimit = .manual(78423) + writeTX.transactionOptions.gasPrice = .manual(20000000000) + + for _ in block..<25 { + let _ = try! writeTX.sendPromise(password: "").wait() + } + } +} diff --git a/Tests/web3swiftTests/localTests/LocalTests.xctestplan b/Tests/web3swiftTests/localTests/LocalTests.xctestplan index 5af7facc1..349bc4b42 100644 --- a/Tests/web3swiftTests/localTests/LocalTests.xctestplan +++ b/Tests/web3swiftTests/localTests/LocalTests.xctestplan @@ -21,7 +21,8 @@ "RemoteParsingTests", "RemoteTests", "ST20AndSecurityTokenTests", - "WebsocketTests" + "WebsocketTests", + "LocalTestCase" ], "target" : { "containerPath" : "container:web3swift.xcodeproj", diff --git a/Tests/web3swiftTests/localTests/web3swiftAdvancedABIv2Tests.swift b/Tests/web3swiftTests/localTests/web3swiftAdvancedABIv2Tests.swift index 76f9f595c..2c803cd2f 100755 --- a/Tests/web3swiftTests/localTests/web3swiftAdvancedABIv2Tests.swift +++ b/Tests/web3swiftTests/localTests/web3swiftAdvancedABIv2Tests.swift @@ -10,16 +10,15 @@ import BigInt @testable import web3swift -class web3swiftAdvancedABIv2Tests: XCTestCase { - +class web3swiftAdvancedABIv2Tests: LocalTestCase { + func testAdvancedABIv2() throws { let abiString = "[{\"constant\":true,\"inputs\":[],\"name\":\"testDynOfDyn\",\"outputs\":[{\"name\":\"ts\",\"type\":\"string[]\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"testStOfDyn\",\"outputs\":[{\"name\":\"ts\",\"type\":\"string[2]\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"testDynArray\",\"outputs\":[{\"components\":[{\"name\":\"number\",\"type\":\"uint256\"},{\"name\":\"someText\",\"type\":\"string\"},{\"name\":\"staticArray\",\"type\":\"uint256[2]\"},{\"name\":\"dynamicArray\",\"type\":\"uint256[]\"},{\"name\":\"anotherDynamicArray\",\"type\":\"string[2]\"}],\"name\":\"ts\",\"type\":\"tuple[]\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"testStaticArray\",\"outputs\":[{\"components\":[{\"name\":\"number\",\"type\":\"uint256\"},{\"name\":\"someText\",\"type\":\"string\"},{\"name\":\"staticArray\",\"type\":\"uint256[2]\"},{\"name\":\"dynamicArray\",\"type\":\"uint256[]\"},{\"name\":\"anotherDynamicArray\",\"type\":\"string[2]\"}],\"name\":\"ts\",\"type\":\"tuple[2]\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"testSingle\",\"outputs\":[{\"components\":[{\"name\":\"number\",\"type\":\"uint256\"},{\"name\":\"someText\",\"type\":\"string\"},{\"name\":\"staticArray\",\"type\":\"uint256[2]\"},{\"name\":\"dynamicArray\",\"type\":\"uint256[]\"},{\"name\":\"anotherDynamicArray\",\"type\":\"string[2]\"}],\"name\":\"t\",\"type\":\"tuple\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]" let bytecode = Data.fromHex("6080604052341561000f57600080fd5b610cb18061001e6000396000f30060806040526004361061006d576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063189533931461007257806338163ff51461009b57806388be6c65146100c4578063e7c1a47c146100ed578063f376e01314610116575b600080fd5b341561007d57600080fd5b61008561013f565b6040516100929190610a9f565b60405180910390f35b34156100a657600080fd5b6100ae610220565b6040516100bb9190610a7d565b60405180910390f35b34156100cf57600080fd5b6100d76102c5565b6040516100e49190610ae3565b60405180910390f35b34156100f857600080fd5b610100610350565b60405161010d9190610ac1565b60405180910390f35b341561012157600080fd5b610129610399565b6040516101369190610b05565b60405180910390f35b6060600260405190808252806020026020018201604052801561017657816020015b60608152602001906001900390816101615790505b5090506040805190810160405280600581526020017f48656c6c6f0000000000000000000000000000000000000000000000000000008152508160008151811015156101be57fe5b906020019060200201819052506040805190810160405280600581526020017f576f726c6400000000000000000000000000000000000000000000000000000081525081600181518110151561021057fe5b9060200190602002018190525090565b610228610546565b6040805190810160405280600581526020017f48656c6c6f00000000000000000000000000000000000000000000000000000081525081600060028110151561026d57fe5b60200201819052506040805190810160405280600581526020017f576f726c640000000000000000000000000000000000000000000000000000008152508160016002811015156102ba57fe5b602002018190525090565b6060600260405190808252806020026020018201604052801561030257816020015b6102ef61056d565b8152602001906001900390816102e75790505b50905061030d610399565b81600081518110151561031c57fe5b90602001906020020181905250610331610399565b81600181518110151561034057fe5b9060200190602002018190525090565b6103586105a9565b610360610399565b81600060028110151561036f57fe5b602002018190525061037f610399565b81600160028110151561038e57fe5b602002018190525090565b6103a16105d8565b60606103ab610614565b6103b3610546565b60036040519080825280602002602001820160405280156103e35781602001602082028038833980820191505090505b50925060008360008151811015156103f757fe5b9060200190602002018181525050600183600181518110151561041657fe5b9060200190602002018181525050600283600281518110151561043557fe5b90602001906020020181815250506040805190810160405280600081526020016001815250915060408051908101604052806040805190810160405280600581526020017f48656c6c6f00000000000000000000000000000000000000000000000000000081525081526020016040805190810160405280600581526020017f576f726c64000000000000000000000000000000000000000000000000000000815250815250905060a060405190810160405280600181526020016040805190810160405280600b81526020017f48656c6c6f20776f726c64000000000000000000000000000000000000000000815250815260200183815260200184815260200182815250935083935050505090565b60408051908101604052806002905b60608152602001906001900390816105555790505090565b60e060405190810160405280600081526020016060815260200161058f610636565b8152602001606081526020016105a3610658565b81525090565b6101c0604051908101604052806002905b6105c261056d565b8152602001906001900390816105ba5790505090565b60e06040519081016040528060008152602001606081526020016105fa610636565b81526020016060815260200161060e610658565b81525090565b6040805190810160405280600290602082028038833980820191505090505090565b6040805190810160405280600290602082028038833980820191505090505090565b60408051908101604052806002905b60608152602001906001900390816106675790505090565b600061068a82610b81565b8360208202850161069a85610b31565b60005b848110156106d35783830388526106b5838351610930565b92506106c082610bdb565b915060208801975060018101905061069d565b508196508694505050505092915050565b60006106ef82610b76565b836020820285016106ff85610b27565b60005b8481101561073857838303885261071a838351610930565b925061072582610bce565b9150602088019750600181019050610702565b508196508694505050505092915050565b600061075482610b8c565b8084526020840193508360208202850161076d85610b3b565b60005b848110156107a6578383038852610788838351610930565b925061079382610be8565b9150602088019750600181019050610770565b508196508694505050505092915050565b60006107c282610b97565b836020820285016107d285610b48565b60005b8481101561080b5783830388526107ed8383516109ea565b92506107f882610bf5565b91506020880197506001810190506107d5565b508196508694505050505092915050565b600061082782610ba2565b8084526020840193508360208202850161084085610b52565b60005b8481101561087957838303885261085b8383516109ea565b925061086682610c02565b9150602088019750600181019050610843565b508196508694505050505092915050565b61089381610bad565b61089c82610b5f565b60005b828110156108ce576108b2858351610a6e565b6108bb82610c0f565b915060208501945060018101905061089f565b5050505050565b60006108e082610bb8565b8084526020840193506108f283610b69565b60005b8281101561092457610908868351610a6e565b61091182610c1c565b91506020860195506001810190506108f5565b50849250505092915050565b600061093b82610bc3565b80845261094f816020860160208601610c33565b61095881610c66565b602085010191505092915050565b600060c08301600083015161097e6000860182610a6e565b50602083015184820360208601526109968282610930565b91505060408301516109ab604086018261088a565b50606083015184820360808601526109c382826108d5565b915050608083015184820360a08601526109dd82826106e4565b9150508091505092915050565b600060c083016000830151610a026000860182610a6e565b5060208301518482036020860152610a1a8282610930565b9150506040830151610a2f604086018261088a565b5060608301518482036080860152610a4782826108d5565b915050608083015184820360a0860152610a6182826106e4565b9150508091505092915050565b610a7781610c29565b82525050565b60006020820190508181036000830152610a97818461067f565b905092915050565b60006020820190508181036000830152610ab98184610749565b905092915050565b60006020820190508181036000830152610adb81846107b7565b905092915050565b60006020820190508181036000830152610afd818461081c565b905092915050565b60006020820190508181036000830152610b1f8184610966565b905092915050565b6000819050919050565b6000819050919050565b6000602082019050919050565b6000819050919050565b6000602082019050919050565b6000819050919050565b6000602082019050919050565b600060029050919050565b600060029050919050565b600081519050919050565b600060029050919050565b600081519050919050565b600060029050919050565b600081519050919050565b600081519050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000819050919050565b60005b83811015610c51578082015181840152602081019050610c36565b83811115610c60576000848401525b50505050565b6000601f19601f83011690509190505600a265627a7a72305820fdaf8ce6fe282a46498c8066d5b5ac382b69969eee38e1ad03c0d501b27f65366c6578706572696d656e74616cf50037")! - - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) - let allAddresses = try web3.eth.getAccounts() - var contract = web3.contract(abiString, at: nil, abiVersion: 2)! - + + let allAddresses = try ganache.eth.getAccounts() + var contract = ganache.contract(abiString, at: nil, abiVersion: 2)! + let parameters = [] as [AnyObject] let deployTx = contract.deploy(bytecode: bytecode, parameters: parameters)! deployTx.transactionOptions.from = allAddresses[0] @@ -27,33 +26,32 @@ class web3swiftAdvancedABIv2Tests: XCTestCase { let result = try deployTx.sendPromise().wait() let txHash = result.hash print("Transaction with hash " + txHash) - + Thread.sleep(forTimeInterval: 1.0) - - let receipt = try web3.eth.getTransactionReceipt(txHash) + + let receipt = try ganache.eth.getTransactionReceipt(txHash) print(receipt) - + switch receipt.status { case .notYetProcessed: return default: break } - - contract = web3.contract(abiString, at: receipt.contractAddress, abiVersion: 2)! + + contract = ganache.contract(abiString, at: receipt.contractAddress, abiVersion: 2)! let tx = contract.read("testSingle") let testSingle = try tx!.callPromise().wait() print(testSingle.description) } - + func testAdvancedABIv2staticArray() throws { let abiString = "[{\"constant\":true,\"inputs\":[],\"name\":\"testDynOfDyn\",\"outputs\":[{\"name\":\"ts\",\"type\":\"string[]\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"testStOfDyn\",\"outputs\":[{\"name\":\"ts\",\"type\":\"string[2]\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"testDynArray\",\"outputs\":[{\"components\":[{\"name\":\"number\",\"type\":\"uint256\"},{\"name\":\"someText\",\"type\":\"string\"},{\"name\":\"staticArray\",\"type\":\"uint256[2]\"},{\"name\":\"dynamicArray\",\"type\":\"uint256[]\"},{\"name\":\"anotherDynamicArray\",\"type\":\"string[2]\"}],\"name\":\"ts\",\"type\":\"tuple[]\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"testStaticArray\",\"outputs\":[{\"components\":[{\"name\":\"number\",\"type\":\"uint256\"},{\"name\":\"someText\",\"type\":\"string\"},{\"name\":\"staticArray\",\"type\":\"uint256[2]\"},{\"name\":\"dynamicArray\",\"type\":\"uint256[]\"},{\"name\":\"anotherDynamicArray\",\"type\":\"string[2]\"}],\"name\":\"ts\",\"type\":\"tuple[2]\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"testSingle\",\"outputs\":[{\"components\":[{\"name\":\"number\",\"type\":\"uint256\"},{\"name\":\"someText\",\"type\":\"string\"},{\"name\":\"staticArray\",\"type\":\"uint256[2]\"},{\"name\":\"dynamicArray\",\"type\":\"uint256[]\"},{\"name\":\"anotherDynamicArray\",\"type\":\"string[2]\"}],\"name\":\"t\",\"type\":\"tuple\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]" let bytecode = Data.fromHex("6080604052341561000f57600080fd5b610cb18061001e6000396000f30060806040526004361061006d576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063189533931461007257806338163ff51461009b57806388be6c65146100c4578063e7c1a47c146100ed578063f376e01314610116575b600080fd5b341561007d57600080fd5b61008561013f565b6040516100929190610a9f565b60405180910390f35b34156100a657600080fd5b6100ae610220565b6040516100bb9190610a7d565b60405180910390f35b34156100cf57600080fd5b6100d76102c5565b6040516100e49190610ae3565b60405180910390f35b34156100f857600080fd5b610100610350565b60405161010d9190610ac1565b60405180910390f35b341561012157600080fd5b610129610399565b6040516101369190610b05565b60405180910390f35b6060600260405190808252806020026020018201604052801561017657816020015b60608152602001906001900390816101615790505b5090506040805190810160405280600581526020017f48656c6c6f0000000000000000000000000000000000000000000000000000008152508160008151811015156101be57fe5b906020019060200201819052506040805190810160405280600581526020017f576f726c6400000000000000000000000000000000000000000000000000000081525081600181518110151561021057fe5b9060200190602002018190525090565b610228610546565b6040805190810160405280600581526020017f48656c6c6f00000000000000000000000000000000000000000000000000000081525081600060028110151561026d57fe5b60200201819052506040805190810160405280600581526020017f576f726c640000000000000000000000000000000000000000000000000000008152508160016002811015156102ba57fe5b602002018190525090565b6060600260405190808252806020026020018201604052801561030257816020015b6102ef61056d565b8152602001906001900390816102e75790505b50905061030d610399565b81600081518110151561031c57fe5b90602001906020020181905250610331610399565b81600181518110151561034057fe5b9060200190602002018190525090565b6103586105a9565b610360610399565b81600060028110151561036f57fe5b602002018190525061037f610399565b81600160028110151561038e57fe5b602002018190525090565b6103a16105d8565b60606103ab610614565b6103b3610546565b60036040519080825280602002602001820160405280156103e35781602001602082028038833980820191505090505b50925060008360008151811015156103f757fe5b9060200190602002018181525050600183600181518110151561041657fe5b9060200190602002018181525050600283600281518110151561043557fe5b90602001906020020181815250506040805190810160405280600081526020016001815250915060408051908101604052806040805190810160405280600581526020017f48656c6c6f00000000000000000000000000000000000000000000000000000081525081526020016040805190810160405280600581526020017f576f726c64000000000000000000000000000000000000000000000000000000815250815250905060a060405190810160405280600181526020016040805190810160405280600b81526020017f48656c6c6f20776f726c64000000000000000000000000000000000000000000815250815260200183815260200184815260200182815250935083935050505090565b60408051908101604052806002905b60608152602001906001900390816105555790505090565b60e060405190810160405280600081526020016060815260200161058f610636565b8152602001606081526020016105a3610658565b81525090565b6101c0604051908101604052806002905b6105c261056d565b8152602001906001900390816105ba5790505090565b60e06040519081016040528060008152602001606081526020016105fa610636565b81526020016060815260200161060e610658565b81525090565b6040805190810160405280600290602082028038833980820191505090505090565b6040805190810160405280600290602082028038833980820191505090505090565b60408051908101604052806002905b60608152602001906001900390816106675790505090565b600061068a82610b81565b8360208202850161069a85610b31565b60005b848110156106d35783830388526106b5838351610930565b92506106c082610bdb565b915060208801975060018101905061069d565b508196508694505050505092915050565b60006106ef82610b76565b836020820285016106ff85610b27565b60005b8481101561073857838303885261071a838351610930565b925061072582610bce565b9150602088019750600181019050610702565b508196508694505050505092915050565b600061075482610b8c565b8084526020840193508360208202850161076d85610b3b565b60005b848110156107a6578383038852610788838351610930565b925061079382610be8565b9150602088019750600181019050610770565b508196508694505050505092915050565b60006107c282610b97565b836020820285016107d285610b48565b60005b8481101561080b5783830388526107ed8383516109ea565b92506107f882610bf5565b91506020880197506001810190506107d5565b508196508694505050505092915050565b600061082782610ba2565b8084526020840193508360208202850161084085610b52565b60005b8481101561087957838303885261085b8383516109ea565b925061086682610c02565b9150602088019750600181019050610843565b508196508694505050505092915050565b61089381610bad565b61089c82610b5f565b60005b828110156108ce576108b2858351610a6e565b6108bb82610c0f565b915060208501945060018101905061089f565b5050505050565b60006108e082610bb8565b8084526020840193506108f283610b69565b60005b8281101561092457610908868351610a6e565b61091182610c1c565b91506020860195506001810190506108f5565b50849250505092915050565b600061093b82610bc3565b80845261094f816020860160208601610c33565b61095881610c66565b602085010191505092915050565b600060c08301600083015161097e6000860182610a6e565b50602083015184820360208601526109968282610930565b91505060408301516109ab604086018261088a565b50606083015184820360808601526109c382826108d5565b915050608083015184820360a08601526109dd82826106e4565b9150508091505092915050565b600060c083016000830151610a026000860182610a6e565b5060208301518482036020860152610a1a8282610930565b9150506040830151610a2f604086018261088a565b5060608301518482036080860152610a4782826108d5565b915050608083015184820360a0860152610a6182826106e4565b9150508091505092915050565b610a7781610c29565b82525050565b60006020820190508181036000830152610a97818461067f565b905092915050565b60006020820190508181036000830152610ab98184610749565b905092915050565b60006020820190508181036000830152610adb81846107b7565b905092915050565b60006020820190508181036000830152610afd818461081c565b905092915050565b60006020820190508181036000830152610b1f8184610966565b905092915050565b6000819050919050565b6000819050919050565b6000602082019050919050565b6000819050919050565b6000602082019050919050565b6000819050919050565b6000602082019050919050565b600060029050919050565b600060029050919050565b600081519050919050565b600060029050919050565b600081519050919050565b600060029050919050565b600081519050919050565b600081519050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000819050919050565b60005b83811015610c51578082015181840152602081019050610c36565b83811115610c60576000848401525b50505050565b6000601f19601f83011690509190505600a265627a7a72305820fdaf8ce6fe282a46498c8066d5b5ac382b69969eee38e1ad03c0d501b27f65366c6578706572696d656e74616cf50037")! - - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) - let allAddresses = try web3.eth.getAccounts() - var contract = web3.contract(abiString, at: nil, abiVersion: 2)! - + + let allAddresses = try ganache.eth.getAccounts() + var contract = ganache.contract(abiString, at: nil, abiVersion: 2)! + let parameters = [] as [AnyObject] let deployTx = contract.deploy(bytecode: bytecode, parameters: parameters)! deployTx.transactionOptions.from = allAddresses[0] @@ -61,33 +59,32 @@ class web3swiftAdvancedABIv2Tests: XCTestCase { let result = try deployTx.sendPromise().wait() let txHash = result.hash print("Transaction with hash " + txHash) - + Thread.sleep(forTimeInterval: 1.0) - - let receipt = try web3.eth.getTransactionReceipt(txHash) + + let receipt = try ganache.eth.getTransactionReceipt(txHash) print(receipt) - + switch receipt.status { case .notYetProcessed: return default: break } - - contract = web3.contract(abiString, at: receipt.contractAddress, abiVersion: 2)! + + contract = ganache.contract(abiString, at: receipt.contractAddress, abiVersion: 2)! let tx = contract.read("testStaticArray") let testStaticArray = try tx!.callPromise().wait() print(testStaticArray.description) } - + func testAdvancedABIv2dynamicArray() throws { let abiString = "[{\"constant\":true,\"inputs\":[],\"name\":\"testDynOfDyn\",\"outputs\":[{\"name\":\"ts\",\"type\":\"string[]\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"testStOfDyn\",\"outputs\":[{\"name\":\"ts\",\"type\":\"string[2]\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"testDynArray\",\"outputs\":[{\"components\":[{\"name\":\"number\",\"type\":\"uint256\"},{\"name\":\"someText\",\"type\":\"string\"},{\"name\":\"staticArray\",\"type\":\"uint256[2]\"},{\"name\":\"dynamicArray\",\"type\":\"uint256[]\"},{\"name\":\"anotherDynamicArray\",\"type\":\"string[2]\"}],\"name\":\"ts\",\"type\":\"tuple[]\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"testStaticArray\",\"outputs\":[{\"components\":[{\"name\":\"number\",\"type\":\"uint256\"},{\"name\":\"someText\",\"type\":\"string\"},{\"name\":\"staticArray\",\"type\":\"uint256[2]\"},{\"name\":\"dynamicArray\",\"type\":\"uint256[]\"},{\"name\":\"anotherDynamicArray\",\"type\":\"string[2]\"}],\"name\":\"ts\",\"type\":\"tuple[2]\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"testSingle\",\"outputs\":[{\"components\":[{\"name\":\"number\",\"type\":\"uint256\"},{\"name\":\"someText\",\"type\":\"string\"},{\"name\":\"staticArray\",\"type\":\"uint256[2]\"},{\"name\":\"dynamicArray\",\"type\":\"uint256[]\"},{\"name\":\"anotherDynamicArray\",\"type\":\"string[2]\"}],\"name\":\"t\",\"type\":\"tuple\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]" let bytecode = Data.fromHex("6080604052341561000f57600080fd5b610cb18061001e6000396000f30060806040526004361061006d576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063189533931461007257806338163ff51461009b57806388be6c65146100c4578063e7c1a47c146100ed578063f376e01314610116575b600080fd5b341561007d57600080fd5b61008561013f565b6040516100929190610a9f565b60405180910390f35b34156100a657600080fd5b6100ae610220565b6040516100bb9190610a7d565b60405180910390f35b34156100cf57600080fd5b6100d76102c5565b6040516100e49190610ae3565b60405180910390f35b34156100f857600080fd5b610100610350565b60405161010d9190610ac1565b60405180910390f35b341561012157600080fd5b610129610399565b6040516101369190610b05565b60405180910390f35b6060600260405190808252806020026020018201604052801561017657816020015b60608152602001906001900390816101615790505b5090506040805190810160405280600581526020017f48656c6c6f0000000000000000000000000000000000000000000000000000008152508160008151811015156101be57fe5b906020019060200201819052506040805190810160405280600581526020017f576f726c6400000000000000000000000000000000000000000000000000000081525081600181518110151561021057fe5b9060200190602002018190525090565b610228610546565b6040805190810160405280600581526020017f48656c6c6f00000000000000000000000000000000000000000000000000000081525081600060028110151561026d57fe5b60200201819052506040805190810160405280600581526020017f576f726c640000000000000000000000000000000000000000000000000000008152508160016002811015156102ba57fe5b602002018190525090565b6060600260405190808252806020026020018201604052801561030257816020015b6102ef61056d565b8152602001906001900390816102e75790505b50905061030d610399565b81600081518110151561031c57fe5b90602001906020020181905250610331610399565b81600181518110151561034057fe5b9060200190602002018190525090565b6103586105a9565b610360610399565b81600060028110151561036f57fe5b602002018190525061037f610399565b81600160028110151561038e57fe5b602002018190525090565b6103a16105d8565b60606103ab610614565b6103b3610546565b60036040519080825280602002602001820160405280156103e35781602001602082028038833980820191505090505b50925060008360008151811015156103f757fe5b9060200190602002018181525050600183600181518110151561041657fe5b9060200190602002018181525050600283600281518110151561043557fe5b90602001906020020181815250506040805190810160405280600081526020016001815250915060408051908101604052806040805190810160405280600581526020017f48656c6c6f00000000000000000000000000000000000000000000000000000081525081526020016040805190810160405280600581526020017f576f726c64000000000000000000000000000000000000000000000000000000815250815250905060a060405190810160405280600181526020016040805190810160405280600b81526020017f48656c6c6f20776f726c64000000000000000000000000000000000000000000815250815260200183815260200184815260200182815250935083935050505090565b60408051908101604052806002905b60608152602001906001900390816105555790505090565b60e060405190810160405280600081526020016060815260200161058f610636565b8152602001606081526020016105a3610658565b81525090565b6101c0604051908101604052806002905b6105c261056d565b8152602001906001900390816105ba5790505090565b60e06040519081016040528060008152602001606081526020016105fa610636565b81526020016060815260200161060e610658565b81525090565b6040805190810160405280600290602082028038833980820191505090505090565b6040805190810160405280600290602082028038833980820191505090505090565b60408051908101604052806002905b60608152602001906001900390816106675790505090565b600061068a82610b81565b8360208202850161069a85610b31565b60005b848110156106d35783830388526106b5838351610930565b92506106c082610bdb565b915060208801975060018101905061069d565b508196508694505050505092915050565b60006106ef82610b76565b836020820285016106ff85610b27565b60005b8481101561073857838303885261071a838351610930565b925061072582610bce565b9150602088019750600181019050610702565b508196508694505050505092915050565b600061075482610b8c565b8084526020840193508360208202850161076d85610b3b565b60005b848110156107a6578383038852610788838351610930565b925061079382610be8565b9150602088019750600181019050610770565b508196508694505050505092915050565b60006107c282610b97565b836020820285016107d285610b48565b60005b8481101561080b5783830388526107ed8383516109ea565b92506107f882610bf5565b91506020880197506001810190506107d5565b508196508694505050505092915050565b600061082782610ba2565b8084526020840193508360208202850161084085610b52565b60005b8481101561087957838303885261085b8383516109ea565b925061086682610c02565b9150602088019750600181019050610843565b508196508694505050505092915050565b61089381610bad565b61089c82610b5f565b60005b828110156108ce576108b2858351610a6e565b6108bb82610c0f565b915060208501945060018101905061089f565b5050505050565b60006108e082610bb8565b8084526020840193506108f283610b69565b60005b8281101561092457610908868351610a6e565b61091182610c1c565b91506020860195506001810190506108f5565b50849250505092915050565b600061093b82610bc3565b80845261094f816020860160208601610c33565b61095881610c66565b602085010191505092915050565b600060c08301600083015161097e6000860182610a6e565b50602083015184820360208601526109968282610930565b91505060408301516109ab604086018261088a565b50606083015184820360808601526109c382826108d5565b915050608083015184820360a08601526109dd82826106e4565b9150508091505092915050565b600060c083016000830151610a026000860182610a6e565b5060208301518482036020860152610a1a8282610930565b9150506040830151610a2f604086018261088a565b5060608301518482036080860152610a4782826108d5565b915050608083015184820360a0860152610a6182826106e4565b9150508091505092915050565b610a7781610c29565b82525050565b60006020820190508181036000830152610a97818461067f565b905092915050565b60006020820190508181036000830152610ab98184610749565b905092915050565b60006020820190508181036000830152610adb81846107b7565b905092915050565b60006020820190508181036000830152610afd818461081c565b905092915050565b60006020820190508181036000830152610b1f8184610966565b905092915050565b6000819050919050565b6000819050919050565b6000602082019050919050565b6000819050919050565b6000602082019050919050565b6000819050919050565b6000602082019050919050565b600060029050919050565b600060029050919050565b600081519050919050565b600060029050919050565b600081519050919050565b600060029050919050565b600081519050919050565b600081519050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000819050919050565b60005b83811015610c51578082015181840152602081019050610c36565b83811115610c60576000848401525b50505050565b6000601f19601f83011690509190505600a265627a7a72305820fdaf8ce6fe282a46498c8066d5b5ac382b69969eee38e1ad03c0d501b27f65366c6578706572696d656e74616cf50037")! - - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) - let allAddresses = try web3.eth.getAccounts() - var contract = web3.contract(abiString, at: nil, abiVersion: 2)! - + + let allAddresses = try ganache.eth.getAccounts() + var contract = ganache.contract(abiString, at: nil, abiVersion: 2)! + let parameters = [] as [AnyObject] let deployTx = contract.deploy(bytecode: bytecode, parameters: parameters)! deployTx.transactionOptions.from = allAddresses[0] @@ -95,33 +92,32 @@ class web3swiftAdvancedABIv2Tests: XCTestCase { let result = try deployTx.sendPromise().wait() let txHash = result.hash print("Transaction with hash " + txHash) - + Thread.sleep(forTimeInterval: 1.0) - - let receipt = try web3.eth.getTransactionReceipt(txHash) + + let receipt = try ganache.eth.getTransactionReceipt(txHash) print(receipt) - + switch receipt.status { case .notYetProcessed: return default: break } - - contract = web3.contract(abiString, at: receipt.contractAddress, abiVersion: 2)! + + contract = ganache.contract(abiString, at: receipt.contractAddress, abiVersion: 2)! let tx = contract.read("testDynArray") let testDynArray = try tx!.callPromise().wait() print(testDynArray.description) } - + func testAdvancedABIv2dynamicArrayOfStrings() throws { let abiString = "[{\"constant\":true,\"inputs\":[],\"name\":\"testDynOfDyn\",\"outputs\":[{\"name\":\"ts\",\"type\":\"string[]\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"testStOfDyn\",\"outputs\":[{\"name\":\"ts\",\"type\":\"string[2]\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"testDynArray\",\"outputs\":[{\"components\":[{\"name\":\"number\",\"type\":\"uint256\"},{\"name\":\"someText\",\"type\":\"string\"},{\"name\":\"staticArray\",\"type\":\"uint256[2]\"},{\"name\":\"dynamicArray\",\"type\":\"uint256[]\"},{\"name\":\"anotherDynamicArray\",\"type\":\"string[2]\"}],\"name\":\"ts\",\"type\":\"tuple[]\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"testStaticArray\",\"outputs\":[{\"components\":[{\"name\":\"number\",\"type\":\"uint256\"},{\"name\":\"someText\",\"type\":\"string\"},{\"name\":\"staticArray\",\"type\":\"uint256[2]\"},{\"name\":\"dynamicArray\",\"type\":\"uint256[]\"},{\"name\":\"anotherDynamicArray\",\"type\":\"string[2]\"}],\"name\":\"ts\",\"type\":\"tuple[2]\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"testSingle\",\"outputs\":[{\"components\":[{\"name\":\"number\",\"type\":\"uint256\"},{\"name\":\"someText\",\"type\":\"string\"},{\"name\":\"staticArray\",\"type\":\"uint256[2]\"},{\"name\":\"dynamicArray\",\"type\":\"uint256[]\"},{\"name\":\"anotherDynamicArray\",\"type\":\"string[2]\"}],\"name\":\"t\",\"type\":\"tuple\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]" let bytecode = Data.fromHex("6080604052341561000f57600080fd5b610cb18061001e6000396000f30060806040526004361061006d576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063189533931461007257806338163ff51461009b57806388be6c65146100c4578063e7c1a47c146100ed578063f376e01314610116575b600080fd5b341561007d57600080fd5b61008561013f565b6040516100929190610a9f565b60405180910390f35b34156100a657600080fd5b6100ae610220565b6040516100bb9190610a7d565b60405180910390f35b34156100cf57600080fd5b6100d76102c5565b6040516100e49190610ae3565b60405180910390f35b34156100f857600080fd5b610100610350565b60405161010d9190610ac1565b60405180910390f35b341561012157600080fd5b610129610399565b6040516101369190610b05565b60405180910390f35b6060600260405190808252806020026020018201604052801561017657816020015b60608152602001906001900390816101615790505b5090506040805190810160405280600581526020017f48656c6c6f0000000000000000000000000000000000000000000000000000008152508160008151811015156101be57fe5b906020019060200201819052506040805190810160405280600581526020017f576f726c6400000000000000000000000000000000000000000000000000000081525081600181518110151561021057fe5b9060200190602002018190525090565b610228610546565b6040805190810160405280600581526020017f48656c6c6f00000000000000000000000000000000000000000000000000000081525081600060028110151561026d57fe5b60200201819052506040805190810160405280600581526020017f576f726c640000000000000000000000000000000000000000000000000000008152508160016002811015156102ba57fe5b602002018190525090565b6060600260405190808252806020026020018201604052801561030257816020015b6102ef61056d565b8152602001906001900390816102e75790505b50905061030d610399565b81600081518110151561031c57fe5b90602001906020020181905250610331610399565b81600181518110151561034057fe5b9060200190602002018190525090565b6103586105a9565b610360610399565b81600060028110151561036f57fe5b602002018190525061037f610399565b81600160028110151561038e57fe5b602002018190525090565b6103a16105d8565b60606103ab610614565b6103b3610546565b60036040519080825280602002602001820160405280156103e35781602001602082028038833980820191505090505b50925060008360008151811015156103f757fe5b9060200190602002018181525050600183600181518110151561041657fe5b9060200190602002018181525050600283600281518110151561043557fe5b90602001906020020181815250506040805190810160405280600081526020016001815250915060408051908101604052806040805190810160405280600581526020017f48656c6c6f00000000000000000000000000000000000000000000000000000081525081526020016040805190810160405280600581526020017f576f726c64000000000000000000000000000000000000000000000000000000815250815250905060a060405190810160405280600181526020016040805190810160405280600b81526020017f48656c6c6f20776f726c64000000000000000000000000000000000000000000815250815260200183815260200184815260200182815250935083935050505090565b60408051908101604052806002905b60608152602001906001900390816105555790505090565b60e060405190810160405280600081526020016060815260200161058f610636565b8152602001606081526020016105a3610658565b81525090565b6101c0604051908101604052806002905b6105c261056d565b8152602001906001900390816105ba5790505090565b60e06040519081016040528060008152602001606081526020016105fa610636565b81526020016060815260200161060e610658565b81525090565b6040805190810160405280600290602082028038833980820191505090505090565b6040805190810160405280600290602082028038833980820191505090505090565b60408051908101604052806002905b60608152602001906001900390816106675790505090565b600061068a82610b81565b8360208202850161069a85610b31565b60005b848110156106d35783830388526106b5838351610930565b92506106c082610bdb565b915060208801975060018101905061069d565b508196508694505050505092915050565b60006106ef82610b76565b836020820285016106ff85610b27565b60005b8481101561073857838303885261071a838351610930565b925061072582610bce565b9150602088019750600181019050610702565b508196508694505050505092915050565b600061075482610b8c565b8084526020840193508360208202850161076d85610b3b565b60005b848110156107a6578383038852610788838351610930565b925061079382610be8565b9150602088019750600181019050610770565b508196508694505050505092915050565b60006107c282610b97565b836020820285016107d285610b48565b60005b8481101561080b5783830388526107ed8383516109ea565b92506107f882610bf5565b91506020880197506001810190506107d5565b508196508694505050505092915050565b600061082782610ba2565b8084526020840193508360208202850161084085610b52565b60005b8481101561087957838303885261085b8383516109ea565b925061086682610c02565b9150602088019750600181019050610843565b508196508694505050505092915050565b61089381610bad565b61089c82610b5f565b60005b828110156108ce576108b2858351610a6e565b6108bb82610c0f565b915060208501945060018101905061089f565b5050505050565b60006108e082610bb8565b8084526020840193506108f283610b69565b60005b8281101561092457610908868351610a6e565b61091182610c1c565b91506020860195506001810190506108f5565b50849250505092915050565b600061093b82610bc3565b80845261094f816020860160208601610c33565b61095881610c66565b602085010191505092915050565b600060c08301600083015161097e6000860182610a6e565b50602083015184820360208601526109968282610930565b91505060408301516109ab604086018261088a565b50606083015184820360808601526109c382826108d5565b915050608083015184820360a08601526109dd82826106e4565b9150508091505092915050565b600060c083016000830151610a026000860182610a6e565b5060208301518482036020860152610a1a8282610930565b9150506040830151610a2f604086018261088a565b5060608301518482036080860152610a4782826108d5565b915050608083015184820360a0860152610a6182826106e4565b9150508091505092915050565b610a7781610c29565b82525050565b60006020820190508181036000830152610a97818461067f565b905092915050565b60006020820190508181036000830152610ab98184610749565b905092915050565b60006020820190508181036000830152610adb81846107b7565b905092915050565b60006020820190508181036000830152610afd818461081c565b905092915050565b60006020820190508181036000830152610b1f8184610966565b905092915050565b6000819050919050565b6000819050919050565b6000602082019050919050565b6000819050919050565b6000602082019050919050565b6000819050919050565b6000602082019050919050565b600060029050919050565b600060029050919050565b600081519050919050565b600060029050919050565b600081519050919050565b600060029050919050565b600081519050919050565b600081519050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000819050919050565b60005b83811015610c51578082015181840152602081019050610c36565b83811115610c60576000848401525b50505050565b6000601f19601f83011690509190505600a265627a7a72305820fdaf8ce6fe282a46498c8066d5b5ac382b69969eee38e1ad03c0d501b27f65366c6578706572696d656e74616cf50037")! - - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) - let allAddresses = try web3.eth.getAccounts() - var contract = web3.contract(abiString, at: nil, abiVersion: 2)! - + + let allAddresses = try ganache.eth.getAccounts() + var contract = ganache.contract(abiString, at: nil, abiVersion: 2)! + let parameters = [] as [AnyObject] let deployTx = contract.deploy(bytecode: bytecode, parameters: parameters)! deployTx.transactionOptions.from = allAddresses[0] @@ -129,33 +125,32 @@ class web3swiftAdvancedABIv2Tests: XCTestCase { let result = try deployTx.sendPromise().wait() let txHash = result.hash print("Transaction with hash " + txHash) - + Thread.sleep(forTimeInterval: 1.0) - - let receipt = try web3.eth.getTransactionReceipt(txHash) + + let receipt = try ganache.eth.getTransactionReceipt(txHash) print(receipt) - + switch receipt.status { case .notYetProcessed: return default: break } - - contract = web3.contract(abiString, at: receipt.contractAddress, abiVersion: 2)! + + contract = ganache.contract(abiString, at: receipt.contractAddress, abiVersion: 2)! let tx = contract.read("testDynOfDyn") let testDynOfDyn = try tx!.callPromise().wait() print(testDynOfDyn.description) } - + func testAdvancedABIv2staticArrayOfStrings() throws { let abiString = "[{\"constant\":true,\"inputs\":[],\"name\":\"testDynOfDyn\",\"outputs\":[{\"name\":\"ts\",\"type\":\"string[]\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"testStOfDyn\",\"outputs\":[{\"name\":\"ts\",\"type\":\"string[2]\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"testDynArray\",\"outputs\":[{\"components\":[{\"name\":\"number\",\"type\":\"uint256\"},{\"name\":\"someText\",\"type\":\"string\"},{\"name\":\"staticArray\",\"type\":\"uint256[2]\"},{\"name\":\"dynamicArray\",\"type\":\"uint256[]\"},{\"name\":\"anotherDynamicArray\",\"type\":\"string[2]\"}],\"name\":\"ts\",\"type\":\"tuple[]\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"testStaticArray\",\"outputs\":[{\"components\":[{\"name\":\"number\",\"type\":\"uint256\"},{\"name\":\"someText\",\"type\":\"string\"},{\"name\":\"staticArray\",\"type\":\"uint256[2]\"},{\"name\":\"dynamicArray\",\"type\":\"uint256[]\"},{\"name\":\"anotherDynamicArray\",\"type\":\"string[2]\"}],\"name\":\"ts\",\"type\":\"tuple[2]\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"testSingle\",\"outputs\":[{\"components\":[{\"name\":\"number\",\"type\":\"uint256\"},{\"name\":\"someText\",\"type\":\"string\"},{\"name\":\"staticArray\",\"type\":\"uint256[2]\"},{\"name\":\"dynamicArray\",\"type\":\"uint256[]\"},{\"name\":\"anotherDynamicArray\",\"type\":\"string[2]\"}],\"name\":\"t\",\"type\":\"tuple\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]" let bytecode = Data.fromHex("6080604052341561000f57600080fd5b610cb18061001e6000396000f30060806040526004361061006d576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063189533931461007257806338163ff51461009b57806388be6c65146100c4578063e7c1a47c146100ed578063f376e01314610116575b600080fd5b341561007d57600080fd5b61008561013f565b6040516100929190610a9f565b60405180910390f35b34156100a657600080fd5b6100ae610220565b6040516100bb9190610a7d565b60405180910390f35b34156100cf57600080fd5b6100d76102c5565b6040516100e49190610ae3565b60405180910390f35b34156100f857600080fd5b610100610350565b60405161010d9190610ac1565b60405180910390f35b341561012157600080fd5b610129610399565b6040516101369190610b05565b60405180910390f35b6060600260405190808252806020026020018201604052801561017657816020015b60608152602001906001900390816101615790505b5090506040805190810160405280600581526020017f48656c6c6f0000000000000000000000000000000000000000000000000000008152508160008151811015156101be57fe5b906020019060200201819052506040805190810160405280600581526020017f576f726c6400000000000000000000000000000000000000000000000000000081525081600181518110151561021057fe5b9060200190602002018190525090565b610228610546565b6040805190810160405280600581526020017f48656c6c6f00000000000000000000000000000000000000000000000000000081525081600060028110151561026d57fe5b60200201819052506040805190810160405280600581526020017f576f726c640000000000000000000000000000000000000000000000000000008152508160016002811015156102ba57fe5b602002018190525090565b6060600260405190808252806020026020018201604052801561030257816020015b6102ef61056d565b8152602001906001900390816102e75790505b50905061030d610399565b81600081518110151561031c57fe5b90602001906020020181905250610331610399565b81600181518110151561034057fe5b9060200190602002018190525090565b6103586105a9565b610360610399565b81600060028110151561036f57fe5b602002018190525061037f610399565b81600160028110151561038e57fe5b602002018190525090565b6103a16105d8565b60606103ab610614565b6103b3610546565b60036040519080825280602002602001820160405280156103e35781602001602082028038833980820191505090505b50925060008360008151811015156103f757fe5b9060200190602002018181525050600183600181518110151561041657fe5b9060200190602002018181525050600283600281518110151561043557fe5b90602001906020020181815250506040805190810160405280600081526020016001815250915060408051908101604052806040805190810160405280600581526020017f48656c6c6f00000000000000000000000000000000000000000000000000000081525081526020016040805190810160405280600581526020017f576f726c64000000000000000000000000000000000000000000000000000000815250815250905060a060405190810160405280600181526020016040805190810160405280600b81526020017f48656c6c6f20776f726c64000000000000000000000000000000000000000000815250815260200183815260200184815260200182815250935083935050505090565b60408051908101604052806002905b60608152602001906001900390816105555790505090565b60e060405190810160405280600081526020016060815260200161058f610636565b8152602001606081526020016105a3610658565b81525090565b6101c0604051908101604052806002905b6105c261056d565b8152602001906001900390816105ba5790505090565b60e06040519081016040528060008152602001606081526020016105fa610636565b81526020016060815260200161060e610658565b81525090565b6040805190810160405280600290602082028038833980820191505090505090565b6040805190810160405280600290602082028038833980820191505090505090565b60408051908101604052806002905b60608152602001906001900390816106675790505090565b600061068a82610b81565b8360208202850161069a85610b31565b60005b848110156106d35783830388526106b5838351610930565b92506106c082610bdb565b915060208801975060018101905061069d565b508196508694505050505092915050565b60006106ef82610b76565b836020820285016106ff85610b27565b60005b8481101561073857838303885261071a838351610930565b925061072582610bce565b9150602088019750600181019050610702565b508196508694505050505092915050565b600061075482610b8c565b8084526020840193508360208202850161076d85610b3b565b60005b848110156107a6578383038852610788838351610930565b925061079382610be8565b9150602088019750600181019050610770565b508196508694505050505092915050565b60006107c282610b97565b836020820285016107d285610b48565b60005b8481101561080b5783830388526107ed8383516109ea565b92506107f882610bf5565b91506020880197506001810190506107d5565b508196508694505050505092915050565b600061082782610ba2565b8084526020840193508360208202850161084085610b52565b60005b8481101561087957838303885261085b8383516109ea565b925061086682610c02565b9150602088019750600181019050610843565b508196508694505050505092915050565b61089381610bad565b61089c82610b5f565b60005b828110156108ce576108b2858351610a6e565b6108bb82610c0f565b915060208501945060018101905061089f565b5050505050565b60006108e082610bb8565b8084526020840193506108f283610b69565b60005b8281101561092457610908868351610a6e565b61091182610c1c565b91506020860195506001810190506108f5565b50849250505092915050565b600061093b82610bc3565b80845261094f816020860160208601610c33565b61095881610c66565b602085010191505092915050565b600060c08301600083015161097e6000860182610a6e565b50602083015184820360208601526109968282610930565b91505060408301516109ab604086018261088a565b50606083015184820360808601526109c382826108d5565b915050608083015184820360a08601526109dd82826106e4565b9150508091505092915050565b600060c083016000830151610a026000860182610a6e565b5060208301518482036020860152610a1a8282610930565b9150506040830151610a2f604086018261088a565b5060608301518482036080860152610a4782826108d5565b915050608083015184820360a0860152610a6182826106e4565b9150508091505092915050565b610a7781610c29565b82525050565b60006020820190508181036000830152610a97818461067f565b905092915050565b60006020820190508181036000830152610ab98184610749565b905092915050565b60006020820190508181036000830152610adb81846107b7565b905092915050565b60006020820190508181036000830152610afd818461081c565b905092915050565b60006020820190508181036000830152610b1f8184610966565b905092915050565b6000819050919050565b6000819050919050565b6000602082019050919050565b6000819050919050565b6000602082019050919050565b6000819050919050565b6000602082019050919050565b600060029050919050565b600060029050919050565b600081519050919050565b600060029050919050565b600081519050919050565b600060029050919050565b600081519050919050565b600081519050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000819050919050565b60005b83811015610c51578082015181840152602081019050610c36565b83811115610c60576000848401525b50505050565b6000601f19601f83011690509190505600a265627a7a72305820fdaf8ce6fe282a46498c8066d5b5ac382b69969eee38e1ad03c0d501b27f65366c6578706572696d656e74616cf50037")! - - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) - let allAddresses = try web3.eth.getAccounts() - var contract = web3.contract(abiString, at: nil, abiVersion: 2)! - + + let allAddresses = try ganache.eth.getAccounts() + var contract = ganache.contract(abiString, at: nil, abiVersion: 2)! + let parameters = [] as [AnyObject] let deployTx = contract.deploy(bytecode: bytecode, parameters: parameters)! deployTx.transactionOptions.from = allAddresses[0] @@ -163,45 +158,43 @@ class web3swiftAdvancedABIv2Tests: XCTestCase { let result = try deployTx.sendPromise().wait() let txHash = result.hash print("Transaction with hash " + txHash) - + Thread.sleep(forTimeInterval: 1.0) - - let receipt = try web3.eth.getTransactionReceipt(txHash) + + let receipt = try ganache.eth.getTransactionReceipt(txHash) print(receipt) - + switch receipt.status { case .notYetProcessed: return default: break } - - contract = web3.contract(abiString, at: receipt.contractAddress, abiVersion: 2)! + + contract = ganache.contract(abiString, at: receipt.contractAddress, abiVersion: 2)! let tx = contract.read("testStOfDyn") let testStOfDyn = try tx!.callPromise().wait() print(testStOfDyn.description) } - + func testEmptyArrayDecoding() throws { let abiString = "[{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"constant\":true,\"inputs\":[],\"name\":\"empty\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256[]\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"}]" let contractAddress = EthereumAddress("0x200eb5ccda1c35b0f5bf82552fdd65a8aee98e79")! - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) - let contract = web3.contract(abiString, at: contractAddress, abiVersion: 2) + let contract = ganache.contract(abiString, at: contractAddress, abiVersion: 2) XCTAssert(contract != nil) let tx = contract?.read("empty") XCTAssertNotNil(tx) let _ = try tx!.callPromise().wait() } - + func testUserCase() throws { let abiString = "[{\"constant\":true,\"inputs\":[],\"name\":\"getFlagData\",\"outputs\":[{\"name\":\"data\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"data\",\"type\":\"string\"}],\"name\":\"setFlagData\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]" let contractAddress = EthereumAddress("0x811411e3cdfd4750cdd3552feb3b89a46ddb612e") - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) - let contract = web3.contract(abiString, at: contractAddress, abiVersion: 2) + let contract = ganache.contract(abiString, at: contractAddress, abiVersion: 2) XCTAssert(contract != nil) let tx = contract?.read("getFlagData") XCTAssertNotNil(tx) let _ = try tx!.call() } - + } diff --git a/Tests/web3swiftTests/localTests/web3swiftBasicLocalNodeTests.swift b/Tests/web3swiftTests/localTests/web3swiftBasicLocalNodeTests.swift index 45eb59935..f7565c858 100755 --- a/Tests/web3swiftTests/localTests/web3swiftBasicLocalNodeTests.swift +++ b/Tests/web3swiftTests/localTests/web3swiftBasicLocalNodeTests.swift @@ -12,85 +12,83 @@ import BigInt @testable import web3swift -class web3swiftBasicLocalNodeTests: XCTestCase { +class web3swiftBasicLocalNodeTests: LocalTestCase { func testDeployWithRemoteSigning() throws { - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) - let allAddresses = try web3.eth.getAccounts() - + let allAddresses = try ganache.eth.getAccounts() + let abiString = "[{\"constant\":true,\"inputs\":[],\"name\":\"getFlagData\",\"outputs\":[{\"name\":\"data\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"data\",\"type\":\"string\"}],\"name\":\"setFlagData\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]" let bytecode = Data.fromHex("6060604052341561000f57600080fd5b6103358061001e6000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063a16e94bf14610051578063a46b5b6b146100df575b600080fd5b341561005c57600080fd5b61006461013c565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100a4578082015181840152602081019050610089565b50505050905090810190601f1680156100d15780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156100ea57600080fd5b61013a600480803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509190505061020d565b005b610144610250565b6000808073ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102035780601f106101d857610100808354040283529160200191610203565b820191906000526020600020905b8154815290600101906020018083116101e657829003601f168201915b5050505050905090565b806000808073ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600001908051906020019061024c929190610264565b5050565b602060405190810160405280600081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102a557805160ff19168380011785556102d3565b828001600101855582156102d3579182015b828111156102d25782518255916020019190600101906102b7565b5b5090506102e091906102e4565b5090565b61030691905b808211156103025760008160009055506001016102ea565b5090565b905600a165627a7a7230582017359d063cd7fdf56f19ca186a54863ce855c8f070acece905d8538fbbc4d1bf0029")! - - let contract = web3.contract(abiString, at: nil, abiVersion: 2)! - + + let contract = ganache.contract(abiString, at: nil, abiVersion: 2)! + let parameters = [] as [AnyObject] let deployTx = contract.deploy(bytecode: bytecode, parameters: parameters)! deployTx.transactionOptions.from = allAddresses[0] deployTx.transactionOptions.gasLimit = .manual(3000000) - + let result = try deployTx.sendPromise().wait() let txHash = result.hash print("Transaction with hash " + txHash) - + Thread.sleep(forTimeInterval: 1.0) - - let receipt = try web3.eth.getTransactionReceipt(txHash) + + let receipt = try ganache.eth.getTransactionReceipt(txHash) print(receipt) - + switch receipt.status { case .notYetProcessed: return default: break } - - let details = try web3.eth.getTransactionDetails(txHash) + + let details = try ganache.eth.getTransactionDetails(txHash) print(details) } func testEthSendExampleWithRemoteSigning() throws { - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) - let allAddresses = try web3.eth.getAccounts() + let allAddresses = try ganache.eth.getAccounts() let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")! - let contract = web3.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)! - + let contract = ganache.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)! + let parameters = [] as [AnyObject] let sendTx = contract.method("fallback", parameters: parameters)! - + let valueToSend = Web3.Utils.parseToBigUInt("1.0", units: .eth) sendTx.transactionOptions.value = valueToSend sendTx.transactionOptions.from = allAddresses[0] - - let balanceBeforeTo = try web3.eth.getBalancePromise(address: sendToAddress).wait() - let balanceBeforeFrom = try web3.eth.getBalancePromise(address: allAddresses[0]).wait() + + let balanceBeforeTo = try ganache.eth.getBalancePromise(address: sendToAddress).wait() + let balanceBeforeFrom = try ganache.eth.getBalancePromise(address: allAddresses[0]).wait() print("Balance before to: " + balanceBeforeTo.description) print("Balance before from: " + balanceBeforeFrom.description) - + let result = try sendTx.sendPromise().wait() let txHash = result.hash print("Transaction with hash " + txHash) - + Thread.sleep(forTimeInterval: 1.0) - - let receipt = try web3.eth.getTransactionReceipt(txHash) + + let receipt = try ganache.eth.getTransactionReceipt(txHash) print(receipt) - + switch receipt.status { case .notYetProcessed: return default: break } - - let details = try web3.eth.getTransactionDetails(txHash) + + let details = try ganache.eth.getTransactionDetails(txHash) print(details) - - - let balanceAfterTo = try web3.eth.getBalancePromise(address: sendToAddress).wait() - let balanceAfterFrom = try web3.eth.getBalancePromise(address: allAddresses[0]).wait() + + + let balanceAfterTo = try ganache.eth.getBalancePromise(address: sendToAddress).wait() + let balanceAfterFrom = try ganache.eth.getBalancePromise(address: allAddresses[0]).wait() print("Balance after to: " + balanceAfterTo.description) print("Balance after from: " + balanceAfterFrom.description) - + XCTAssert(balanceAfterTo - balanceBeforeTo == valueToSend) let txnGasPrice = details.transaction.parameters.gasPrice ?? 0 XCTAssert(balanceBeforeFrom - (balanceAfterFrom + receipt.gasUsed * txnGasPrice) == valueToSend) @@ -99,21 +97,19 @@ class web3swiftBasicLocalNodeTests: XCTestCase { // FIXME: Crashes on CI/CD // FIXME: Fails on ganache // func testSignPersonal() throws { - // let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) - // let allAddresses = try web3.eth.getAccounts() - - // let response = try web3.personal.signPersonalMessage(message: "hello world".data(using: .utf8)!, from: allAddresses[0]) - + // let allAddresses = try ganache.eth.getAccounts() + + // let response = try ganache.personal.signPersonalMessage(message: "hello world".data(using: .utf8)!, from: allAddresses[0]) + // XCTAssert(response.toHexString() == "b686c8ddc854bd49de9eb62eb4e52af4c69a89802b40fe9a295e346b111406393c6e3f05114561ab845a47196ad22c33cec67592af9a9e42bfc067a20c7d4b6101") // } - + // MARK: Ganache doesn't support a mempool for now // func testTxPoolStatus() throws { -// let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) -// let allAddresses = try web3.eth.getAccounts() +// let allAddresses = try ganache.eth.getAccounts() // // let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")! -// let contract = web3.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)! +// let contract = ganache.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)! // // let parameters = [] as [AnyObject] // let sendTx = contract.method("fallback", parameters: parameters)! @@ -124,17 +120,16 @@ class web3swiftBasicLocalNodeTests: XCTestCase { // // let _ = try sendTx.sendPromise().wait() // -// let result = try web3.txPool.getStatus() +// let result = try ganache.txPool.getStatus() // // print(result) // } // // func testTxPoolInspect() throws { -// let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) -// let allAddresses = try web3.eth.getAccounts() +// let allAddresses = try ganache.eth.getAccounts() // // let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")! -// let contract = web3.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)! +// let contract = ganache.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)! // // let parameters = [] as [AnyObject] // let sendTx = contract.method("fallback", parameters: parameters)! @@ -145,17 +140,16 @@ class web3swiftBasicLocalNodeTests: XCTestCase { // // let _ = try sendTx.sendPromise().wait() // -// let result = try web3.txPool.getInspect() +// let result = try ganache.txPool.getInspect() // // print(result) // } // // func testTxPoolContent() throws { -// let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) -// let allAddresses = try web3.eth.getAccounts() +// let allAddresses = try ganache.eth.getAccounts() // // let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")! -// let contract = web3.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)! +// let contract = ganache.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)! // // let parameters = [] as [AnyObject] // let sendTx = contract.method("fallback", parameters: parameters)! @@ -166,7 +160,7 @@ class web3swiftBasicLocalNodeTests: XCTestCase { // // let _ = try sendTx.sendPromise().wait() // -// let result = try web3.txPool.getContent() +// let result = try ganache.txPool.getContent() // // print(result) // } diff --git a/Tests/web3swiftTests/localTests/web3swiftDecodeSolidityErrorType.swift b/Tests/web3swiftTests/localTests/web3swiftDecodeSolidityErrorType.swift index 43cdd901f..01de061ca 100644 --- a/Tests/web3swiftTests/localTests/web3swiftDecodeSolidityErrorType.swift +++ b/Tests/web3swiftTests/localTests/web3swiftDecodeSolidityErrorType.swift @@ -11,12 +11,11 @@ import web3swift /// Since solidity 0.8.4 a new type was introduced called `error`. /// Contracts' ABI with this type were not decodable. -class web3swiftDecodeSolidityErrorType: XCTestCase { +class web3swiftDecodeSolidityErrorType: LocalTestCase { func testStructuredErrorTypeDecoding() throws { let contractAbiWithErrorTypes = "[{\"inputs\":[{\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"disallowedAddress\",\"type\":\"address\"}],\"name\":\"NotAllowedAddress\",\"type\":\"error\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"internalType\":\"bytes4\",\"name\":\"disallowedFunction\",\"type\":\"bytes4\"}],\"name\":\"NotAllowedFunction\",\"type\":\"error\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"internalType\":\"string\",\"name\":\"permission\",\"type\":\"string\"}],\"name\":\"NotAuthorised\",\"type\":\"error\"}]" - let web3Instance = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) - let contract = web3.web3contract(web3: web3Instance, abiString: contractAbiWithErrorTypes) + let contract = web3.web3contract(web3: ganache, abiString: contractAbiWithErrorTypes) assert(contract != nil) } } diff --git a/Tests/web3swiftTests/localTests/web3swiftEIP67Tests.swift b/Tests/web3swiftTests/localTests/web3swiftEIP67Tests.swift index 1cebc28b3..722bd0ed6 100755 --- a/Tests/web3swiftTests/localTests/web3swiftEIP67Tests.swift +++ b/Tests/web3swiftTests/localTests/web3swiftEIP67Tests.swift @@ -10,8 +10,8 @@ import BigInt @testable import web3swift -class web3swiftEIP67Tests: XCTestCase { - +class web3swiftEIP67Tests: LocalTestCase { + func testEIP67encoding() throws { var eip67Data = Web3.EIP67Code.init(address: EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!) eip67Data.gasLimit = BigUInt(21000) @@ -20,7 +20,7 @@ class web3swiftEIP67Tests: XCTestCase { let encoding = eip67Data.toString() print(encoding) } - + func testEIP67codeGeneration() throws { var eip67Data = Web3.EIP67Code.init(address: EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!) eip67Data.gasLimit = BigUInt(21000) @@ -29,18 +29,17 @@ class web3swiftEIP67Tests: XCTestCase { let encoding = eip67Data.toImage(scale: 5.0) XCTAssert(encoding != CIImage()) } - + func testEIP67decoding() throws { var eip67Data = Web3.EIP67Code.init(address: EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!) eip67Data.gasLimit = BigUInt(21000) eip67Data.amount = BigUInt("1000000000000000000") // eip67Data.data = let encoding = eip67Data.toString() - guard let code = Web3.EIP67CodeParser.parse(encoding) else {return XCTFail()} + guard let code = Web3.EIP67CodeParser.parse(encoding) else { return XCTFail() } XCTAssert(code.address == eip67Data.address) XCTAssert(code.gasLimit == eip67Data.gasLimit) XCTAssert(code.amount == eip67Data.amount) } - -} +} diff --git a/Tests/web3swiftTests/localTests/web3swiftEIP681Tests.swift b/Tests/web3swiftTests/localTests/web3swiftEIP681Tests.swift index d8cd76b61..7246cf168 100755 --- a/Tests/web3swiftTests/localTests/web3swiftEIP681Tests.swift +++ b/Tests/web3swiftTests/localTests/web3swiftEIP681Tests.swift @@ -7,25 +7,24 @@ import XCTest @testable import web3swift -class web3swiftEIP681Tests: XCTestCase { - - //Custom payment - //ethereum:0xfb6916095ca1df60bb79Ce92ce3ea74c37c5d359?value=2.014e18 - - - //ERC20 transfer - //ethereum:0x8932404A197D84Ec3Ea55971AADE11cdA1dddff1/transfer?address=0x6891dC3962e710f0ff711B9c6acc26133Fd35Cb4&uint256=1 - +class web3swiftEIP681Tests: LocalTestCase { + + // Custom payment + // ethereum:0xfb6916095ca1df60bb79Ce92ce3ea74c37c5d359?value=2.014e18 + + // ERC20 transfer + // ethereum:0x8932404A197D84Ec3Ea55971AADE11cdA1dddff1/transfer?address=0x6891dC3962e710f0ff711B9c6acc26133Fd35Cb4&uint256=1 + func testEIP681Parsing() throws { let parsed = Web3.EIP681CodeParser.parse("ethereum:0x5ffc014343cd971b7eb70732021e26c35b744cc4?value=2.014e18") XCTAssert(parsed != nil) } - + func testEIP681Parsing2() throws { let parsed = Web3.EIP681CodeParser.parse("ethereum:0x8932404A197D84Ec3Ea55971AADE11cdA1dddff1/transfer?address=0x6891dC3962e710f0ff711B9c6acc26133Fd35Cb4&uint256=1") XCTAssert(parsed != nil) } - + func testEIP681ENSParsing() throws { let parsed = Web3.EIP681CodeParser.parse("ethereum:somename.eth/transfer?address=somename.eth&uint256=1") XCTAssert(parsed != nil) diff --git a/Tests/web3swiftTests/localTests/web3swiftERC20ClassTests.swift b/Tests/web3swiftTests/localTests/web3swiftERC20ClassTests.swift index 0d114f5ec..62122f38c 100755 --- a/Tests/web3swiftTests/localTests/web3swiftERC20ClassTests.swift +++ b/Tests/web3swiftTests/localTests/web3swiftERC20ClassTests.swift @@ -9,23 +9,23 @@ import BigInt @testable import web3swift -class web3swiftERC20ClassTests: XCTestCase { - +class web3swiftERC20ClassTests: LocalTestCase { + func testERC20TokenCreation() throws { - let (web3, _, receipt, _) = try web3swiftHelpers.localDeployERC20() - let erc20token = ERC20.init(web3: web3, provider: web3.provider, address: receipt.contractAddress!) + let (_, receipt, _) = try web3swiftHelpers.localDeployERC20(ganache) + let erc20token = ERC20.init(web3: ganache, provider: ganache.provider, address: receipt.contractAddress!) erc20token.readProperties() XCTAssert(erc20token.symbol == "w3s") XCTAssert(erc20token.name == "web3swift") XCTAssert(erc20token.decimals == 18) } - + func testERC20tokenBalanceAndAllowance() throws { - let (web3, _, receipt, _) = try web3swiftHelpers.localDeployERC20() - let erc20token = ERC20.init(web3: web3, provider: web3.provider, address: receipt.contractAddress!) - + let (_, receipt, _) = try web3swiftHelpers.localDeployERC20(ganache) + let erc20token = ERC20.init(web3: ganache, provider: ganache.provider, address: receipt.contractAddress!) + let userAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")! - + let balance = try erc20token.getBalance(account: userAddress) let allowance = try erc20token.getAllowance(originalOwner: userAddress, delegate: userAddress) XCTAssert(String(balance) == "1024") diff --git a/Tests/web3swiftTests/localTests/web3swiftERC20Tests.swift b/Tests/web3swiftTests/localTests/web3swiftERC20Tests.swift index d4f0d8e7e..a5d179fbc 100755 --- a/Tests/web3swiftTests/localTests/web3swiftERC20Tests.swift +++ b/Tests/web3swiftTests/localTests/web3swiftERC20Tests.swift @@ -9,30 +9,30 @@ import BigInt @testable import web3swift -class web3swiftERC20Tests: XCTestCase { - +class web3swiftERC20Tests: LocalTestCase { + func testERC20name() throws { - let (web3, _, receipt, _) = try web3swiftHelpers.localDeployERC20() - + let (_, receipt, _) = try web3swiftHelpers.localDeployERC20(ganache) + let parameters = [] as [AnyObject] - let contract = web3.contract(Web3.Utils.erc20ABI, at: receipt.contractAddress!)! - let readTX = contract.read("name", parameters:parameters)! + let contract = ganache.contract(Web3.Utils.erc20ABI, at: receipt.contractAddress!)! + let readTX = contract.read("name", parameters: parameters)! readTX.transactionOptions.from = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901") let response = try readTX.callPromise().wait() let name = response["0"] as? String XCTAssert(name == "web3swift", "Failed to create ERC20 name transaction") } - + func testERC20tokenBalance() throws { - let (web3, _, receipt, _) = try web3swiftHelpers.localDeployERC20() - + let (_, receipt, _) = try web3swiftHelpers.localDeployERC20(ganache) + let addressOfUser = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")! - let contract = web3.contract(Web3.Utils.erc20ABI, at: receipt.contractAddress!, abiVersion: 2)! - guard let readTX = contract.read("balanceOf", parameters: [addressOfUser] as [AnyObject]) else {return XCTFail()} + let contract = ganache.contract(Web3.Utils.erc20ABI, at: receipt.contractAddress!, abiVersion: 2)! + guard let readTX = contract.read("balanceOf", parameters: [addressOfUser] as [AnyObject]) else { return XCTFail() } readTX.transactionOptions.from = addressOfUser let tokenBalance = try readTX.callPromise().wait() - guard let bal = tokenBalance["0"] as? BigUInt else {return XCTFail()} + guard let bal = tokenBalance["0"] as? BigUInt else { return XCTFail() } print(String(bal)) } - + } diff --git a/Tests/web3swiftTests/localTests/web3swiftEventloopTests.swift b/Tests/web3swiftTests/localTests/web3swiftEventloopTests.swift index fafaf3ede..075b63718 100755 --- a/Tests/web3swiftTests/localTests/web3swiftEventloopTests.swift +++ b/Tests/web3swiftTests/localTests/web3swiftEventloopTests.swift @@ -8,7 +8,7 @@ import XCTest @testable import web3swift -class web3swiftEventloopTests: XCTestCase { +class web3swiftEventloopTests: LocalTestCase { func testBasicEventLoop() throws { var ticksToWait = 5 @@ -25,29 +25,27 @@ class web3swiftEventloopTests: XCTestCase { print(error) } } - - let web3main = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) + let functionToCall: web3.Eventloop.EventLoopCall = getBlockNumber - let monitoredProperty = web3.Eventloop.MonitoredProperty.init(name: "onNewBlock", queue: web3main.requestDispatcher.queue, calledFunction: functionToCall) - web3main.eventLoop.monitoredProperties.append(monitoredProperty) - web3main.eventLoop.start(5) - + let monitoredProperty = web3.Eventloop.MonitoredProperty.init(name: "onNewBlock", queue: ganache.requestDispatcher.queue, calledFunction: functionToCall) + ganache.eventLoop.monitoredProperties.append(monitoredProperty) + ganache.eventLoop.start(5) + waitForExpectations(timeout: 60, handler: nil) } - + // func testNonceMiddleware() throws { - // let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) // let middleware = Web3.Utils.NonceMiddleware() - // middleware.attach(web3) + // middleware.attach(ganache) // let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")! // let ksData = getKeystoreData() // FIXME: Ganache crash here // let tempKeystore = EthereumKeystoreV3(ksData!) // let keystoreManager = KeystoreManager([tempKeystore!]) - // web3.addKeystoreManager(keystoreManager) + // ganache.addKeystoreManager(keystoreManager) - // var tx = web3.eth.sendETH(to: sendToAddress, amount: 1000) + // var tx = ganache.eth.sendETH(to: sendToAddress, amount: 1000) // tx!.transactionOptions.from = tempKeystore!.addresses!.first! // var result = try! tx!.send(password: "web3swift") // let newNonce = result.transaction.nonce @@ -55,7 +53,7 @@ class web3swiftEventloopTests: XCTestCase { // let hookNewNonce = middleware.nonceLookups[tempKeystore!.addresses!.first!]! // XCTAssertEqual(newNonce, hookNewNonce) - // tx = web3.eth.sendETH(to: sendToAddress, amount: 1000) + // tx = ganache.eth.sendETH(to: sendToAddress, amount: 1000) // tx!.transactionOptions.from = tempKeystore!.addresses!.first! // result = try! tx!.send(password: "web3swift") // sleep(1) @@ -63,12 +61,12 @@ class web3swiftEventloopTests: XCTestCase { // let hookNewNonce2 = middleware.nonceLookups[tempKeystore!.addresses!.first!]! // XCTAssert(newNonce2 == hookNewNonce2) // } - + func getKeystoreData() -> Data? { let bundle = Bundle(for: type(of: self)) - guard let path = bundle.path(forResource: "key", ofType: "json") else {return nil} - guard let data = NSData(contentsOfFile: path) else {return nil} + guard let path = bundle.path(forResource: "key", ofType: "json") else { return nil } + guard let data = NSData(contentsOfFile: path) else { return nil } return data as Data } - + } diff --git a/Tests/web3swiftTests/localTests/web3swiftHelpers.swift b/Tests/web3swiftTests/localTests/web3swiftHelpers.swift index cda674141..9f8ad8a66 100644 --- a/Tests/web3swiftTests/localTests/web3swiftHelpers.swift +++ b/Tests/web3swiftTests/localTests/web3swiftHelpers.swift @@ -12,14 +12,13 @@ import BigInt import web3swift class web3swiftHelpers { - static func localDeployERC20() throws -> (web3, TransactionSendingResult, TransactionReceipt, String) { + static func localDeployERC20(_ web3: web3) throws -> (TransactionSendingResult, TransactionReceipt, String) { let abiString = "[{\"inputs\":[{\"internalType\":\"string\",\"name\":\"name_\",\"type\":\"string\"},{\"internalType\":\"string\",\"name\":\"symbol_\",\"type\":\"string\"},{\"internalType\":\"address\",\"name\":\"mintRecipient\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"mintAmount\",\"type\":\"uint256\"}],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Approval\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"approve\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"subtractedValue\",\"type\":\"uint256\"}],\"name\":\"decreaseAllowance\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"addedValue\",\"type\":\"uint256\"}],\"name\":\"increaseAllowance\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"mint\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"recipient\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"transfer\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Transfer\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"sender\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"recipient\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"transferFrom\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"}],\"name\":\"transferOwnership\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"}],\"name\":\"allowance\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"decimals\",\"outputs\":[{\"internalType\":\"uint8\",\"name\":\"\",\"type\":\"uint8\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"name\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"symbol\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"totalSupply\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"}]" let bytecode = Data.fromHex("60806040523480156200001157600080fd5b5060405162001f0c38038062001f0c8339818101604052810190620000379190620003af565b83600490805190602001906200004f9291906200025f565b508260059080519060200190620000689291906200025f565b5033600360006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16146200012b576000811162000125576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016200011c90620004ce565b60405180910390fd5b620001aa565b6000811115620001a957600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415620001a8576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016200019f90620004ac565b60405180910390fd5b5b5b806000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040516200024d9190620004f0565b60405180910390a3505050506200079e565b8280546200026d90620005f1565b90600052602060002090601f016020900481019282620002915760008555620002dd565b82601f10620002ac57805160ff1916838001178555620002dd565b82800160010185558215620002dd579182015b82811115620002dc578251825591602001919060010190620002bf565b5b509050620002ec9190620002f0565b5090565b5b808211156200030b576000816000905550600101620002f1565b5090565b600062000326620003208462000536565b6200050d565b9050828152602081018484840111156200033f57600080fd5b6200034c848285620005bb565b509392505050565b60008151905062000365816200076a565b92915050565b600082601f8301126200037d57600080fd5b81516200038f8482602086016200030f565b91505092915050565b600081519050620003a98162000784565b92915050565b60008060008060808587031215620003c657600080fd5b600085015167ffffffffffffffff811115620003e157600080fd5b620003ef878288016200036b565b945050602085015167ffffffffffffffff8111156200040d57600080fd5b6200041b878288016200036b565b93505060406200042e8782880162000354565b9250506060620004418782880162000398565b91505092959194509250565b60006200045c6037836200056c565b91506200046982620006cc565b604082019050919050565b600062000483602e836200056c565b915062000490826200071b565b604082019050919050565b620004a681620005b1565b82525050565b60006020820190508181036000830152620004c7816200044d565b9050919050565b60006020820190508181036000830152620004e98162000474565b9050919050565b60006020820190506200050760008301846200049b565b92915050565b6000620005196200052c565b905062000527828262000627565b919050565b6000604051905090565b600067ffffffffffffffff8211156200055457620005536200068c565b5b6200055f82620006bb565b9050602081019050919050565b600082825260208201905092915050565b60006200058a8262000591565b9050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b60005b83811015620005db578082015181840152602081019050620005be565b83811115620005eb576000848401525b50505050565b600060028204905060018216806200060a57607f821691505b602082108114156200062157620006206200065d565b5b50919050565b6200063282620006bb565b810181811067ffffffffffffffff821117156200065457620006536200068c565b5b80604052505050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6000601f19601f8301169050919050565b7f45524332303a206d696e7420726563697069656e7420697320656d7074792c2060008201527f627574206d696e7420616d6f756e742069736e27742030000000000000000000602082015250565b7f45524332303a206d696e7420616d6f756e74206973203020746f206e6f6e2d6560008201527f6d70747920726563697069656e74000000000000000000000000000000000000602082015250565b62000775816200057d565b81146200078157600080fd5b50565b6200078f81620005b1565b81146200079b57600080fd5b50565b61175e80620007ae6000396000f3fe608060405234801561001057600080fd5b50600436106100cf5760003560e01c806340c10f191161008c578063a457c2d711610066578063a457c2d714610228578063a9059cbb14610258578063dd62ed3e14610288578063f2fde38b146102b8576100cf565b806340c10f19146101be57806370a08231146101da57806395d89b411461020a576100cf565b806306fdde03146100d4578063095ea7b3146100f257806318160ddd1461012257806323b872dd14610140578063313ce56714610170578063395093511461018e575b600080fd5b6100dc6102d4565b6040516100e9919061116c565b60405180910390f35b61010c60048036038101906101079190610f74565b610366565b6040516101199190611151565b60405180910390f35b61012a610384565b60405161013791906112ae565b60405180910390f35b61015a60048036038101906101559190610f25565b61038e565b6040516101679190611151565b60405180910390f35b610178610486565b60405161018591906112c9565b60405180910390f35b6101a860048036038101906101a39190610f74565b61048f565b6040516101b59190611151565b60405180910390f35b6101d860048036038101906101d39190610f74565b61053b565b005b6101f460048036038101906101ef9190610ec0565b6106fa565b60405161020191906112ae565b60405180910390f35b610212610742565b60405161021f919061116c565b60405180910390f35b610242600480360381019061023d9190610f74565b6107d4565b60405161024f9190611151565b60405180910390f35b610272600480360381019061026d9190610f74565b6108bf565b60405161027f9190611151565b60405180910390f35b6102a2600480360381019061029d9190610ee9565b6108dd565b6040516102af91906112ae565b60405180910390f35b6102d260048036038101906102cd9190610ec0565b610964565b005b6060600480546102e3906113de565b80601f016020809104026020016040519081016040528092919081815260200182805461030f906113de565b801561035c5780601f106103315761010080835404028352916020019161035c565b820191906000526020600020905b81548152906001019060200180831161033f57829003601f168201915b5050505050905090565b600061037a610373610a38565b8484610a40565b6001905092915050565b6000600254905090565b600061039b848484610c0b565b6000600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006103e6610a38565b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905082811015610466576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161045d9061120e565b60405180910390fd5b61047a85610472610a38565b858403610a40565b60019150509392505050565b60006012905090565b600061053161049c610a38565b8484600160006104aa610a38565b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008873ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205461052c9190611300565b610a40565b6001905092915050565b600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146105cb576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016105c2906111ee565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16141561063b576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016106329061128e565b60405180910390fd5b806000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282546106899190611300565b925050819055508173ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040516106ee91906112ae565b60405180910390a35050565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b606060058054610751906113de565b80601f016020809104026020016040519081016040528092919081815260200182805461077d906113de565b80156107ca5780601f1061079f576101008083540402835291602001916107ca565b820191906000526020600020905b8154815290600101906020018083116107ad57829003601f168201915b5050505050905090565b600080600160006107e3610a38565b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050828110156108a0576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016108979061126e565b60405180910390fd5b6108b46108ab610a38565b85858403610a40565b600191505092915050565b60006108d36108cc610a38565b8484610c0b565b6001905092915050565b6000600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905092915050565b600360009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146109f4576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016109eb906111ee565b60405180910390fd5b80600360006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b600033905090565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff161415610ab0576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610aa79061124e565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415610b20576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610b17906111ae565b60405180910390fd5b80600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92583604051610bfe91906112ae565b60405180910390a3505050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff161415610c7b576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610c729061122e565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415610ceb576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610ce29061118e565b60405180910390fd5b610cf6838383610e8c565b60008060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905081811015610d7c576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610d73906111ce565b60405180910390fd5b8181036000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254610e0f9190611300565b925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef84604051610e7391906112ae565b60405180910390a3610e86848484610e91565b50505050565b505050565b505050565b600081359050610ea5816116fa565b92915050565b600081359050610eba81611711565b92915050565b600060208284031215610ed257600080fd5b6000610ee084828501610e96565b91505092915050565b60008060408385031215610efc57600080fd5b6000610f0a85828601610e96565b9250506020610f1b85828601610e96565b9150509250929050565b600080600060608486031215610f3a57600080fd5b6000610f4886828701610e96565b9350506020610f5986828701610e96565b9250506040610f6a86828701610eab565b9150509250925092565b60008060408385031215610f8757600080fd5b6000610f9585828601610e96565b9250506020610fa685828601610eab565b9150509250929050565b610fb981611368565b82525050565b6000610fca826112e4565b610fd481856112ef565b9350610fe48185602086016113ab565b610fed8161146e565b840191505092915050565b60006110056023836112ef565b91506110108261147f565b604082019050919050565b60006110286022836112ef565b9150611033826114ce565b604082019050919050565b600061104b6026836112ef565b91506110568261151d565b604082019050919050565b600061106e6010836112ef565b91506110798261156c565b602082019050919050565b60006110916028836112ef565b915061109c82611595565b604082019050919050565b60006110b46025836112ef565b91506110bf826115e4565b604082019050919050565b60006110d76024836112ef565b91506110e282611633565b604082019050919050565b60006110fa6025836112ef565b915061110582611682565b604082019050919050565b600061111d601f836112ef565b9150611128826116d1565b602082019050919050565b61113c81611394565b82525050565b61114b8161139e565b82525050565b60006020820190506111666000830184610fb0565b92915050565b600060208201905081810360008301526111868184610fbf565b905092915050565b600060208201905081810360008301526111a781610ff8565b9050919050565b600060208201905081810360008301526111c78161101b565b9050919050565b600060208201905081810360008301526111e78161103e565b9050919050565b6000602082019050818103600083015261120781611061565b9050919050565b6000602082019050818103600083015261122781611084565b9050919050565b60006020820190508181036000830152611247816110a7565b9050919050565b60006020820190508181036000830152611267816110ca565b9050919050565b60006020820190508181036000830152611287816110ed565b9050919050565b600060208201905081810360008301526112a781611110565b9050919050565b60006020820190506112c36000830184611133565b92915050565b60006020820190506112de6000830184611142565b92915050565b600081519050919050565b600082825260208201905092915050565b600061130b82611394565b915061131683611394565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0382111561134b5761134a611410565b5b828201905092915050565b600061136182611374565b9050919050565b60008115159050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b600060ff82169050919050565b60005b838110156113c95780820151818401526020810190506113ae565b838111156113d8576000848401525b50505050565b600060028204905060018216806113f657607f821691505b6020821081141561140a5761140961143f565b5b50919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000601f19601f8301169050919050565b7f45524332303a207472616e7366657220746f20746865207a65726f206164647260008201527f6573730000000000000000000000000000000000000000000000000000000000602082015250565b7f45524332303a20617070726f766520746f20746865207a65726f20616464726560008201527f7373000000000000000000000000000000000000000000000000000000000000602082015250565b7f45524332303a207472616e7366657220616d6f756e742065786365656473206260008201527f616c616e63650000000000000000000000000000000000000000000000000000602082015250565b7f45524332303a204e6f74206f776e657200000000000000000000000000000000600082015250565b7f45524332303a207472616e7366657220616d6f756e742065786365656473206160008201527f6c6c6f77616e6365000000000000000000000000000000000000000000000000602082015250565b7f45524332303a207472616e736665722066726f6d20746865207a65726f20616460008201527f6472657373000000000000000000000000000000000000000000000000000000602082015250565b7f45524332303a20617070726f76652066726f6d20746865207a65726f2061646460008201527f7265737300000000000000000000000000000000000000000000000000000000602082015250565b7f45524332303a2064656372656173656420616c6c6f77616e63652062656c6f7760008201527f207a65726f000000000000000000000000000000000000000000000000000000602082015250565b7f45524332303a206d696e7420746f20746865207a65726f206164647265737300600082015250565b61170381611356565b811461170e57600080fd5b50565b61171a81611394565b811461172557600080fd5b5056fea2646970667358221220b6ac3a3baa1a92f9f5628a993657177a7d65adf57696ee461d89686f85a28d6164736f6c63430008040033")! - - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) + let allAddresses = try web3.eth.getAccounts() let contract = web3.contract(abiString, at: nil, abiVersion: 2)! - + let parameters = [ "web3swift", "w3s", @@ -32,19 +31,19 @@ class web3swiftHelpers { let result = try deployTx.sendPromise().wait() let txHash = result.hash print("Transaction with hash " + txHash) - + Thread.sleep(forTimeInterval: 1.0) - + let receipt = try web3.eth.getTransactionReceipt(txHash) print(receipt) - + switch receipt.status { case .notYetProcessed: throw Web3Error.processingError(desc: "Transaction is unprocessed") default: break } - - return (web3, result, receipt, abiString) + + return (result, receipt, abiString) } } diff --git a/Tests/web3swiftTests/localTests/web3swiftKeystoresTests.swift b/Tests/web3swiftTests/localTests/web3swiftKeystoresTests.swift index 1ef769a51..4d8245c43 100755 --- a/Tests/web3swiftTests/localTests/web3swiftKeystoresTests.swift +++ b/Tests/web3swiftTests/localTests/web3swiftKeystoresTests.swift @@ -9,10 +9,10 @@ import CryptoSwift @testable import web3swift -class web3swiftKeystoresTests: XCTestCase { - +class web3swiftKeystoresTests: LocalTestCase { + let mnemonic = "fruit wave dwarf banana earth journey tattoo true farm silk olive fence" - + func testBIP39 () throws { var entropy = Data.fromHex("00000000000000000000000000000000")! var phrase = BIP39.generateMnemonicsFromEntropy(entropy: entropy) @@ -25,7 +25,7 @@ class web3swiftKeystoresTests: XCTestCase { seed = BIP39.seedFromMmemonics(phrase!, password: "TREZOR") XCTAssert(seed?.toHexString() == "64c87cde7e12ecf6704ab95bb1408bef047c22db4cc7491c4271d170a1b213d20b385bc1588d9c7b38f1b39d415665b8a9030c9ec653d75e65f847d8fc1fc440") } - + func testBIP39SeedAndMnemConversions() throws { let seed = Data.randomBytes(length: 32)! let mnemonics = BIP39.generateMnemonicsFromEntropy(entropy: seed) @@ -69,33 +69,33 @@ class web3swiftKeystoresTests: XCTestCase { let mnemonic_27 = "clock venue style demise net float differ click object poet afraid october hurry organ faint inject cart trade test immense gentle speak almost rude success drip spoon" XCTAssertNil(BIP39.mnemonicsToEntropy(mnemonic_27, language: .english)) } - + func testHMAC() throws { let seed = Data.fromHex("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b")! let data = Data.fromHex("4869205468657265")! let hmac = try! HMAC.init(key: seed.bytes, variant: HMAC.Variant.sha2(.sha512)).authenticate(data.bytes) XCTAssert(Data(hmac).toHexString() == "87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854") } - + func testV3keystoreExportPrivateKey() throws { - let keystore = try! EthereumKeystoreV3(password: ""); + let keystore = try! EthereumKeystoreV3(password: "") XCTAssertNotNil(keystore) let account = keystore!.addresses![0] print(account) let data = try! keystore!.serialize() - print(try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions(rawValue:0))) + print(try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions(rawValue: 0))) let key = try! keystore!.UNSAFE_getPrivateKeyData(password: "", account: account) XCTAssertNotNil(key) } - + func testV3keystoreSerialization() throws { - let keystore = try! EthereumKeystoreV3(password: ""); + let keystore = try! EthereumKeystoreV3(password: "") XCTAssertNotNil(keystore) let account = keystore!.addresses![0] let data = try! keystore!.serialize() let key = try! keystore!.UNSAFE_getPrivateKeyData(password: "", account: account) XCTAssertNotNil(key) - + let restored = EthereumKeystoreV3(data!) XCTAssertNotNil(restored) XCTAssertEqual(keystore!.addresses!.first!, restored!.addresses!.first!) @@ -103,20 +103,20 @@ class web3swiftKeystoresTests: XCTestCase { XCTAssertNotNil(restoredKey) XCTAssertEqual(key, restoredKey) } - + func testNewBIP32keystore() throws { let mnemonic = try! BIP39.generateMnemonics(bitsOfEntropy: 256)! let keystore = try! BIP32Keystore(mnemonics: mnemonic, password: "", mnemonicsPassword: "") XCTAssert(keystore != nil) } - + func testSameAddressesFromTheSameMnemonics() throws { let mnemonic = try! BIP39.generateMnemonics(bitsOfEntropy: 256)! let keystore1 = try! BIP32Keystore(mnemonics: mnemonic, password: "", mnemonicsPassword: "") let keystore2 = try! BIP32Keystore(mnemonics: mnemonic, password: "", mnemonicsPassword: "") XCTAssert(keystore1?.addresses?.first == keystore2?.addresses?.first) } - + func testBIP32keystoreExportPrivateKey() throws { let mnemonic = "normal dune pole key case cradle unfold require tornado mercy hospital buyer" let keystore = try! BIP32Keystore(mnemonics: mnemonic, password: "", mnemonicsPassword: "") @@ -125,32 +125,32 @@ class web3swiftKeystoresTests: XCTestCase { let key = try! keystore!.UNSAFE_getPrivateKeyData(password: "", account: account) XCTAssertNotNil(key) } - + func testBIP32keystoreMatching() throws { let keystore = try! BIP32Keystore(mnemonics: mnemonic, password: "", mnemonicsPassword: "banana") XCTAssertNotNil(keystore) let account = keystore!.addresses![0] let key = try! keystore!.UNSAFE_getPrivateKeyData(password: "", account: account) - let pubKey = Web3.Utils.privateToPublic(key, compressed: true); + let pubKey = Web3.Utils.privateToPublic(key, compressed: true) XCTAssert(pubKey?.toHexString() == "027160bd3a4d938cac609ff3a11fe9233de7b76c22a80d2b575e202cbf26631659") } - + func testBIP32keystoreMatchingRootNode() throws { let keystore = try! BIP32Keystore(mnemonics: mnemonic, password: "", mnemonicsPassword: "banana") XCTAssertNotNil(keystore) let rootNode = try! keystore!.serializeRootNodeToString(password: "") XCTAssert(rootNode == "xprvA2KM71v838kPwE8Lfr12m9DL939TZmPStMnhoFcZkr1nBwDXSG7c3pjYbMM9SaqcofK154zNSCp7W7b4boEVstZu1J3pniLQJJq7uvodfCV") } - + func testBIP32keystoreCustomPathMatching() throws { - let keystore = try! BIP32Keystore(mnemonics: mnemonic, password: "", mnemonicsPassword: "banana", prefixPath:"m/44'/60'/0'/0") + let keystore = try! BIP32Keystore(mnemonics: mnemonic, password: "", mnemonicsPassword: "banana", prefixPath: "m/44'/60'/0'/0") XCTAssertNotNil(keystore) let account = keystore!.addresses![0] let key = try! keystore!.UNSAFE_getPrivateKeyData(password: "", account: account) - let pubKey = Web3.Utils.privateToPublic(key, compressed: true); + let pubKey = Web3.Utils.privateToPublic(key, compressed: true) XCTAssert(pubKey?.toHexString() == "027160bd3a4d938cac609ff3a11fe9233de7b76c22a80d2b575e202cbf26631659") } - + func testByBIP32keystoreCreateChildAccount() throws { let mnemonic = "normal dune pole key case cradle unfold require tornado mercy hospital buyer" let keystore = try! BIP32Keystore(mnemonics: mnemonic, password: "", mnemonicsPassword: "") @@ -162,7 +162,7 @@ class web3swiftKeystoresTests: XCTestCase { let key = try! keystore!.UNSAFE_getPrivateKeyData(password: "", account: account) XCTAssertNotNil(key) } - + func testByBIP32keystoreCreateCustomChildAccount() throws { let mnemonic = "normal dune pole key case cradle unfold require tornado mercy hospital buyer" let keystore = try! BIP32Keystore(mnemonics: mnemonic, password: "", mnemonicsPassword: "") @@ -175,7 +175,7 @@ class web3swiftKeystoresTests: XCTestCase { XCTAssertNotNil(key) print(keystore!.addressStorage.paths) } - + func testByBIP32keystoreSaveAndDeriva() throws { let mnemonic = "normal dune pole key case cradle unfold require tornado mercy hospital buyer" let keystore = try! BIP32Keystore(mnemonics: mnemonic, password: "", mnemonicsPassword: "", prefixPath: "m/44'/60'/0'") @@ -194,20 +194,20 @@ class web3swiftKeystoresTests: XCTestCase { XCTAssert(keystore?.addresses![0] == recreatedStore?.addresses![0]) XCTAssert(keystore?.addresses![1] == recreatedStore?.addresses![1]) } - + func testPBKDF2() throws { let pass = "passDATAb00AB7YxDTTl".data(using: .utf8)! let salt = "saltKEYbcTcXHCBxtjD2".data(using: .utf8)! let dataArray = try? PKCS5.PBKDF2(password: pass.bytes, salt: salt.bytes, iterations: 100000, keyLength: 65, variant: HMAC.Variant.sha2(.sha512)).calculate() XCTAssert(Data(dataArray!).toHexString().addHexPrefix().lowercased() == "0x594256B0BD4D6C9F21A87F7BA5772A791A10E6110694F44365CD94670E57F1AECD797EF1D1001938719044C7F018026697845EB9AD97D97DE36AB8786AAB5096E7".lowercased()) } - + func testRIPEMD() throws { let data = "message digest".data(using: .ascii) let hash = try! RIPEMD160.hash(message: data!) XCTAssert(hash.toHexString() == "5d0689ef49d2fae572b881b123a85ffa21595f36") } - + func testHD32() throws { let seed = Data.fromHex("000102030405060708090a0b0c0d0e0f")! let node = HDNode(seed: seed)! @@ -216,7 +216,7 @@ class web3swiftKeystoresTests: XCTestCase { let serializedPriv = node.serializeToString(serializePublic: false) XCTAssert(serialized == "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8") XCTAssert(serializedPriv == "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi") - + let deserializedNode = HDNode(serializedPriv!) XCTAssert(deserializedNode != nil) XCTAssert(deserializedNode?.depth == 0) @@ -226,7 +226,7 @@ class web3swiftKeystoresTests: XCTestCase { XCTAssert(deserializedNode?.privateKey == node.privateKey) XCTAssert(deserializedNode?.publicKey == node.publicKey) XCTAssert(deserializedNode?.chaincode == node.chaincode) - + let nextNode = node.derive(index: 0, derivePrivateKey: true) XCTAssert(nextNode?.depth == 1) XCTAssert(nextNode?.index == UInt32(0)) @@ -235,7 +235,7 @@ class web3swiftKeystoresTests: XCTestCase { XCTAssert(nextNode?.publicKey.toHexString() == "027c4b09ffb985c298afe7e5813266cbfcb7780b480ac294b0b43dc21f2be3d13c") XCTAssert(nextNode?.serializeToString() == "xpub68Gmy5EVb2BdFbj2LpWrk1M7obNuaPTpT5oh9QCCo5sRfqSHVYWex97WpDZzszdzHzxXDAzPLVSwybe4uPYkSk4G3gnrPqqkV9RyNzAcNJ1") XCTAssert(nextNode?.serializeToString(serializePublic: false) == "xprv9uHRZZhbkedL37eZEnyrNsQPFZYRAvjy5rt6M1nbEkLSo378x1CQQLo2xxBvREwiK6kqf7GRNvsNEchwibzXaV6i5GcsgyjBeRguXhKsi4R") - + let nextNodeHardened = node.derive(index: 0, derivePrivateKey: true, hardened: true) XCTAssert(nextNodeHardened?.depth == 1) XCTAssert(nextNodeHardened?.index == UInt32(0)) @@ -244,14 +244,14 @@ class web3swiftKeystoresTests: XCTestCase { XCTAssert(nextNodeHardened?.publicKey.toHexString() == "035a784662a4a20a65bf6aab9ae98a6c068a81c52e4b032c0fb5400c706cfccc56") XCTAssert(nextNodeHardened?.serializeToString() == "xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw") XCTAssert(nextNodeHardened?.serializeToString(serializePublic: false) == "xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7") - + let treeNode = node.derive(path: HDNode.defaultPath) XCTAssert(treeNode != nil) XCTAssert(treeNode?.depth == 4) XCTAssert(treeNode?.serializeToString() == "xpub6DZ3xpo1ixWwwNDQ7KFTamRVM46FQtgcDxsmAyeBpTHEo79E1n1LuWiZSMSRhqMQmrHaqJpek2TbtTzbAdNWJm9AhGdv7iJUpDjA6oJD84b") XCTAssert(treeNode?.serializeToString(serializePublic: false) == "xprv9zZhZKG7taxeit8w1HiTDdUko2Fm1RxkrjxANbEaG7kFvJp5UEh6MiQ5b5XvwWg8xdHMhueagettVG2AbfqSRDyNpxRDBLyMSbNq1KhZ8ai") } - + func testBIP32derivation2() throws { let seed = Data.fromHex("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542")! let node = HDNode(seed: seed)! @@ -262,7 +262,7 @@ class web3swiftKeystoresTests: XCTestCase { XCTAssert(treeNode?.serializeToString() == "xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt") XCTAssert(treeNode?.serializeToString(serializePublic: false) == "xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j") } - + func testKeystoreDerivationTime() throws { let privateKey = Data.randomBytes(length: 32)! measure { @@ -271,10 +271,10 @@ class web3swiftKeystoresTests: XCTestCase { let _ = try! ks.UNSAFE_getPrivateKeyData(password: "TEST", account: account) } } - + func testSingleScryptDerivation() throws { let privateKey = Data.randomBytes(length: 32)! let _ = try! EthereumKeystoreV3(privateKey: privateKey, password: "TEST")! } - + } diff --git a/Tests/web3swiftTests/localTests/web3swiftNumberFormattingUtilTests.swift b/Tests/web3swiftTests/localTests/web3swiftNumberFormattingUtilTests.swift index 8c4dfadaa..1282a6f09 100755 --- a/Tests/web3swiftTests/localTests/web3swiftNumberFormattingUtilTests.swift +++ b/Tests/web3swiftTests/localTests/web3swiftNumberFormattingUtilTests.swift @@ -10,61 +10,61 @@ import BigInt @testable import web3swift -class web3swiftNumberFormattingUtilTests: XCTestCase { - +class web3swiftNumberFormattingUtilTests: LocalTestCase { + func testNumberFormattingUtil() throws { let balance = BigInt("-1000000000000000000") print("this is print") let formatted = Web3.Utils.formatToPrecision(balance, numberDecimals: 18, formattingDecimals: 4, decimalSeparator: ",") XCTAssert(formatted == "-1") } - + func testNumberFormattingUtil2() throws { let balance = BigInt("-1000000000000000") let formatted = Web3.Utils.formatToPrecision(balance, numberDecimals: 18, formattingDecimals: 4, decimalSeparator: ",") XCTAssert(formatted == "-0,0010") } - + func testNumberFormattingUtil3() throws { let balance = BigInt("-1000000000000") let formatted = Web3.Utils.formatToPrecision(balance, numberDecimals: 18, formattingDecimals: 4, decimalSeparator: ",") XCTAssert(formatted == "-0,0000") } - + func testNumberFormattingUtil4() throws { let balance = BigInt("-1000000000000") let formatted = Web3.Utils.formatToPrecision(balance, numberDecimals: 18, formattingDecimals: 9, decimalSeparator: ",") XCTAssert(formatted == "-0,000001000") } - + func testNumberFormattingUtil5() throws { let balance = BigInt("-1") let formatted = Web3.Utils.formatToPrecision(balance, numberDecimals: 18, formattingDecimals: 9, decimalSeparator: ",", fallbackToScientific: true) XCTAssert(formatted == "-1e-18") } - + func testNumberFormattingUtil6() throws { let balance = BigInt("0") let formatted = Web3.Utils.formatToPrecision(balance, numberDecimals: 18, formattingDecimals: 9, decimalSeparator: ",") XCTAssert(formatted == "0") } - + func testNumberFormattingUtil7() throws { let balance = BigInt("-1100000000000000000") let formatted = Web3.Utils.formatToPrecision(balance, numberDecimals: 18, formattingDecimals: 4, decimalSeparator: ",") XCTAssert(formatted == "-1,1000") } - + func testNumberFormattingUtil8() throws { let balance = BigInt("100") let formatted = Web3.Utils.formatToPrecision(balance, numberDecimals: 18, formattingDecimals: 4, decimalSeparator: ",", fallbackToScientific: true) XCTAssert(formatted == "1,00e-16") } - + func testNumberFormattingUtil9() throws { let balance = BigInt("1000000") let formatted = Web3.Utils.formatToPrecision(balance, numberDecimals: 18, formattingDecimals: 4, decimalSeparator: ",", fallbackToScientific: true) XCTAssert(formatted == "1,0000e-12") } - + } diff --git a/Tests/web3swiftTests/localTests/web3swiftObjCTests.swift b/Tests/web3swiftTests/localTests/web3swiftObjCTests.swift index aaadf0172..167345181 100755 --- a/Tests/web3swiftTests/localTests/web3swiftObjCTests.swift +++ b/Tests/web3swiftTests/localTests/web3swiftObjCTests.swift @@ -8,7 +8,7 @@ // @testable import web3swift -// class web3swiftObjCTests: XCTestCase { +// class web3swiftObjCTests: LocalTestCase { // func testBalance() { // let web3 = _ObjCWeb3.InfuraMainnetWeb3() // let address = _ObjCEthereumAddress(address: "0xe22b8979739D724343bd002F9f432F5990879901") @@ -17,16 +17,16 @@ // XCTAssert(err?.pointee == nil) // XCTAssert(balance != nil) // } - + // func testGettingBlockNumber() { // let web3 = _ObjCWeb3.InfuraMainnetWeb3() // let err: NSErrorPointer = NSErrorPointer(nilLiteral: ()) // let blockNumber = web3.web3Eth.getBlockNumber(error: err) // XCTAssert(err?.pointee == nil) // XCTAssert(blockNumber != nil) - + // } - + // func testGasPrice(){ // let web3 = _ObjCWeb3.InfuraMainnetWeb3() // let err: NSErrorPointer = NSErrorPointer(nilLiteral: ()) diff --git a/Tests/web3swiftTests/localTests/web3swiftPersonalSignatureTests.swift b/Tests/web3swiftTests/localTests/web3swiftPersonalSignatureTests.swift index e2602f046..60a36fc8d 100755 --- a/Tests/web3swiftTests/localTests/web3swiftPersonalSignatureTests.swift +++ b/Tests/web3swiftTests/localTests/web3swiftPersonalSignatureTests.swift @@ -10,81 +10,79 @@ import BigInt @testable import web3swift -class web3swiftPersonalSignatureTests: XCTestCase { - +class web3swiftPersonalSignatureTests: LocalTestCase { + func testPersonalSignature() throws { - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) let tempKeystore = try! EthereumKeystoreV3(password: "") let keystoreManager = KeystoreManager([tempKeystore!]) - web3.addKeystoreManager(keystoreManager) + ganache.addKeystoreManager(keystoreManager) let message = "Hello World" let expectedAddress = keystoreManager.addresses![0] print(expectedAddress) - let signature = try web3.personal.signPersonalMessage(message: message.data(using: .utf8)!, from: expectedAddress, password: "") + let signature = try ganache.personal.signPersonalMessage(message: message.data(using: .utf8)!, from: expectedAddress, password: "") let unmarshalledSignature = SECP256K1.unmarshalSignature(signatureData: signature)! print("V = " + String(unmarshalledSignature.v)) print("R = " + Data(unmarshalledSignature.r).toHexString()) print("S = " + Data(unmarshalledSignature.s).toHexString()) print("Personal hash = " + Web3.Utils.hashPersonalMessage(message.data(using: .utf8)!)!.toHexString()) - let signer = try web3.personal.ecrecover(personalMessage: message.data(using: .utf8)!, signature: signature) + let signer = try ganache.personal.ecrecover(personalMessage: message.data(using: .utf8)!, signature: signature) XCTAssert(expectedAddress == signer, "Failed to sign personal message") } - + // TODO: - write contract func testPersonalSignatureOnContract() throws { - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) // Deploying contract let abiString = "[{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"constant\":true,\"inputs\":[{\"name\":\"_message\",\"type\":\"string\"}],\"name\":\"hashPersonalMessage\",\"outputs\":[{\"name\":\"hash\",\"type\":\"bytes32\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_message\",\"type\":\"string\"},{\"name\":\"v\",\"type\":\"uint8\"},{\"name\":\"r\",\"type\":\"bytes32\"},{\"name\":\"s\",\"type\":\"bytes32\"}],\"name\":\"recoverSigner\",\"outputs\":[{\"name\":\"signer\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"}]" let bytecode = Data.fromHex("0x608060405234801561001057600080fd5b506105ba806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680632785e93714610051578063d01ec9a514610123575b600080fd5b34801561005d57600080fd5b506100e1600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290803560ff169060200190929190803560001916906020019092919080356000191690602001909291905050506101a8565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34801561012f57600080fd5b5061018a600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509192919290505050610258565b60405180826000191660001916815260200191505060405180910390f35b6000806101b486610258565b9050601b8560ff1610156101c957601b850194505b600181868686604051600081526020016040526040518085600019166000191681526020018460ff1660ff1681526020018360001916600019168152602001826000191660001916815260200194505050505060206040516020810390808403906000865af1158015610240573d6000803e3d6000fd5b50505060206040510351915081915050949350505050565b600080825190506040805190810160405280601a81526020017f19457468657265756d205369676e6564204d6573736167653a0a00000000000081525061029e826103b4565b846040518084805190602001908083835b6020831015156102d457805182526020820191506020810190506020830392506102af565b6001836020036101000a03801982511681845116808217855250505050505090500183805190602001908083835b6020831015156103275780518252602082019150602081019050602083039250610302565b6001836020036101000a03801982511681845116808217855250505050505090500182805190602001908083835b60208310151561037a5780518252602082019150602081019050602083039250610355565b6001836020036101000a03801982511681845116808217855250505050505090500193505050506040518091039020915081915050919050565b606060006060600080600060649450846040519080825280601f01601f1916602001820160405280156103f65781602001602082028038833980820191505090505b509350600092505b60008714151561049557600a8781151561041457fe5b069150600a8781151561042357fe5b049650816030017f010000000000000000000000000000000000000000000000000000000000000002848480600101955081518110151561046057fe5b9060200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053506103fe565b826040519080825280601f01601f1916602001820160405280156104c85781602001602082028038833980820191505090505b509550600090505b8281101561058157838160018503038151811015156104eb57fe5b9060200101517f010000000000000000000000000000000000000000000000000000000000000090047f010000000000000000000000000000000000000000000000000000000000000002868281518110151561054457fe5b9060200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a90535080806001019150506104d0565b85955050505050509190505600a165627a7a723058204b567e6628d988bde2e19a4e9a457bf84bbeac15069a74fe993aba215fb024330029")! - var contract = web3.contract(abiString, at: nil, abiVersion: 2)! + var contract = ganache.contract(abiString, at: nil, abiVersion: 2)! let parameters = [] as [AnyObject] let deployTx = contract.deploy(bytecode: bytecode, parameters: parameters)! - let allAddresses = try web3.eth.getAccounts() + let allAddresses = try ganache.eth.getAccounts() deployTx.transactionOptions.from = allAddresses[0] deployTx.transactionOptions.gasLimit = .manual(3000000) let deployResult = try deployTx.sendPromise().wait() let txHash = deployResult.hash print("Transaction with hash " + txHash) - + Thread.sleep(forTimeInterval: 1.0) - - let receipt = try web3.eth.getTransactionReceipt(txHash) + + let receipt = try ganache.eth.getTransactionReceipt(txHash) print(receipt) - + switch receipt.status { case .notYetProcessed: return default: break } - + // Signing let tempKeystore = try! EthereumKeystoreV3(password: "") let keystoreManager = KeystoreManager([tempKeystore!]) - web3.addKeystoreManager(keystoreManager) + ganache.addKeystoreManager(keystoreManager) let message = "Hello World" let expectedAddress = keystoreManager.addresses![0] print(expectedAddress) - let signature = try web3.personal.signPersonalMessage(message: message.data(using: .utf8)!, from: expectedAddress, password: "") + let signature = try ganache.personal.signPersonalMessage(message: message.data(using: .utf8)!, from: expectedAddress, password: "") let unmarshalledSignature = SECP256K1.unmarshalSignature(signatureData: signature)! print("V = " + String(unmarshalledSignature.v)) print("R = " + Data(unmarshalledSignature.r).toHexString()) print("S = " + Data(unmarshalledSignature.s).toHexString()) print("Personal hash = " + Web3.Utils.hashPersonalMessage(message.data(using: .utf8)!)!.toHexString()) - + // Calling contract - contract = web3.contract(abiString, at: receipt.contractAddress!)! + contract = ganache.contract(abiString, at: receipt.contractAddress!)! var tx = contract.read("hashPersonalMessage", parameters: [message as AnyObject]) tx?.transactionOptions.from = expectedAddress var result = try tx!.call() guard let hash = result["hash"]! as? Data else {return XCTFail()} XCTAssert(Web3.Utils.hashPersonalMessage(message.data(using: .utf8)!)! == hash) - + tx = contract.read("recoverSigner", parameters: [message, unmarshalledSignature.v, Data(unmarshalledSignature.r), Data(unmarshalledSignature.s)] as [AnyObject]) tx?.transactionOptions.from = expectedAddress result = try tx!.call() - guard let signer = result["signer"]! as? EthereumAddress else {return XCTFail()} + guard let signer = result["signer"]! as? EthereumAddress else { return XCTFail() } XCTAssert(signer == expectedAddress) } - + } diff --git a/Tests/web3swiftTests/localTests/web3swiftPromisesTests.swift b/Tests/web3swiftTests/localTests/web3swiftPromisesTests.swift index 93731d7f5..29bc3d500 100755 --- a/Tests/web3swiftTests/localTests/web3swiftPromisesTests.swift +++ b/Tests/web3swiftTests/localTests/web3swiftPromisesTests.swift @@ -9,41 +9,37 @@ import XCTest import PromiseKit import BigInt -//import EthereumAddress @testable import web3swift -class web3swiftPromisesTests: XCTestCase { - var urlSession : URLSession? - +class web3swiftPromisesTests: LocalTestCase { + var urlSession: URLSession? + func testGetBalancePromise() throws { - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) - let balance = try web3.eth.getBalancePromise(address: "0xe22b8979739D724343bd002F9f432F5990879901").wait() + let balance = try ganache.eth.getBalancePromise(address: "0xe22b8979739D724343bd002F9f432F5990879901").wait() print(balance) } - + func testGetTransactionDetailsPromise() throws { let gasLimit = BigUInt(78423) - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")! - let allAddresses = try web3.eth.getAccounts() - guard let writeTX = web3.eth.sendETH(to: sendToAddress, amount: "0.001") else {return XCTFail()} + let allAddresses = try ganache.eth.getAccounts() + guard let writeTX = ganache.eth.sendETH(to: sendToAddress, amount: "0.001") else {return XCTFail()} writeTX.transactionOptions.from = allAddresses[0] writeTX.transactionOptions.gasLimit = .manual(gasLimit) let writeResult = try writeTX.sendPromise().wait() let txHash = writeResult.hash - let result = try web3.eth.getTransactionDetailsPromise(txHash).wait() + let result = try ganache.eth.getTransactionDetailsPromise(txHash).wait() print(result) XCTAssert(result.transaction.parameters.gasLimit == BigUInt(gasLimit)) } - + func testEstimateGasPromise() throws { - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901") let tempKeystore = try! EthereumKeystoreV3(password: "") let keystoreManager = KeystoreManager([tempKeystore!]) - web3.addKeystoreManager(keystoreManager) - let contract = web3.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2) + ganache.addKeystoreManager(keystoreManager) + let contract = ganache.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2) guard let writeTX = contract?.write("fallback") else {return XCTFail()} writeTX.transactionOptions.from = tempKeystore!.addresses?.first writeTX.transactionOptions.value = BigUInt("1.0", .eth) @@ -51,15 +47,14 @@ class web3swiftPromisesTests: XCTestCase { print(estimate) XCTAssert(estimate == 21000) } - + func testEstimateGasFixPromise() throws { // Deploy contract let bytecode = Data.fromHex("0x608060405234801561001057600080fd5b50610100806100206000396000f30060806040526004361060525763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416630dbe671f8114605757806329e99f0714607b5780634df7e3d0146092575b600080fd5b348015606257600080fd5b50606960a4565b60408051918252519081900360200190f35b348015608657600080fd5b50609060043560aa565b005b348015609d57600080fd5b50606960ce565b60005481565b803a111560ba57600160005560cb565b803a101560cb576001600081905580555b50565b600154815600a165627a7a723058200327a504a24f70cf740239fad2ad203f21caf0ef05f7870bd88482f6fa3cf1080029")! - - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) - let allAddresses = try web3.eth.getAccounts() - let contract = web3.contract(Web3.Utils.estimateGasTestABI, at: nil, abiVersion: 2)! - + + let allAddresses = try ganache.eth.getAccounts() + let contract = ganache.contract(Web3.Utils.estimateGasTestABI, at: nil, abiVersion: 2)! + let parameters = [] as [AnyObject] let deployTx = contract.deploy(bytecode: bytecode, parameters: parameters)! deployTx.transactionOptions.from = allAddresses[0] @@ -67,36 +62,36 @@ class web3swiftPromisesTests: XCTestCase { let result = try deployTx.sendPromise().wait() let txHash = result.hash print("Transaction with hash " + txHash) - + Thread.sleep(forTimeInterval: 1.0) - - let receipt = try web3.eth.getTransactionReceipt(txHash) + + let receipt = try ganache.eth.getTransactionReceipt(txHash) print(receipt) - + switch receipt.status { case .notYetProcessed: return default: break } - + let tempKeystore = try! EthereumKeystoreV3(password: "") let keystoreManager = KeystoreManager([tempKeystore!]) - web3.addKeystoreManager(keystoreManager) - + ganache.addKeystoreManager(keystoreManager) + guard let contractAddress = receipt.contractAddress, - let contract = web3.contract(Web3.Utils.estimateGasTestABI, + let contract = ganache.contract(Web3.Utils.estimateGasTestABI, at: contractAddress, abiVersion: 2) else { return } - + var options = TransactionOptions.defaultOptions let fromAddress = tempKeystore!.addresses?.first options.from = fromAddress - + let amount1 = Web3.Utils.parseToBigUInt("0.000000000000000001", units: .eth) // 1 wei - + guard let tx1 = contract.write("test", parameters: [amount1] as [AnyObject], extraData: Data(), @@ -105,7 +100,7 @@ class web3swiftPromisesTests: XCTestCase { } let estimate1 = try tx1.estimateGasPromise().wait() print(estimate1) - + let amount2 = Web3.Utils.parseToBigUInt("0.00000005", units: .eth) // 50 gwei guard let tx2 = contract.write("test", parameters: [amount2] as [AnyObject], @@ -117,29 +112,26 @@ class web3swiftPromisesTests: XCTestCase { print(estimate2) XCTAssert(estimate2 - estimate1 <= 22000) } - + func testSendETHPromise() throws { - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) - let allAddresses = try web3.eth.getAccounts() - let gasPrice = try web3.eth.getGasPrice() + let allAddresses = try ganache.eth.getAccounts() + let gasPrice = try ganache.eth.getGasPrice() let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")! - guard let writeTX = web3.eth.sendETH(to: sendToAddress, amount: "0.001") else {return XCTFail()} + guard let writeTX = ganache.eth.sendETH(to: sendToAddress, amount: "0.001") else { return XCTFail() } writeTX.transactionOptions.from = allAddresses[0] writeTX.transactionOptions.gasPrice = .manual(gasPrice) let result = try writeTX.sendPromise().wait() print(result) } - + func testERC20tokenBalancePromise() throws { - let (web3, _, receipt, _) = try web3swiftHelpers.localDeployERC20() - - let token = web3.contract(Web3.Utils.erc20ABI, at: receipt.contractAddress, abiVersion: 2)! - + let (_, receipt, _) = try web3swiftHelpers.localDeployERC20(ganache) + + let token = ganache.contract(Web3.Utils.erc20ABI, at: receipt.contractAddress, abiVersion: 2)! + let userAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")! let tokenBalance = try token.read("balanceOf", parameters: [userAddress] as [AnyObject])!.callPromise().wait() - guard let bal = tokenBalance["0"] as? BigUInt else {return XCTFail()} + guard let bal = tokenBalance["0"] as? BigUInt else { return XCTFail() } print(String(bal)) } } - - diff --git a/Tests/web3swiftTests/localTests/web3swiftRLPTests.swift b/Tests/web3swiftTests/localTests/web3swiftRLPTests.swift index cb70f8f11..2f28d5e11 100755 --- a/Tests/web3swiftTests/localTests/web3swiftRLPTests.swift +++ b/Tests/web3swiftTests/localTests/web3swiftRLPTests.swift @@ -9,8 +9,8 @@ import BigInt @testable import web3swift -class web3swiftRLPTests: XCTestCase { - +class web3swiftRLPTests: LocalTestCase { + func testRLPdecodeTransaction() throws { let input = Data.fromHex("0xf90890558504e3b292008309153a8080b9083d6060604052336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550341561004f57600080fd5b60405160208061081d83398101604052808051906020019091905050600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff16141515156100a757600080fd5b80600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050610725806100f86000396000f300606060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680638da5cb5b14610067578063b2b2c008146100bc578063d59ba0df146101eb578063d8ffdcc414610247575b600080fd5b341561007257600080fd5b61007a61029c565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34156100c757600080fd5b61019460048080359060200190820180359060200190808060200260200160405190810160405280939291908181526020018383602002808284378201915050505050509190803590602001908201803590602001908080602002602001604051908101604052809392919081815260200183836020028082843782019150505050505091908035906020019082018035906020019080806020026020016040519081016040528093929190818152602001838360200280828437820191505050505050919050506102c1565b6040518080602001828103825283818151815260200191508051906020019060200280838360005b838110156101d75780820151818401526020810190506101bc565b505050509050019250505060405180910390f35b34156101f657600080fd5b61022d600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919080351515906020019091905050610601565b604051808215151515815260200191505060405180910390f35b341561025257600080fd5b61025a6106bf565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6102c96106e5565b6102d16106e5565b6000806000600260003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff16151561032e57600080fd5b8651885114151561033e57600080fd5b875160405180591061034d5750595b9080825280602002602001820160405250935060009250600091505b87518210156105f357600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166323b872dd87848151811015156103be57fe5b906020019060200201518a858151811015156103d657fe5b906020019060200201518a868151811015156103ee57fe5b906020019060200201516000604051602001526040518463ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018281526020019350505050602060405180830381600087803b15156104b857600080fd5b6102c65a03f115156104c957600080fd5b50505060405180519050905080156105e65787828151811015156104e957fe5b90602001906020020151848481518110151561050157fe5b9060200190602002019073ffffffffffffffffffffffffffffffffffffffff16908173ffffffffffffffffffffffffffffffffffffffff16815250508280600101935050868281518110151561055357fe5b90602001906020020151888381518110151561056b57fe5b9060200190602002015173ffffffffffffffffffffffffffffffffffffffff16878481518110151561059957fe5b9060200190602002015173ffffffffffffffffffffffffffffffffffffffff167f334b3b1d4ad406523ee8e24beb689f5adbe99883a662c37d43275de52389da1460405160405180910390a45b8180600101925050610369565b839450505050509392505050565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561065e57600080fd5b81600260008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548160ff0219169083151502179055506001905092915050565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6020604051908101604052806000815250905600a165627a7a723058200618093d895b780d4616f24638637da0e0f9767e6d3675a9525fee1d6ed7f431002900000000000000000000000045245bc59219eeaaf6cd3f382e078a461ff9de7b25a0d1efc3c97d1aa9053aa0f59bf148d73f59764343bf3cae576c8769a14866948da0613d0265634fddd436397bc858e2672653833b57a05cfc8b93c14a6c05166e4a")! let transaction = EthereumTransaction(rawValue: input) diff --git a/Tests/web3swiftTests/localTests/web3swiftTests.swift b/Tests/web3swiftTests/localTests/web3swiftTests.swift index d77325de3..d31431e15 100755 --- a/Tests/web3swiftTests/localTests/web3swiftTests.swift +++ b/Tests/web3swiftTests/localTests/web3swiftTests.swift @@ -4,15 +4,14 @@ // Copyright © 2018 Alex Vlasov. All rights reserved. // - import XCTest import CryptoSwift import BigInt @testable import web3swift -class web3swiftTests: XCTestCase { - +class web3swiftTests: LocalTestCase { + func testBitFunctions () throws { let data = Data([0xf0, 0x02, 0x03]) let firstBit = data.bitsInRange(0,1) @@ -20,7 +19,7 @@ class web3swiftTests: XCTestCase { let first4bits = data.bitsInRange(0,4) XCTAssert(first4bits == 0x0f) } - + func testCombiningPublicKeys() throws { let priv1 = Data(repeating: 0x01, count: 32) let pub1 = Web3.Utils.privateToPublic(priv1, compressed: true)! @@ -31,28 +30,28 @@ class web3swiftTests: XCTestCase { let compinedPub = Web3.Utils.privateToPublic(compinedPriv, compressed: true) XCTAssert(compinedPub == combined) } - + func testChecksumAddress() throws { let input = "0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359" - let output = EthereumAddress.toChecksumAddress(input); + let output = EthereumAddress.toChecksumAddress(input) XCTAssert(output == "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359", "Failed to checksum address") } - + func testChecksumAddressParsing() throws { let input = "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359" - let addr = EthereumAddress(input); - XCTAssert(addr != nil); + let addr = EthereumAddress(input) + XCTAssert(addr != nil) let invalidInput = "0xfb6916095ca1df60bB79Ce92cE3Ea74c37c5d359" - let invalidAddr = EthereumAddress(invalidInput); - XCTAssert(invalidAddr == nil); + let invalidAddr = EthereumAddress(invalidInput) + XCTAssert(invalidAddr == nil) } - + func testBigUIntFromHex() throws { let hexRepresentation = "0x1c31de57e49fc00".stripHexPrefix() let biguint = BigUInt(hexRepresentation, radix: 16)! XCTAssert(biguint == BigUInt("126978086000000000")) } - + func testBloom() throws { let positive = [ "testtest", @@ -88,7 +87,7 @@ class web3swiftTests: XCTestCase { let privKey = SECP256K1.generatePrivateKey() XCTAssert(privKey != nil, "Failed to create new private key") } - + func testIBANcreation() throws { let iban = "XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS" let native = Web3.Utils.Iban(iban) @@ -96,25 +95,24 @@ class web3swiftTests: XCTestCase { let expectedAddress = "0x00c5496aEe77C1bA1f0854206A26DdA82a81D6D8" let createdAddress = native?.toEthereumAddress()?.address XCTAssert(createdAddress == expectedAddress) - + let address = EthereumAddress("0x03c5496aee77c1ba1f0854206a26dda82a81d6d8")! let fromAddress = Web3.Utils.Iban(address) let ibn = fromAddress?.iban XCTAssert(ibn == "XE83FUTTUNPK7WZJSGGCWVEBARQWQ8YML4") } - + func testGenericRPCresponse() throws { let hex = "0x1" let rpcResponse = JSONRPCresponse(id: 1, jsonrpc: "2.0", result: hex, error: nil) let value: BigUInt? = rpcResponse.getValue() XCTAssert(value == 1) } - + func testPublicMappingsAccess() throws { let jsonString = "[{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"users\",\"outputs\":[{\"name\":\"name\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"address\"}],\"name\":\"userDeviceCount\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"totalUsers\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"}]" - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) - guard let addr = EthereumAddress("0xdef61132a0c1259464b19e4590e33666aae38574") else {return XCTFail()} - let contract = web3.contract(jsonString, at: addr, abiVersion: 2) + guard let addr = EthereumAddress("0xdef61132a0c1259464b19e4590e33666aae38574") else { return XCTFail() } + let contract = ganache.contract(jsonString, at: addr, abiVersion: 2) XCTAssert(contract != nil) let allMethods = contract!.contract.allMethods let userDeviceCount = try contract!.read("userDeviceCount", parameters: [addr as AnyObject])?.callPromise().wait() @@ -125,6 +123,5 @@ class web3swiftTests: XCTestCase { print(user!) print(allMethods) } - -} +} diff --git a/Tests/web3swiftTests/localTests/web3swiftTransactionsTests.swift b/Tests/web3swiftTests/localTests/web3swiftTransactionsTests.swift index 07f3efd4f..dd48fe366 100755 --- a/Tests/web3swiftTests/localTests/web3swiftTransactionsTests.swift +++ b/Tests/web3swiftTests/localTests/web3swiftTransactionsTests.swift @@ -14,7 +14,7 @@ import BigInt @testable import web3swift -class web3swiftTransactionsTests: XCTestCase { +class web3swiftTransactionsTests: LocalTestCase { enum TestCase: Int { case legacyFallback = 0 @@ -625,10 +625,9 @@ class web3swiftTransactionsTests: XCTestCase { func testEthSendExampleAndGetTransactionReceiptAndDetails() { do { - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")! - let allAddresses = try web3.eth.getAccounts() - let contract = web3.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2) + let allAddresses = try ganache.eth.getAccounts() + let contract = ganache.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2) let value = Web3.Utils.parseToBigUInt("1.0", units: .eth) let from = allAddresses[0] let writeTX = contract!.write("fallback")! @@ -641,7 +640,7 @@ class web3swiftTransactionsTests: XCTestCase { Thread.sleep(forTimeInterval: 1.0) - let receipt = try web3.eth.getTransactionReceipt(txHash) + let receipt = try ganache.eth.getTransactionReceipt(txHash) print(receipt) XCTAssert(receipt.status == .ok) @@ -652,12 +651,12 @@ class web3swiftTransactionsTests: XCTestCase { break } - let details = try web3.eth.getTransactionDetails(txHash) + let details = try ganache.eth.getTransactionDetails(txHash) print(details) let txnGasLimit = details.transaction.parameters.gasLimit XCTAssert(txnGasLimit == BigUInt(78423)) } catch Web3Error.nodeError(let descr) { - guard descr == "insufficient funds for gas * price + value" else {return XCTFail()} + guard descr == "insufficient funds for gas * price + value" else { return XCTFail() } } catch { print(error) XCTFail() diff --git a/Tests/web3swiftTests/localTests/web3swiftUserCases.swift b/Tests/web3swiftTests/localTests/web3swiftUserCases.swift index 9fe20a52d..e55a029b1 100755 --- a/Tests/web3swiftTests/localTests/web3swiftUserCases.swift +++ b/Tests/web3swiftTests/localTests/web3swiftUserCases.swift @@ -6,42 +6,39 @@ import XCTest import BigInt -//import EthereumAddress @testable import web3swift -class web3swiftUserCases: XCTestCase { - +class web3swiftUserCases: LocalTestCase { + func getKeystoreData() -> Data? { let bundle = Bundle(for: type(of: self)) - guard let path = bundle.path(forResource: "key", ofType: "json") else {return nil} - guard let data = NSData(contentsOfFile: path) else {return nil} + guard let path = bundle.path(forResource: "key", ofType: "json") else { return nil } + guard let data = NSData(contentsOfFile: path) else { return nil } return data as Data } - + func testUserCase1() throws { - let (web3, _, receipt, abiString) = try web3swiftHelpers.localDeployERC20() + let (_, receipt, abiString) = try web3swiftHelpers.localDeployERC20(ganache) let account = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")! - let contract = web3.contract(abiString, at: receipt.contractAddress!)! - let readTransaction = contract.read("balanceOf", parameters:[account] as [AnyObject])! + let contract = ganache.contract(abiString, at: receipt.contractAddress!)! + let readTransaction = contract.read("balanceOf", parameters: [account] as [AnyObject])! readTransaction.transactionOptions.from = account let response = try readTransaction.callPromise().wait() let balance = response["0"] as? BigUInt print(balance!.description) } - + func testUserCase2() { - let url = URL(string: "http://127.0.0.1:8545")! - let web3 = try? Web3.new(url) + let web3 = try? Web3.new(LocalTestCase.url) XCTAssert(web3 != nil, "Failed to create web3 for custom provider") } - + func testProperGasLimit() throws { - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) - let allAddresses = try web3.eth.getAccounts() - let gasPrice = try web3.eth.getGasPrice() + let allAddresses = try ganache.eth.getAccounts() + let gasPrice = try ganache.eth.getGasPrice() let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")! - guard let writeTX = web3.eth.sendETH(to: sendToAddress, amount: "0.001") else {return XCTFail()} + guard let writeTX = ganache.eth.sendETH(to: sendToAddress, amount: "0.001") else { return XCTFail() } writeTX.transactionOptions.from = allAddresses[0] writeTX.transactionOptions.gasPrice = .manual(gasPrice) let gasEstimate = try writeTX.estimateGasPromise().wait() @@ -52,11 +49,10 @@ class web3swiftUserCases: XCTestCase { } func testProperGasPrice() throws { - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) - let allAddresses = try web3.eth.getAccounts() - let gasPrice = try web3.eth.getGasPrice() + let allAddresses = try ganache.eth.getAccounts() + let gasPrice = try ganache.eth.getGasPrice() let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")! - guard let writeTX = web3.eth.sendETH(to: sendToAddress, amount: "0.001") else {return XCTFail()} + guard let writeTX = ganache.eth.sendETH(to: sendToAddress, amount: "0.001") else { return XCTFail() } writeTX.transactionOptions.from = allAddresses[0] writeTX.transactionOptions.gasPrice = .manual(gasPrice * 2) let gasEstimate = try writeTX.estimateGasPromise().wait() @@ -64,18 +60,17 @@ class web3swiftUserCases: XCTestCase { let assembled = try writeTX.assemblePromise().wait() let txnGasLimit = assembled.parameters.gasLimit let txnGasPrice = assembled.parameters.gasPrice - + XCTAssert(txnGasLimit == gasEstimate + 1234) XCTAssert(txnGasPrice == gasPrice * 2) } - - func testParseTransactionDetailsForContractCreation() throws {// Deploy contract + + func testParseTransactionDetailsForContractCreation() throws { // Deploy contract let bytecode = Data.fromHex("0x608060405234801561001057600080fd5b50610100806100206000396000f30060806040526004361060525763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416630dbe671f8114605757806329e99f0714607b5780634df7e3d0146092575b600080fd5b348015606257600080fd5b50606960a4565b60408051918252519081900360200190f35b348015608657600080fd5b50609060043560aa565b005b348015609d57600080fd5b50606960ce565b60005481565b803a111560ba57600160005560cb565b803a101560cb576001600081905580555b50565b600154815600a165627a7a723058200327a504a24f70cf740239fad2ad203f21caf0ef05f7870bd88482f6fa3cf1080029")! - - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) - let allAddresses = try web3.eth.getAccounts() - let contract = web3.contract(Web3.Utils.estimateGasTestABI, at: nil, abiVersion: 2)! - + + let allAddresses = try ganache.eth.getAccounts() + let contract = ganache.contract(Web3.Utils.estimateGasTestABI, at: nil, abiVersion: 2)! + let parameters = [] as [AnyObject] let deployTx = contract.deploy(bytecode: bytecode, parameters: parameters)! deployTx.transactionOptions.from = allAddresses[0] @@ -83,30 +78,29 @@ class web3swiftUserCases: XCTestCase { let result = try deployTx.sendPromise().wait() let txHash = result.hash print("Transaction with hash " + txHash) - + Thread.sleep(forTimeInterval: 1.0) - - let receipt = try web3.eth.getTransactionReceipt(txHash) + + let receipt = try ganache.eth.getTransactionReceipt(txHash) print(receipt) XCTAssert(receipt.contractAddress != nil) - + switch receipt.status { case .notYetProcessed: return default: break } - - let details = try web3.eth.getTransactionDetails(txHash) + + let details = try ganache.eth.getTransactionDetails(txHash) print(details) XCTAssert(details.transaction.to == .contractDeploymentAddress()) } - + func testNonBatchedRequest() throws { - let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!) let address = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")! - web3.requestDispatcher.policy = .NoBatching - let balanceResult = try web3.eth.getBalance(address: address) + ganache.requestDispatcher.policy = .NoBatching + let balanceResult = try ganache.eth.getBalance(address: address) print(balanceResult) } } diff --git a/Tests/web3swiftTests/remoteTests/RemoteTests.xctestplan b/Tests/web3swiftTests/remoteTests/RemoteTests.xctestplan index 6f7f05fa1..216a81766 100644 --- a/Tests/web3swiftTests/remoteTests/RemoteTests.xctestplan +++ b/Tests/web3swiftTests/remoteTests/RemoteTests.xctestplan @@ -49,7 +49,8 @@ "web3swift_numberFormattingUtil_Tests", "web3swift_personalSignature_Tests", "web3swift_promises_Tests", - "web3swift_transactions_Tests" + "web3swift_transactions_Tests", + "LocalTestCase" ], "target" : { "containerPath" : "container:web3swift.xcodeproj", diff --git a/Tests/web3swiftTests/web3swift.xctestplan b/Tests/web3swiftTests/web3swift.xctestplan index 0ce49df4f..3ca33455a 100644 --- a/Tests/web3swiftTests/web3swift.xctestplan +++ b/Tests/web3swiftTests/web3swift.xctestplan @@ -19,6 +19,9 @@ "testTargets" : [ { "parallelizable" : true, + "skippedTests" : [ + "LocalTestCase" + ], "target" : { "containerPath" : "container:web3swift.xcodeproj", "identifier" : "139751B6219AF76D0044D2B0", diff --git a/web3swift.xcodeproj/project.pbxproj b/web3swift.xcodeproj/project.pbxproj index 3d919dc39..9e6f66e26 100755 --- a/web3swift.xcodeproj/project.pbxproj +++ b/web3swift.xcodeproj/project.pbxproj @@ -193,6 +193,7 @@ 5CF7E8B0276B792A0009900F /* web3swiftEventloopTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5CF7E89F276B79280009900F /* web3swiftEventloopTests.swift */; }; 5CF7E8B1276B792A0009900F /* web3swiftAdvancedABIv2Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5CF7E8A0276B79290009900F /* web3swiftAdvancedABIv2Tests.swift */; }; 5CF7E8B2276B792A0009900F /* web3swiftEIP67Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5CF7E8A1276B79290009900F /* web3swiftEIP67Tests.swift */; }; + 6029BE722817A4A8006FA0DF /* LocalTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6029BE712817A4A8006FA0DF /* LocalTestCase.swift */; }; 6049F410280616FC00DFE624 /* AbstractEnvelope.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6049F409280616FC00DFE624 /* AbstractEnvelope.swift */; }; 6049F411280616FC00DFE624 /* EIP1559Envelope.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6049F40A280616FC00DFE624 /* EIP1559Envelope.swift */; }; 6049F412280616FC00DFE624 /* EIP2718Envelope.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6049F40B280616FC00DFE624 /* EIP2718Envelope.swift */; }; @@ -305,7 +306,6 @@ 3AA815372276E44100F5DB52 /* EthereumKeystoreV3.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EthereumKeystoreV3.swift; sourceTree = ""; }; 3AA815392276E44100F5DB52 /* PlainKeystore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PlainKeystore.swift; sourceTree = ""; }; 3AA8153A2276E44100F5DB52 /* AbstractKeystore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AbstractKeystore.swift; sourceTree = ""; }; - 3AA8153B2276E44100F5DB52 /* KeystoreV3JSONStructure.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeystoreV3JSONStructure.swift; sourceTree = ""; }; 3AA8153D2276E44100F5DB52 /* BloomFilter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BloomFilter.swift; sourceTree = ""; }; 3AA8153F2276E44100F5DB52 /* EthereumTransaction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EthereumTransaction.swift; sourceTree = ""; }; 3AA815412276E44100F5DB52 /* Web3+Wallet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Web3+Wallet.swift"; sourceTree = ""; }; @@ -425,6 +425,7 @@ 5CF7E89F276B79280009900F /* web3swiftEventloopTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = web3swiftEventloopTests.swift; sourceTree = ""; }; 5CF7E8A0276B79290009900F /* web3swiftAdvancedABIv2Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = web3swiftAdvancedABIv2Tests.swift; sourceTree = ""; }; 5CF7E8A1276B79290009900F /* web3swiftEIP67Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = web3swiftEIP67Tests.swift; sourceTree = ""; }; + 6029BE712817A4A8006FA0DF /* LocalTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LocalTestCase.swift; sourceTree = ""; }; 6049F409280616FC00DFE624 /* AbstractEnvelope.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AbstractEnvelope.swift; sourceTree = ""; }; 6049F40A280616FC00DFE624 /* EIP1559Envelope.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EIP1559Envelope.swift; sourceTree = ""; }; 6049F40B280616FC00DFE624 /* EIP2718Envelope.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EIP2718Envelope.swift; sourceTree = ""; }; @@ -705,7 +706,6 @@ 3AA815372276E44100F5DB52 /* EthereumKeystoreV3.swift */, 3AA815392276E44100F5DB52 /* PlainKeystore.swift */, 3AA8153A2276E44100F5DB52 /* AbstractKeystore.swift */, - 3AA8153B2276E44100F5DB52 /* KeystoreV3JSONStructure.swift */, ); path = KeystoreManager; sourceTree = ""; @@ -1024,6 +1024,7 @@ E252E68126B542D000717C16 /* localTests */ = { isa = PBXGroup; children = ( + 6029BE712817A4A8006FA0DF /* LocalTestCase.swift */, 5C26D89F27F3725500431EB0 /* EIP1559BlockTests.swift */, 604FA4FE27ECBDC80021108F /* DataConversionTests.swift */, 5CF7E8A0276B79290009900F /* web3swiftAdvancedABIv2Tests.swift */, @@ -1449,6 +1450,7 @@ 5CF7E8B2276B792A0009900F /* web3swiftEIP67Tests.swift in Sources */, 5CF7E8AE276B792A0009900F /* web3swiftPromisesTests.swift in Sources */, 5CF7E8A2276B79290009900F /* web3swiftEIP681Tests.swift in Sources */, + 6029BE722817A4A8006FA0DF /* LocalTestCase.swift in Sources */, 5CC0333A28023387006AD710 /* ST20AndSecurityTokenTests.swift in Sources */, 604FA4FF27ECBDC80021108F /* DataConversionTests.swift in Sources */, 5CF7E8B1276B792A0009900F /* web3swiftAdvancedABIv2Tests.swift in Sources */,