forked from zeriontech/Web3Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
zeriontech#139 - added EthToWei conversion
- Loading branch information
1 parent
59e8bbd
commit c690e9c
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
|
||
} |