Skip to content

Commit

Permalink
refactor: name change transferFromAndVerify to safeTransferFrom
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyhyde committed Dec 17, 2024
1 parent af230b5 commit 4681241
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
4 changes: 2 additions & 2 deletions pool/pool.gno
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ func Mint(
_, amount0, amount1 := pool.modifyPosition(position)

if amount0.Gt(u256.Zero()) {
pool.transferFromAndVerify(positionCaller, consts.POOL_ADDR, pool.token0Path, amount0, true)
pool.safeTransferFrom(positionCaller, consts.POOL_ADDR, pool.token0Path, amount0, true)
}

if amount1.Gt(u256.Zero()) {
pool.transferFromAndVerify(positionCaller, consts.POOL_ADDR, pool.token1Path, amount1, false)
pool.safeTransferFrom(positionCaller, consts.POOL_ADDR, pool.token1Path, amount1, false)
}

return amount0.ToString(), amount1.ToString()
Expand Down
43 changes: 28 additions & 15 deletions pool/pool_transfer.gno
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,39 @@ func (p *Pool) transferAndVerify(
}
}

// transferFromAndVerify performs a token transfer into the pool using transferFrom
// while updating the pool's internal accounting. This function is typically used
// during swaps and liquidity additions.
// safeTransferFrom securely transfers tokens into the pool while ensuring balance consistency.
//
// The function assumes the sender has approved the pool to spend their tokens.
// This function performs the following steps:
// 1. Validates and converts the transfer amount to `uint64` using `safeConvertToUint64`.
// 2. Executes the token transfer using `TransferFrom` via the token teller contract.
// 3. Verifies that the destination balance reflects the correct amount after transfer.
// 4. Updates the pool's internal balances (`token0` or `token1`) and validates the updated state.
//
// Parameters:
// - from: source address for the transfer
// - to: destination address (typically the pool)
// - tokenPath: path identifier of the token to transfer
// - amount: amount to transfer (must be positive)
// - isToken0: true if transferring token0, false for token1
// - from (std.Address): Source address for the token transfer.
// - to (std.Address): Destination address, typically the pool address.
// - tokenPath (string): Path identifier for the token being transferred.
// - amount (*u256.Uint): The amount of tokens to transfer (must be a positive value).
// - isToken0 (bool): A flag indicating whether the token being transferred is token0 (`true`) or token1 (`false`).
//
// The function will:
// 1. Convert amount to uint64 (must fit)
// 2. Execute the transferFrom
// 3. Update pool's internal balance
// Panics:
// - If the `amount` exceeds the uint64 range during conversion.
// - If the token transfer (`TransferFrom`) fails.
// - If the destination balance after the transfer does not match the expected amount.
// - If the pool's internal balances (`token0` or `token1`) overflow or become inconsistent.
//
// Notes:
// - The function assumes that the sender (`from`) has approved the pool to spend the specified tokens.
// - The balance consistency check ensures that no tokens are lost or double-counted during the transfer.
// - Pool balance updates are performed atomically to ensure internal consistency.
//
// Example:
// p.safeTransferFrom(
//
// sender, poolAddress, "path/to/token0", u256.MustFromDecimal("1000"), true
//
// Panics if the amount conversion fails or if the transfer fails
func (p *Pool) transferFromAndVerify(
// )
func (p *Pool) safeTransferFrom(
from, to std.Address,
tokenPath string,
amount *u256.Uint,
Expand Down
6 changes: 3 additions & 3 deletions pool/pool_transfer_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestTransferFromAndVerify(t *testing.T) {
TokenFaucet(t, tt.tokenPath, pusers.AddressOrName(tt.from))
TokenApprove(t, tt.tokenPath, pusers.AddressOrName(tt.from), pool, u256.MustFromDecimal(tt.amount.ToString()).Uint64())

tt.pool.transferFromAndVerify(tt.from, tt.to, tt.tokenPath, u256.MustFromDecimal(tt.amount.ToString()), tt.isToken0)
tt.pool.safeTransferFrom(tt.from, tt.to, tt.tokenPath, u256.MustFromDecimal(tt.amount.ToString()), tt.isToken0)

if !tt.pool.balances.token0.Eq(tt.expectedBal0) {
t.Errorf("token0 balance mismatch: expected %s, got %s",
Expand Down Expand Up @@ -165,7 +165,7 @@ func TestTransferFromAndVerify(t *testing.T) {

TokenFaucet(t, fooPath, pusers.AddressOrName(testutils.TestAddress("from_addr")))
TokenApprove(t, fooPath, pusers.AddressOrName(testutils.TestAddress("from_addr")), pusers.AddressOrName(consts.POOL_ADDR), u256.MustFromDecimal(negativeAmount.Abs().ToString()).Uint64())
pool.transferFromAndVerify(
pool.safeTransferFrom(
testutils.TestAddress("from_addr"),
testutils.TestAddress("to_addr"),
fooPath,
Expand Down Expand Up @@ -197,7 +197,7 @@ func TestTransferFromAndVerify(t *testing.T) {
}
}()

pool.transferFromAndVerify(
pool.safeTransferFrom(
testutils.TestAddress("from_addr"),
testutils.TestAddress("to_addr"),
fooPath,
Expand Down
4 changes: 2 additions & 2 deletions pool/swap.gno
Original file line number Diff line number Diff line change
Expand Up @@ -512,12 +512,12 @@ func tickTransition(step StepComputations, zeroForOne bool, state SwapState, poo
func (p *Pool) swapTransfers(zeroForOne bool, payer, recipient std.Address, amount0, amount1 *i256.Int) {
if zeroForOne {
// payer > POOL
p.transferFromAndVerify(payer, consts.POOL_ADDR, p.token0Path, amount0.Abs(), true)
p.safeTransferFrom(payer, consts.POOL_ADDR, p.token0Path, amount0.Abs(), true)
// POOL > recipient
p.transferAndVerify(recipient, p.token1Path, amount1, false)
} else {
// payer > POOL
p.transferFromAndVerify(payer, consts.POOL_ADDR, p.token1Path, amount1.Abs(), false)
p.safeTransferFrom(payer, consts.POOL_ADDR, p.token1Path, amount1.Abs(), false)
// POOL > recipient
p.transferAndVerify(recipient, p.token0Path, amount0, true)
}
Expand Down

0 comments on commit 4681241

Please sign in to comment.