Skip to content

Commit 97ba8e9

Browse files
feat: mocking network responses
1 parent d08a2de commit 97ba8e9

File tree

5 files changed

+178
-67
lines changed

5 files changed

+178
-67
lines changed

Sources/web3swift/EthereumAPICalls/Ethereum/Eth+Call.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66
import Foundation
77
import Core
88

9-
extension Web3.Eth {
10-
public func callTransaction(_ transaction: CodableTransaction) async throws -> Data {
9+
public protocol IEth {
10+
var provider: Web3Provider { get }
11+
func callTransaction(_ transaction: CodableTransaction) async throws -> Data
12+
}
13+
14+
public extension IEth {
15+
func callTransaction(_ transaction: CodableTransaction) async throws -> Data {
1116
let request = APIRequest.call(transaction, transaction.callOnBlock ?? .latest)
1217
return try await APIRequest.sendRequest(with: provider, for: request).result
1318
}

Sources/web3swift/Web3/Web3+Instance.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,20 @@ public class Web3 {
3535
}
3636

3737
// FIXME: Rewrite this to CodableTransaction
38-
public class Eth {
39-
var provider: Web3Provider
40-
// weak var web3: web3?
38+
public class Eth: IEth {
39+
public var provider: Web3Provider
40+
// FIXME: web3 must be weak
4141
var web3: Web3
4242

4343
public init(provider prov: Web3Provider, web3 web3instance: Web3) {
4444
provider = prov
4545
web3 = web3instance
4646
}
47+
48+
public func callTransaction(_ transaction: CodableTransaction) async throws -> Data {
49+
let request = APIRequest.call(transaction, transaction.callOnBlock ?? .latest)
50+
return try await APIRequest.sendRequest(with: provider, for: request).result
51+
}
4752
}
4853

4954
var personalInstance: Web3.Personal?
@@ -60,7 +65,7 @@ public class Web3 {
6065
// FIXME: Rewrite this to CodableTransaction
6166
public class Personal {
6267
var provider: Web3Provider
63-
// weak var web3: web3?
68+
// FIXME: web3 must be weak
6469
var web3: Web3
6570
public init(provider prov: Web3Provider, web3 web3instance: Web3) {
6671
provider = prov
@@ -82,7 +87,7 @@ public class Web3 {
8287
// FIXME: Rewrite this to CodableTransaction
8388
public class TxPool {
8489
var provider: Web3Provider
85-
// weak var web3: web3?
90+
// FIXME: web3 must be weak
8691
var web3: Web3
8792
public init(provider prov: Web3Provider, web3 web3instance: Web3) {
8893
provider = prov
@@ -103,7 +108,7 @@ public class Web3 {
103108

104109
public class Web3Wallet {
105110
var provider: Web3Provider
106-
// weak var web3: web3?
111+
// FIXME: web3 must be weak
107112
var web3: Web3
108113
public init(provider prov: Web3Provider, web3 web3instance: Web3) {
109114
provider = prov
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// Mocks.swift
3+
//
4+
// Created by JeneaVranceanu on 07.12.2022.
5+
//
6+
7+
import Foundation
8+
@testable import web3swift
9+
@testable import Core
10+
11+
class Web3EthMock: Web3.Eth {
12+
var onCallTransaction: ((CodableTransaction) -> Data)?
13+
14+
override func callTransaction(_ transaction: CodableTransaction) async throws -> Data {
15+
onCallTransaction?(transaction) ?? Data()
16+
}
17+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
//
2+
// web3swift_ST20_Tests.swift
3+
// web3swift-iOS_Tests
4+
//
5+
// Created by Anton on 15/03/2019.
6+
// Copyright © 2019 The Matter Inc. All rights reserved.
7+
//
8+
import XCTest
9+
import BigInt
10+
import web3swift
11+
import Core
12+
13+
@testable import web3swift
14+
15+
class ST20AndSecurityTokenTests: XCTestCase {
16+
17+
var web3: Web3!
18+
var ethMock: Web3EthMock!
19+
var st20token: ST20!
20+
var securityToken: SecurityToken!
21+
22+
override func setUp() async throws {
23+
web3 = await Web3.InfuraGoerliWeb3(accessToken: Constants.infuraToken)
24+
ethMock = Web3EthMock(provider: web3.provider, web3: web3)
25+
web3.ethInstance = ethMock
26+
st20token = ST20.init(web3: web3, provider: web3.provider, address: .contractDeploymentAddress())
27+
securityToken = SecurityToken.init(web3: web3, provider: web3.provider, address: .contractDeploymentAddress())
28+
}
29+
30+
func testST20TokenPropertiesBasedOnERC20() async throws {
31+
let expectedSymbol = "RandomTokenSymbol953"
32+
let expectedName = "WhatA NAME - l953. Never seen again!"
33+
let expectedDecimals = UInt8.random(in: 0...255)
34+
35+
ethMock.onCallTransaction = { transaction in
36+
guard let function = self.st20token.contract.contract.getFunctionCalled(transaction.data) else {
37+
XCTFail("Failed to decode function call to determine what shall be returned")
38+
return Data()
39+
}
40+
switch function.name {
41+
case "symbol":
42+
return ABIEncoder.encode(types: [.string], values: [expectedSymbol] as [AnyObject])!
43+
case "name":
44+
return ABIEncoder.encode(types: [.string], values: [expectedName] as [AnyObject])!
45+
case "decimals":
46+
return ABIEncoder.encode(types: [.uint(bits: 8)], values: [expectedDecimals] as [AnyObject])!
47+
default:
48+
// Unexpected function called
49+
XCTFail("Called function '\(String(describing: function.name))' which wasn't supposed to be called.")
50+
return Data()
51+
}
52+
}
53+
54+
try await st20token.readProperties()
55+
XCTAssertEqual(st20token.symbol, expectedSymbol)
56+
XCTAssertEqual(st20token.name, expectedName)
57+
XCTAssertEqual(st20token.decimals, expectedDecimals)
58+
}
59+
60+
func testST20TokenBalanceAndAllowance() async throws {
61+
let expectedAllowance = BigUInt.randomInteger(lessThan: BigUInt(10000000000))
62+
let expectedBalance = BigUInt.randomInteger(lessThan: BigUInt(10000000000))
63+
64+
let userAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!
65+
let delegate = EthereumAddress("0x2dD33957C90880bE4Ee9fd5F703110BDA2E579EC")!
66+
67+
ethMock.onCallTransaction = { transaction in
68+
guard let function = self.st20token.contract.contract.getFunctionCalled(transaction.data) else {
69+
XCTFail("Failed to decode function call to determine what shall be returned")
70+
return Data()
71+
}
72+
switch function.name {
73+
case "balanceOf":
74+
let address = function.decodeInputData(transaction.data)?["0"] as? EthereumAddress
75+
XCTAssertEqual(address, userAddress)
76+
return ABIEncoder.encode(types: [.uint(bits: 256)], values: [expectedBalance] as [AnyObject])!
77+
case "allowance":
78+
let transactionInput = function.decodeInputData(transaction.data)
79+
XCTAssertEqual(transactionInput?["0"] as? EthereumAddress, userAddress)
80+
XCTAssertEqual(transactionInput?["1"] as? EthereumAddress, delegate)
81+
return ABIEncoder.encode(types: [.uint(bits: 256)], values: [expectedAllowance] as [AnyObject])!
82+
default:
83+
// Unexpected function called
84+
XCTFail("Called function '\(String(describing: function.name))' which wasn't supposed to be called.")
85+
return Data()
86+
}
87+
}
88+
89+
let balance = try await st20token.getBalance(account: userAddress)
90+
let allowance = try await st20token.getAllowance(originalOwner: userAddress, delegate: delegate)
91+
XCTAssertEqual(balance, expectedBalance)
92+
XCTAssertEqual(allowance, expectedAllowance)
93+
}
94+
95+
func testSecurityTokenInvestors() async throws {
96+
let expectedNumberOfInvestors = BigUInt.randomInteger(lessThan: BigUInt(10000000000))
97+
// let expectedInvestors = [EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!,EthereumAddress("0x2dD33957C90880bE4Ee9fd5F703110BDA2E579EC")!]
98+
99+
ethMock.onCallTransaction = { transaction in
100+
guard let function = self.securityToken.contract.contract.getFunctionCalled(transaction.data) else {
101+
XCTFail("Failed to decode function call to determine what shall be returned")
102+
return Data()
103+
}
104+
switch function.name {
105+
case "investorCount":
106+
return ABIEncoder.encode(types: [.uint(bits: 256)], values: [expectedNumberOfInvestors] as [AnyObject])!
107+
// case "investors":
108+
// return ABIEncoder.encode(types: [.array(type: .address, length: 0)],
109+
// values: [expectedInvestors] as [AnyObject])!
110+
default:
111+
// Unexpected function called
112+
XCTFail("Called function '\(String(describing: function.name))' which wasn't supposed to be called.")
113+
return Data()
114+
}
115+
}
116+
117+
let investorsCount = try await securityToken.investorCount()
118+
XCTAssertEqual(investorsCount, expectedNumberOfInvestors)
119+
// XCTAssertEqual(investors, expectedInvestors)
120+
}
121+
122+
func testSecurityTokenGranularity() async throws {
123+
let expectedGranularity = BigUInt.randomInteger(lessThan: BigUInt(10000000000))
124+
125+
ethMock.onCallTransaction = { transaction in
126+
guard let function = self.securityToken.contract.contract.getFunctionCalled(transaction.data) else {
127+
XCTFail("Failed to decode function call to determine what shall be returned")
128+
return Data()
129+
}
130+
switch function.name {
131+
case "granularity":
132+
return ABIEncoder.encode(types: [.uint(bits: 256)], values: [expectedGranularity] as [AnyObject])!
133+
default:
134+
// Unexpected function called
135+
XCTFail("Called function '\(String(describing: function.name))' which wasn't supposed to be called.")
136+
return Data()
137+
}
138+
}
139+
140+
let granularity = try await securityToken.getGranularity()
141+
XCTAssertEqual(granularity, expectedGranularity)
142+
}
143+
}

Tests/web3swiftTests/remoteTests/ST20AndSecurityTokenTests.swift

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)