// Package main is an example DChain smart contract written in Go. // // # Build // // tinygo build -o hello_go.wasm -target wasip1 -no-debug . // // # Deploy // // client deploy-contract --key /keys/node1.json \ // --wasm hello_go.wasm --abi hello_go_abi.json \ // --node http://localhost:8081 // // # Use // // client call-contract --key /keys/node1.json --contract $ID \ // --method increment --gas 5000 --node http://localhost:8081 // // The contract implements a simple counter with owner-gated reset. // It demonstrates every SDK primitive: arguments, state, caller, logging, // and inter-contract calls. package main import ( "strconv" dc "go-blockchain/contracts/sdk" ) // increment adds 1 to the counter and logs the new value. // //export increment func increment() { v := dc.GetU64("counter") v++ dc.PutU64("counter", v) dc.Log("counter: " + strconv.FormatUint(v, 10)) } // get logs the current counter value without changing state. // //export get func get() { v := dc.GetU64("counter") dc.Log("counter: " + strconv.FormatUint(v, 10)) } // reset sets the counter to 0. On first call the caller becomes the owner. // Subsequent calls are restricted to the owner. // //export reset func reset() { owner := dc.GetStateStr("owner") caller := dc.Caller() if owner == "" { dc.SetStateStr("owner", caller) dc.PutU64("counter", 0) dc.Log("initialized — owner: " + caller[:min8(caller)]) return } if caller != owner { dc.Log("unauthorized: " + caller[:min8(caller)]) return } dc.PutU64("counter", 0) dc.Log("reset by owner") } // greet logs a personalised greeting using the first call argument. // //export greet func greet() { name := dc.ArgStr(0, 64) if name == "" { name = "world" } dc.Log("hello, " + name + "! block=" + strconv.FormatUint(dc.BlockHeight(), 10)) } // ping calls another contract's method (demonstrates inter-contract calls). // Args: contract_id (string), method (string) // //export ping func ping() { target := dc.ArgStr(0, 64) method := dc.ArgStr(1, 64) if target == "" || method == "" { dc.Log("ping: target and method required") return } if dc.CallContract(target, method, "[]") { dc.Log("ping: " + method + " ok") } else { dc.Log("ping: " + method + " failed") } } func min8(s string) int { if len(s) < 8 { return len(s) } return 8 } func main() {}