Luca
02/01/2022, 5:38 AMweb3.eth.Contract
? Forgive me if this is a stupid question, as I am not the most familiar with js. I am more used to standard kotlin/OOP.
I've figured out that an instance of web3.eth.Contract
must be created where web3
must be an actual instance of a Web3
class (created new Web3(...)
)
So I cannot simply create a contract instance by calling new Web3.eth.Contract(...)
For example js("""new (new Web3(window.ethereum)).eth.Contract(JSON.parse('...'), '...' )""")
will create a valid instance of a Contract
But, when I attempt to create an instance of a kotlin defined contract:
@JsModule("web3-eth-contract")
@JsNonModule
open external class Contract constructor(jsonInterface: Array<AbiItem>, address: String, options: ContractOptions)
by calling
val contract = Contract (jsonInterface = abis as Array<AbiItem>, address = adr)
An instance of Contract
IS created successfully, however it is worthless because the created contract seems to rely on a parent instance of Web3
, (like in the first example I mentioned).
How can I create a Contract class in kotlin, that is instantiated in the same way as js("""new (new Web3(window.ethereum)).eth.Contract(JSON.parse('...'), '...' )""")
?Big Chungus
02/01/2022, 8:27 AMLuca
02/02/2022, 8:38 AMLuca
02/02/2022, 8:39 AMBig Chungus
02/02/2022, 9:07 AMBig Chungus
02/02/2022, 9:08 AMexternal class Eth {
val Contract: dynamic
{
Big Chungus
02/02/2022, 9:09 AMBig Chungus
02/02/2022, 9:10 AMLuca
02/02/2022, 7:35 PMLuca
02/03/2022, 11:03 PMexternal class Eth {
val Contract: (Array<AbiItem>, String) -> Contract
}
But I get the error: Error: Please use the "new" keyword to instantiate a web3.eth.Contract() object!
So I think I'm getting closer. But, not sure how to handle the new keyword. Any ideas?Big Chungus
02/03/2022, 11:46 PMLuca
02/04/2022, 1:58 AMjs(...)
previously I had only tried using variables by prepending $
to the varable names inside js(...)
and that doesn't work
external class Eth {
...
val Contract: (Array<AbiItem>, String, ContractOptions?) -> Contract
...
}
val abis : Array<AbiItemImpl> = ...
val adr = "..."
val contractFunction = web3Wrapper.web3.eth.Contract
val contract = js("""new contractFunction(abis, adr)""" )
But, is there anyway I can avoid using resorting to using js(...)
and use actual kotlin code to instantiate the Contract?Big Chungus
02/04/2022, 8:20 AMLuca
02/06/2022, 2:22 AMLuca
02/06/2022, 2:25 AMexternal class Eth {
...
val Contract: ContractFunction0
interface ContractFunction0 {
@nativeInvoke
fun invoke(a: Array<AbiItem>, b: String?, c: ContractOptions?): web3.eth.Contract
}
...
}
val abis : Array<AbiItemImpl> = ...
val adr = "..."
val contract = web3Wrapper.web3.eth.Contract.invoke(abis, adr, null)
However I still get the same result Error: Please use the "new" keyword to instantiate a web3.eth.Contract() object!
Big Chungus
02/06/2022, 10:43 AMContract
property is referring to Contract
class constructor, not just a function returning a Contract
. Unfortunatelly, currently there's no way to express constructor reference types in K/JS yet, so you'll need to just wrap Constructor
class from web3-eth-contract module separately and use that to build your Contracts. Eth class is not needed to build an instance of the contract. The constructor reference there is just for convenience.Big Chungus
02/06/2022, 10:44 AMLuca
02/06/2022, 8:53 PMLuca
02/06/2022, 8:55 PM