|
| 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 | +} |
0 commit comments