Skip to content

Commit

Permalink
Add topup command line
Browse files Browse the repository at this point in the history
  • Loading branch information
Lbqds committed Oct 31, 2023
1 parent dedc711 commit fc6fd60
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 22 deletions.
23 changes: 21 additions & 2 deletions clients/js/alph.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CONFIGS, Guardians, NetworkType } from './configs'
import { impossible } from './utils'
import { web3, binToHex, addressFromContractId, ExecuteScriptResult, SignerProvider, ExecuteScriptParams, HexString, ExecutableScript } from '@alephium/web3'
import { web3, binToHex, addressFromContractId, ExecuteScriptResult, SignerProvider, ExecuteScriptParams, HexString, ExecutableScript, ONE_ALPH, DUST_AMOUNT } from '@alephium/web3'
import { PrivateKeyWallet } from '@alephium/web3-wallet'
import { registerChain, GovernancePayload, alephium_contracts, deserializeTransferFeeVAA } from '@alephium/wormhole-sdk'
import {
Expand All @@ -12,9 +12,28 @@ import {
UpdateRefundAddress,
UpgradeTokenBridgeContract,
Governance,
TokenBridge
TokenBridge,
AddRewards
} from '@alephium/wormhole-sdk/lib/cjs/alephium-contracts/ts'

export async function topupRewards(
alphAmount: bigint,
networkType: NetworkType,
nodeUrl?: string
) {
const network = CONFIGS[networkType]['alephium']
const signer = getSignerProvider(network, nodeUrl)
const amount = alphAmount * ONE_ALPH
const result = await AddRewards.execute(signer, {
initialFields: {
bridgeRewardRouter: network.bridgeRewardRouter,
amount: amount
},
attoAlphAmount: amount + DUST_AMOUNT
})
console.log(`TxId: ${result.txId}`)
}

function getSignerProvider(network: any, nodeUrl?: string) {
const rpc = nodeUrl ?? network.rpc
if (rpc === undefined) {
Expand Down
3 changes: 3 additions & 0 deletions clients/js/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const MAINNET = {
key: get_env_var('ALPH_KEY'),
tokenBridgeAddress: alephiumMainnetConfig.contracts.tokenBridge,
governanceAddress: alephiumMainnetConfig.contracts.governance,
bridgeRewardRouter: alephiumMainnetConfig.contracts.bridgeRewardRouter,
groupIndex: alephiumMainnetConfig.groupIndex,
}
};
Expand Down Expand Up @@ -207,6 +208,7 @@ const TESTNET = {
key: get_env_var('ALPH_KEY'),
tokenBridgeAddress: alephiumTestnetConfig.contracts.tokenBridge,
governanceAddress: alephiumTestnetConfig.contracts.governance,
bridgeRewardRouter: alephiumTestnetConfig.contracts.bridgeRewardRouter,
groupIndex: alephiumTestnetConfig.groupIndex
}
};
Expand Down Expand Up @@ -298,6 +300,7 @@ const DEVNET = {
key: testPrivateKey,
tokenBridgeAddress: alephiumDevnetConfig.contracts.tokenBridge,
governanceAddress: alephiumDevnetConfig.contracts.governance,
bridgeRewardRouter: alephiumDevnetConfig.contracts.bridgeRewardRouter,
groupIndex: alephiumDevnetConfig.groupIndex
}
};
Expand Down
68 changes: 49 additions & 19 deletions clients/js/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
isEVMChain,
toChainId,
} from "@alephium/wormhole-sdk";
import { executeGovernanceAlph, getNextGovernanceSequence } from "./alph";
import { executeGovernanceAlph, getNextGovernanceSequence, topupRewards } from "./alph";
import { default as guardianDevnetConfig } from '../../configs/guardian/devnet.json'
import { CONFIGS, NetworkType } from "./configs";

Expand Down Expand Up @@ -422,14 +422,7 @@ yargs(hideBin(process.argv))
async (argv) => {
assertChain(argv["chain"]);
assertEVMChain(argv["chain"]);
const network = argv.network.toUpperCase();
if (
network !== "MAINNET" &&
network !== "TESTNET" &&
network !== "DEVNET"
) {
throw Error(`Unknown network: ${network}`);
}
const network = getNetworkType(argv.network)
let module = argv["module"] as "Core" | "NFTBridge" | "TokenBridge";
let rpc = argv["rpc"] ?? CONFIGS[network][argv["chain"]].rpc;
if (argv["implementation-only"]) {
Expand Down Expand Up @@ -520,7 +513,7 @@ yargs(hideBin(process.argv))
});
},
async (argv) => {
const network = argv.network.toUpperCase() as NetworkType
const network = getNetworkType(argv.network)
const sequence = await getNextGovernanceSequence(network, argv['explorer-api-server-url'])
console.log(`The next governnace sequence is: ${sequence}`)
},
Expand Down Expand Up @@ -567,14 +560,7 @@ yargs(hideBin(process.argv))

console.log(parsedVaa.body.payload);

const network = argv.network.toUpperCase();
if (
network !== "MAINNET" &&
network !== "TESTNET" &&
network !== "DEVNET"
) {
throw Error(`Unknown network: ${network}`);
}
const network = getNetworkType(argv.network)

// We figure out the target chain to submit the VAA to.
// The VAA might specify this itself (for example a contract upgrade VAA
Expand Down Expand Up @@ -633,7 +619,51 @@ yargs(hideBin(process.argv))
impossible(chain);
}
}
).argv
)
.command(
"topup",
"Topup ALPH rewards",
(yargs) => {
return yargs
.option("amount", {
alias: "a",
describe: "ALPH amount",
type: "number",
required: true,
})
.option("network", {
alias: "n",
describe: "network",
type: "string",
choices: ["mainnet", "testnet", "devnet"],
required: true,
})
.option('node-url', {
describe: "full node url",
type: "string",
required: false,
});
},
async (argv) => {
const network = getNetworkType(argv.network)
const alphAmount = BigInt(argv.amount)
const nodeUrl = argv['node-url']
await topupRewards(alphAmount, network, nodeUrl)
}
)
.argv

function getNetworkType(network: string): NetworkType {
const networkType = network.toUpperCase();
if (
networkType !== "MAINNET" &&
networkType !== "TESTNET" &&
networkType !== "DEVNET"
) {
throw Error(`Unknown network: ${network}`);
}
return networkType as NetworkType
}

function exitOnError(msg: string) {
console.log(msg)
Expand Down
4 changes: 4 additions & 0 deletions integration_test/src/alph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import { default as alephiumDevnetConfig } from '../../configs/alephium/devnet.j

export type AlephiumBridgeChain = BridgeChain & {
groupIndex: number
nodeProvider: NodeProvider
bridgeRewardRouter: string
tokenBridgeContractId: string
attestWithTokenInfo(tokenId: string, decimals: number, symbol: string, name: string): Promise<Uint8Array>
getContractState<I extends ContractInstance, F extends Fields>(
Expand Down Expand Up @@ -447,6 +449,8 @@ export async function createAlephium(): Promise<AlephiumBridgeChain> {
genMultiSigAddress: genMultiSigAddress,

groupIndex: groupIndex,
nodeProvider: nodeProvider,
bridgeRewardRouter: contracts.bridgeRewardRouter,
tokenBridgeContractId: tokenBridgeContractId,
getLocalTokenInfo: _getLocalTokenInfo,
getWrappedTokenTotalSupply: getWrappedTokenTotalSupply,
Expand Down
18 changes: 17 additions & 1 deletion integration_test/src/token_bridge/transfer_token_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { AlephiumBridgeChain } from '../alph'
import { assert, getBridgeChains, normalizeTokenId, randomBigInt } from '../utils'
import { TransferTokenTest } from './transfer_token'
import { BridgeChain } from '../bridge_chain'
import { CHAIN_ID_ALEPHIUM } from '@alephium/wormhole-sdk'
import { CHAIN_ID_ALEPHIUM, waitAlphTxConfirmed } from '@alephium/wormhole-sdk'
import { BridgeRewardRouter } from '@alephium/wormhole-sdk/lib/cjs/alephium-contracts/ts'
import { ONE_ALPH, addressFromContractId } from '@alephium/web3'

async function attestInvalidToken(alph: AlephiumBridgeChain) {
const testTokenInfo = await alph.getLocalTokenInfo(alph.testTokenId)
Expand Down Expand Up @@ -150,6 +152,18 @@ class ChainPair {
}
}

async function testTopupRewards(alph: AlephiumBridgeChain) {
const contractAddress = addressFromContractId(alph.bridgeRewardRouter)
const balance0 = (await alph.getContractState(BridgeRewardRouter, contractAddress)).asset.alphAmount
const alphAmount = 100
const command = `npm --prefix ../clients/js start -- topup -a ${alphAmount} -n devnet`
const output = execSync(command)
const txId = output.toString('utf8').slice(output.lastIndexOf(':') + 2)
await waitAlphTxConfirmed(alph.nodeProvider, txId, 1)
const balance1 = (await alph.getContractState(BridgeRewardRouter, contractAddress)).asset.alphAmount
assert(BigInt(balance0) + BigInt(alphAmount) * ONE_ALPH === BigInt(balance1))
}

async function test() {
const chains = await getBridgeChains()
const alph = chains.alph
Expand All @@ -173,6 +187,8 @@ async function test() {
console.log('================== transfer between ETH and BSC ==================')
const pair2 = new ChainPair(eth, bsc)
await pair2.test(50)

await testTopupRewards(alph)
}

test()

0 comments on commit fc6fd60

Please sign in to comment.