From 021c155273ce5e4cf0ad89c429aad0091c0350e4 Mon Sep 17 00:00:00 2001 From: shashkevichfrida Date: Mon, 8 Jan 2024 17:42:36 +0300 Subject: [PATCH] fix: transfer token --- Exchanger/config.json | 2 +- Exchanger/exchanger.go | 83 +++++++++++++++++++++++++------------- Exchanger/exchanger.nef | Bin 628 -> 718 bytes Exchanger/exchanger.yml | 5 +-- ZaCoin/config.json | 2 +- ZaCoin/zaCoin.nef | Bin 809 -> 810 bytes ZaCoin/zacoin_contract.go | 3 +- 7 files changed, 60 insertions(+), 35 deletions(-) diff --git a/Exchanger/config.json b/Exchanger/config.json index d4abf69..b76b7b6 100755 --- a/Exchanger/config.json +++ b/Exchanger/config.json @@ -1 +1 @@ -{"name":"Exchanger","abi":{"methods":[{"name":"_initialize","offset":0,"parameters":[],"returntype":"Void","safe":false},{"name":"_deploy","offset":33,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"buyZaCoin","offset":128,"parameters":[{"name":"gasCount","type":"Integer"}],"returntype":"Boolean","safe":false},{"name":"onNEP17Payment","offset":350,"parameters":[{"name":"from","type":"Hash160"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false}],"events":[]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null} \ No newline at end of file +{"name":"Exchanger","abi":{"methods":[{"name":"_initialize","offset":0,"parameters":[],"returntype":"Void","safe":false},{"name":"_deploy","offset":31,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"buyZaCoin","offset":126,"parameters":[{"name":"count","type":"Integer"}],"returntype":"Void","safe":false},{"name":"onNEP17Payment","offset":161,"parameters":[{"name":"from","type":"Hash160"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false}],"events":[]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null} \ No newline at end of file diff --git a/Exchanger/exchanger.go b/Exchanger/exchanger.go index d7a0ff7..5298bfe 100644 --- a/Exchanger/exchanger.go +++ b/Exchanger/exchanger.go @@ -7,6 +7,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/interop/storage" "github.com/nspcc-dev/neo-go/pkg/interop/native/gas" "github.com/nspcc-dev/neo-go/pkg/interop/lib/address" + //"github.com/nspcc-dev/neo-go/pkg/interop/util" ) const ( @@ -25,50 +26,76 @@ func _deploy(data interface{}, isUpdate bool) { }) if len(args.zaCoinHash) != interop.Hash160Len { - panic("invalid hash of zaCoin contract") + panic("Invalid hash of zaCoin contract") } ctx := storage.GetContext() storage.Put(ctx, zaCoinHashKey, args.zaCoinHash) } -func BuyZaCoin(gasCount int) bool { +func BuyZaCoin(count int) { ctx := storage.GetContext() - zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160) playerOwner := runtime.GetScriptContainer().Sender - playerBalance := gas.BalanceOf(playerOwner) + //balanceBefore := gas.BalanceOf(runtime.GetExecutingScriptHash()) + gasTransfer(playerOwner, count) + //balanceAfter := gas.BalanceOf(runtime.GetExecutingScriptHash()) - if playerBalance < gasCount { - panic("Insufficient funds") - } - - //contractHash := runtime.GetExecutingScriptHash() - transferredGas := gas.Transfer(playerOwner, walletAddress, gasCount, nil) - - if !transferredGas { - panic("failed to transfer gas") - } - resultAmountZaCoin := gasCount * 57 - transferredZaCoin := contract.Call(zaCoinHash, "transfer", contract.All, walletAddress, playerBalance, resultAmountZaCoin, nil).(bool) - - if !transferredZaCoin { - panic("Failed to transfer zaCoins") - } else { - runtime.Log("Gas balance: "+ string(gas.BalanceOf(playerOwner))) - //runtime.Log("ZaCoin balance: " + string(contract.Call(zaCoinHash) + "balanceOf" +string(contract.ReadStates) + string(playerOwner))) - } - - return transferredZaCoin + changePlayerBalance(ctx, playerOwner, count) + //if (balanceBefore - balanceAfter == count) { + // changePlayerBalance(ctx, playerOwner, count) + //} else { + // util.Abort() + //} } - func OnNEP17Payment(from interop.Hash160, amount int, data any) { + zacoinStr := "zacoin" ctx := storage.GetContext() zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160) callingHash := runtime.GetCallingScriptHash() - if !callingHash.Equals(zaCoinHash) { - panic("Only ZC is accepted") + if data == zacoinStr { + if !callingHash.Equals(zaCoinHash) { + panic("only ZC is accepted") + } + } else { + if !callingHash.Equals(gas.Hash) { + panic("only GAS is accepted") + } + } +} + +func gasTransfer(playerOwner interop.Hash160, gasCount int) { + contractHash := runtime.GetExecutingScriptHash() + transferredGas := gas.Transfer(playerOwner, contractHash, gasCount, nil) + + if !transferredGas { + panic("Failed to transfer gas") + } +} + + +func changePlayerBalance(ctx storage.Context, playerOwner interop.Hash160, balanceChange int) { + zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160) + playerContract := runtime.GetExecutingScriptHash() + + var from, to interop.Hash160 + var transferAmount int + if balanceChange > 0 { + // Transfer funds from contract to player owner + from = playerContract + to = playerOwner + transferAmount = balanceChange + } else { + // Transfer funds from player owner to contract + from = playerOwner + to = playerContract + transferAmount = -balanceChange // We flip sender/receiver, but keep amount positive + } + + transferred := contract.Call(zaCoinHash, "transfer", contract.All, from, to, transferAmount, "zacoin").(bool) + if !transferred { + panic("failed to transfer zaCoins") } } diff --git a/Exchanger/exchanger.nef b/Exchanger/exchanger.nef index 29d256f64d621660afbeef7d2818f74c9b5a7004..8d32c2cfcb674f2fcc6d04dbd04f8184b731cd60 100755 GIT binary patch literal 718 zcmeZsbu-RO&DTxO*EP^HG%(gPWFP}Do-cdUeSuBK%f0V$Xdu~-ZQT(F()%cAtSLkLm@v+p(@ciKQm7uIX@3* zS8|D!=2TJ0 zxuGJNbwfoo$LppB&>tFEPUN0h+qp)u<>5GH&mQ)$g61OLhZt?0No*yJR)F+xH|@;xC9gvj<^4(~O*_kVl105xx3cyAcau;zI3zSp| z7;$A5RTfv3C~;O6WfoV>F3HNyfdzVq$bqs99(cl$&kJ!(s*Mg>wW>6Cm6|+{R2ruJ NkeCL#!Qd&AH2`nJ>E8eV delta 343 zcmX@d`h{hJi@9({NlAf~zJ6*&Vs1fBs$Oz_u08|P`Lajd7ua;X-1{DfCNc_3em=kC z5@%9kPGVkis(%_I1LH(DRhDy%VT==VWf>DDb}KVxPF!!x$;s$A`&;_Cf{e+EjFRSs zj=F&y2EylxvdkG6inA;9#Ce1~^NLH;(lV1XQ}ap`(n|AEimkFDD!4<;85v4)R;fwz zh^0-|XA;w?V6a?J8j+pL9U^j|oIR6=qogP?uQ)BWNIsuE#4)KhI%w6Zvb!yYZ%WaMBa*lis$MetKr{y_>voIsMF|g3lGC0qs zD62U8h8kPu4JBqy6@}~@Dw3HuR7A6(+B2c_bfByfGsh}52_9kpyqrpfC})MtVui%y Q