public void SwapTokens(Address buyer, Address seller, string baseSymbol, string quoteSymbol, BigInteger amount, BigInteger price, byte[] signature) { Runtime.Expect(Runtime.IsWitness(buyer), "invalid witness"); Runtime.Expect(seller != buyer, "invalid seller"); Runtime.Expect(seller.IsUser, "seller must be user address"); Runtime.Expect(Runtime.TokenExists(baseSymbol), "invalid base token"); var baseToken = Runtime.GetToken(baseSymbol); Runtime.Expect(baseToken.Flags.HasFlag(TokenFlags.Fungible), "token must be fungible"); var baseBalance = Runtime.GetBalance(baseSymbol, seller); Runtime.Expect(baseBalance >= amount, "invalid amount"); var swap = new TokenSwap() { baseSymbol = baseSymbol, quoteSymbol = quoteSymbol, buyer = buyer, seller = seller, price = price, value = amount, }; var msg = Serialization.Serialize(swap); Runtime.Expect(Ed25519.Verify(signature, msg, seller.PublicKey), "invalid signature"); Runtime.Expect(Runtime.TokenExists(quoteSymbol), "invalid quote token"); var quoteToken = Runtime.GetToken(quoteSymbol); Runtime.Expect(quoteToken.Flags.HasFlag(TokenFlags.Fungible), "token must be fungible"); var quoteBalance = Runtime.GetBalance(quoteSymbol, buyer); Runtime.Expect(quoteBalance >= price, "invalid balance"); Runtime.Expect(Runtime.TransferTokens(quoteSymbol, buyer, seller, price), "payment failed"); Runtime.Expect(Runtime.TransferTokens(baseSymbol, seller, buyer, amount), "transfer failed"); Runtime.Notify(EventKind.TokenSend, seller, new TokenEventData() { chainAddress = this.Address, symbol = baseSymbol, value = amount }); Runtime.Notify(EventKind.TokenSend, buyer, new TokenEventData() { chainAddress = this.Address, symbol = quoteSymbol, value = price }); Runtime.Notify(EventKind.TokenReceive, seller, new TokenEventData() { chainAddress = this.Address, symbol = quoteSymbol, value = price }); Runtime.Notify(EventKind.TokenReceive, buyer, new TokenEventData() { chainAddress = this.Address, symbol = baseSymbol, value = amount }); }
public void SwapToken(Address buyer, Address seller, string baseSymbol, string quoteSymbol, BigInteger tokenID, BigInteger price, byte[] signature) { Runtime.Expect(Runtime.IsWitness(buyer), "invalid witness"); Runtime.Expect(seller != buyer, "invalid seller"); Runtime.Expect(seller.IsUser, "seller must be user address"); Runtime.Expect(Runtime.TokenExists(baseSymbol), "invalid base token"); var baseToken = Runtime.GetToken(baseSymbol); Runtime.Expect(!baseToken.Flags.HasFlag(TokenFlags.Fungible), "token must be non-fungible"); var nft = Runtime.ReadToken(baseSymbol, tokenID); Runtime.Expect(nft.CurrentChain == Runtime.Chain.Name, "invalid owner"); var owner = nft.CurrentOwner; Runtime.Expect(owner == seller, "invalid owner"); var swap = new TokenSwap() { baseSymbol = baseSymbol, quoteSymbol = quoteSymbol, buyer = buyer, seller = seller, price = price, value = tokenID, }; var msg = Serialization.Serialize(swap); Runtime.Expect(Ed25519.Verify(signature, msg, seller.ToByteArray().Skip(1).ToArray()), "invalid signature"); Runtime.Expect(Runtime.TokenExists(quoteSymbol), "invalid quote token"); var quoteToken = Runtime.GetToken(quoteSymbol); Runtime.Expect(quoteToken.Flags.HasFlag(TokenFlags.Fungible), "token must be fungible"); var balance = Runtime.GetBalance(quoteSymbol, buyer); Runtime.Expect(balance >= price, "invalid balance"); Runtime.TransferTokens(quoteSymbol, buyer, owner, price); Runtime.TransferToken(baseSymbol, owner, buyer, tokenID); }
public void SwapTokens(Address buyer, Address seller, string baseSymbol, string quoteSymbol, BigInteger amount, BigInteger price, byte[] signature) { Runtime.Expect(Runtime.IsWitness(buyer), "invalid witness"); Runtime.Expect(seller != buyer, "invalid seller"); Runtime.Expect(seller.IsUser, "seller must be user address"); Runtime.Expect(Runtime.TokenExists(baseSymbol), "invalid base token"); var baseToken = Runtime.GetToken(baseSymbol); Runtime.Expect(baseToken.Flags.HasFlag(TokenFlags.Fungible), "token must be fungible"); var baseBalance = Runtime.GetBalance(baseSymbol, seller); Runtime.Expect(baseBalance >= amount, "invalid amount"); var swap = new TokenSwap() { baseSymbol = baseSymbol, quoteSymbol = quoteSymbol, buyer = buyer, seller = seller, price = price, value = amount, }; var msg = Serialization.Serialize(swap); Runtime.Expect(Ed25519.Verify(signature, msg, seller.ToByteArray().Skip(2).ToArray()), "invalid signature"); Runtime.Expect(Runtime.TokenExists(quoteSymbol), "invalid quote token"); var quoteToken = Runtime.GetToken(quoteSymbol); Runtime.Expect(quoteToken.Flags.HasFlag(TokenFlags.Fungible), "token must be fungible"); var quoteBalance = Runtime.GetBalance(quoteSymbol, buyer); Runtime.Expect(quoteBalance >= price, "invalid balance"); Runtime.TransferTokens(quoteSymbol, buyer, seller, price); Runtime.TransferTokens(baseSymbol, seller, buyer, amount); }