Created
September 22, 2019 12:38
-
-
Save frol/bb8185948388335df27980db64eec301 to your computer and use it in GitHub Desktop.
Example of a chained smart-contracts in NEAR
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
import { context, storage, logging, ContractPromise, util } from "near-runtime-ts"; | |
export class AddArgs { | |
a: i32; | |
b: i32; | |
}; | |
export class MultiplyByArgs { | |
x: i32; | |
}; | |
const PROMISE_RESULT_INCOMPLETE = 0; | |
const PROMISE_RESULT_SUCCESS = 1; | |
const PROMISE_RESULT_ERROR = 2; | |
export function add(a: i32, b: i32): i32 { | |
return a + b; | |
} | |
export function multiplyBy(x: i32): i32 { | |
const result = ContractPromise.getResults()[0]; | |
if (result.status == PROMISE_RESULT_SUCCESS) { | |
return x * util.parseFromBytes<i32>(result.buffer); | |
} | |
return 0; | |
} | |
export function calculate100plus200times5(): void { | |
const addArgs: AddArgs = { | |
a: 100, | |
b: 200, | |
}; | |
const multiplyByAgrs: MultiplyByArgs = { | |
x: 5, | |
}; | |
let promise = ContractPromise.create(context.contractName, "add", addArgs.encode().serialize(), 100000); | |
promise = promise.then(context.contractName, "multiplyBy", multiplyByAgrs.encode().serialize(), 100000); | |
promise.returnAsResult(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment