diff --git a/Example/Tests/NumberScalar/EthToWeiTests.swift b/Example/Tests/NumberScalar/EthToWeiTests.swift new file mode 100644 index 0000000..8426c31 --- /dev/null +++ b/Example/Tests/NumberScalar/EthToWeiTests.swift @@ -0,0 +1,30 @@ +// +// This source file is part of the Web3Swift.io open source project +// Copyright 2018 The Web3Swift Authors +// Licensed under Apache License v2.0 +// +// EthToWeiTests.swift +// +// Created by Timofey Solonin on 08/06/2018 +// + +import Nimble +import Quick +@testable import Web3Swift + +final class EthToWeiTests: XCTestCase { + + func testEthToWeiIsConvertedCorrectly() { + expect{ + try HexAsDecimalString( + hex: EthToWei( + amount: 1 + ) + ).value() + }.to( + equal("1000000000000000000"), + description: "1 ether is expected to be equal to 1000000000000000000 wei" + ) + } + +} \ No newline at end of file diff --git a/Example/Web3Swift.xcodeproj/project.pbxproj b/Example/Web3Swift.xcodeproj/project.pbxproj index 23f4e98..331ea08 100644 --- a/Example/Web3Swift.xcodeproj/project.pbxproj +++ b/Example/Web3Swift.xcodeproj/project.pbxproj @@ -117,6 +117,7 @@ 584FACBD6ABDDB76A219D2E7 /* IntegerTypeSizeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 584FA3D160E799DDD8895EC3 /* IntegerTypeSizeTests.swift */; }; 584FACDDE9E98EDA9C414EF6 /* RandomNonceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 584FAB47440D3CE3C6ED0684 /* RandomNonceTests.swift */; }; 584FACEC7F5AD9060590ED93 /* URL.swift in Sources */ = {isa = PBXBuildFile; fileRef = 584FA82731B90F65C00992E6 /* URL.swift */; }; + 584FACFEB25733D90A1A0F26 /* EthToWeiTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 584FA9E48598513D93A7000E /* EthToWeiTests.swift */; }; 584FAD2C1F95B71430856825 /* NaturalIntegerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 584FA4AD9F175F65449C2E14 /* NaturalIntegerTests.swift */; }; 584FAD3793AFB655815DBBA6 /* ChainIDProcedureTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 584FA8BDDA8B4B1DF68FA7AA /* ChainIDProcedureTests.swift */; }; 584FAD6A547499538A33A63B /* FirstBytesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 584FADA4B17A275E320F82F7 /* FirstBytesTests.swift */; }; @@ -245,6 +246,7 @@ 584FA9721120A205A9959167 /* HexStringTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HexStringTests.swift; sourceTree = ""; }; 584FA9C0E212B75D9FFEE329 /* StatesTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatesTests.swift; sourceTree = ""; }; 584FA9D0D639D3F5E5025055 /* ContractDeploymentAndCallIT.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContractDeploymentAndCallIT.swift; sourceTree = ""; }; + 584FA9E48598513D93A7000E /* EthToWeiTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EthToWeiTests.swift; sourceTree = ""; }; 584FAA2AA63184DE9EC11F74 /* EthContractCallTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EthContractCallTests.swift; sourceTree = ""; }; 584FAA3DAF64A2A798B424AC /* DecodedABIDynamicCollectionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DecodedABIDynamicCollectionTests.swift; sourceTree = ""; }; 584FAA56B9ACBC59A8922392 /* SizeConstrainedCollectionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SizeConstrainedCollectionTests.swift; sourceTree = ""; }; @@ -713,6 +715,7 @@ 584FA6E27706136910DAAD74 /* UnsignedNumbersExponentiationTests.swift */, 584FA1881DBE9A400A529BC5 /* EthNumberTests.swift */, 584FAD10CFD47ACAE96F474C /* HexAsDecimalStringTests.swift */, + 584FA9E48598513D93A7000E /* EthToWeiTests.swift */, ); path = NumberScalar; sourceTree = ""; @@ -1267,6 +1270,7 @@ 584FA5AC71FE3208939495C3 /* EthContractCallTests.swift in Sources */, 584FAA6740136B110E21E620 /* JSONResultStringTests.swift in Sources */, 584FA9958A514A58DF0BD029 /* StatesTests.swift in Sources */, + 584FACFEB25733D90A1A0F26 /* EthToWeiTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Web3Swift/NumberScalar/EthToWei.swift b/Web3Swift/NumberScalar/EthToWei.swift new file mode 100644 index 0000000..7b87833 --- /dev/null +++ b/Web3Swift/NumberScalar/EthToWei.swift @@ -0,0 +1,73 @@ +// +// This source file is part of the Web3Swift.io open source project +// Copyright 2018 The Web3Swift Authors +// Licensed under Apache License v2.0 +// +// EthToWei.swift +// +// Created by Timofey Solonin on 08/06/2018 +// + +import Foundation + +//A whole amount of ether converted to an amount of wei +public final class EthToWei: BytesScalar { + + private let amount: BytesScalar + + /** + Ctor + + - parameters: + - amount: amount of ethereum to convert to wei + */ + public init(amount: BytesScalar) { + self.amount = UnsignedNumbersProduct( + terms: [ + amount, + SimpleBytes( + bytes: [ + 0x0d, 0xe0, 0xb6, 0xb3, 0xa7, 0x64, 0x00, 0x00 + ] + ) + ] + ) + } + + /** + Ctor + + - parameters: + - amount: amount of ethereum to convert to wei + */ + public convenience init(amount: IntegerScalar) { + self.init( + amount: EthNumber( + value: amount + ) + ) + } + + /** + Ctor + + - parameters: + - amount: amount of ethereum to convert to wei + */ + public convenience init(amount: Int) { + self.init( + amount: SimpleInteger( + integer: amount + ) + ) + } + + /** + - returns: + The amount of wei in the specified amount of ether (i.e. multiplied by 10^18) + */ + public func value() throws -> Data { + return try amount.value() + } + +} \ No newline at end of file