-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
136 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package itest | ||
|
||
import ( | ||
"context" | ||
|
||
taprootassets "github.com/lightninglabs/taproot-assets" | ||
"github.com/lightninglabs/taproot-assets/address" | ||
"github.com/lightninglabs/taproot-assets/tapfreighter" | ||
"github.com/lightninglabs/taproot-assets/tappsbt" | ||
"github.com/lightninglabs/taproot-assets/taprpc" | ||
wrpc "github.com/lightninglabs/taproot-assets/taprpc/assetwalletrpc" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// testBurnAssets tests that we're able to mint assets and then burn assets | ||
// again. | ||
func testBurnAssets(t *harnessTest) { | ||
rpcSimpleAssets := mintAssetsConfirmBatch(t, t.tapd, simpleAssets) | ||
|
||
// We first fan out the assets we have to different outputs. | ||
var ( | ||
chainParams = &address.RegressionNetTap | ||
simpleAsset = rpcSimpleAssets[0] | ||
genInfo = simpleAsset.AssetGenesis | ||
id [32]byte | ||
) | ||
copy(id[:], genInfo.AssetId) | ||
|
||
ctxb := context.Background() | ||
ctxt, cancel := context.WithTimeout(ctxb, defaultWaitTimeout) | ||
defer cancel() | ||
|
||
// We need to derive two sets of keys, one for the new script key and | ||
// one for the internal key each. | ||
scriptKey1, anchorInternalKeyDesc1 := deriveKeys(t.t, t.tapd) | ||
scriptKey2, anchorInternalKeyDesc2 := deriveKeys(t.t, t.tapd) | ||
scriptKey3, anchorInternalKeyDesc3 := deriveKeys(t.t, t.tapd) | ||
scriptKey4, _ := deriveKeys(t.t, t.tapd) | ||
|
||
// We create the following outputs: | ||
// anchor index 0 (anchor internal key 1): | ||
// - 1200 units to scriptKey1 | ||
// - 1300 units to scriptKey2 | ||
// anchor index 1 (anchor internal key 2): | ||
// - 1400 units to scriptKey3 | ||
// anchor index 2 (anchor internal key 3): | ||
// - 800 units to scriptKey4 | ||
// anchor index 3 (automatic change output): | ||
// - 300 units to new script key | ||
outputAmounts := []uint64{1200, 1300, 1400, 800, 300} | ||
vPkt := tappsbt.ForInteractiveSend( | ||
id, outputAmounts[0], scriptKey1, 0, anchorInternalKeyDesc1, | ||
chainParams, | ||
) | ||
tappsbt.AddOutput( | ||
vPkt, outputAmounts[1], scriptKey2, 0, anchorInternalKeyDesc1, | ||
) | ||
tappsbt.AddOutput( | ||
vPkt, outputAmounts[2], scriptKey3, 1, anchorInternalKeyDesc2, | ||
) | ||
tappsbt.AddOutput( | ||
vPkt, outputAmounts[3], scriptKey4, 2, anchorInternalKeyDesc3, | ||
) | ||
|
||
// Next, we'll attempt to complete a transfer with PSBTs from our node | ||
// to ourselves, using the partial amount. | ||
fundResp := fundPacket(t, t.tapd, vPkt) | ||
signResp, err := t.tapd.SignVirtualPsbt( | ||
ctxt, &wrpc.SignVirtualPsbtRequest{ | ||
FundedPsbt: fundResp.FundedPsbt, | ||
}, | ||
) | ||
require.NoError(t.t, err) | ||
|
||
// Now we'll attempt to complete the transfer. | ||
sendResp, err := t.tapd.AnchorVirtualPsbts( | ||
ctxt, &wrpc.AnchorVirtualPsbtsRequest{ | ||
VirtualPsbts: [][]byte{signResp.SignedPsbt}, | ||
}, | ||
) | ||
require.NoError(t.t, err) | ||
|
||
// We end up with a transfer with 5 outputs: 2 grouped into the first | ||
// anchor output and then 3 each in their own output. So there are 4 BTC | ||
// anchor outputs but 5 asset transfer outputs. | ||
numOutputs := 5 | ||
confirmAndAssetOutboundTransferWithOutputs( | ||
t, t.tapd, sendResp, genInfo.AssetId, outputAmounts, 0, 1, | ||
numOutputs, | ||
) | ||
|
||
// We'll now try to the exact amount of the largest output, which should | ||
// still select exactly that one largest output, which is located alone | ||
// in an anchor output. When attempting to burn this, we should get an | ||
// error saying that we cannot completely burn all assets in an output. | ||
_, err = t.tapd.BurnAsset(ctxt, &taprpc.BurnAssetRequest{ | ||
Asset: &taprpc.BurnAssetRequest_AssetId{ | ||
AssetId: id[:], | ||
}, | ||
AmountToBurn: outputAmounts[2], | ||
ConfirmationText: taprootassets.AssetBurnConfirmationText, | ||
}) | ||
require.ErrorContains( | ||
t.t, err, tapfreighter.ErrFullBurnNotSupported.Error(), | ||
) | ||
|
||
// We'll now try to burn a small amount of assets, which should select | ||
// the largest output, which is located alone in an anchor output. | ||
const burnAmt = 100 | ||
burnResp, err := t.tapd.BurnAsset(ctxt, &taprpc.BurnAssetRequest{ | ||
Asset: &taprpc.BurnAssetRequest_AssetId{ | ||
AssetId: id[:], | ||
}, | ||
AmountToBurn: burnAmt, | ||
ConfirmationText: taprootassets.AssetBurnConfirmationText, | ||
}) | ||
require.NoError(t.t, err) | ||
|
||
burnRespJSON, err := formatProtoJSON(burnResp) | ||
require.NoError(t.t, err) | ||
t.Logf("Got response from burning assets: %v", burnRespJSON) | ||
|
||
assertAssetOutboundTransferWithOutputs( | ||
t, t.tapd, burnResp.BurnTransfer, genInfo.AssetId, | ||
[]uint64{outputAmounts[2] - burnAmt, burnAmt}, 1, 2, 2, true, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters