@@ -3,18 +3,19 @@ package app
33import (
44 "encoding/json"
55 "fmt"
6- "github.com/horizen-pes-nova/payment-app/utils "
6+ "math/big "
77
8+ ethCommon "github.com/ethereum/go-ethereum/common"
89 "github.com/horizen-pes/pkg/common"
910 wasmCommon "github.com/horizen-pes/pkg/wasm/common"
1011)
1112
1213// --- High-Level Application Logic ---
1314
14- func LoadModule (appId string ) []byte {
15+ func LoadModule (appId int64 ) []byte {
1516 initialState := & ApplicationInternalState {
1617 AppID : appId ,
17- Accounts : make (map [string ]* AccountState ),
18+ Accounts : make (map [ethCommon. Address ]* AccountState ),
1819 Nonce : 0 ,
1920 }
2021 stateJSON , err := json .Marshal (initialState )
@@ -24,29 +25,36 @@ func LoadModule(appId string) []byte {
2425 return stateJSON
2526}
2627
27- func DepositFunds (sender string , value uint64 , stateJSON string ) wasmCommon.DepositResult {
28+ func DepositFunds (senderPtr * ethCommon.Address , value * big.Int , stateJSON string ) wasmCommon.DepositResult {
29+ if senderPtr == nil {
30+ return wasmCommon.DepositResult {Error : "Sender address is missing" }
31+ }
32+
33+ sender := * senderPtr
34+ //This should never happens but just in case
35+ if value == nil {
36+ return wasmCommon.DepositResult {Error : "value is nil" }
37+ }
38+
2839 var currentState ApplicationInternalState
2940 if err := json .Unmarshal ([]byte (stateJSON ), & currentState ); err != nil {
3041 return wasmCommon.DepositResult {Error : "Failed to parse application state" }
3142 }
32- if ! utils .IsValidAddress (sender ) {
33- return wasmCommon.DepositResult {Error : fmt .Sprintf ("sender address is not valid: %s" , sender )}
34- }
3543
3644 var events []common.PlainEvent
3745
3846 // Handle deposit
39- if value > 0 {
47+ if value . Sign () > 0 {
4048 // Ensure sender account exists
4149 if currentState .Accounts [sender ] == nil {
4250 currentState .Accounts [sender ] = & AccountState {
4351 Address : sender ,
44- Balance : 0 ,
52+ Balance : big . NewInt ( 0 ) ,
4553 }
4654 }
4755
4856 // Add deposit to sender's balance
49- currentState .Accounts [sender ].Balance += value
57+ currentState .Accounts [sender ].Balance . Add ( currentState . Accounts [ sender ]. Balance , value )
5058 currentState .Nonce ++
5159
5260 // Create deposit event
@@ -75,15 +83,16 @@ func DepositFunds(sender string, value uint64, stateJSON string) wasmCommon.Depo
7583 return wasmCommon.DepositResult {State : newStateBytes , Events : events }
7684}
7785
78- func ProcessRequest (sender , payloadJSON , stateJSON string ) wasmCommon.ProcessResult {
86+ func ProcessRequest (senderPtr * ethCommon.Address , payloadJSON , stateJSON string ) wasmCommon.ProcessResult {
87+ if senderPtr == nil {
88+ return wasmCommon.ProcessResult {Error : "Sender address is missing" }
89+ }
90+ sender := * senderPtr
7991 // Deserialize current state
8092 var currentState ApplicationInternalState
8193 if err := json .Unmarshal ([]byte (stateJSON ), & currentState ); err != nil {
8294 return wasmCommon.ProcessResult {Error : "Failed to parse application state" }
8395 }
84- if ! utils .IsValidAddress (sender ) {
85- return wasmCommon.ProcessResult {Error : fmt .Sprintf ("sender address is not valid: %s" , sender )}
86- }
8796
8897 var events []common.PlainEvent
8998 var withdrawals []common.Withdrawal
@@ -101,29 +110,26 @@ func ProcessRequest(sender, payloadJSON, stateJSON string) wasmCommon.ProcessRes
101110 return wasmCommon.ProcessResult {Error : "Transfer instruction is missing" }
102111 }
103112
104- if ! utils .IsValidAddress (instructions .Transfer .To ) {
105- return wasmCommon.ProcessResult {Error : fmt .Sprintf ("Transfer destination address is not valid: %s" , instructions .Transfer .To )}
106- }
107113
108114 // Validate sender account exists and has sufficient balance
109115 if currentState .Accounts [sender ] == nil {
110- return wasmCommon.ProcessResult {Error : fmt .Sprintf ("Account does not exist: %s" , sender )}
116+ return wasmCommon.ProcessResult {Error : fmt .Sprintf ("Account does not exist: %s" , sender . Hex () )}
111117 }
112- if currentState .Accounts [sender ].Balance < instructions .Transfer .Amount {
118+ if currentState .Accounts [sender ].Balance . Cmp ( instructions .Transfer .Amount ) < 0 {
113119 return wasmCommon.ProcessResult {Error : "Insufficient balance for transfer" }
114120 }
115121
116122 // Ensure recipient account exists
117123 if currentState .Accounts [instructions .Transfer .To ] == nil {
118124 currentState .Accounts [instructions .Transfer .To ] = & AccountState {
119125 Address : instructions .Transfer .To ,
120- Balance : 0 ,
126+ Balance : big . NewInt ( 0 ) ,
121127 }
122128 }
123129
124130 // Execute transfer
125- currentState .Accounts [sender ].Balance -= instructions .Transfer .Amount
126- currentState .Accounts [instructions .Transfer .To ].Balance += instructions .Transfer .Amount
131+ currentState .Accounts [sender ].Balance . Sub ( currentState . Accounts [ sender ]. Balance , instructions .Transfer .Amount )
132+ currentState .Accounts [instructions .Transfer .To ].Balance . Add ( currentState . Accounts [ instructions . Transfer . To ]. Balance , instructions .Transfer .Amount )
127133 currentState .Nonce ++
128134
129135 // Create events for both parties
@@ -171,12 +177,12 @@ func ProcessRequest(sender, payloadJSON, stateJSON string) wasmCommon.ProcessRes
171177 return wasmCommon.ProcessResult {Error : "Account does not exist" }
172178 }
173179
174- if currentState .Accounts [sender ].Balance < instructions .Withdraw .Amount {
180+ if currentState .Accounts [sender ].Balance . Cmp ( instructions .Withdraw .Amount ) < 0 {
175181 return wasmCommon.ProcessResult {Error : "Insufficient balance for withdrawal" }
176182 }
177183
178184 // Execute withdrawal
179- currentState .Accounts [sender ].Balance -= instructions .Withdraw .Amount
185+ currentState .Accounts [sender ].Balance . Sub ( currentState . Accounts [ sender ]. Balance , instructions .Withdraw .Amount )
180186 currentState .Nonce ++
181187
182188 // Create withdrawal
@@ -252,27 +258,27 @@ func GenerateDeanonymizationReport(payloadJSON, stateJSON string) wasmCommon.Dea
252258
253259// AccountState represents the state of a user account
254260type AccountState struct {
255- Address string `json:"address"`
256- Balance uint64 `json:"balance"`
261+ Address ethCommon. Address `json:"address"`
262+ Balance * big. Int `json:"balance"`
257263}
258264
259265// ApplicationInternalState represents the internal state of the application
260266type ApplicationInternalState struct {
261- AppID string `json:"appId"`
262- Accounts map [string ]* AccountState `json:"accounts"`
263- Nonce uint64 `json:"nonce"`
267+ AppID int64 `json:"appId"`
268+ Accounts map [ethCommon. Address ]* AccountState `json:"accounts"`
269+ Nonce uint64 `json:"nonce"`
264270}
265271
266272// TransferInstruction represents instructions for transferring funds
267273type TransferInstruction struct {
268- To string `json:"to"`
269- Amount uint64 `json:"amount"`
274+ To ethCommon. Address `json:"to"`
275+ Amount * big. Int `json:"amount"`
270276}
271277
272278// WithdrawInstruction represents instructions for withdrawing funds
273279type WithdrawInstruction struct {
274- To string `json:"to"`
275- Amount uint64 `json:"amount"`
280+ To ethCommon. Address `json:"to"`
281+ Amount * big. Int `json:"amount"`
276282}
277283
278284// PayloadInstructions represents the deserialized payload instructions
@@ -283,7 +289,7 @@ type PayloadInstructions struct {
283289}
284290
285291type UnencryptedDeanonymizationReportData struct {
286- Accounts map [string ]* AccountState `json:"accounts"`
292+ Accounts map [ethCommon. Address ]* AccountState `json:"accounts"`
287293 Nonce uint64 `json:"nonce"`
288294}
289295
0 commit comments