Smart Contract
IPierPair
A.609e10301860b683.IPierPair
1import FungibleToken from 0xf233dcee88fe0abe
2import PierLPToken from 0x609e10301860b683
3import PierMath from 0xa378eeb799df8387
4
5/**
6
7IPierPair is the interface that defines the parts of a pair
8that should be exposed to public access.
9
10@author Metapier Foundation Ltd.
11
12 */
13pub contract interface IPierPair {
14
15 // Event that is emitted when the contract is created
16 pub event ContractInitialized()
17
18 // Event that is emitted when swap is executed
19 pub event Swap(poolId: UInt64, amountIn: UFix64, amountOut: UFix64, swapAForB: Bool)
20
21 // Event that is emitted when mint is executed
22 pub event Mint(poolId: UInt64, amountAIn: UFix64, amountBIn: UFix64)
23
24 // Event that is emitted when burn is executed
25 pub event Burn(poolId: UInt64, amountLP: UFix64, amountAOut: UFix64, amountBOut: UFix64)
26
27 // IPool defines the exposed components of a liquidity pool
28 pub resource interface IPool {
29
30 // *** Basic Information ***
31
32 // The identifier (owner's address) of the pool
33 pub let poolId: UInt64
34 // The K value computed by the last mint or burn
35 pub var kLast: UInt256
36
37 // Type of token A's vault (e.g., A.0x1654653399040a61.FlowToken.Vault)
38 pub let tokenAType: Type
39 // Type of token B's vault (e.g., A.0x3c5959b568896393.FUSD.Vault)
40 pub let tokenBType: Type
41
42 // returns [token A reserve, token B reserve]
43 pub fun getReserves(): [UFix64; 2]
44
45 // *** TWAP Information for Oracles ***
46
47 // The timestamp of the most recent burn, mint, or swap
48 pub var lastBlockTimestamp: UFix64
49
50 // use Word64 instead of UFix64 because overflow is acceptable
51 // as long as the delta can be computed correctly
52 pub var lastPriceACumulative: Word64
53 pub var lastPriceBCumulative: Word64
54
55 // *** Trading & Liquidity ***
56
57 // Takes in a vault of token A or token B, then returns a vault of the other token
58 // with its balance equals to `forAmount`. It throws an error if the `xy = k` curve
59 // cannot be maintained.
60 //
61 // @param fromVault The input vault of the swap (either token A or token B)
62 // @param forAmount The expected output balance of the swap
63 // @return A vault of the other token in the pool with its balance equals to `forAmount`
64 pub fun swap(fromVault: @FungibleToken.Vault, forAmount: UFix64): @FungibleToken.Vault {
65 pre {
66 forAmount > 0.0: "Metapier IPierPair: Zero swap output amount"
67 fromVault.balance > 0.0: "Metapier IPierPair: Zero swap input amount"
68 fromVault.isInstance(self.tokenAType) || fromVault.isInstance(self.tokenBType): "Metapier IPierPair: Invalid swap input type"
69 }
70 post {
71 !result.isInstance(before(fromVault.getType())): "Metapier IPierPair: Unexpected swap output"
72 result.isInstance(self.tokenAType) || result.isInstance(self.tokenBType): "Metapier IPierPair: Unexpected swap output"
73 result.balance == forAmount: "Metapier IPierPair: Inaccurate swap output amount"
74 }
75 }
76
77 // Mints new LP tokens by providing liquidity to the pool.
78 //
79 // @param vaultA Liquidity to provide for token A
80 // @param vaultB Liquidity to provide for token B
81 // @return New LP tokens as a share of the pool
82 pub fun mint(vaultA: @FungibleToken.Vault, vaultB: @FungibleToken.Vault): @PierLPToken.Vault {
83 pre {
84 vaultA.balance > 0.0 && vaultB.balance > 0.0: "Metapier IPierPair: Zero mint input amount"
85 vaultA.isInstance(self.tokenAType): "Metapier IPierPair: Invalid token A for mint"
86 vaultB.isInstance(self.tokenBType): "Metapier IPierPair: Invalid token B for mint"
87 }
88 post {
89 result.tokenId == self.poolId: "Metapier IPierPair: Unexpected LP token minted"
90 }
91 }
92
93 // Burns the given LP tokens to withdraw its share of the pool.
94 //
95 // @param lpTokenVault The LP tokens to burn
96 // @return The withdrawn share of the pool as [token A vault, token B vault]
97 pub fun burn(lpTokenVault: @PierLPToken.Vault): @[FungibleToken.Vault; 2] {
98 pre {
99 lpTokenVault.tokenId == self.poolId: "Metapier IPierPair: Invalid LP token to burn"
100 lpTokenVault.balance > 0.0: "Metapier IPierPair: Zero balance to burn"
101 }
102 post {
103 result[0].isInstance(self.tokenAType): "Metapier IPierPair: Unexpected burn output"
104 result[1].isInstance(self.tokenBType): "Metapier IPierPair: Unexpected burn output"
105 }
106 }
107 }
108}
109