import "constants"; import "messages"; fun stakingDepositMessage(value: Int, pool: Address): SendParameters { return SendParameters{ to: pool, value: value, body: beginCell().storeSlice("Deposit".asSlice()).endCell() }; } fun stakingWithdrawMessage(value: Int, pool: Address): SendParameters { return SendParameters{ to: pool, value: ton("0.05"), body: beginCell().storeUint(3665837821, 32).storeCoins(value).endCell() }; } struct WithdrawalRequests { addresses: map[Int]Address; amounts: map[Int]Int; n_requests: Int = 0; } fun withdrawalSum(requests: WithdrawalRequests): Int { let sum: Int = 0; let i: Int = 0; while(i < requests.n_requests) { sum = sum + requests.amounts.get(i)!!; i = i + 1; } return sum; } trait StakingTrait { staking_pool: Address?; withdrawal_requests: WithdrawalRequests; fun sendStake() { if(self.staking_pool == null) { return; } let value: Int = myBalance() - tonb_floor; if(value < ton("0.05")) { return; } send(stakingDepositMessage(value, self.staking_pool!!)); } fun sendWithdrawal() { if(self.staking_pool == null) { return; } let value: Int = withdrawalSum(self.withdrawal_requests); send(stakingWithdrawMessage(value, self.staking_pool!!)); } receive("Withdraw completed") { let i: Int = 0; while(i < self.withdrawal_requests.n_requests) { send(SendParameters{ to: self.withdrawal_requests.addresses.get(i)!!, value: self.withdrawal_requests.amounts.get(i)!! }); i = i + 1; } // self.withdrawal_requests = WithdrawalRequests{ // addresses: addresses, // amounts: amounts // }; // todo: initialize withdrawal_requests } fun requestWithdrawal(address: Address, value: Int) { self.withdrawal_requests.addresses.set(self.withdrawal_requests.n_requests, address); self.withdrawal_requests.amounts.set(self.withdrawal_requests.n_requests, value); self.withdrawal_requests.n_requests = self.withdrawal_requests.n_requests + 1; } }