You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
5.1 KiB
166 lines
5.1 KiB
import "@stdlib/ownable"; |
|
import "./messages"; |
|
import "./linker"; |
|
|
|
|
|
|
|
@interface("org.ton.jetton.wallet") |
|
contract TONBWallet { |
|
|
|
const minTonsForStorage: Int = ton("0.01"); |
|
const gasConsumption: Int = ton("0.01"); |
|
|
|
balance: Int; |
|
owner: Address; |
|
master: Address; |
|
linker: Address?; |
|
|
|
init(master: Address, owner: Address) { |
|
self.balance = 0; |
|
self.owner = owner; |
|
self.master = master; |
|
} |
|
|
|
receive(msg: TokenTransfer) { |
|
|
|
// Check sender |
|
let ctx: Context = context(); |
|
require(ctx.sender == self.owner, "Invalid sender"); |
|
|
|
// Update balance |
|
self.balance = self.balance - msg.amount; |
|
require(self.balance >= 0, "Invalid balance"); |
|
|
|
// Gas checks |
|
let fwdFee: Int = ctx.readForwardFee(); |
|
let fwdCount: Int = 1; |
|
if (msg.forwardTonAmount > 0) { |
|
fwdCount = 2; |
|
} |
|
require(ctx.value > fwdCount * fwdFee + 2 * self.gasConsumption + self.minTonsForStorage, "Invalid value"); |
|
|
|
// Send tokens |
|
let init: StateInit = initOf TONBWallet(self.master, msg.destination); |
|
let walletAddress: Address = contractAddress(init); |
|
send(SendParameters{ |
|
to: walletAddress, |
|
value: 0, |
|
mode: SendRemainingValue, |
|
bounce: true, |
|
body: TokenTransferInternal{ |
|
amount: msg.amount, |
|
queryId: msg.queryId, |
|
from: self.owner, |
|
responseAddress: self.owner, |
|
forwardTonAmount: msg.forwardTonAmount, |
|
forwardPayload: msg.forwardPayload, |
|
setLinker: null |
|
}.toCell(), |
|
code: init.code, |
|
data: init.data |
|
}); |
|
} |
|
|
|
receive(msg: TokenTransferInternal) { |
|
|
|
// Check sender |
|
let ctx: Context = context(); |
|
if (self.linker == null) { |
|
self.linker = msg.setLinker; |
|
} |
|
if (ctx.sender != self.master && ctx.sender != self.linker) { |
|
let sinit: StateInit = initOf TONBWallet(self.master, msg.from); |
|
require(contractAddress(sinit) == ctx.sender, "Invalid sender"); |
|
} |
|
|
|
// Update balance |
|
self.balance = self.balance + msg.amount; |
|
require(self.balance >= 0, "Invalid balance"); |
|
|
|
// Adjust value for gas |
|
let msgValue: Int = ctx.value; |
|
let tonBalanceBeforeMsg: Int = myBalance() - msgValue; |
|
let storageFee: Int = self.minTonsForStorage - min(tonBalanceBeforeMsg, self.minTonsForStorage); |
|
msgValue = msgValue - (storageFee + self.gasConsumption); |
|
|
|
// Forward ton |
|
if (msg.forwardTonAmount > 0) { |
|
let fwdFee: Int = ctx.readForwardFee(); |
|
msgValue = msgValue - (msg.forwardTonAmount + fwdFee); |
|
send(SendParameters{ |
|
to: self.owner, |
|
value: msg.forwardTonAmount, |
|
bounce: false, |
|
body: TokenNotification{ |
|
queryId: msg.queryId, |
|
amount: msg.amount, |
|
from: msg.from, |
|
forwardPayload: msg.forwardPayload |
|
}.toCell() |
|
}); |
|
} |
|
|
|
// Cashback |
|
if (msg.responseAddress != null && msgValue > 0) { |
|
send(SendParameters{ |
|
to: msg.responseAddress!!, |
|
value: msgValue, |
|
bounce: false, |
|
body: TokenExcesses{ |
|
queryId: msg.queryId |
|
}.toCell() |
|
}); |
|
} |
|
} |
|
|
|
receive(msg: TokenBurn) { |
|
|
|
// Check sender |
|
let ctx: Context = context(); |
|
require(ctx.sender == self.owner || ctx.sender == self.master || ctx.sender == self.linker, "Invalid sender"); |
|
|
|
// Update balance |
|
self.balance = self.balance - msg.amount; |
|
require(self.balance >= 0, "Invalid balance"); |
|
|
|
// Gas checks |
|
let fwdFee: Int = ctx.readForwardFee(); |
|
require(ctx.value > fwdFee + 2 * self.gasConsumption + self.minTonsForStorage, "Invalid value"); |
|
|
|
// Burn tokens |
|
send(SendParameters{ |
|
to: self.master, |
|
value: 0, |
|
mode: SendRemainingValue, |
|
bounce: true, |
|
body: TokenBurnNotification{ |
|
queryId: msg.queryId, |
|
amount: msg.amount, |
|
owner: self.owner, |
|
responseAddress: self.owner |
|
}.toCell() |
|
}); |
|
} |
|
|
|
bounced(msg: Slice) { |
|
|
|
// Parse bounced message |
|
msg.skipBits(32); // 0xFFFFFFFF |
|
let op: Int = msg.loadUint(32); |
|
let queryId: Int = msg.loadUint(64); |
|
let jettonAmount: Int = msg.loadCoins(); |
|
require(op == 0x178d4519 || op == 0x7bdd97de, "Invalid bounced message"); |
|
|
|
// Update balance |
|
self.balance = self.balance + jettonAmount; |
|
} |
|
|
|
get fun get_wallet_data(): JettonWalletData { |
|
return JettonWalletData{ |
|
balance: self.balance, |
|
owner: self.owner, |
|
master: self.master, |
|
walletCode: (initOf TONBWallet(self.master, self.owner)).code |
|
}; |
|
} |
|
}
|
|
|