How can I call on chain module without it's source code?

194 views Asked by At

I want to call other module's function in my contract, but I haven't it's source code, just can see it's bytecode onchain;

In sodility has interface contract, and just need a proto (interface), then can call any contract even has no source code as fllow:

So how to do it in sui move?

pragma solidity ^0.4.0;

contract InfoFeed {

    function info() payable returns (uint ret);
}

contract Consumer {
    function deposit() payable returns (uint){
        return msg.value;
    } 

    function left() constant returns (uint){
         return this.balance;
    }

    function callFeed(address addr) returns (uint) { 
        return InfoFeed(addr).info.value(1).gas(8000)(); 
        // here i no need to get source code for InfoFeed implements, and can call it with address 
    }
}
1

There are 1 answers

0
P S On

First of all if you only have the modules bytecode and still want to call a function from it means that you really understand what that code does. If it is the case then you must also have understanding of what parameters it takes.

With above assumption you can, very easily, call any public function from any already deployed package by updating two files:

1. move.toml under [addresses] add the address of the package where the function you want to call is defined.

[addresses]
package_from_where_you_want_to_call_func = "0x5306f64e312b581766351c07af79c72fcb1cd25147157fdc2f8ad76de9a3fb6a"

2. src/your_module.move: import the function you want to use.

module tutorial::your_module {
    use sui::object::{Self, UID};
    use sui::transfer;
    use sui::tx_context::{Self, TxContext};
    use package_from_where_you_want_to_call_func::desired_module as remote_moduel
    struct ColorObject has key {
        id: UID,
        red: u8,
        green: u8,
        blue: u8,
    }
    remote_module::function_you_want_to_call(....);
    ....

}

Here's a sample of a function from bytecode for a package:

entry public mint_nft(Arg0: String, Arg1: String, Arg2: vector
<u8>, Arg3: vector<String>, Arg4: vector<String>, Arg5: &mut MintCap<Fuddies>, Arg6: &mut Warehouse<Fuddies>, Arg7: &mut TxContext) 
{
    ...
}