@@ -361,7 +361,7 @@ func (c *contractorV0) estimateInitGas(ctx context.Context, n int) (uint64, erro
361361func (c * contractorV0 ) estimateGas (ctx context.Context , value * big.Int , method string , args ... any ) (uint64 , error ) {
362362 data , err := c .abi .Pack (method , args ... )
363363 if err != nil {
364- return 0 , fmt .Errorf ("Pack error: %v" , err )
364+ return 0 , fmt .Errorf ("pack error: %v" , err )
365365 }
366366
367367 return c .cb .EstimateGas (ctx , ethereum.CallMsg {
@@ -424,34 +424,36 @@ func (c *contractorV0) outgoingValue(tx *types.Transaction) (swapped uint64) {
424424
425425// erc20Contractor supports the ERC20 ABI. Embedded in token contractors.
426426type erc20Contractor struct {
427- tokenContract * erc20.IERC20
428- acct common.Address
429- contract common.Address
427+ cb bind.ContractBackend
428+ tokenContract * erc20.IERC20
429+ tokenAddr common.Address
430+ acctAddr common.Address
431+ swapContractAddr common.Address
430432}
431433
432434// balance exposes the read-only balanceOf method of the erc20 token contract.
433435func (c * erc20Contractor ) balance (ctx context.Context ) (* big.Int , error ) {
434436 callOpts := & bind.CallOpts {
435- From : c .acct ,
437+ From : c .acctAddr ,
436438 Context : ctx ,
437439 }
438440
439- return c .tokenContract .BalanceOf (callOpts , c .acct )
441+ return c .tokenContract .BalanceOf (callOpts , c .acctAddr )
440442}
441443
442444// allowance exposes the read-only allowance method of the erc20 token contract.
443445func (c * erc20Contractor ) allowance (ctx context.Context ) (* big.Int , error ) {
444446 callOpts := & bind.CallOpts {
445- From : c .acct ,
447+ From : c .acctAddr ,
446448 Context : ctx ,
447449 }
448- return c .tokenContract .Allowance (callOpts , c .acct , c .contract )
450+ return c .tokenContract .Allowance (callOpts , c .acctAddr , c .swapContractAddr )
449451}
450452
451453// approve sends an approve transaction approving the linked contract to call
452454// transferFrom for the specified amount.
453455func (c * erc20Contractor ) approve (txOpts * bind.TransactOpts , amount * big.Int ) (* types.Transaction , error ) {
454- return c .tokenContract .Approve (txOpts , c .contract , amount )
456+ return c .tokenContract .Approve (txOpts , c .swapContractAddr , amount )
455457}
456458
457459// transfer calls the transfer method of the erc20 token contract. Used for
@@ -460,12 +462,22 @@ func (c *erc20Contractor) transfer(txOpts *bind.TransactOpts, addr common.Addres
460462 return c .tokenContract .Transfer (txOpts , addr , amount )
461463}
462464
465+ // estimateApproveGas estimates the gas needed to send an approve tx.
466+ func (c * erc20Contractor ) estimateApproveGas (ctx context.Context , amount * big.Int ) (uint64 , error ) {
467+ return estimateGas (ctx , c .acctAddr , c .tokenAddr , erc20 .ERC20ABI , c .cb , new (big.Int ), "approve" , c .swapContractAddr , amount )
468+ }
469+
470+ // estimateTransferGas estimates the gas needed for a transfer tx. The account
471+ // needs to have > amount tokens to use this method.
472+ func (c * erc20Contractor ) estimateTransferGas (ctx context.Context , amount * big.Int ) (uint64 , error ) {
473+ return estimateGas (ctx , c .acctAddr , c .swapContractAddr , erc20 .ERC20ABI , c .cb , new (big.Int ), "transfer" , c .acctAddr , amount )
474+ }
475+
463476// tokenContractorV0 is a contractor that implements the tokenContractor
464477// methods, providing access to the methods of the token's ERC20 contract.
465478type tokenContractorV0 struct {
466479 * contractorV0
467480 * erc20Contractor
468- tokenAddr common.Address
469481}
470482
471483var _ contractor = (* tokenContractorV0 )(nil )
@@ -514,32 +526,17 @@ func newV0TokenContractor(net dex.Network, token *dexeth.Token, acctAddr common.
514526 evmify : token .AtomicToEVM ,
515527 atomize : token .EVMToAtomic ,
516528 },
517- tokenAddr : tokenAddr ,
529+
518530 erc20Contractor : & erc20Contractor {
519- tokenContract : tokenContract ,
520- acct : acctAddr ,
521- contract : swapContractAddr ,
531+ cb : cb ,
532+ tokenContract : tokenContract ,
533+ tokenAddr : tokenAddr ,
534+ acctAddr : acctAddr ,
535+ swapContractAddr : swapContractAddr ,
522536 },
523537 }, nil
524538}
525539
526- // estimateApproveGas estimates the gas needed to send an approve tx.
527- func (c * tokenContractorV0 ) estimateApproveGas (ctx context.Context , amount * big.Int ) (uint64 , error ) {
528- return c .estimateGas (ctx , "approve" , c .contractAddr , amount )
529- }
530-
531- // estimateTransferGas esimates the gas needed for a transfer tx. The account
532- // needs to have > amount tokens to use this method.
533- func (c * tokenContractorV0 ) estimateTransferGas (ctx context.Context , amount * big.Int ) (uint64 , error ) {
534- return c .estimateGas (ctx , "transfer" , c .acctAddr , amount )
535- }
536-
537- // estimateGas estimates the gas needed for methods on the ERC20 token contract.
538- // For estimating methods on the swap contract, use (contractorV0).estimateGas.
539- func (c * tokenContractorV0 ) estimateGas (ctx context.Context , method string , args ... interface {}) (uint64 , error ) {
540- return estimateGas (ctx , c .acctAddr , c .contractAddr , c .abi , c .cb , new (big.Int ), method , args ... )
541- }
542-
543540// value finds incoming or outgoing value for the tx to either the swap contract
544541// or the erc20 token contract. For the token contract, only transfer and
545542// transferFrom are parsed. It is not an error if this tx is a call to another
@@ -555,7 +552,7 @@ func (c *tokenContractorV0) value(ctx context.Context, tx *types.Transaction) (i
555552
556553 // Consider removing. We'll never be sending transferFrom transactions
557554 // directly.
558- if sender , _ , value , err := erc20 .ParseTransferFromData (tx .Data ()); err == nil && sender == c .acctAddr {
555+ if sender , _ , value , err := erc20 .ParseTransferFromData (tx .Data ()); err == nil && sender == c .contractorV0 . acctAddr {
559556 return 0 , c .atomize (value ), nil
560557 }
561558
@@ -827,7 +824,6 @@ func (c *contractorV1) value(ctx context.Context, tx *types.Transaction) (in, ou
827824type tokenContractorV1 struct {
828825 * contractorV1
829826 * erc20Contractor
830- tokenAddr common.Address
831827}
832828
833829func newV1TokenContractor (net dex.Network , token * dexeth.Token , acctAddr common.Address , cb bind.ContractBackend ) (tokenContractor , error ) {
@@ -871,30 +867,17 @@ func newV1TokenContractor(net dex.Network, token *dexeth.Token, acctAddr common.
871867 evmify : token .AtomicToEVM ,
872868 atomize : token .EVMToAtomic ,
873869 },
874- tokenAddr : tokenAddr ,
870+
875871 erc20Contractor : & erc20Contractor {
876- tokenContract : tokenContract ,
877- acct : acctAddr ,
878- contract : swapContractAddr ,
872+ cb : cb ,
873+ tokenContract : tokenContract ,
874+ tokenAddr : tokenAddr ,
875+ acctAddr : acctAddr ,
876+ swapContractAddr : swapContractAddr ,
879877 },
880878 }, nil
881879}
882880
883- // estimateApproveGas estimates the gas needed to send an approve tx.
884- func (c * tokenContractorV1 ) estimateApproveGas (ctx context.Context , amount * big.Int ) (uint64 , error ) {
885- return c .estimateTokenContractGas (ctx , "approve" , c .contractAddr , amount )
886- }
887-
888- // estimateTransferGas estimates the gas needed for a transfer tx. The account
889- // needs to have > amount tokens to use this method.
890- func (c * tokenContractorV1 ) estimateTransferGas (ctx context.Context , amount * big.Int ) (uint64 , error ) {
891- return c .estimateTokenContractGas (ctx , "transfer" , c .acctAddr , amount )
892- }
893-
894- func (c * tokenContractorV1 ) estimateTokenContractGas (ctx context.Context , method string , args ... interface {}) (uint64 , error ) {
895- return estimateGas (ctx , c .acctAddr , c .tokenAddr , erc20 .ERC20ABI , c .cb , new (big.Int ), method , args ... )
896- }
897-
898881// value finds incoming or outgoing value for the tx to either the swap contract
899882// or the erc20 token contract. For the token contract, only transfer and
900883// transferFrom are parsed. It is not an error if this tx is a call to another
@@ -910,7 +893,7 @@ func (c *tokenContractorV1) value(ctx context.Context, tx *types.Transaction) (i
910893
911894 // Consider removing. We'll never be sending transferFrom transactions
912895 // directly.
913- if sender , _ , value , err := erc20 .ParseTransferFromData (tx .Data ()); err == nil && sender == c .acctAddr {
896+ if sender , _ , value , err := erc20 .ParseTransferFromData (tx .Data ()); err == nil && sender == c .contractorV1 . acctAddr {
914897 return 0 , c .atomize (value ), nil
915898 }
916899
0 commit comments