Skip to content

Commit 6df9c13

Browse files
committed
Add a ganache preloader set-up to ensure ganache has some history
1 parent 1c5145c commit 6df9c13

12 files changed

+89
-7
lines changed

Sources/web3swift/Web3/Web3+MutatingTransaction.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public class WriteTransaction: ReadTransaction {
7676
optionsForGasEstimation.type = mergedOptions.type
7777
optionsForGasEstimation.accessList = mergedOptions.accessList
7878

79-
8079
// assemble promise for gasLimit
8180
var gasEstimatePromise: Promise<BigUInt>? = nil
8281
guard let gasLimitPolicy = mergedOptions.gasLimit else {

Tests/web3swiftTests/localTests/web3swiftAdvancedABIv2Tests.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ import BigInt
1111
@testable import web3swift
1212

1313
class web3swiftAdvancedABIv2Tests: XCTestCase {
14-
14+
15+
// this is needed for any test suites that depend on the block-chain state from Ganache
16+
override class func setUp() {
17+
super.setUp()
18+
preloadGanache()
19+
}
20+
1521
func testAdvancedABIv2() throws {
1622
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\"}]"
1723
let bytecode = Data.fromHex("6080604052341561000f57600080fd5b610cb18061001e6000396000f30060806040526004361061006d576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063189533931461007257806338163ff51461009b57806388be6c65146100c4578063e7c1a47c146100ed578063f376e01314610116575b600080fd5b341561007d57600080fd5b61008561013f565b6040516100929190610a9f565b60405180910390f35b34156100a657600080fd5b6100ae610220565b6040516100bb9190610a7d565b60405180910390f35b34156100cf57600080fd5b6100d76102c5565b6040516100e49190610ae3565b60405180910390f35b34156100f857600080fd5b610100610350565b60405161010d9190610ac1565b60405180910390f35b341561012157600080fd5b610129610399565b6040516101369190610b05565b60405180910390f35b6060600260405190808252806020026020018201604052801561017657816020015b60608152602001906001900390816101615790505b5090506040805190810160405280600581526020017f48656c6c6f0000000000000000000000000000000000000000000000000000008152508160008151811015156101be57fe5b906020019060200201819052506040805190810160405280600581526020017f576f726c6400000000000000000000000000000000000000000000000000000081525081600181518110151561021057fe5b9060200190602002018190525090565b610228610546565b6040805190810160405280600581526020017f48656c6c6f00000000000000000000000000000000000000000000000000000081525081600060028110151561026d57fe5b60200201819052506040805190810160405280600581526020017f576f726c640000000000000000000000000000000000000000000000000000008152508160016002811015156102ba57fe5b602002018190525090565b6060600260405190808252806020026020018201604052801561030257816020015b6102ef61056d565b8152602001906001900390816102e75790505b50905061030d610399565b81600081518110151561031c57fe5b90602001906020020181905250610331610399565b81600181518110151561034057fe5b9060200190602002018190525090565b6103586105a9565b610360610399565b81600060028110151561036f57fe5b602002018190525061037f610399565b81600160028110151561038e57fe5b602002018190525090565b6103a16105d8565b60606103ab610614565b6103b3610546565b60036040519080825280602002602001820160405280156103e35781602001602082028038833980820191505090505b50925060008360008151811015156103f757fe5b9060200190602002018181525050600183600181518110151561041657fe5b9060200190602002018181525050600283600281518110151561043557fe5b90602001906020020181815250506040805190810160405280600081526020016001815250915060408051908101604052806040805190810160405280600581526020017f48656c6c6f00000000000000000000000000000000000000000000000000000081525081526020016040805190810160405280600581526020017f576f726c64000000000000000000000000000000000000000000000000000000815250815250905060a060405190810160405280600181526020016040805190810160405280600b81526020017f48656c6c6f20776f726c64000000000000000000000000000000000000000000815250815260200183815260200184815260200182815250935083935050505090565b60408051908101604052806002905b60608152602001906001900390816105555790505090565b60e060405190810160405280600081526020016060815260200161058f610636565b8152602001606081526020016105a3610658565b81525090565b6101c0604051908101604052806002905b6105c261056d565b8152602001906001900390816105ba5790505090565b60e06040519081016040528060008152602001606081526020016105fa610636565b81526020016060815260200161060e610658565b81525090565b6040805190810160405280600290602082028038833980820191505090505090565b6040805190810160405280600290602082028038833980820191505090505090565b60408051908101604052806002905b60608152602001906001900390816106675790505090565b600061068a82610b81565b8360208202850161069a85610b31565b60005b848110156106d35783830388526106b5838351610930565b92506106c082610bdb565b915060208801975060018101905061069d565b508196508694505050505092915050565b60006106ef82610b76565b836020820285016106ff85610b27565b60005b8481101561073857838303885261071a838351610930565b925061072582610bce565b9150602088019750600181019050610702565b508196508694505050505092915050565b600061075482610b8c565b8084526020840193508360208202850161076d85610b3b565b60005b848110156107a6578383038852610788838351610930565b925061079382610be8565b9150602088019750600181019050610770565b508196508694505050505092915050565b60006107c282610b97565b836020820285016107d285610b48565b60005b8481101561080b5783830388526107ed8383516109ea565b92506107f882610bf5565b91506020880197506001810190506107d5565b508196508694505050505092915050565b600061082782610ba2565b8084526020840193508360208202850161084085610b52565b60005b8481101561087957838303885261085b8383516109ea565b925061086682610c02565b9150602088019750600181019050610843565b508196508694505050505092915050565b61089381610bad565b61089c82610b5f565b60005b828110156108ce576108b2858351610a6e565b6108bb82610c0f565b915060208501945060018101905061089f565b5050505050565b60006108e082610bb8565b8084526020840193506108f283610b69565b60005b8281101561092457610908868351610a6e565b61091182610c1c565b91506020860195506001810190506108f5565b50849250505092915050565b600061093b82610bc3565b80845261094f816020860160208601610c33565b61095881610c66565b602085010191505092915050565b600060c08301600083015161097e6000860182610a6e565b50602083015184820360208601526109968282610930565b91505060408301516109ab604086018261088a565b50606083015184820360808601526109c382826108d5565b915050608083015184820360a08601526109dd82826106e4565b9150508091505092915050565b600060c083016000830151610a026000860182610a6e565b5060208301518482036020860152610a1a8282610930565b9150506040830151610a2f604086018261088a565b5060608301518482036080860152610a4782826108d5565b915050608083015184820360a0860152610a6182826106e4565b9150508091505092915050565b610a7781610c29565b82525050565b60006020820190508181036000830152610a97818461067f565b905092915050565b60006020820190508181036000830152610ab98184610749565b905092915050565b60006020820190508181036000830152610adb81846107b7565b905092915050565b60006020820190508181036000830152610afd818461081c565b905092915050565b60006020820190508181036000830152610b1f8184610966565b905092915050565b6000819050919050565b6000819050919050565b6000602082019050919050565b6000819050919050565b6000602082019050919050565b6000819050919050565b6000602082019050919050565b600060029050919050565b600060029050919050565b600081519050919050565b600060029050919050565b600081519050919050565b600060029050919050565b600081519050919050565b600081519050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000602082019050919050565b6000819050919050565b60005b83811015610c51578082015181840152602081019050610c36565b83811115610c60576000848401525b50505050565b6000601f19601f83011690509190505600a265627a7a72305820fdaf8ce6fe282a46498c8066d5b5ac382b69969eee38e1ad03c0d501b27f65366c6578706572696d656e74616cf50037")!

Tests/web3swiftTests/localTests/web3swiftBasicLocalNodeTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ import BigInt
1414

1515
class web3swiftBasicLocalNodeTests: XCTestCase {
1616

17+
// this is needed for any test suites that depend on the block-chain state from Ganache
18+
override class func setUp() {
19+
super.setUp()
20+
preloadGanache()
21+
}
22+
1723
func testDeployWithRemoteSigning() throws {
1824
let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!)
1925
let allAddresses = try web3.eth.getAccounts()

Tests/web3swiftTests/localTests/web3swiftDecodeSolidityErrorType.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ import web3swift
1313
/// Contracts' ABI with this type were not decodable.
1414
class web3swiftDecodeSolidityErrorType: XCTestCase {
1515

16+
// this is needed for any test suites that depend on the block-chain state from Ganache
17+
override class func setUp() {
18+
super.setUp()
19+
preloadGanache()
20+
}
21+
1622
func testStructuredErrorTypeDecoding() throws {
1723
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\"}]"
1824
let web3Instance = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!)

Tests/web3swiftTests/localTests/web3swiftERC20ClassTests.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import BigInt
1010
@testable import web3swift
1111

1212
class web3swiftERC20ClassTests: XCTestCase {
13-
13+
// this is needed for any test suites that depend on the block-chain state from Ganache
14+
override class func setUp() {
15+
super.setUp()
16+
preloadGanache()
17+
}
18+
1419
func testERC20TokenCreation() throws {
1520
let (web3, _, receipt, _) = try web3swiftHelpers.localDeployERC20()
1621
let erc20token = ERC20.init(web3: web3, provider: web3.provider, address: receipt.contractAddress!)

Tests/web3swiftTests/localTests/web3swiftERC20Tests.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import BigInt
1010
@testable import web3swift
1111

1212
class web3swiftERC20Tests: XCTestCase {
13-
13+
// this is needed for any test suites that depend on the block-chain state from Ganache
14+
override class func setUp() {
15+
super.setUp()
16+
preloadGanache()
17+
}
18+
1419
func testERC20name() throws {
1520
let (web3, _, receipt, _) = try web3swiftHelpers.localDeployERC20()
1621

Tests/web3swiftTests/localTests/web3swiftEventloopTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import XCTest
99
@testable import web3swift
1010

1111
class web3swiftEventloopTests: XCTestCase {
12+
// this is needed for any test suites that depend on the block-chain state from Ganache
13+
override class func setUp() {
14+
super.setUp()
15+
preloadGanache()
16+
}
1217

1318
func testBasicEventLoop() throws {
1419
var ticksToWait = 5

Tests/web3swiftTests/localTests/web3swiftHelpers.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,32 @@ class web3swiftHelpers {
4848
return (web3, result, receipt, abiString)
4949
}
5050
}
51+
52+
53+
public func preloadGanache() {
54+
let web3 = try! Web3.new(URL.init(string: "http://127.0.0.1:8545")!)
55+
56+
let block = try! web3.eth.getBlockNumber()
57+
if block >= 25 { return }
58+
59+
print("\n ****** Preloading Ganache (\(25-block) blocks) *****\n")
60+
61+
let allAddresses = try! web3.eth.getAccounts()
62+
let sendToAddress = allAddresses[0]
63+
let contract = web3.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)
64+
let value = Web3.Utils.parseToBigUInt("1.0", units: .eth)
65+
66+
67+
let from = allAddresses[0]
68+
let writeTX = contract!.write("fallback")!
69+
writeTX.transactionOptions.from = from
70+
writeTX.transactionOptions.value = value
71+
writeTX.transactionOptions.gasLimit = .manual(78423)
72+
writeTX.transactionOptions.gasPrice = .manual(20000000000)
73+
74+
for _ in block..<25 {
75+
let result = try! writeTX.sendPromise(password: "").wait()
76+
}
77+
78+
79+
}

Tests/web3swiftTests/localTests/web3swiftPersonalSignatureTests.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import BigInt
1111
@testable import web3swift
1212

1313
class web3swiftPersonalSignatureTests: XCTestCase {
14-
14+
// this is needed for any test suites that depend on the block-chain state from Ganache
15+
override class func setUp() {
16+
super.setUp()
17+
preloadGanache()
18+
}
19+
1520
func testPersonalSignature() throws {
1621
let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!)
1722
let tempKeystore = try! EthereumKeystoreV3(password: "")

Tests/web3swiftTests/localTests/web3swiftPromisesTests.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ import BigInt
1515

1616
class web3swiftPromisesTests: XCTestCase {
1717
var urlSession : URLSession?
18-
18+
// this is needed for any test suites that depend on the block-chain state from Ganache
19+
override class func setUp() {
20+
super.setUp()
21+
preloadGanache()
22+
}
23+
1924
func testGetBalancePromise() throws {
2025
let web3 = try Web3.new(URL.init(string: "http://127.0.0.1:8545")!)
2126
let balance = try web3.eth.getBalancePromise(address: "0xe22b8979739D724343bd002F9f432F5990879901").wait()

0 commit comments

Comments
 (0)