Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sascha1337/762080dc670d8565291e29a0c5b33016 to your computer and use it in GitHub Desktop.
Save sascha1337/762080dc670d8565291e29a0c5b33016 to your computer and use it in GitHub Desktop.

How to connect a IOTA Smart Contract with a Web Frontend

Goal of this tutorial is deploy a Hello World Smart Contract, and displays a "Hello World" string on a Website.

Hello Frontend Smart Contract

use wasmlib::*;
#[no_mangle]
fn on_load() {
    let exports = ScExports::new();
    exports.add_view("getHelloWorld", view_get_hello_world);
}
pub fn view_get_hello_world(ctx: &ScViewContext) {
    ctx.log("Get 'Hello, world!'");https://hackmd.io/@sascha1337/fiat500-todo/%2F%40sascha1337%2Ffiat500-terra-limit-orders
    ctx.results().get_string("helloWorld").set_value("Hello, world!");
}

Call from frontend

http://<your_ip>:9090/contract/<contractID>/callview/<hname>

Demo (TODO):

https://icsp.einnfachiota.de/contract/xxz/callview/getHelloWorld

WASP Web API

https://github.com/iotaledger/wasp/blob/develop/packages/webapi/routes/routes.go

func Info() string {
	return "/info"
}

func CallView(contractID string, hname string) string {
	return "/contract/" + contractID + "/callview/" + hname
}

func RequestStatus(chainID string, reqID string) string {
	return "/chain/" + chainID + "/request/" + reqID + "/status"
}

func WaitRequestProcessed(chainID string, reqID string) string {
	return "/chain/" + chainID + "/request/" + reqID + "/wait"
}

func StateQuery(chainID string) string {
	return "/chain/" + chainID + "/state/query"
}

func PutBlob() string { // PR to change GET to POST open
	return "/blob/put"
}

func GetBlob(hash string) string {
	return "/blob/get/" + hash
}

func HasBlob(hash string) string {
	return "/blob/has/" + hash
}

func ActivateChain(chainID string) string {
	return "/adm/chain/" + chainID + "/activate"
}

func DeactivateChain(chainID string) string {
	return "/adm/chain/" + chainID + "/deactivate"
}

func ListChainRecords() string {
	return "/adm/chainrecords"
}

func PutChainRecord() string {
	return "/adm/chainrecord"
}

func GetChainRecord(chainID string) string {
	return "/adm/chainrecord/" + chainID
}

func DKSharesPost() string {
	return "/adm/dks"
}

func DKSharesGet(sharedAddress string) string {
	return "/adm/dks/" + sharedAddress
}

func DumpState(contractID string) string {
	return "/adm/contract/" + contractID + "/dumpstate"
}

func Shutdown() string {
	return "/adm/shutdown"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment