Git Product home page Git Product logo

moko-web3's Introduction

moko-web3
GitHub license Download kotlin-version

⚠️ The library is being maintained here

symbiosis-finance/moko-web3

Mobile Kotlin web3

This is a Kotlin MultiPlatform library that allow you to interact with ethereum networks by Web3 protocol.

Table of Contents

Features

...

Requirements

  • Gradle version 6.8+
  • Android API 16+
  • iOS version 11.0+

Installation

root build.gradle

allprojects {
    repositories {
        mavenCentral()
    }
}

project build.gradle

dependencies {
    commonMainApi("dev.icerock.moko:web3:0.18.0")
}

Usage

...

Samples

More examples can be found in the sample directory.

Set Up Locally

Contributing

All development (both new features and bug fixes) is performed in develop branch. This way master sources always contain sources of the most recently released version. Please send PRs with bug fixes to develop branch. Fixes to documentation in markdown files are an exception to this rule. They are updated directly in master.

The develop branch is pushed to master during release.

More detailed guide for contributers see in contributing guide.

License

Copyright 2021 IceRock MAG Inc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

moko-web3's People

Contributors

alex009 avatar anton6tak avatar y9san9 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

moko-web3's Issues

Refactor `SmartContract` class

Problem: Now the class has dependence on web3. SmartContract can do requests to write/read self to/from blockchain. As I see it is violation of SRP
Solution: To communicate via web3 protocol we have Web3 entity, so we can add extensions for it (or just member functions) like web3.writeContract/web3.readContract. If it is important to have such functions in SmartContract, we can also add write and read methods, but we should pass web3 there explicitly, so it won't be violation of SRP in my opinion since we just delegate the work to web3.

Invalid HexString.fillToSizedHex behaviour

append here references to the StringBuilder but not the the String

internal fun String.hexStringFillToSizedHex(size: Int): String {
val currentSize = length / 2
require(size <= currentSize) { "This hex string already has more bytes than the target one" }
val isStrictlyValid = length % 2 == 0
return buildString {
repeat(times = size - currentSize) {
append("00")
}
if (!isStrictlyValid)
append("0")
append(this)
}
}

Add shortcuts for Web3Serializer

abstract class Web3Deserializer : DeserializationStrategy {
    override fun descriptor = /* STRING */
    override fun decode(decoder) {
        val string = decoder.decodeString()
        return decode(string.chunked(size = PART_SIZE * 2).map { it.hexStringToByteArray() })
    }
    abstract fun decode(chunks: List<ByteArray>)
}

And other help classes for SerializationStrategy and KSerializer (composition of this twos), so I won't write boilerplate with chunked and converting to hex

iosSimulatorArm64 support

So I already implemented this on my fork by creating a separate branch: ionspinbignum

It is using ionspin's bignum library which supports arm64 targets already instead of soywiz's kbignum as the big number library. kbignum would need to put a new release out to support arm64.

In addition, to me, it seems like ionspin's library is more actively maintained and has more features. Would switching to using ionspin's big number library be a possibility to support arm64 targets?

Refactor types for HEX

We should introduce convenient types for HEX strings:

  • hex string interface with 3 members HexString#withoutPrefix, HexString#prefixed, HexString#bigInt.
  • value class for any valid hex string. checks if the string matches format.
  • value class HexN(size: Int) for sized hexes (N - bytes count), delegates hex string interface with value class and additionally check the size
  • shortcuts such as Hex32, Hex16, etc. for use in library.

And make ContractAddress (and similar classes) also delegate hex string interface with Hex20 class.

Add serializers instead of mappings after request

There are already serializers for all entities except TransactionHash. Since this is an inline class, I can just mark it with Serializable and it will be automatically serialized as primitive

This issue closes: #34

Transaction signing

I know it was mentioned that transaction signing would be supported soon in #5. But, I see there's no issue created for it, so I'm just making one here. I'm just wondering is the eta is still for this month? Also, if any help is needed I'd be to contribute, lmk!

Implement sample app

sample app:

  • show balance of wallet (input field at screen)
  • show transactions of wallet

Documentation or example of calling a Contract method?

I'm trying to simply get the name of a ERC721 contract.

    val apiKey = "..."
    val web3: Web3 = Web3("https://mainnet.infura.io/v3/$apiKey")
    val smartContractString = "..."
    val smartContractJson = Json.parseToJsonElement(smartContractString).jsonObject
    val networkData : JsonObject = smartContractJson["networks"]!!.jsonObject[networkId]?.jsonObject!!
    val adr  = networkData["address"]?.jsonPrimitive?.content!!
    val abis = smartContractJson["abi"]!!.jsonArray

    val sc = SmartContract(web3, ContractAddress(adr), abis))
    
    val names = sc.read("name", emptyList())
            { l ->
                l.map {
                    it.toString()
                }
            }
    val contractName = names[0]
        

First of all I'm not really sure what a valid mapper would look like? Or why it would return multiple values from one read. Do you have any examples? And second, names ends up being empty, so nothing is being returned? Any help on the correct way to call a contract method would be appreciated.

Refactor String#hexStringToByteArray

  • automatically remove "0x" prefix if there is the one
  • throw an exception if the input is invalid (length is even or contains not hex symbols)
  • replace forEach iterator step 2 with chunked and map

fun String.hexStringToByteArray(): ByteArray {
val processedString = if (length.rem(2) != 0) "0$this"
else this
val result = ByteArray(processedString.length / 2)
val uppercased = processedString.toUpperCase()
for (i in 0 until processedString.length step 2) {
val firstIndex = HEX_CHARS.indexOf(uppercased[i]);
val secondIndex = HEX_CHARS.indexOf(uppercased[i + 1]);
val octet = firstIndex.shl(4).or(secondIndex)
result[i.shr(1)] = octet.toByte()
}
return result
}

Add transform argument for Web3RpcRequest

Web3RpcRequest(
    method = "eth_sendRawTransaction",
    params = listOf(signedTransaction),
    paramsSerializer = String.serializer(),
    resultSerializer = String.serializer(),
    transform = ::TransactionHash
)

@Alex009, what do you think about such a thing?

It's useful for cases when we want to wrap request result in typed stuff like on screenshot below:
image

Now that works fine and I can add a request to the separate entity, but when I try to use it with a batch request, it becomes kinda verbose because I have a String output, but not a TokenCryptoCurrency? one.

Remove infura dependence

Since Infura is just a realization of web3 protocol, all names related to infura should be changed to names related to web3 (e.g. infuraUrl => web3Url).
Pros:
It is open source project and we won't push users to use infura.
Cons:
Migration, but it shouldn't cause a lot of problems since there is only one tech-preview project use this library.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.