nearprotocol / near-runtime-ts

Typescript library for writing near smart contracts
22 stars 7 forks source link

Improve cross-contract API #13

Open evgenykuzyakov opened 5 years ago

evgenykuzyakov commented 5 years ago

Some scratch options for the new API:

import {ContractPromise} from "../../../../code/metanear-src/out/near";

export function getWeather(location): Weather {
    return new Weather("shitty");
}

export function getWeather(location): void {
    ContractPromise.create(bjklbdsklnsbklnb, ,asdf/ asdf).returnAsResult();
}

export async function getWeather(location): Weather {
    return ContractPromise.create(bjklbdsklnsbklnb, ,asdf/ asdf);
}

export function getWeather(location): ContractPromise {
    return ContractPromise.create(bjklbdsklnsbklnb, ,asdf/ asdf);
}

------------------------------------------------------------------

export function ww_getWeather(): void {
    input;
    ....
    let result;
    if (result != null) {
        if (result.isConstant()) {
            return_value(result.data.encode());
        } else {
            return_promise(result.id);
        }

    }
}

export function getWeather(lociont): ContractPromise<string> {
    if (useLocalApi) {
        return ContractPromise.constant("crappy")
    } else {
        return ContractPromise.create(...)
    }

}

///

export async function getWeather(location): string {
    if (useLocalApi) {
        return "crappy";
    } else {
        return ContractPromise.create(bjklbdsklnsbklnb, ,asdf/ asdf);
    }
}

export async function shitty(bla): string {
    return getWeather(bla).then("parseShit");
}

export async function shitty(bla): string {
    let bla = getWeather(bla);
    if (bla.isConstantPromise) {
        return ConstantPromise(parseShit(bla.unwrap()));
    } else {
        return bla.then("parseShit");
    }
}

export function parseShit(): string {
    ContractPromise.getResults();
}

export function getWeather(location): void {
    let p = ContractPromise.create(bjklbdsklnsbklnb, ,asdf/ asdf);
    p.then("_parseWeather", ...).returnAsResult();
}

export function _parseWeather(): Weather {
    ...
    return weather;
}

////////////////////////////

export async function getWeather(location): Weather {
    let p = ContractPromise.create(bjklbdsklnsbklnb, ,asdf/ asdf);
    return p.then("_parseWeather", ...);
}

export function _parseWeather(): Weather {
...
    return weather;
}

///

export function getWeather(location): ContractPromise {
    return ContractPromise.create(bjklbdsklnsbklnb, ,asdf/ asdf);
}

export function isCloudy(location): ContractPromise {
    return getWeather(location).then("_parseWeather", {check: isClody}.encode());
}

export function _parseWeather(check: string): Weather {
...
    return weather;
}

//////////////////

export async function getWeather(location): Weather {
    return ContractPromise.create(bjklbdsklnsbklnb, ,asdf/ asdf);
}

export function isCloudy(location): ContractPromise {
    return getWeather(location).then("_parseWeather", {check: isClody}.encode());
}

export function _parseWeather(check: string): Weather {
...
    return weather;
}
vgrichina commented 5 years ago

Posting my previous notes on the topic as well:

PromiseId {
    nonce,
    account
}

Promise {
    id: PromiseId,
    callback: MethodRef
    args: ByteBuffer
}

Can we use promises instead of callbacks when communicating cross-shard?

let promise1 = contract1.withMana(10).doSomething(...);
let promise2 = contract2.withMana(10).doSomething(...);
return Promise.all([promise1, promise2]);

Promise can potentially be resolved outside of smart contract by original caller.

Also can be handled inside smart contract code:

promise.then(handleEverythingOk, handleError); // Handle in same contract
promise.then(contract3.withMana(10).handleEverythingOk, contract3.withMana(10).handleError); // Handle in other contract 

// bound args serialized in caller shard together with promise id
// this allows easily passing context to callback
promise.then(handleEverythingOk.bind(...));