Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: paused checking for authRedeem on MarketPlace #420

Merged
merged 1 commit into from Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion Makefile
Expand Up @@ -7,6 +7,7 @@
.PHONY: compile_solidity_mock_euler_token compile_go_mock_euler_token compile_mock_euler_token
.PHONY: compile_solidity_mock_creator compile_go_mock_creator
.PHONY: compile_solidity_mock_marketplace compile_go_mock_marketplace
.PHONY: compile_solidity_mock_swivel compile_go_mock_swivel
.PHONY: compile_mocks

.PHONY: compile_solidity_sig_fake compile_go_sig_fake compile_sig_fake
Expand Down Expand Up @@ -137,7 +138,17 @@ compile_go_mock_marketplace:

compile_mock_marketplace: compile_solidity_mock_marketplace compile_go_mock_marketplace

compile_mocks: compile_mock_erc compile_mock_compound_token compile_mock_erc_4626 compile_mock_yearn_vault compile_mock_aave_token compile_mock_aave_pool compile_mock_euler_token compile_mock_creator compile_mock_marketplace
compile_solidity_mock_swivel:
@echo "compiling Mock Swivel solidity source into abi and bin files"
solc -o ./test/mocks --abi --bin --overwrite ./test/mocks/Swivel.sol

compile_go_mock_swivel:
@echo "compiling abi and bin files to golang"
abigen --abi ./test/mocks/Swivel.abi --bin ./test/mocks/Swivel.bin -pkg mocks -type Swivel -out ./test/mocks/swivel.go

compile_mock_swivel: compile_solidity_mock_swivel compile_go_mock_swivel

compile_mocks: compile_mock_erc compile_mock_compound_token compile_mock_erc_4626 compile_mock_yearn_vault compile_mock_aave_token compile_mock_aave_pool compile_mock_euler_token compile_mock_creator compile_mock_marketplace compile_mock_swivel

# -------------------------------------------------- FAKES -----------------------------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion build/marketplace/MarketPlace.bin

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion build/marketplace/MarketPlace.sol
Expand Up @@ -166,7 +166,12 @@ contract MarketPlace {
/// @param t Address of the user receiving underlying
/// @param a Amount of zcTokens being redeemed
/// @return Amount of underlying being withdrawn (needed for 5095 return)
function authRedeem(uint8 p, address u, uint256 m, address f, address t, uint256 a) public authorized(markets[p][u][m].zcToken) returns (uint256) {
function authRedeem(uint8 p, address u, uint256 m, address f, address t, uint256 a) public authorized(markets[p][u][m].zcToken) unpaused(p) returns (uint256) {
/// @dev swiv needs to be set or the call to authRedeem there will be faulty
if (swivel == address(0)) {
revert Exception(21, 0, 0, address(0), address(0));
}

Market memory market = markets[p][u][m];
// if the market has not matured, mature it...
if (market.maturityRate == 0) {
Expand Down
2 changes: 1 addition & 1 deletion build/marketplace/marketplace.go

Large diffs are not rendered by default.

260 changes: 260 additions & 0 deletions pkg/marketplacetesting/auth_redeem_test.go
@@ -0,0 +1,260 @@
package marketplacetesting

import (
"math/big"
test "testing"
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
assertions "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"github.com/swivel-finance/gost/test/marketplace"
"github.com/swivel-finance/gost/test/mocks"
)

type authRedeemSuite struct {
suite.Suite
Env *Env
Dep *Dep
Erc20 *mocks.Erc20Session
Erc4626 *mocks.Erc4626Session
CompoundToken *mocks.CompoundTokenSession
Creator *mocks.CreatorSession
ZcToken *mocks.ZcTokenSession
VaultTracker *mocks.VaultTrackerSession
Swivel *mocks.SwivelSession
MarketPlace *marketplace.MarketPlaceSession // *Session objects are created by the go bindings
}

func (s *authRedeemSuite) SetupTest() {
var err error
assert := assertions.New(s.T())

s.Env = NewEnv(big.NewInt(ONE_ETH)) // each of the wallets in the env will begin with this balance
s.Dep, err = Deploy(s.Env)
assert.Nil(err)

err = s.Env.Blockchain.AdjustTime(0) // set bc timestamp to 0
assert.Nil(err)

s.Env.Blockchain.Commit()

s.Erc20 = &mocks.Erc20Session{
Contract: s.Dep.Erc20,
CallOpts: bind.CallOpts{From: s.Env.Owner.Opts.From, Pending: false},
TransactOpts: bind.TransactOpts{
From: s.Env.Owner.Opts.From,
Signer: s.Env.Owner.Opts.Signer,
},
}

s.Erc4626 = &mocks.Erc4626Session{
Contract: s.Dep.Erc4626,
CallOpts: bind.CallOpts{From: s.Env.Owner.Opts.From, Pending: false},
TransactOpts: bind.TransactOpts{
From: s.Env.Owner.Opts.From,
Signer: s.Env.Owner.Opts.Signer,
},
}

s.CompoundToken = &mocks.CompoundTokenSession{
Contract: s.Dep.CompoundToken,
CallOpts: bind.CallOpts{From: s.Env.Owner.Opts.From, Pending: false},
TransactOpts: bind.TransactOpts{
From: s.Env.Owner.Opts.From,
Signer: s.Env.Owner.Opts.Signer,
},
}

s.Creator = &mocks.CreatorSession{
Contract: s.Dep.Creator,
CallOpts: bind.CallOpts{From: s.Env.Owner.Opts.From, Pending: false},
TransactOpts: bind.TransactOpts{
From: s.Env.Owner.Opts.From,
Signer: s.Env.Owner.Opts.Signer,
},
}

s.ZcToken = &mocks.ZcTokenSession{
Contract: s.Dep.ZcToken,
CallOpts: bind.CallOpts{From: s.Env.Owner.Opts.From, Pending: false},
TransactOpts: bind.TransactOpts{
From: s.Env.Owner.Opts.From,
Signer: s.Env.Owner.Opts.Signer,
},
}

s.VaultTracker = &mocks.VaultTrackerSession{
Contract: s.Dep.VaultTracker,
CallOpts: bind.CallOpts{From: s.Env.Owner.Opts.From, Pending: false},
TransactOpts: bind.TransactOpts{
From: s.Env.Owner.Opts.From,
Signer: s.Env.Owner.Opts.Signer,
},
}

s.Swivel = &mocks.SwivelSession{
Contract: s.Dep.Swivel,
CallOpts: bind.CallOpts{From: s.Env.Owner.Opts.From, Pending: false},
TransactOpts: bind.TransactOpts{
From: s.Env.Owner.Opts.From,
Signer: s.Env.Owner.Opts.Signer,
},
}

// binding owner to both, kind of why it exists - but could be any of the env wallets
s.MarketPlace = &marketplace.MarketPlaceSession{
Contract: s.Dep.MarketPlace,
CallOpts: bind.CallOpts{From: s.Env.Owner.Opts.From, Pending: false},
TransactOpts: bind.TransactOpts{
From: s.Env.Owner.Opts.From,
Signer: s.Env.Owner.Opts.Signer,
},
}

// authRedeem requires that the swivel address be the actual mock
_, err = s.MarketPlace.SetSwivel(s.Dep.SwivelAddress)
assert.Nil(err)
s.Env.Blockchain.Commit()
}

func (s *authRedeemSuite) TestAuthRedeemFailsOnPaused() {
assert := assertions.New(s.T())

underlying := s.Dep.Erc20Address
maturity := s.Dep.Maturity
amount := big.NewInt(123456789)
cToken := s.Dep.CompoundTokenAddress

tx, err := s.Erc20.DecimalsReturns(uint8(18))
assert.Nil(err)
assert.NotNil(tx)
s.Env.Blockchain.Commit()

tx, err = s.CompoundToken.UnderlyingReturns(underlying)
assert.Nil(err)
assert.NotNil(tx)
s.Env.Blockchain.Commit()

tx, err = s.Creator.CreateReturns(s.Dep.ZcTokenAddress, s.Dep.VaultTrackerAddress)
assert.Nil(err)
assert.NotNil(tx)
s.Env.Blockchain.Commit()

// a market must be created in order for the zcToken authorized check to succeed
tx, err = s.MarketPlace.CreateMarket(
uint8(1),
maturity,
cToken,
"awesome market",
"AM",
)
assert.Nil(err)
assert.NotNil(tx)
s.Env.Blockchain.Commit()

// stub the returns for zctoken and vaulttracker
tx, err = s.ZcToken.BurnReturns(true)
assert.Nil(err)
assert.NotNil(tx)
s.Env.Blockchain.Commit()

tx, err = s.VaultTracker.MatureVaultReturns(true)
assert.Nil(err)
assert.NotNil(tx)
s.Env.Blockchain.Commit()

// move past the maturity date
err = s.Env.Blockchain.AdjustTime(MATURITY * time.Second)
assert.Nil(err)
s.Env.Blockchain.Commit()

// force the paued modifier to fail here
tx, err = s.MarketPlace.Pause(uint8(1), true)
assert.Nil(err)
assert.NotNil(tx)
s.Env.Blockchain.Commit()

tx, err = s.ZcToken.AuthRedeem(uint8(1), underlying, maturity, s.Env.Owner.Opts.From, s.Env.User1.Opts.From, amount)
assert.Nil(tx)
assert.NotNil(err)
s.Env.Blockchain.Commit()

// unpause so the other tests don't fail
tx, err = s.MarketPlace.Pause(uint8(1), false)
assert.Nil(err)
assert.NotNil(tx)
s.Env.Blockchain.Commit()
}

func (s *authRedeemSuite) TestAuthRedeem() {
assert := assertions.New(s.T())

underlying := s.Dep.Erc20Address
maturity := s.Dep.Maturity
amount := big.NewInt(123456789)
cToken := s.Dep.CompoundTokenAddress

tx, err := s.Erc20.DecimalsReturns(uint8(18))
assert.Nil(err)
assert.NotNil(tx)
s.Env.Blockchain.Commit()

tx, err = s.CompoundToken.UnderlyingReturns(underlying)
assert.Nil(err)
assert.NotNil(tx)
s.Env.Blockchain.Commit()

tx, err = s.Creator.CreateReturns(s.Dep.ZcTokenAddress, s.Dep.VaultTrackerAddress)
assert.Nil(err)
assert.NotNil(tx)
s.Env.Blockchain.Commit()

// a market must be created in order for the zcToken authorized check to succeed
tx, err = s.MarketPlace.CreateMarket(
uint8(1),
maturity,
cToken,
"awesome market",
"AM",
)
assert.Nil(err)
assert.NotNil(tx)
s.Env.Blockchain.Commit()

// stub the returns for zctoken and vaulttracker
tx, err = s.ZcToken.BurnReturns(true)
assert.Nil(err)
assert.NotNil(tx)
s.Env.Blockchain.Commit()

tx, err = s.VaultTracker.MatureVaultReturns(true)
assert.Nil(err)
assert.NotNil(tx)
s.Env.Blockchain.Commit()

// move past the maturity date
err = s.Env.Blockchain.AdjustTime(MATURITY * time.Second)
assert.Nil(err)
s.Env.Blockchain.Commit()

// we'll use the exposed `authRedeem` method on the ZcToken mock which will call it's redeemer (MarketPlace) thus preserving the sender we need
// and be passed directly thru to the MarketPlace.AuthRedeem method we are testing
tx, err = s.ZcToken.AuthRedeem(uint8(1), underlying, maturity, s.Env.Owner.Opts.From, s.Env.User1.Opts.From, amount)
assert.Nil(err)
assert.NotNil(tx)
s.Env.Blockchain.Commit()

// the swivel mock should have seen the call
args, err := s.Swivel.AuthRedeemCalled(uint8(1))
assert.Nil(err)
assert.NotNil(args)
assert.Equal(args.Underlying, underlying)
assert.Equal(args.One, cToken)
assert.Equal(args.Two, s.Env.User1.Opts.From)
assert.Equal(args.Amount, amount)
}

func TestAuthRedeemSuite(t *test.T) {
suite.Run(t, &authRedeemSuite{})
}