-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Pixura/migration_scripts
Migration scripts
- Loading branch information
Showing
12 changed files
with
3,473 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ node_modules | |
output/ | ||
.psc-ide-port | ||
.purs-repl | ||
**/__compiler/ | ||
**/__compiler/ | ||
deploy-configs/ |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
107 changes: 107 additions & 0 deletions
107
purs-contracts/src/Migrations/SuperRareMarketAuctionV2.purs
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,107 @@ | ||
module Migrations.SuperRareMarketAuctionV2 where | ||
|
||
import Prelude | ||
import Chanterelle.Internal.Logging (LogLevel(..), log) | ||
import Chanterelle.Internal.Types (DeployConfig(..)) | ||
import Contracts.V5.SuperRareMarketAuctionV2 (markTokensAsSold) | ||
import Control.Monad.Reader (ask) | ||
import Data.Array (catMaybes, drop, nub, take, (:)) | ||
import Data.Either (Either(..), either) | ||
import Data.Lens ((?~)) | ||
import Data.Maybe (Maybe(..), fromMaybe) | ||
import Data.Traversable (for) | ||
import Deploy.Contracts.SuperRareMarketAuctionV2 (deployScriptWithGasSettings) | ||
import Deploy.Utils (GasSettings, awaitTxSuccessWeb3, txOptsWithGasSettings) | ||
import Effect (Effect) | ||
import Effect.Aff (joinFiber, launchAff, runAff_) | ||
import Effect.Aff.Class (class MonadAff, liftAff) | ||
import Effect.Class (liftEffect) | ||
import Effect.Exception (throw, throwException) | ||
import Migrations.Utils (emptyGasSettings, runMigration) | ||
import Network.Ethereum.Web3 (Address, UIntN, _from, _to, embed, runWeb3, uIntNFromBigNumber) | ||
import Network.Ethereum.Web3.Solidity.Sizes (S256, s256) | ||
import Simple.Graphql.Query (runQuery) | ||
import Simple.Graphql.Types (GraphQlQuery(..), runQueryT) | ||
|
||
type MigrationArgs | ||
= { superRareV2ContractAddress :: Address | ||
, pixuraApi :: { url :: String, apiKey :: String } | ||
} | ||
|
||
main :: Effect Unit | ||
main = | ||
runAff_ (either throwException (const $ log Info "Completed Migration")) | ||
$ runMigration \(args :: { gasSettings :: Maybe GasSettings, migrationArgs :: MigrationArgs }) -> do | ||
DeployConfig { provider, primaryAccount } <- ask | ||
let | ||
{ migrationArgs | ||
, gasSettings: mgs | ||
} = args | ||
|
||
{ superRareV2ContractAddress: _originContract | ||
, pixuraApi: { url, apiKey } | ||
} = migrationArgs | ||
|
||
gasSettings = fromMaybe emptyGasSettings mgs | ||
|
||
txOpts = txOptsWithGasSettings gasSettings # _from ?~ primaryAccount | ||
fibTokens <- liftEffect $ launchAff (lookUpSoldTokens { tokenContract: _originContract, url, apiKey }) | ||
{ superRareMarketAuctionV2: { deployAddress } } <- deployScriptWithGasSettings gasSettings | ||
res <- | ||
liftAff | ||
$ runWeb3 provider do | ||
soldTokens <- liftAff $ joinFiber fibTokens | ||
for (chunk 100 soldTokens) \_tokenIds -> do | ||
txHash <- | ||
markTokensAsSold | ||
(txOpts # _to ?~ deployAddress) | ||
{ _originContract, _tokenIds } | ||
log Info $ "Batch marking tokens sold: " <> show _tokenIds | ||
log Info $ "Polling for markTokensAsSold transaction receipt: " <> show txHash | ||
awaitTxSuccessWeb3 txHash | ||
case res of | ||
Left err -> liftEffect $ throw $ show err | ||
_ -> pure unit | ||
where | ||
chunk n [] = [] | ||
|
||
chunk n xs = take n xs : chunk n (drop n xs) | ||
|
||
type LookUpSoldTokensRes | ||
= { allNonFungibleTokenEvents :: | ||
{ nodes :: Array { tokenId :: Int } | ||
} | ||
} | ||
|
||
lookUpSoldTokens :: | ||
forall m. | ||
MonadAff m => | ||
{ tokenContract :: Address, url :: String, apiKey :: String } -> m (Array (UIntN S256)) | ||
lookUpSoldTokens { tokenContract, url, apiKey } = do | ||
res <- liftAff $ runQueryT (runQuery url (Just apiKey) gqlQuery) | ||
case res of | ||
{ data: Nothing, errors: Nothing } -> liftEffect $ throw $ "No response for query \n" <> show gqlQuery | ||
{ data: Nothing, errors: Just err } -> liftEffect $ throw $ show err | ||
{ data: Just { allNonFungibleTokenEvents: { nodes } } } -> | ||
pure $ nub $ catMaybes | ||
$ nodes | ||
<#> \{ tokenId } -> uIntNFromBigNumber s256 (embed tokenId) | ||
where | ||
gqlQuery :: GraphQlQuery { contractAddress :: Address } LookUpSoldTokensRes | ||
gqlQuery = GraphQlQuery { query, variables: { contractAddress: tokenContract } } | ||
|
||
query = | ||
""" | ||
query getNft($contractAddress: String!) { | ||
allNonFungibleTokenEvents( | ||
orderBy: TIMESTAMP_DESC | ||
condition: { tokenContractAddress: $contractAddress } | ||
filter: { nftEventType: { in: [SALE, ACCEPT_BID] } } | ||
) { | ||
totalCount | ||
nodes { | ||
tokenId | ||
} | ||
} | ||
} | ||
""" |
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,60 @@ | ||
module Migrations.Utils where | ||
|
||
import Prelude | ||
import Chanterelle.Deploy (deployWithProvider) | ||
import Chanterelle.Internal.Types (DeployM) | ||
import Data.Either (Either(..)) | ||
import Data.Maybe (Maybe(..), maybe) | ||
import Data.Nullable (null) | ||
import Deploy.Utils (GasSettings(..)) | ||
import Effect (Effect) | ||
import Effect.Aff (Aff) | ||
import Effect.Class (liftEffect) | ||
import Effect.Exception (throw) | ||
import Network.Ethereum.Web3 (Provider, httpProvider) | ||
import Network.Ethereum.Web3.Types.HdWalletProvider (hdWalletProvider, unHdWalletProvider) | ||
import Node.Encoding (Encoding(..)) | ||
import Node.FS.Aff (readTextFile) | ||
import Node.Process (lookupEnv) | ||
import Simple.JSON as JSON | ||
|
||
runMigration :: | ||
forall a b. | ||
JSON.ReadForeign b => | ||
( { migrationArgs :: b | ||
, gasSettings :: | ||
Maybe GasSettings | ||
} -> | ||
DeployM a | ||
) -> | ||
Aff a | ||
runMigration migration = do | ||
config@{ migrationArgs, gasSettings } <- loadMigrationConfig | ||
provider <- liftEffect $ mkProvider config | ||
deployWithProvider provider (60 * 1000) (migration { migrationArgs, gasSettings }) | ||
|
||
loadMigrationConfig :: forall a. JSON.ReadForeign a => Aff (MigrationConfig a) | ||
loadMigrationConfig = do | ||
cfgPath <- maybe (liftEffect $ throw "`CONFIG` environment variable not found") pure =<< (liftEffect $ lookupEnv "CONFIG") | ||
contents <- readTextFile UTF8 cfgPath | ||
case JSON.readJSON contents of | ||
Left err -> liftEffect $ throw $ show err | ||
Right a -> pure a | ||
|
||
emptyGasSettings :: GasSettings | ||
emptyGasSettings = GasSettings { gasPrice: Nothing, gasLimit: Nothing } | ||
|
||
mkProvider :: forall a. (MigrationConfig (a)) -> Effect Provider | ||
mkProvider cfg@{ rpcUrl } = | ||
liftEffect case cfg of | ||
{ mnemonic: Just mnemonic } -> | ||
unHdWalletProvider | ||
<$> hdWalletProvider { mnemonic, rpc: rpcUrl, path: null, numberOfAccounts: null } | ||
_ -> httpProvider rpcUrl | ||
|
||
type MigrationConfig a | ||
= { rpcUrl :: String | ||
, mnemonic :: Maybe String | ||
, gasSettings :: Maybe GasSettings | ||
, migrationArgs :: a | ||
} |
Oops, something went wrong.