Skip to content

Commit 12cbfdf

Browse files
committed
wip: refactoring createAssetTx builder
1 parent a759c24 commit 12cbfdf

File tree

3 files changed

+272
-53
lines changed

3 files changed

+272
-53
lines changed

vms/avm/service.go

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -753,32 +753,6 @@ func (s *Service) buildCreateAssetTx(args *CreateAssetArgs) (*txs.Tx, ids.ShortI
753753
return nil, ids.ShortEmpty, err
754754
}
755755

756-
amountsSpent, ins, keys, err := s.vm.Spend(
757-
utxos,
758-
kc,
759-
map[ids.ID]uint64{
760-
s.vm.feeAssetID: s.vm.CreateAssetTxFee,
761-
},
762-
)
763-
if err != nil {
764-
return nil, ids.ShortEmpty, err
765-
}
766-
767-
outs := []*avax.TransferableOutput{}
768-
if amountSpent := amountsSpent[s.vm.feeAssetID]; amountSpent > s.vm.CreateAssetTxFee {
769-
outs = append(outs, &avax.TransferableOutput{
770-
Asset: avax.Asset{ID: s.vm.feeAssetID},
771-
Out: &secp256k1fx.TransferOutput{
772-
Amt: amountSpent - s.vm.CreateAssetTxFee,
773-
OutputOwners: secp256k1fx.OutputOwners{
774-
Locktime: 0,
775-
Threshold: 1,
776-
Addrs: []ids.ShortID{changeAddr},
777-
},
778-
},
779-
})
780-
}
781-
782756
initialState := &txs.InitialState{
783757
FxIndex: 0, // TODO: Should lookup secp256k1fx FxID
784758
Outs: make([]verify.State, 0, len(args.InitialHolders)+len(args.MinterSets)),
@@ -815,19 +789,55 @@ func (s *Service) buildCreateAssetTx(args *CreateAssetArgs) (*txs.Tx, ids.ShortI
815789
codec := s.vm.parser.Codec()
816790
initialState.Sort(codec)
817791

818-
tx := &txs.Tx{Unsigned: &txs.CreateAssetTx{
792+
return buildCreateAssetTx(
793+
s.vm,
794+
args.Name,
795+
args.Symbol,
796+
args.Denomination,
797+
[]*txs.InitialState{initialState},
798+
utxos,
799+
kc,
800+
changeAddr,
801+
)
802+
}
803+
804+
func buildCreateAssetTx(
805+
vm *VM,
806+
name, symbol string,
807+
denomination byte,
808+
initialStates []*txs.InitialState,
809+
utxos []*avax.UTXO,
810+
kc *secp256k1fx.Keychain,
811+
changeAddr ids.ShortID,
812+
) (*txs.Tx, ids.ShortID, error) {
813+
uTx := &txs.CreateAssetTx{
819814
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
820-
NetworkID: s.vm.ctx.NetworkID,
821-
BlockchainID: s.vm.ctx.ChainID,
822-
Outs: outs,
823-
Ins: ins,
815+
NetworkID: vm.ctx.NetworkID,
816+
BlockchainID: vm.ctx.ChainID,
824817
}},
825-
Name: args.Name,
826-
Symbol: args.Symbol,
827-
Denomination: args.Denomination,
828-
States: []*txs.InitialState{initialState},
829-
}}
830-
return tx, changeAddr, tx.SignSECP256K1Fx(codec, keys)
818+
Name: name,
819+
Symbol: symbol,
820+
Denomination: denomination,
821+
States: initialStates,
822+
}
823+
824+
toBurn := map[ids.ID]uint64{
825+
vm.feeAssetID: vm.CreateAssetTxFee,
826+
}
827+
ins, outs, keys, err := vm.NewSpend(
828+
utxos,
829+
kc,
830+
toBurn,
831+
changeAddr,
832+
)
833+
if err != nil {
834+
return nil, ids.ShortEmpty, err
835+
}
836+
837+
uTx.Ins = ins
838+
uTx.Outs = outs
839+
tx := &txs.Tx{Unsigned: uTx}
840+
return tx, changeAddr, tx.SignSECP256K1Fx(vm.parser.Codec(), keys)
831841
}
832842

833843
// CreateFixedCapAsset returns ID of the newly created asset

vms/avm/service_test.go

Lines changed: 122 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,10 @@ func TestServiceGetTxJSON_CreateAssetTx(t *testing.T) {
699699
require := require.New(t)
700700

701701
env := setup(t, &envConfig{
702-
vmStaticConfig: &config.Config{},
702+
vmStaticConfig: &config.Config{
703+
CreateAssetTxFee: testTxFee,
704+
DurangoTime: time.Time{},
705+
},
703706
additionalFxs: []*common.Fx{{
704707
ID: propertyfx.ID,
705708
Fx: &propertyfx.Fx{},
@@ -712,7 +715,7 @@ func TestServiceGetTxJSON_CreateAssetTx(t *testing.T) {
712715
env.vm.ctx.Lock.Unlock()
713716
}()
714717

715-
createAssetTx := newAvaxCreateAssetTxWithOutputs(t, env.vm.ctx.ChainID, env.vm.parser)
718+
createAssetTx := newAvaxCreateAssetTxWithOutputs(t, env)
716719
issueAndAccept(require, env.vm, env.issuer, createAssetTx)
717720

718721
reply := api.GetTxReply{}
@@ -730,8 +733,34 @@ func TestServiceGetTxJSON_CreateAssetTx(t *testing.T) {
730733
"unsignedTx": {
731734
"networkID": 10,
732735
"blockchainID": "PLACEHOLDER_BLOCKCHAIN_ID",
733-
"outputs": null,
734-
"inputs": null,
736+
"outputs": [
737+
{
738+
"assetID": "2XGxUr7VF7j1iwUp2aiGe4b6Ue2yyNghNS1SuNTNmZ77dPpXFZ",
739+
"fxID": "spdxUxVJQbX85MGxMHbKw1sHxMnSqJ3QBzDyDYEP3h6TLuxqQ",
740+
"output": {
741+
"addresses": [
742+
"X-testing1lnk637g0edwnqc2tn8tel39652fswa3xk4r65e"
743+
],
744+
"amount": 49000,
745+
"locktime": 0,
746+
"threshold": 1
747+
}
748+
}
749+
],
750+
"inputs": [
751+
{
752+
"txID": "2XGxUr7VF7j1iwUp2aiGe4b6Ue2yyNghNS1SuNTNmZ77dPpXFZ",
753+
"outputIndex": 2,
754+
"assetID": "2XGxUr7VF7j1iwUp2aiGe4b6Ue2yyNghNS1SuNTNmZ77dPpXFZ",
755+
"fxID": "spdxUxVJQbX85MGxMHbKw1sHxMnSqJ3QBzDyDYEP3h6TLuxqQ",
756+
"input": {
757+
"amount": 50000,
758+
"signatureIndices": [
759+
0
760+
]
761+
}
762+
}
763+
],
735764
"memo": "0x",
736765
"name": "Team Rocket",
737766
"symbol": "TR",
@@ -801,7 +830,16 @@ func TestServiceGetTxJSON_CreateAssetTx(t *testing.T) {
801830
}
802831
]
803832
},
804-
"credentials": null,
833+
"credentials": [
834+
{
835+
"fxID": "spdxUxVJQbX85MGxMHbKw1sHxMnSqJ3QBzDyDYEP3h6TLuxqQ",
836+
"credential": {
837+
"signatures": [
838+
"0xa036832b708b377ef9719ffbc834a3fb933191a467994f844c23f4a44350e86a7151cdbbbfa479d93a4906cc22254dcc5ccc2a6099492e7a9f9e8b575048d69701"
839+
]
840+
}
841+
}
842+
],
805843
"id": "PLACEHOLDER_TX_ID"
806844
}`
807845

@@ -829,7 +867,7 @@ func TestServiceGetTxJSON_OperationTxWithNftxMintOp(t *testing.T) {
829867
}()
830868

831869
key := keys[0]
832-
createAssetTx := newAvaxCreateAssetTxWithOutputs(t, env.vm.ctx.ChainID, env.vm.parser)
870+
createAssetTx := newAvaxCreateAssetTxWithOutputs(t, env)
833871
issueAndAccept(require, env.vm, env.issuer, createAssetTx)
834872

835873
mintNFTTx := buildOperationTxWithOp(env.vm.ctx.ChainID, buildNFTxMintOp(createAssetTx, key, 2, 1))
@@ -928,7 +966,7 @@ func TestServiceGetTxJSON_OperationTxWithMultipleNftxMintOp(t *testing.T) {
928966
}()
929967

930968
key := keys[0]
931-
createAssetTx := newAvaxCreateAssetTxWithOutputs(t, env.vm.ctx.ChainID, env.vm.parser)
969+
createAssetTx := newAvaxCreateAssetTxWithOutputs(t, env)
932970
issueAndAccept(require, env.vm, env.issuer, createAssetTx)
933971

934972
mintOp1 := buildNFTxMintOp(createAssetTx, key, 2, 1)
@@ -1066,7 +1104,7 @@ func TestServiceGetTxJSON_OperationTxWithSecpMintOp(t *testing.T) {
10661104
}()
10671105

10681106
key := keys[0]
1069-
createAssetTx := newAvaxCreateAssetTxWithOutputs(t, env.vm.ctx.ChainID, env.vm.parser)
1107+
createAssetTx := newAvaxCreateAssetTxWithOutputs(t, env)
10701108
issueAndAccept(require, env.vm, env.issuer, createAssetTx)
10711109

10721110
mintSecpOpTx := buildOperationTxWithOp(env.vm.ctx.ChainID, buildSecpMintOp(createAssetTx, key, 0))
@@ -1169,7 +1207,7 @@ func TestServiceGetTxJSON_OperationTxWithMultipleSecpMintOp(t *testing.T) {
11691207
}()
11701208

11711209
key := keys[0]
1172-
createAssetTx := newAvaxCreateAssetTxWithOutputs(t, env.vm.ctx.ChainID, env.vm.parser)
1210+
createAssetTx := newAvaxCreateAssetTxWithOutputs(t, env)
11731211
issueAndAccept(require, env.vm, env.issuer, createAssetTx)
11741212

11751213
op1 := buildSecpMintOp(createAssetTx, key, 0)
@@ -1315,7 +1353,7 @@ func TestServiceGetTxJSON_OperationTxWithPropertyFxMintOp(t *testing.T) {
13151353
}()
13161354

13171355
key := keys[0]
1318-
createAssetTx := newAvaxCreateAssetTxWithOutputs(t, env.vm.ctx.ChainID, env.vm.parser)
1356+
createAssetTx := newAvaxCreateAssetTxWithOutputs(t, env)
13191357
issueAndAccept(require, env.vm, env.issuer, createAssetTx)
13201358

13211359
mintPropertyFxOpTx := buildOperationTxWithOp(env.vm.ctx.ChainID, buildPropertyFxMintOp(createAssetTx, key, 4))
@@ -1415,7 +1453,7 @@ func TestServiceGetTxJSON_OperationTxWithPropertyFxMintOpMultiple(t *testing.T)
14151453
}()
14161454

14171455
key := keys[0]
1418-
createAssetTx := newAvaxCreateAssetTxWithOutputs(t, env.vm.ctx.ChainID, env.vm.parser)
1456+
createAssetTx := newAvaxCreateAssetTxWithOutputs(t, env)
14191457
issueAndAccept(require, env.vm, env.issuer, createAssetTx)
14201458

14211459
op1 := buildPropertyFxMintOp(createAssetTx, key, 4)
@@ -1553,10 +1591,78 @@ func newAvaxExportTxWithOutputs(t *testing.T, genesisBytes []byte, chainID ids.I
15531591
return tx
15541592
}
15551593

1556-
func newAvaxCreateAssetTxWithOutputs(t *testing.T, chainID ids.ID, parser txs.Parser) *txs.Tx {
1557-
key := keys[0]
1558-
tx := buildCreateAssetTx(chainID, key)
1559-
require.NoError(t, tx.Initialize(parser.Codec()))
1594+
func newAvaxCreateAssetTxWithOutputs(t *testing.T, env *environment) *txs.Tx {
1595+
var (
1596+
key = keys[0]
1597+
kc = secp256k1fx.NewKeychain()
1598+
)
1599+
kc.Add(key)
1600+
utxos, err := avax.GetAllUTXOs(env.vm.state, kc.Addresses())
1601+
require.NoError(t, err)
1602+
tx, _, err := buildCreateAssetTx(
1603+
env.vm,
1604+
"Team Rocket", // name
1605+
"TR", // symbol
1606+
0, // denomination
1607+
[]*txs.InitialState{
1608+
{
1609+
FxIndex: 0,
1610+
Outs: []verify.State{
1611+
&secp256k1fx.MintOutput{
1612+
OutputOwners: secp256k1fx.OutputOwners{
1613+
Threshold: 1,
1614+
Addrs: []ids.ShortID{key.PublicKey().Address()},
1615+
},
1616+
}, &secp256k1fx.MintOutput{
1617+
OutputOwners: secp256k1fx.OutputOwners{
1618+
Threshold: 1,
1619+
Addrs: []ids.ShortID{key.PublicKey().Address()},
1620+
},
1621+
},
1622+
},
1623+
},
1624+
{
1625+
FxIndex: 1,
1626+
Outs: []verify.State{
1627+
&nftfx.MintOutput{
1628+
GroupID: 1,
1629+
OutputOwners: secp256k1fx.OutputOwners{
1630+
Threshold: 1,
1631+
Addrs: []ids.ShortID{key.PublicKey().Address()},
1632+
},
1633+
},
1634+
&nftfx.MintOutput{
1635+
GroupID: 2,
1636+
OutputOwners: secp256k1fx.OutputOwners{
1637+
Threshold: 1,
1638+
Addrs: []ids.ShortID{key.PublicKey().Address()},
1639+
},
1640+
},
1641+
},
1642+
},
1643+
{
1644+
FxIndex: 2,
1645+
Outs: []verify.State{
1646+
&propertyfx.MintOutput{
1647+
OutputOwners: secp256k1fx.OutputOwners{
1648+
Threshold: 1,
1649+
Addrs: []ids.ShortID{keys[0].PublicKey().Address()},
1650+
},
1651+
},
1652+
&propertyfx.MintOutput{
1653+
OutputOwners: secp256k1fx.OutputOwners{
1654+
Threshold: 1,
1655+
Addrs: []ids.ShortID{keys[0].PublicKey().Address()},
1656+
},
1657+
},
1658+
},
1659+
},
1660+
},
1661+
utxos,
1662+
kc,
1663+
key.Address(),
1664+
)
1665+
require.NoError(t, err)
15601666
return tx
15611667
}
15621668

@@ -1628,7 +1734,7 @@ func buildExportTx(avaxTx *txs.Tx, chainID ids.ID, fee uint64, key *secp256k1.Pr
16281734
}}
16291735
}
16301736

1631-
func buildCreateAssetTx(chainID ids.ID, key *secp256k1.PrivateKey) *txs.Tx {
1737+
func buildTestCreateAssetTx(chainID ids.ID, key *secp256k1.PrivateKey) *txs.Tx {
16321738
return &txs.Tx{Unsigned: &txs.CreateAssetTx{
16331739
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
16341740
NetworkID: constants.UnitTestID,

0 commit comments

Comments
 (0)