This repository has been archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
swap: use the SimpleSwapFactory for deployment and verification
- Loading branch information
1 parent
5f00a9c
commit b066a40
Showing
17 changed files
with
2,599 additions
and
78 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
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
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,100 @@ | ||
package swap | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
"github.com/ethereum/go-ethereum/common" | ||
contractFactory "github.com/ethersphere/go-sw3/contracts-v0-1-1/simpleswapfactory" | ||
) | ||
|
||
var ( | ||
// ErrNotDeployedByFactory is given when a contract was not deployed by the factory | ||
ErrNotDeployedByFactory = errors.New("not deployed by factory") | ||
) | ||
|
||
type simpleSwapFactory struct { | ||
instance *contractFactory.SimpleSwapFactory | ||
address common.Address | ||
backend Backend | ||
} | ||
|
||
type SimpleSwapFactory interface { | ||
DeploySimpleSwap(auth *bind.TransactOpts, issuer common.Address, defaultHardDepositTimeoutDuration *big.Int) (Contract, error) | ||
VerifyContract(address common.Address) error | ||
VerifySelf() error | ||
} | ||
|
||
func FactoryAt(address common.Address, backend Backend) (SimpleSwapFactory, error) { | ||
simple, err := contractFactory.NewSimpleSwapFactory(address, backend) | ||
if err != nil { | ||
return nil, err | ||
} | ||
c := simpleSwapFactory{instance: simple, address: address, backend: backend} | ||
return c, err | ||
} | ||
|
||
func (sf simpleSwapFactory) VerifySelf() error { | ||
code, err := sf.backend.CodeAt(context.Background(), sf.address, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
referenceCode := common.FromHex(contractFactory.SimpleSwapFactoryDeployedCode) | ||
if !bytes.Equal(code, referenceCode) { | ||
return errors.New("not a valid factory contract") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (sf simpleSwapFactory) DeploySimpleSwap(auth *bind.TransactOpts, issuer common.Address, defaultHardDepositTimeoutDuration *big.Int) (Contract, error) { | ||
auth.GasLimit = 1700000 | ||
tx, err := sf.instance.DeploySimpleSwap(auth, issuer, defaultHardDepositTimeoutDuration) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
receipt, err := WaitFunc(auth, sf.backend, tx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
address := common.Address{} | ||
|
||
for _, log := range receipt.Logs { | ||
if log.Address != sf.address { | ||
continue | ||
} | ||
|
||
if event, err := sf.instance.ParseSimpleSwapDeployed(*log); err == nil { | ||
address = event.ContractAddress | ||
break | ||
} | ||
} | ||
|
||
if (address == common.Address{}) { | ||
return nil, errors.New("contract deployment failed") | ||
} | ||
|
||
simpleSwap, err := InstanceAt(address, sf.backend) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return simpleSwap, nil | ||
} | ||
|
||
func (sf simpleSwapFactory) VerifyContract(address common.Address) error { | ||
isDeployed, err := sf.instance.DeployedContracts(&bind.CallOpts{}, address) | ||
if err != nil { | ||
return err | ||
} | ||
if !isDeployed { | ||
return ErrNotDeployedByFactory | ||
} | ||
return nil | ||
} |
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
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
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
Oops, something went wrong.