forked from TrueCloudLab/neoneo-go
Merge pull request #1712 from nspcc-dev/nep17-nep11-onpayments
NEP17/NEP11 onPayments
This commit is contained in:
commit
aad2b3adad
8 changed files with 60 additions and 9 deletions
2
cli/testdata/verify.go
vendored
2
cli/testdata/verify.go
vendored
|
@ -4,5 +4,5 @@ func Verify() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func OnPayment(from []byte, amount int, data interface{}) {
|
func OnNEP17Payment(from []byte, amount int, data interface{}) {
|
||||||
}
|
}
|
||||||
|
|
2
cli/testdata/verify.manifest.json
vendored
2
cli/testdata/verify.manifest.json
vendored
|
@ -1 +1 @@
|
||||||
{"name":"verify","abi":{"methods":[{"name":"verify","offset":0,"parameters":[],"returntype":"Boolean","safe":false},{"name":"onPayment","offset":5,"parameters":[{"name":"from","type":"ByteArray"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false}],"events":[{"name":"Hello world!","parameters":[{"name":"args","type":"Array"}]}]},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null}
|
{"name":"verify","abi":{"methods":[{"name":"verify","offset":0,"parameters":[],"returntype":"Boolean","safe":false},{"name":"onNEP17Payment","offset":5,"parameters":[{"name":"from","type":"ByteArray"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false}],"events":[{"name":"Hello world!","parameters":[{"name":"args","type":"Array"}]}]},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null}
|
||||||
|
|
|
@ -340,13 +340,20 @@ func getTestContractState(bc *Blockchain) (*state.Contract, *state.Contract) {
|
||||||
emit.Syscall(w.BinWriter, interopnames.SystemStorageGetContext)
|
emit.Syscall(w.BinWriter, interopnames.SystemStorageGetContext)
|
||||||
emit.Syscall(w.BinWriter, interopnames.SystemStorageGet)
|
emit.Syscall(w.BinWriter, interopnames.SystemStorageGet)
|
||||||
emit.Opcodes(w.BinWriter, opcode.RET)
|
emit.Opcodes(w.BinWriter, opcode.RET)
|
||||||
onPaymentOff := w.Len()
|
onNEP17PaymentOff := w.Len()
|
||||||
emit.Syscall(w.BinWriter, interopnames.SystemRuntimeGetCallingScriptHash)
|
emit.Syscall(w.BinWriter, interopnames.SystemRuntimeGetCallingScriptHash)
|
||||||
emit.Int(w.BinWriter, 4)
|
emit.Int(w.BinWriter, 4)
|
||||||
emit.Opcodes(w.BinWriter, opcode.PACK)
|
emit.Opcodes(w.BinWriter, opcode.PACK)
|
||||||
emit.String(w.BinWriter, "LastPayment")
|
emit.String(w.BinWriter, "LastPayment")
|
||||||
emit.Syscall(w.BinWriter, interopnames.SystemRuntimeNotify)
|
emit.Syscall(w.BinWriter, interopnames.SystemRuntimeNotify)
|
||||||
emit.Opcodes(w.BinWriter, opcode.RET)
|
emit.Opcodes(w.BinWriter, opcode.RET)
|
||||||
|
onNEP11PaymentOff := w.Len()
|
||||||
|
emit.Syscall(w.BinWriter, interopnames.SystemRuntimeGetCallingScriptHash)
|
||||||
|
emit.Int(w.BinWriter, 4)
|
||||||
|
emit.Opcodes(w.BinWriter, opcode.PACK)
|
||||||
|
emit.String(w.BinWriter, "LostPayment")
|
||||||
|
emit.Syscall(w.BinWriter, interopnames.SystemRuntimeNotify)
|
||||||
|
emit.Opcodes(w.BinWriter, opcode.RET)
|
||||||
update3Off := w.Len()
|
update3Off := w.Len()
|
||||||
emit.Int(w.BinWriter, 3)
|
emit.Int(w.BinWriter, 3)
|
||||||
emit.Opcodes(w.BinWriter, opcode.JMP, 2+1)
|
emit.Opcodes(w.BinWriter, opcode.JMP, 2+1)
|
||||||
|
@ -456,8 +463,18 @@ func getTestContractState(bc *Blockchain) (*state.Contract, *state.Contract) {
|
||||||
ReturnType: smartcontract.VoidType,
|
ReturnType: smartcontract.VoidType,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "onPayment",
|
Name: manifest.MethodOnNEP11Payment,
|
||||||
Offset: onPaymentOff,
|
Offset: onNEP11PaymentOff,
|
||||||
|
Parameters: []manifest.Parameter{
|
||||||
|
manifest.NewParameter("from", smartcontract.Hash160Type),
|
||||||
|
manifest.NewParameter("amount", smartcontract.IntegerType),
|
||||||
|
manifest.NewParameter("tokenid", smartcontract.ByteArrayType),
|
||||||
|
},
|
||||||
|
ReturnType: smartcontract.VoidType,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: manifest.MethodOnNEP17Payment,
|
||||||
|
Offset: onNEP17PaymentOff,
|
||||||
Parameters: []manifest.Parameter{
|
Parameters: []manifest.Parameter{
|
||||||
manifest.NewParameter("from", smartcontract.Hash160Type),
|
manifest.NewParameter("from", smartcontract.Hash160Type),
|
||||||
manifest.NewParameter("amount", smartcontract.IntegerType),
|
manifest.NewParameter("amount", smartcontract.IntegerType),
|
||||||
|
|
|
@ -143,7 +143,7 @@ func (c *nep17TokenNative) postTransfer(ic *interop.Context, from, to *util.Uint
|
||||||
stackitem.NewBigInteger(amount),
|
stackitem.NewBigInteger(amount),
|
||||||
data,
|
data,
|
||||||
}
|
}
|
||||||
if err := contract.CallFromNative(ic, c.Hash, cs, manifest.MethodOnPayment, args, false); err != nil {
|
if err := contract.CallFromNative(ic, c.Hash, cs, manifest.MethodOnNEP17Payment, args, false); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/core/dao"
|
"github.com/nspcc-dev/neo-go/pkg/core/dao"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
||||||
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/contract"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/core/interop/runtime"
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/runtime"
|
||||||
istorage "github.com/nspcc-dev/neo-go/pkg/core/interop/storage"
|
istorage "github.com/nspcc-dev/neo-go/pkg/core/interop/storage"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
||||||
|
@ -280,6 +281,26 @@ func (n *nonfungible) postTransfer(ic *interop.Context, from, to *util.Uint160,
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
ic.Notifications = append(ic.Notifications, ne)
|
ic.Notifications = append(ic.Notifications, ne)
|
||||||
|
if to == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cs, err := ic.GetContract(*to)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fromArg := stackitem.Item(stackitem.Null{})
|
||||||
|
if from != nil {
|
||||||
|
fromArg = stackitem.NewByteArray((*from).BytesBE())
|
||||||
|
}
|
||||||
|
args := []stackitem.Item{
|
||||||
|
fromArg,
|
||||||
|
stackitem.NewBigInteger(intOne),
|
||||||
|
stackitem.NewByteArray(tokenID),
|
||||||
|
}
|
||||||
|
if err := contract.CallFromNative(ic, n.Hash, cs, manifest.MethodOnNEP11Payment, args, false); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *nonfungible) burn(ic *interop.Context, tokenID []byte) {
|
func (n *nonfungible) burn(ic *interop.Context, tokenID []byte) {
|
||||||
|
|
|
@ -55,7 +55,7 @@ var maxNotValidBeforeDeltaKey = []byte{10}
|
||||||
func newNotary() *Notary {
|
func newNotary() *Notary {
|
||||||
n := &Notary{ContractMD: *interop.NewContractMD(nativenames.Notary, notaryContractID)}
|
n := &Notary{ContractMD: *interop.NewContractMD(nativenames.Notary, notaryContractID)}
|
||||||
|
|
||||||
desc := newDescriptor("onPayment", smartcontract.VoidType,
|
desc := newDescriptor("onNEP17Payment", smartcontract.VoidType,
|
||||||
manifest.NewParameter("from", smartcontract.Hash160Type),
|
manifest.NewParameter("from", smartcontract.Hash160Type),
|
||||||
manifest.NewParameter("amount", smartcontract.IntegerType),
|
manifest.NewParameter("amount", smartcontract.IntegerType),
|
||||||
manifest.NewParameter("data", smartcontract.AnyType))
|
manifest.NewParameter("data", smartcontract.AnyType))
|
||||||
|
|
|
@ -287,6 +287,16 @@ func TestTransfer(t *testing.T) {
|
||||||
testNameServiceInvokeAux(t, bc, defaultNameServiceSysfee, from, "totalSupply", 1)
|
testNameServiceInvokeAux(t, bc, defaultNameServiceSysfee, from, "totalSupply", 1)
|
||||||
testNameServiceInvokeAux(t, bc, defaultNameServiceSysfee, from, "ownerOf",
|
testNameServiceInvokeAux(t, bc, defaultNameServiceSysfee, from, "ownerOf",
|
||||||
to.Contract.ScriptHash().BytesBE(), []byte("neo.com"))
|
to.Contract.ScriptHash().BytesBE(), []byte("neo.com"))
|
||||||
|
cs, cs2 := getTestContractState(bc) // cs2 doesn't have OnNEP11Transfer
|
||||||
|
require.NoError(t, bc.contracts.Management.PutContractState(bc.dao, cs))
|
||||||
|
require.NoError(t, bc.contracts.Management.PutContractState(bc.dao, cs2))
|
||||||
|
testNameServiceInvokeAux(t, bc, defaultRegisterSysfee, to, "transfer",
|
||||||
|
nil, cs2.Hash.BytesBE(), []byte("neo.com"))
|
||||||
|
testNameServiceInvokeAux(t, bc, defaultRegisterSysfee, to, "transfer",
|
||||||
|
true, cs.Hash.BytesBE(), []byte("neo.com"))
|
||||||
|
testNameServiceInvokeAux(t, bc, defaultNameServiceSysfee, from, "totalSupply", 1)
|
||||||
|
testNameServiceInvokeAux(t, bc, defaultNameServiceSysfee, from, "ownerOf",
|
||||||
|
cs.Hash.BytesBE(), []byte("neo.com"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTokensOf(t *testing.T) {
|
func TestTokensOf(t *testing.T) {
|
||||||
|
|
|
@ -21,8 +21,11 @@ const (
|
||||||
// MethodVerify is a name for default verification method.
|
// MethodVerify is a name for default verification method.
|
||||||
MethodVerify = "verify"
|
MethodVerify = "verify"
|
||||||
|
|
||||||
// MethodOnPayment is name of the method which is called when contract receives funds.
|
// MethodOnNEP17Payment is name of the method which is called when contract receives NEP-17 tokens.
|
||||||
MethodOnPayment = "onPayment"
|
MethodOnNEP17Payment = "onNEP17Payment"
|
||||||
|
|
||||||
|
// MethodOnNEP11Payment is the name of the method which is called when contract receives NEP-11 tokens.
|
||||||
|
MethodOnNEP11Payment = "onNEP11Payment"
|
||||||
|
|
||||||
// NEP10StandardName represents the name of NEP10 smartcontract standard.
|
// NEP10StandardName represents the name of NEP10 smartcontract standard.
|
||||||
NEP10StandardName = "NEP-10"
|
NEP10StandardName = "NEP-10"
|
||||||
|
|
Loading…
Reference in a new issue