Skip to content

Commit

Permalink
Merge branch 'main' into enable-wasm-stargate
Browse files Browse the repository at this point in the history
  • Loading branch information
inon-man authored Jul 21, 2023
2 parents 76ea3a9 + 33b8d5e commit b8d0aab
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 32 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ Ref: https://keepachangelog.com/en/1.0.0/
# Changelog
## [Unreleased]

### State Machine Breaking

* (treasury) [#272](https://github.com/classic-terra/core/pull/272) Split the burn tax to the distribution module

## [Unreleased-v2.1.x]

### Features
* (wasm) [#309](https://github.com/classic-terra/core/pull/309) Support WASM stargate queries

Expand Down
14 changes: 3 additions & 11 deletions custom/auth/ante/burntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,14 @@ func (btfd BurnTaxFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate
burnSplitRate := btfd.treasuryKeeper.GetBurnSplitRate(ctx)

if burnSplitRate.IsPositive() {
communityDeltaCoins := sdk.NewCoins()
distributionDeltaCoins := sdk.NewCoins()

for _, taxCoin := range taxes {
splitcoinAmount := burnSplitRate.MulInt(taxCoin.Amount).RoundInt()
communityDeltaCoins = communityDeltaCoins.Add(sdk.NewCoin(taxCoin.Denom, splitcoinAmount))
distributionDeltaCoins = distributionDeltaCoins.Add(sdk.NewCoin(taxCoin.Denom, splitcoinAmount))
}

taxes = taxes.Sub(communityDeltaCoins)

if err = btfd.distrKeeper.FundCommunityPool(
ctx,
communityDeltaCoins,
btfd.accountKeeper.GetModuleAddress(types.FeeCollectorName),
); err != nil {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, err.Error())
}
taxes = taxes.Sub(distributionDeltaCoins)
}

if !taxes.IsZero() {
Expand Down
34 changes: 14 additions & 20 deletions custom/auth/ante/burntax_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func (suite *AnteTestSuite) runSplitTaxTest(burnSplitRate sdk.Dec) {
tk := suite.app.TreasuryKeeper
bk := suite.app.BankKeeper
dk := suite.app.DistrKeeper
mfd := ante.NewBurnTaxFeeDecorator(suite.app.AccountKeeper, tk, bk, dk)
ak := suite.app.AccountKeeper
mfd := ante.NewBurnTaxFeeDecorator(ak, tk, bk, dk)
antehandler := sdk.ChainAnteDecorators(mfd)

// Set the blockheight past the burn tax height block
Expand Down Expand Up @@ -62,7 +63,7 @@ func (suite *AnteTestSuite) runSplitTaxTest(burnSplitRate sdk.Dec) {

// Send taxes to fee collector to simulate DeductFeeDecorator antehandler
taxes := suite.DeductFees(sendAmount)
feeCollector := suite.app.AccountKeeper.GetModuleAccount(suite.ctx, types.FeeCollectorName)
feeCollector := ak.GetModuleAccount(suite.ctx, types.FeeCollectorName)

// expected: fee collector = taxes
amountFeeBefore := bk.GetAllBalances(suite.ctx, feeCollector.GetAddress())
Expand All @@ -71,32 +72,30 @@ func (suite *AnteTestSuite) runSplitTaxTest(burnSplitRate sdk.Dec) {
totalSupplyBefore, _, err := bk.GetPaginatedTotalSupply(suite.ctx, &query.PageRequest{})
require.NoError(err)
fmt.Printf(
"Before: TotalSupply %v, Community %v, FeeCollector %v\n",
"Before: TotalSupply %v, FeeCollector %v\n",
totalSupplyBefore,
dk.GetFeePool(suite.ctx).CommunityPool,
amountFeeBefore,
)

// send tx to BurnTaxFeeDecorator antehandler
_, err = antehandler(suite.ctx, tx, false)
require.NoError(err)

communityPoolAfter := dk.GetFeePool(suite.ctx).CommunityPool
// burn the burn account
tk.BurnCoinsFromBurnAccount(suite.ctx)

feeCollectorAfter := sdk.NewDecCoinsFromCoins(bk.GetAllBalances(suite.ctx, ak.GetModuleAddress(types.FeeCollectorName))...)
burnTax := sdk.NewDecCoinsFromCoins(taxes...)

if burnSplitRate.IsPositive() {
splitTaxesDecCoins := burnTax.MulDec(burnSplitRate)
distributionDeltaCoins := burnTax.MulDec(burnSplitRate)

// expected: community pool 50%
require.Equal(communityPoolAfter, splitTaxesDecCoins)

fmt.Printf("BurnSplitRate %v, splitTaxes %v\n", burnSplitRate, splitTaxesDecCoins)
burnTax = burnTax.Sub(splitTaxesDecCoins)
fmt.Printf("BurnSplitRate %v, DistributionDeltaCoins %v\n", burnSplitRate, distributionDeltaCoins)
require.Equal(feeCollectorAfter, distributionDeltaCoins)
burnTax = burnTax.Sub(distributionDeltaCoins)
}

// burn the burn account
tk.BurnCoinsFromBurnAccount(suite.ctx)

totalSupplyAfter, _, err := bk.GetPaginatedTotalSupply(suite.ctx, &query.PageRequest{})
require.NoError(err)
if !burnTax.Empty() {
Expand All @@ -107,15 +106,10 @@ func (suite *AnteTestSuite) runSplitTaxTest(burnSplitRate sdk.Dec) {
)
}

amountFeeAfter := bk.GetAllBalances(suite.ctx, feeCollector.GetAddress())
// expected: fee collector = 0
require.True(amountFeeAfter.Empty())

fmt.Printf(
"After: TotalSupply %v, Community %v, FeeCollector %v\n",
"After: TotalSupply %v, FeeCollector %v\n",
totalSupplyAfter,
communityPoolAfter,
amountFeeAfter,
feeCollectorAfter,
)
}

Expand Down
Binary file not shown.
Binary file added scripts/wasm/contracts/old_anc_token.wasm
Binary file not shown.
Binary file added scripts/wasm/contracts/old_astroc_token.wasm
Binary file not shown.
File renamed without changes.
83 changes: 83 additions & 0 deletions scripts/wasm/token-migration-after.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#! /bin/bash

BINARY=_build/new/terrad
CONTRACTPATH="scripts/wasm/contracts/new_anc_token.wasm"
RECEIVERPATH="scripts/wasm/contracts/cw20_receiver_template.wasm"
KEYRING_BACKEND="test"
HOME=mytestnet
CHAIN_ID=localterra

if [ -z "$PRE_UPGRADE_CONTRACT_ADDR" ]; then
echo "PRE_UPGRADE_CONTRACT_ADDR is empty"
exit 1
fi

### DEBUG ###
#contract_addr="terra18vd8fpwxzck93qlwghaj6arh4p7c5n896xzem5"
### DEBUG ###
addr2=$($BINARY keys show test1 -a --home $HOME --keyring-backend $KEYRING_BACKEND)

echo "STORE DUMMY RECEIVER"
res=$($BINARY tx wasm store $RECEIVERPATH --from test0 --output json --gas auto --gas-adjustment 2.3 --fees 100000000uluna --chain-id $CHAIN_ID --home $HOME --broadcast-mode block --keyring-backend $KEYRING_BACKEND -y)
tx=$(echo $res | jq -r ."txhash")
res=$($BINARY q tx --output json ${tx})
code=$(echo "$res" | jq -r ."code")
if [ "$code" != "0" ]; then
echo "store contract failed"
exit -1
fi
code_id=$(echo "$res" | jq -r '.logs[0].events[] | select(.type == "store_code") | .attributes[] | select(.key == "code_id") | .value')
echo "CODE = ${code_id}"
echo ""

echo "INSTANTIATE DUMMY RECEIVER"
res=$($BINARY tx wasm instantiate $code_id '{}' --label "contract_${code_id}" --no-admin --from test0 --output json --gas auto --gas-adjustment 2.3 --fees 100000000uluna --chain-id $CHAIN_ID --home $HOME --broadcast-mode block --keyring-backend $KEYRING_BACKEND -y)
code=$(echo $res | jq -r ."code")
tx=$(echo $res | jq -r ."txhash")
if [ "$code" != 0 ]; then
echo "instantiate contract failed"
exit -1
fi
receiver=$($BINARY q tx --output json ${tx} | jq -r '.logs[0].events[] | select(.type == "instantiate") | .attributes[] | select(.key == "_contract_address") | .value')
echo "ADDRESS = ${receiver}"
echo ""

echo "TRANSFER P2P - BEFORE MIGRATION"
msg=$(jq -n '
{
"transfer": {
"amount": "100000",
"recipient": "'$addr2'"
}
}')
echo $msg
res=$($BINARY tx wasm execute "$PRE_UPGRADE_CONTRACT_ADDR" "$msg" --from test0 --output json --gas auto --gas-adjustment 2.3 --fees 100000000uluna --chain-id $CHAIN_ID --home $HOME --keyring-backend $KEYRING_BACKEND -y)
tx=$(echo $res | jq -r ."txhash")
code=$(echo $res | jq -r ."code")
if [ "$code" != "0" ]; then
echo "transfer message failed"
exit -1
fi

echo $res

sleep 5

echo "SEND - BEFORE MIGRATION"
msg=$(jq -n '
{
"send": {
"amount": "1",
"contract": "'$receiver'",
"msg": "eyJ0ZXJtIjp7ImFtb3VudCI6IjEwMDAwMCJ9fQ=="
}
}')
echo $msg
res=$($BINARY tx wasm execute "$PRE_UPGRADE_CONTRACT_ADDR" "$msg" --from test0 --output json --gas auto --gas-adjustment 2.3 --fees 100000000uluna --chain-id $CHAIN_ID --home $HOME --keyring-backend $KEYRING_BACKEND -y)
tx=$(echo $res | jq -r ."txhash")
code=$(echo $res | jq -r ."code")
if [ "$code" != "0" ]; then
echo "transfer message failed"
exit -1
fi
echo $res
72 changes: 72 additions & 0 deletions scripts/wasm/token-migration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/bin/sh

BINARY=_build/old/terrad
CONTRACTPATH="scripts/wasm/contracts/old_anc_token.wasm"
KEYRING_BACKEND="test"
HOME=mytestnet
CHAIN_ID=localterra

# upload old contracts
echo "... stores a wasm"
addr=$($BINARY keys show test0 -a --home $HOME --keyring-backend $KEYRING_BACKEND)
addr2=$($BINARY keys show test1 -a --home $HOME --keyring-backend $KEYRING_BACKEND)
out=$($BINARY tx wasm store ${CONTRACTPATH} --from test0 --output json --gas auto --gas-adjustment 2.3 --fees 100000000uluna --chain-id $CHAIN_ID --home $HOME --keyring-backend $KEYRING_BACKEND -y)
code=$(echo $out | jq -r '.code')
if [ "$code" != "0" ]; then
echo "... Could not store binary" >&2
echo $out >&2
exit $code
fi
sleep 10
txhash=$(echo $out | jq -r '.txhash')
id=$($BINARY q tx $txhash -o json | jq -r '.raw_log' | jq -r '.[0].events[1].attributes[1].value')
echo "CODE = $id"
echo ""

# upload old contracts
echo "... stores a second wasm"
addr=$($BINARY keys show test0 -a --home $HOME --keyring-backend $KEYRING_BACKEND)
addr2=$($BINARY keys show test1 -a --home $HOME --keyring-backend $KEYRING_BACKEND)
out=$($BINARY tx wasm store ${CONTRACTPATH} --from test0 --output json --gas auto --gas-adjustment 2.3 --fees 100000000uluna --chain-id $CHAIN_ID --home $HOME --keyring-backend $KEYRING_BACKEND -y)
code=$(echo $out | jq -r '.code')
if [ "$code" != "0" ]; then
echo "... Could not store binary" >&2
echo $out >&2
exit $code
fi
sleep 10
txhash=$(echo $out | jq -r '.txhash')
# commented out on purpose
# we wanna use the first id to instantiate
# a contract from
# id=$($BINARY q tx $txhash -o json | jq -r '.raw_log' | jq -r '.[0].events[1].attributes[1].value')
echo "CODE = $id"
echo ""

# instantiates contract
echo "... instantiates contract"
msg=$(jq -n '
{
"decimals":8,
"initial_balances":[
{
"address":"'$addr'",
"amount":"1000000000"
}
],
"name":"Anchor Token",
"symbol":"ANC"
}')
echo $msg
out=$($BINARY tx wasm instantiate $id "$msg" --from test0 --output json --gas auto --gas-adjustment 2.3 --fees 20000000uluna --chain-id $CHAIN_ID --home $HOME --keyring-backend $KEYRING_BACKEND -y)
code=$(echo $out | jq -r '.code')
if [ "$code" != "0" ]; then
echo "... Could not instantiate contract" >&2
echo $out >&2
exit $code
fi
sleep 10
txhash=$(echo $out | jq -r '.txhash')

PRE_UPGRADE_CONTRACT_ADDR=$($BINARY q tx $txhash -o json | jq -r '.raw_log' | jq -r '.[0].events[0].attributes[3].value')
export PRE_UPGRADE_CONTRACT_ADDR
2 changes: 1 addition & 1 deletion scripts/wasm/wasm-deploy.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh

BINARY=_build/old/terrad
CONTRACTPATH="scripts/wasm/old_cw721_base.wasm"
CONTRACTPATH="scripts/wasm/contracts/old_cw721_base.wasm"
KEYRING_BACKEND="test"
HOME=mytestnet
CHAIN_ID=localterra
Expand Down
1 change: 1 addition & 0 deletions scripts/wasm_command.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ADDITIONAL_PRE_SCRIPTS=scripts/wasm/wasm-deploy.sh bash scripts/upgrade-test.sh
ADDITIONAL_PRE_SCRIPTS=scripts/wasm/token-migration.sh ADDITIONAL_AFTER_SCRIPTS=scripts/wasm/token-migration-after.sh bash scripts/upgrade-test.sh
ADDITIONAL_PRE_SCRIPTS=scripts/wasm/wasm-deploy.sh ADDITIONAL_AFTER_SCRIPTS=scripts/wasm/wasm-tx-check.sh bash scripts/upgrade-test.sh
FORK=true ADDITIONAL_AFTER_SCRIPTS=scripts/wasm/stargate-after-upgrade.sh bash scripts/upgrade-test.sh
./_build/new/terrad tx wasm execute terra18vd8fpwxzck93qlwghaj6arh4p7c5n896xzem5 '{"mint":{"token_id":"'4'","owner":"'terra1p20jxrllewr5meecvhtmpddexr0kkz3tdename'"}}' --from test0 --output json --gas auto --gas-adjustment 2.3 --fees 20000000uluna --chain-id test --home mytestnet --keyring-backend test -y
Expand Down

0 comments on commit b8d0aab

Please sign in to comment.