-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add TypeAdapters and Factory * Introducing common-adapter-moshi module * Adding more tests * DefaultNumberAdapter should parse decimal numbers * Adds tests for parseToBigInteger
- Loading branch information
Showing
9 changed files
with
199 additions
and
1 deletion.
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 @@ | ||
/build |
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,24 @@ | ||
apply plugin: 'com.android.library' | ||
apply plugin: 'kotlin-android' | ||
apply plugin: 'kotlin-kapt' | ||
apply from: '../buildsystem/coverageReport.gradle' | ||
|
||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
|
||
dependencies { | ||
// We specify junit before anything else to make sure that this version is prioritized over the | ||
// one bundled in the android.jar | ||
testImplementation "junit:junit:$versions.junit" | ||
|
||
implementation project(":models") | ||
implementation project(":utils") | ||
implementation project(":crypto") | ||
|
||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$versions.kotlin" | ||
implementation "com.github.gnosis.bivrost-kotlin:bivrost-solidity-types:$versions.bivrost" | ||
|
||
// Moshi | ||
implementation "com.squareup.moshi:moshi:$versions.moshi" | ||
kapt "com.squareup.moshi:moshi-kotlin-codegen:$versions.moshi" | ||
} |
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,5 @@ | ||
<manifest | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="pm.gnosis.common.adapter.moshi"> | ||
|
||
</manifest> |
78 changes: 78 additions & 0 deletions
78
common-adapter-moshi/src/main/java/pm/gnosis/common/adapter/moshi/TypeAdapters.kt
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,78 @@ | ||
package pm.gnosis.common.adapters.moshi | ||
|
||
import com.squareup.moshi.FromJson | ||
import com.squareup.moshi.JsonQualifier | ||
import com.squareup.moshi.Moshi | ||
import com.squareup.moshi.ToJson | ||
import pm.gnosis.crypto.utils.asEthereumAddressChecksumString | ||
import pm.gnosis.model.Solidity | ||
import pm.gnosis.models.Wei | ||
import pm.gnosis.utils.asEthereumAddress | ||
import pm.gnosis.utils.hexAsBigInteger | ||
import pm.gnosis.utils.parseToBigInteger | ||
import pm.gnosis.utils.toHexString | ||
import java.math.BigInteger | ||
|
||
object MoshiBuilderFactory { | ||
fun makeMoshiBuilder(): Moshi.Builder { | ||
return Moshi.Builder() | ||
.add(WeiAdapter()) | ||
.add(HexNumberAdapter()) | ||
.add(DecimalNumberAdapter()) | ||
.add(DefaultNumberAdapter()) | ||
.add(SolidityAddressAdapter()) | ||
} | ||
} | ||
|
||
class WeiAdapter { | ||
@ToJson | ||
fun toJson(wei: Wei): String = | ||
wei.value.toHexString() | ||
|
||
@FromJson | ||
fun fromJson(wei: String): Wei { | ||
return Wei(wei.parseToBigInteger()) | ||
} | ||
} | ||
|
||
class HexNumberAdapter { | ||
@ToJson | ||
fun toJson(@HexNumber hexNumber: BigInteger): String = hexNumber.toHexString() | ||
|
||
@FromJson | ||
@HexNumber | ||
fun fromJson(hexNumber: String): BigInteger = hexNumber.hexAsBigInteger() | ||
} | ||
|
||
class DecimalNumberAdapter { | ||
@ToJson | ||
fun toJson(@DecimalNumber bigInteger: BigInteger): String = bigInteger.toString() | ||
|
||
@FromJson | ||
@DecimalNumber | ||
fun fromJson(decimalNumber: String): BigInteger = decimalNumber.toBigInteger() | ||
} | ||
|
||
class DefaultNumberAdapter { | ||
@ToJson | ||
fun toJson(hexNumber: BigInteger): String = hexNumber.toHexString() | ||
|
||
@FromJson | ||
fun fromJson(hexNumber: String): BigInteger = hexNumber.parseToBigInteger() | ||
} | ||
|
||
class SolidityAddressAdapter { | ||
@ToJson | ||
fun toJson(address: Solidity.Address): String = address.asEthereumAddressChecksumString() | ||
|
||
@FromJson | ||
fun fromJson(address: String): Solidity.Address = address.asEthereumAddress()!! | ||
} | ||
|
||
@Retention(AnnotationRetention.RUNTIME) | ||
@JsonQualifier | ||
annotation class HexNumber | ||
|
||
@Retention(AnnotationRetention.RUNTIME) | ||
@JsonQualifier | ||
annotation class DecimalNumber |
76 changes: 76 additions & 0 deletions
76
common-adapter-moshi/src/test/java/pm/gnosis/common/adapter/moshi/TypeAdaptersTest.kt
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,76 @@ | ||
package pm.gnosis.common.adapters.moshi | ||
|
||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
import pm.gnosis.model.Solidity | ||
import pm.gnosis.models.Wei | ||
import java.math.BigInteger | ||
|
||
class TypeAdaptersTest { | ||
|
||
@Test | ||
fun testWeiAdapterFromJson() { | ||
val weiValue = WeiAdapter().fromJson("0xde0b6b3a7640000") | ||
assertEquals(BigInteger("1000000000000000000"), weiValue.value) | ||
} | ||
|
||
@Test | ||
fun testWeiAdapterToJson() { | ||
val weiString = WeiAdapter().toJson(Wei(BigInteger("1000000000000000000"))) | ||
assertEquals("0xde0b6b3a7640000", weiString) | ||
} | ||
|
||
@Test | ||
fun testHexNumberAdapterFromJson() { | ||
val value = HexNumberAdapter().fromJson("0xde0b6b3a7640000") | ||
assertEquals(BigInteger("1000000000000000000"), value) | ||
} | ||
|
||
@Test | ||
fun testHexNumberAdapterToJson() { | ||
val hexNumberString = HexNumberAdapter().toJson(BigInteger("1000000000000000000")) | ||
assertEquals("0xde0b6b3a7640000", hexNumberString) | ||
} | ||
|
||
@Test | ||
fun testDecimalNumberAdapterFromJson() { | ||
val value = DecimalNumberAdapter().fromJson("1000000000000000000") | ||
assertEquals(BigInteger("1000000000000000000"), value) | ||
} | ||
|
||
@Test | ||
fun testDecimalNumberAdapterToJson() { | ||
val decimalString = DecimalNumberAdapter().toJson(BigInteger("1000000000000000000")) | ||
assertEquals("1000000000000000000", decimalString) | ||
} | ||
|
||
@Test | ||
fun testDefaultNumberAdapterFromJsonWithHexNumber() { | ||
val value = DefaultNumberAdapter().fromJson("0xde0b6b3a7640000") | ||
assertEquals(BigInteger("1000000000000000000"), value) | ||
} | ||
|
||
@Test | ||
fun testDefaultNumberAdapterFromJsonWithDecimalNumber() { | ||
val value = DefaultNumberAdapter().fromJson("1000000000000000000") | ||
assertEquals(BigInteger("1000000000000000000"), value) | ||
} | ||
|
||
@Test | ||
fun testDefaultNumberAdapterToJson() { | ||
val numberString = DefaultNumberAdapter().toJson(BigInteger("1000000000000000000")) | ||
assertEquals("0xde0b6b3a7640000", numberString) | ||
} | ||
|
||
@Test | ||
fun testSolidityAddressAdapterFromJson() { | ||
val addressValue = SolidityAddressAdapter().fromJson("0x0000000000000000000000000de0b6B3a7640000") | ||
assertEquals(BigInteger("1000000000000000000"), addressValue.value) | ||
} | ||
|
||
@Test | ||
fun testSolidityAddressAdapterToJson() { | ||
val addressString = SolidityAddressAdapter().toJson(Solidity.Address(BigInteger("1000000000000000000"))) | ||
assertEquals("0x0000000000000000000000000de0b6B3a7640000", addressString) | ||
} | ||
} |
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
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