diff --git a/examples/token/nep5/nep5.go b/examples/token/nep5/nep5.go index 301ff6117..cd38ab81a 100644 --- a/examples/token/nep5/nep5.go +++ b/examples/token/nep5/nep5.go @@ -4,6 +4,7 @@ import ( "github.com/CityOfZion/neo-storm/interop/engine" "github.com/CityOfZion/neo-storm/interop/runtime" "github.com/CityOfZion/neo-storm/interop/storage" + "github.com/CityOfZion/neo-storm/interop/util" ) // Token holds all token info @@ -85,28 +86,10 @@ func IsUsableAddress(addr []byte) bool { // Check if a smart contract is calling scripthash callingScriptHash := engine.GetCallingScriptHash() - if EqualAddresses(callingScriptHash, addr) { + if util.Equals(callingScriptHash, addr) { return true } } return false } - -// EqualAddresses compares two addresses if they're equal -// also returns false if one of the two - or both - aren't actual addresses -func EqualAddresses(a []byte, b []byte) bool { - aLen := len(a) - bLen := len(b) - if aLen != bLen || aLen != 20 || bLen != 20 { - return false - } - - for i := 0; i < aLen; i++ { - if a[i] != b[i] { - return false - } - } - - return true -} diff --git a/examples/token/token/token.go b/examples/token/token/token.go deleted file mode 100644 index 9aa10e6b2..000000000 --- a/examples/token/token/token.go +++ /dev/null @@ -1,97 +0,0 @@ -package token - -import ( - "github.com/CityOfZion/neo-storm/interop/engine" - "github.com/CityOfZion/neo-storm/interop/runtime" - "github.com/CityOfZion/neo-storm/interop/storage" - "github.com/CityOfZion/neo-storm/interop/util" -) - -// Token holds all token info -type Token struct { - // Token name - Name string - // Ticker symbol - Symbol string - // Amount of decimals - Decimals int - // Token owner address - Owner []byte - // Total tokens * multiplier - TotalSupply int - // Storage key for circulation value - CirculationKey string -} - -// TODO: Transfer event -// DoTransfer := action.RegisterAction("transfer", "from", "to", "amount") - -// GetSupply gets the token totalSupply value from VM storage -func (t Token) GetSupply(ctx storage.Context) interface{} { - return storage.Get(ctx, t.CirculationKey) -} - -// BalanceOf gets the token balance of a specific address -func (t Token) BalanceOf(ctx storage.Context, hodler []byte) interface{} { - return storage.Get(ctx, hodler) -} - -// Transfer token from one user to another -func (t Token) Transfer(ctx storage.Context, from []byte, to []byte, amount int) bool { - amountFrom := t.CanTransfer(ctx, from, to, amount) - if amountFrom == -1 { - return false - } - - if amountFrom == 0 { - storage.Delete(ctx, from) - } - - if amountFrom > 0 { - diff := amountFrom - amount - storage.Put(ctx, from, diff) - } - - amountTo := storage.Get(ctx, to).(int) - totalAmountTo := amountTo + amount - storage.Put(ctx, to, totalAmountTo) - // DoTransfer(from, to, amount) - return true -} - -// CanTransfer returns the amount it can transfer -func (t Token) CanTransfer(ctx storage.Context, from []byte, to []byte, amount int) int { - if len(to) != 20 && !IsUsableAddress(from) { - return -1 - } - - amountFrom := storage.Get(ctx, from).(int) - if amountFrom < amount { - return -1 - } - - // Tell Transfer the result is equal - special case since it uses Delete - if amountFrom == amount { - return 0 - } - - // return amountFrom value back to Transfer, reduces extra Get - return amountFrom -} - -// IsUsableAddress checks if the sender is either the correct NEO address or SC address -func IsUsableAddress(addr []byte) bool { - if len(addr) == 20 { - if runtime.CheckWitness(addr) { - return true - } - - // Check if a smart contract is calling scripthash - callingScriptHash := engine.GetCallingScriptHash() - if util.CompareBytes(callingScriptHash, addr) { - return true - } - } - - return false -} diff --git a/interop/util/util.go b/interop/util/util.go index 02dce23a3..1f05d4941 100644 --- a/interop/util/util.go +++ b/interop/util/util.go @@ -5,8 +5,8 @@ func FromAddress(address string) []byte { return nil } -// CompareBytes compares a with b and will return true whether a and b +// Equals compares a with b and will return true whether a and b // are equal. -func CompareBytes(a, b []byte) bool { +func Equals(a, b interface{}) bool { return false } diff --git a/pkg/vm/compiler/analysis.go b/pkg/vm/compiler/analysis.go index aef876a7f..011901f2a 100644 --- a/pkg/vm/compiler/analysis.go +++ b/pkg/vm/compiler/analysis.go @@ -14,7 +14,7 @@ var ( builtinFuncs = []string{ "len", "append", "SHA256", "SHA1", "Hash256", "Hash160", - "FromAddress", "CompareBytes", + "FromAddress", "Equals", } ) diff --git a/pkg/vm/compiler/codegen.go b/pkg/vm/compiler/codegen.go index 9d872e2d3..fbc7bb63e 100644 --- a/pkg/vm/compiler/codegen.go +++ b/pkg/vm/compiler/codegen.go @@ -571,7 +571,7 @@ func (c *codegen) convertBuiltin(expr *ast.CallExpr) { emitOpcode(c.prog, vm.HASH256) case "Hash160": emitOpcode(c.prog, vm.HASH160) - case "CompareBytes": + case "Equals": emitOpcode(c.prog, vm.EQUAL) case "FromAddress": // We can be sure that this is a ast.BasicLit just containing a simple