import { getConstant , getSDK } from '@cardstack/cardpay-sdk' ; import { getWeb3 } from './utils' ; import { RewardProgramInfo } from '@cardstack/cardpay-sdk' ; import { fromWei } from 'web3-utils' ; export async function registerRewardProgram ( network , prepaidCard , admin , mnemonic ? ) { let web3 = await getWeb3 ( network , mnemonic ) ; let rewardManagerAPI = await getSDK ( 'RewardManager' , web3 ) ; let blockExplorer = await getConstant ( 'blockExplorer' , web3 ) ; let { rewardProgramId } = await rewardManagerAPI . registerRewardProgram ( prepaidCard , admin , { onTxnHash : ( txnHash ) => console . log ( `Transaction hash: ${ blockExplorer }/tx/${ txnHash }/token-transfers` ) , } ) ; console . log ( `Registered reward program ${ rewardProgramId } with admin ${ admin }` ) ; } export async function registerRewardee ( network , prepaidCard , rewardProgramId , mnemonic ? ) { let web3 = await getWeb3 ( network , mnemonic ) ; let rewardManagerAPI = await getSDK ( 'RewardManager' , web3 ) ; let blockExplorer = await getConstant ( 'blockExplorer' , web3 ) ; let { rewardSafe } = await rewardManagerAPI . registerRewardee ( prepaidCard , rewardProgramId , { onTxnHash : ( txnHash ) => console . log ( `Transaction hash: ${ blockExplorer }/tx/${ txnHash }/token-transfers` ) , } ) ; console . log ( `Registered rewardee for reward program ${ rewardProgramId }. Created reward safe: ${ rewardSafe }` ) ; } export async function lockRewardProgram ( network , prepaidCard , rewardProgramId , mnemonic ? ) { let web3 = await getWeb3 ( network , mnemonic ) ; let rewardManagerAPI = await getSDK ( 'RewardManager' , web3 ) ; let blockExplorer = await getConstant ( 'blockExplorer' , web3 ) ; await rewardManagerAPI . lockRewardProgram ( prepaidCard , rewardProgramId , { onTxnHash : ( txnHash ) => console . log ( `Transaction hash: ${ blockExplorer }/tx/${ txnHash }/token-transfers` ) , } ) ; console . log ( `Locked reward program ${ rewardProgramId }` ) ; } export async function isRewardProgramLocked ( network , rewardProgramId , mnemonic ? ) { let web3 = await getWeb3 ( network , mnemonic ) ; let rewardManager = await getSDK ( 'RewardManager' , web3 ) ; const locked = await rewardManager . isLocked ( rewardProgramId ) ; console . log ( `Reward program ${ rewardProgramId } is ${ locked ? 'locked' : 'NOT locked' }` ) ; } export async function updateRewardProgramAdmin ( network , prepaidCard , rewardProgramId , newAdmin , mnemonic ? ) { let web3 = await getWeb3 ( network , mnemonic ) ; let rewardManagerAPI = await getSDK ( 'RewardManager' , web3 ) ; let blockExplorer = await getConstant ( 'blockExplorer' , web3 ) ; await rewardManagerAPI . updateRewardProgramAdmin ( prepaidCard , rewardProgramId , newAdmin , { onTxnHash : ( txnHash ) => console . log ( `Transaction hash: ${ blockExplorer }/tx/${ txnHash }/token-transfers` ) , } ) ; console . log ( `Updated admin of reward program ${ rewardProgramId } to ${ newAdmin }` ) ; } export async function addRewardRule ( network , prepaidCard , rewardProgramId , blob , mnemonic ? ) { let web3 = await getWeb3 ( network , mnemonic ) ; let rewardManagerAPI = await getSDK ( 'RewardManager' , web3 ) ; let blockExplorer = await getConstant ( 'blockExplorer' , web3 ) ; await rewardManagerAPI . addRewardRule ( prepaidCard , rewardProgramId , blob , { onTxnHash : ( txnHash ) => console . log ( `Transaction hash: ${ blockExplorer }/tx/${ txnHash }/token-transfers` ) , } ) ; console . log ( `Updated reward rule of reward program ${ rewardProgramId } to ${ blob }` ) ; } export async function rewardProgramAdmin ( network , rewardProgramId , mnemonic ? ) { let web3 = await getWeb3 ( network , mnemonic ) ; let rewardManager = await getSDK ( 'RewardManager' , web3 ) ; const admin = await rewardManager . getRewardProgramAdmin ( rewardProgramId ) ; console . log ( `Reward program admin of ${ rewardProgramId } is ${ admin }` ) ; } export async function rewardRule ( network , rewardProgramId , mnemonic ? ) { let web3 = await getWeb3 ( network , mnemonic ) ; let rewardManager = await getSDK ( 'RewardManager' , web3 ) ; const rewardRule = await rewardManager . getRewardRule ( rewardProgramId ) ; console . log ( `Reward Rule of ${ rewardProgramId } is ${ rewardRule }` ) ; } export async function withdraw ( network , rewardSafe , to , tokenAddress , amount , mnemonic ? ) { let web3 = await getWeb3 ( network , mnemonic ) ; let rewardManagerAPI = await getSDK ( 'RewardManager' , web3 ) ; let blockExplorer = await getConstant ( 'blockExplorer' , web3 ) ; await rewardManagerAPI . withdraw ( rewardSafe , to , tokenAddress , amount , { onTxnHash : ( txnHash ) => console . log ( `Transaction hash: ${ blockExplorer }/tx/${ txnHash }/token-transfers` ) , } ) ; console . log ( `Withdraw ${ amount } of ${ tokenAddress } out of ${ rewardSafe } to ${ to }` ) ; } export async function transferRewardSafe ( network , rewardSafe , newOwner , mnemonic ? ) { let web3 = await getWeb3 ( network , mnemonic ) ; let rewardManagerAPI = await getSDK ( 'RewardManager' , web3 ) ; let blockExplorer = await getConstant ( 'blockExplorer' , web3 ) ; await rewardManagerAPI . transfer ( rewardSafe , newOwner , { onTxnHash : ( txnHash ) => console . log ( `Transaction hash: ${ blockExplorer }/tx/${ txnHash }/token-transfers` ) , } ) ; console . log ( `Transfer reward safe ${ rewardSafe } ownership to ${ newOwner }` ) ; } export async function viewRewardProgram ( network , rewardProgramId , mnemonic ? ) { let web3 = await getWeb3 ( network , mnemonic ) ; let rewardManagerAPI = await getSDK ( 'RewardManager' , web3 ) ; const rewardProgramInfo = await rewardManagerAPI . getRewardProgramInfo ( rewardProgramId ) ; displayRewardProgramInfo ( rewardProgramInfo ) ; } export async function viewRewardPrograms ( network , mnemonic ? ) { let web3 = await getWeb3 ( network , mnemonic ) ; let rewardManagerAPI = await getSDK ( 'RewardManager' , web3 ) ; let rewardProgramInfos = await rewardManagerAPI . getRewardProgramsInfo ( ) ; rewardProgramInfos . map ( ( rewardProgramInfo ) => displayRewardProgramInfo ( rewardProgramInfo ) ) ; } function displayRewardProgramInfo ( rewardProgramInfo ) { let { rewardProgramId , rewardProgramAdmin , locked , rule , tokenBalances } = rewardProgramInfo ; console . log ( `
  rewardProgramId : ${ rewardProgramId }
  rewardProgramAdmin : ${ rewardProgramAdmin }
  locked : ${ locked }
  rule : ${ rule ? rule : 'No rule' }
  ${ tokenBalances . length > 0 ? 'balance:' : 'balance: No balance' }
  ` ) ; tokenBalances . map ( ( { tokenSymbol , balance } ) => { console . log ( `    ${ tokenSymbol } : ${ fromWei ( balance ) }` ) ; } ) ; }