Skip to content

Commit a07a5ff

Browse files
committed
add openwallet.Error
1 parent 91cdc96 commit a07a5ff

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

openwtester/transcation_assets_test.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func TestTransfer_TRX(t *testing.T) {
126126

127127
testGetAssetsAccountBalance(tm, walletID, accountID)
128128

129-
rawTx, err := testCreateTransactionStep(tm, walletID, accountID, to, "0.3", "", nil)
129+
rawTx, err := testCreateTransactionStep(tm, walletID, accountID, to, "0.03", "", nil)
130130
if err != nil {
131131
return
132132
}
@@ -157,29 +157,29 @@ func TestTransfer_TRC20(t *testing.T) {
157157
accountID := "4pF3jRC2XokaaLZWiiLvxXrD8SKRYNuzcVCFkJdu6rkt"
158158
to := "TRJJ9Mq4aMjdmKWpTDJAgbYNoY2P9Facg5"
159159

160-
//contract := openwallet.SmartContract{
161-
// Address: "THvZvKPLHKLJhEFYKiyqj6j8G8nGgfg7ur",
162-
// Symbol: "TRX",
163-
// Name: "TRONdice",
164-
// Token: "DICE",
165-
// Decimals: 6,
166-
// Protocol: "trc20",
167-
//}
168-
169160
contract := openwallet.SmartContract{
170-
Address: "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",
161+
Address: "THvZvKPLHKLJhEFYKiyqj6j8G8nGgfg7ur",
171162
Symbol: "TRX",
172-
Name: "Tether USD",
173-
Token: "USDT",
163+
Name: "TRONdice",
164+
Token: "DICE",
174165
Decimals: 6,
175166
Protocol: "trc20",
176167
}
177168

169+
//contract := openwallet.SmartContract{
170+
// Address: "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",
171+
// Symbol: "TRX",
172+
// Name: "Tether USD",
173+
// Token: "USDT",
174+
// Decimals: 6,
175+
// Protocol: "trc20",
176+
//}
177+
178178
testGetAssetsAccountBalance(tm, walletID, accountID)
179179

180180
testGetAssetsAccountTokenBalance(tm, walletID, accountID, contract)
181181

182-
rawTx, err := testCreateTransactionStep(tm, walletID, accountID, to, "1.5", "", &contract)
182+
rawTx, err := testCreateTransactionStep(tm, walletID, accountID, to, "9.5", "", &contract)
183183
if err != nil {
184184
return
185185
}

tron/tx_decode.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (decoder *TransactionDecoder) CreateSimpleTransaction(wrapper openwallet.Wa
100100
}
101101

102102
if len(addresses) == 0 {
103-
return fmt.Errorf("[%s] have not addresses", accountID)
103+
return openwallet.Errorf(openwallet.ErrAccountNotAddress,"[%s] have not addresses", accountID)
104104
}
105105

106106
searchAddrs := make([]string, 0)
@@ -163,7 +163,7 @@ func (decoder *TransactionDecoder) CreateSimpleTransaction(wrapper openwallet.Wa
163163
}
164164

165165
if findAddrBalance == nil {
166-
return fmt.Errorf("the balance: %s is not enough", amountStr)
166+
return openwallet.Errorf(openwallet.ErrInsufficientBalanceOfAccount, "the balance: %s is not enough", amountStr)
167167
}
168168

169169
//最后创建交易单
@@ -231,6 +231,10 @@ func (decoder *TransactionDecoder) CreateTokenTransaction(wrapper openwallet.Wal
231231
}
232232
})
233233

234+
tokenBalanceNotEnough := false
235+
balanceNotEnough := false
236+
errStr := ""
237+
234238
amountDec, _ := decimal.NewFromString(amountStr)
235239

236240
for _, addrBalance := range addrBalanceArray {
@@ -265,6 +269,7 @@ func (decoder *TransactionDecoder) CreateTokenTransaction(wrapper openwallet.Wal
265269

266270
//总消耗数量 = 转账数量 + 手续费
267271
if addrBalance_dec.LessThan(amountDec.Add(feeInfo.Fee)) {
272+
tokenBalanceNotEnough = true
268273
continue
269274
}
270275

@@ -273,7 +278,10 @@ func (decoder *TransactionDecoder) CreateTokenTransaction(wrapper openwallet.Wal
273278
//判断账户资源是否足够
274279
isEnoughEnegry, energyRest, feeMini := decoder.wm.IsEnoughEnergyToTransferTRC20(addrBalance.Balance.Address, trxBalance)
275280
if !isEnoughEnegry {
276-
return openwallet.Errorf(openwallet.ErrInsufficientFees, "address[%s] available energy: %d is less than feeMini: %d", addrBalance.Balance.Address, energyRest, feeMini)
281+
balanceNotEnough = true
282+
errStr = fmt.Sprintf("address[%s] available energy: %d is less than feeMini: %d", addrBalance.Balance.Address, energyRest, feeMini)
283+
continue
284+
277285
}
278286
}
279287

@@ -287,7 +295,12 @@ func (decoder *TransactionDecoder) CreateTokenTransaction(wrapper openwallet.Wal
287295
}
288296

289297
if findAddrBalance == nil {
290-
return fmt.Errorf("the balance: %s is not enough", amountStr)
298+
if tokenBalanceNotEnough {
299+
return openwallet.Errorf(openwallet.ErrInsufficientTokenBalanceOfAddress,"the balance: %s is not enough", amountStr)
300+
}
301+
if balanceNotEnough {
302+
return openwallet.Errorf(openwallet.ErrInsufficientFees, errStr)
303+
}
291304
}
292305

293306
//最后创建交易单

0 commit comments

Comments
 (0)