UNPKG

@silvana-one/token

Version:

Silvana Fungible Token Library

170 lines 7.25 kB
import { __decorate, __metadata } from "tslib"; import { AccountUpdate, method, Permissions, PublicKey, State, state, UInt64, SmartContract, Bool, Struct, } from "o1js"; import { Whitelist } from "@silvana-one/storage"; import { FungibleToken } from "./FungibleToken.js"; import { mulDiv } from "./div.js"; export class OfferEvent extends Struct({ amount: UInt64, address: PublicKey, }) { } export class FungibleTokenOfferContract extends SmartContract { constructor() { super(...arguments); this.price = State(); this.seller = State(); this.token = State(); this.whitelist = State(); this.events = { offer: OfferEvent, withdraw: OfferEvent, buy: OfferEvent, updateWhitelist: Whitelist, }; } async deploy(args) { await super.deploy(args); this.whitelist.set(args.whitelist); this.account.permissions.set({ ...Permissions.default(), send: Permissions.proof(), setVerificationKey: Permissions.VerificationKey.impossibleDuringCurrentVersion(), setPermissions: Permissions.impossible(), }); } async initialize(seller, // we are short of AccountUpdates here, so we use this parameter instead of this.sender.getUnconstrained() token, amount, price) { this.account.provedState.requireEquals(Bool(false)); const tokenContract = new FungibleToken(token); const tokenId = tokenContract.deriveTokenId(); tokenId.assertEquals(this.tokenId); await tokenContract.transfer(seller, this.address, amount); this.seller.set(seller); this.price.set(price); this.token.set(token); this.emitEvent("offer", { amount, address: seller }); } async offer(amount, price) { const seller = this.seller.getAndRequireEquals(); const token = this.token.getAndRequireEquals(); const tokenContract = new FungibleToken(token); const tokenId = tokenContract.deriveTokenId(); tokenId.assertEquals(this.tokenId); const balance = this.account.balance.getAndRequireEquals(); const oldPrice = this.price.getAndRequireEquals(); // Price can be changed only when the balance is 0 price .equals(oldPrice) .or(balance.equals(UInt64.from(0))) .assertTrue(); this.price.set(price); const sender = this.sender.getUnconstrained(); const senderUpdate = AccountUpdate.createSigned(sender); senderUpdate.body.useFullCommitment = Bool(true); sender.assertEquals(seller); await tokenContract.transfer(sender, this.address, amount); this.emitEvent("offer", { amount, address: sender }); } async withdraw(amount) { amount.equals(UInt64.from(0)).assertFalse(); this.account.balance.requireBetween(amount, UInt64.MAXINT()); const seller = this.seller.getAndRequireEquals(); const token = this.token.getAndRequireEquals(); const tokenContract = new FungibleToken(token); const tokenId = tokenContract.deriveTokenId(); tokenId.assertEquals(this.tokenId); const sender = this.sender.getUnconstrained(); const senderUpdate = AccountUpdate.createSigned(sender, tokenId); senderUpdate.body.useFullCommitment = Bool(true); sender.assertEquals(seller); let offerUpdate = this.send({ to: senderUpdate, amount }); offerUpdate.body.mayUseToken = AccountUpdate.MayUseToken.InheritFromParent; offerUpdate.body.useFullCommitment = Bool(true); this.emitEvent("withdraw", { amount, address: sender }); } async buy(amount) { amount.equals(UInt64.from(0)).assertFalse(); this.account.balance.requireBetween(amount, UInt64.MAXINT()); const seller = this.seller.getAndRequireEquals(); const token = this.token.getAndRequireEquals(); const tokenContract = new FungibleToken(token); const tokenId = tokenContract.deriveTokenId(); tokenId.assertEquals(this.tokenId); const price = this.price.getAndRequireEquals(); const totalPrice = mulDiv({ value: price, multiplier: amount, denominator: UInt64.from(1_000_000_000), }).result; const buyer = this.sender.getUnconstrained(); const buyerUpdate = AccountUpdate.createSigned(buyer); buyerUpdate.send({ to: seller, amount: totalPrice }); buyerUpdate.body.useFullCommitment = Bool(true); let offerUpdate = this.send({ to: buyer, amount }); offerUpdate.body.mayUseToken = AccountUpdate.MayUseToken.InheritFromParent; offerUpdate.body.useFullCommitment = Bool(true); const whitelist = this.whitelist.getAndRequireEquals(); const whitelistedAmount = await whitelist.getWhitelistedAmount(buyer); amount.assertLessThanOrEqual(whitelistedAmount.assertSome("Cannot buy more than whitelisted amount")); this.emitEvent("buy", { amount, address: buyer }); } async updateWhitelist(whitelist) { const seller = this.seller.getAndRequireEquals(); const sender = this.sender.getUnconstrained(); const senderUpdate = AccountUpdate.createSigned(sender); senderUpdate.body.useFullCommitment = Bool(true); sender.assertEquals(seller); this.whitelist.set(whitelist); this.emitEvent("updateWhitelist", whitelist); } } __decorate([ state(UInt64), __metadata("design:type", Object) ], FungibleTokenOfferContract.prototype, "price", void 0); __decorate([ state(PublicKey), __metadata("design:type", Object) ], FungibleTokenOfferContract.prototype, "seller", void 0); __decorate([ state(PublicKey), __metadata("design:type", Object) ], FungibleTokenOfferContract.prototype, "token", void 0); __decorate([ state(Whitelist), __metadata("design:type", Object) ], FungibleTokenOfferContract.prototype, "whitelist", void 0); __decorate([ method, __metadata("design:type", Function), __metadata("design:paramtypes", [PublicKey, PublicKey, UInt64, UInt64]), __metadata("design:returntype", Promise) ], FungibleTokenOfferContract.prototype, "initialize", null); __decorate([ method, __metadata("design:type", Function), __metadata("design:paramtypes", [UInt64, UInt64]), __metadata("design:returntype", Promise) ], FungibleTokenOfferContract.prototype, "offer", null); __decorate([ method, __metadata("design:type", Function), __metadata("design:paramtypes", [UInt64]), __metadata("design:returntype", Promise) ], FungibleTokenOfferContract.prototype, "withdraw", null); __decorate([ method, __metadata("design:type", Function), __metadata("design:paramtypes", [UInt64]), __metadata("design:returntype", Promise) ], FungibleTokenOfferContract.prototype, "buy", null); __decorate([ method, __metadata("design:type", Function), __metadata("design:paramtypes", [Whitelist]), __metadata("design:returntype", Promise) ], FungibleTokenOfferContract.prototype, "updateWhitelist", null); //# sourceMappingURL=offer.js.map