Skip to content

Commit

Permalink
add PHA token to support Rhala (#676)
Browse files Browse the repository at this point in the history
  • Loading branch information
freddyli7 authored Jun 12, 2024
1 parent af2161f commit 76387d3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 15 deletions.
24 changes: 21 additions & 3 deletions runtime/testnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1799,6 +1799,8 @@ impl MatchesFungibles<AssetId, Balance> for SimpleForeignAssetConverter {
(Fungible(ref amount), AssetId(ref id)) => {
if id == &SygUSDLocation::get() {
Ok((SygUSDAssetId::get(), *amount))
} else if id == &PHALocation::get() {
Ok((PHAAssetId::get(), *amount))
} else {
Err(ExecutionError::AssetNotHandled)
}
Expand Down Expand Up @@ -1839,11 +1841,11 @@ impl sygma_percentage_feehandler::Config for Runtime {
}

parameter_types! {
// tTNT
// tTNT: native asset is always a reserved asset
pub NativeLocation: Location = Location::here();
pub NativeSygmaResourceId: [u8; 32] = hex_literal::hex!("0000000000000000000000000000000000000000000000000000000000002000");

// SygUSD
// SygUSD: a non-reserved asset
pub SygUSDLocation: Location = Location::new(
1,
[
Expand All @@ -1856,12 +1858,27 @@ parameter_types! {
pub SygUSDAssetId: AssetId = 2000;
// SygUSDResourceId is the resourceID that mapping with the foreign asset SygUSD
pub SygUSDResourceId: ResourceId = hex_literal::hex!("0000000000000000000000000000000000000000000000000000000000001100");

// PHA: a reserved asset
pub PHALocation: Location = Location::new(
1,
[
Parachain(2004),
slice_to_generalkey(b"sygma"),
slice_to_generalkey(b"pha"),
],
);
// PHAAssetId is the substrate assetID of PHA
pub PHAAssetId: AssetId = 2001;
// PHAResourceId is the resourceID that mapping with the foreign asset PHA
pub PHAResourceId: ResourceId = hex_literal::hex!("0000000000000000000000000000000000000000000000000000000000001000");
}

fn bridge_accounts_generator() -> BTreeMap<XcmAssetId, AccountId32> {
let mut account_map: BTreeMap<XcmAssetId, AccountId32> = BTreeMap::new();
account_map.insert(NativeLocation::get().into(), BridgeAccountNative::get());
account_map.insert(SygUSDLocation::get().into(), BridgeAccountOtherToken::get());
account_map.insert(PHALocation::get().into(), BridgeAccountOtherToken::get());
account_map
}

Expand Down Expand Up @@ -1915,9 +1932,10 @@ parameter_types! {
pub ResourcePairs: Vec<(XcmAssetId, ResourceId)> = vec![
(NativeLocation::get().into(), NativeSygmaResourceId::get()),
(SygUSDLocation::get().into(), SygUSDResourceId::get()),
(PHALocation::get().into(), PHAResourceId::get()),
];

pub AssetDecimalPairs: Vec<(XcmAssetId, u8)> = vec![(NativeLocation::get().into(), 18u8), (SygUSDLocation::get().into(), 6u8)];
pub AssetDecimalPairs: Vec<(XcmAssetId, u8)> = vec![(NativeLocation::get().into(), 18u8), (SygUSDLocation::get().into(), 6u8), (PHALocation::get().into(), 12u8)];
}

pub struct ReserveChecker;
Expand Down
38 changes: 26 additions & 12 deletions scripts/sygma-setup/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ const {
setFee,
setFeeRate,
getNativeAssetId,
getERC20TSTAssetId,
getERC20TSTD20AssetId,
getPHAAssetId,
createAsset,
setAssetMetadata,
mintAsset,
Expand All @@ -20,9 +19,9 @@ const {
} = require("./util");

const BN = require('bn.js');
const bn1e6 = new BN(10).pow(new BN(6));
const bn1e12 = new BN(10).pow(new BN(12));
const bn1e18 = new BN(10).pow(new BN(18));
const bn1e20 = new BN(10).pow(new BN(20));

const feeHandlerType = {
BasicFeeHandler: "BasicFeeHandler",
Expand Down Expand Up @@ -51,10 +50,10 @@ async function main() {
await cryptoWaitReady();
const keyring = new Keyring({type: 'sr25519'});
const sudo = keyring.addFromUri('//Alice');
const basicFeeAmount = bn1e12.mul(new BN(1)); // 1 * 10 ** 12
const basicFeeAmount = bn1e18.mul(new BN(1)); // 1 * 10 ** 18
const percentageFeeRate = 500; // 5%
const feeRateLowerBound = 0;
const feeRateUpperBound = bn1e12.mul(new BN(1000)); // 1000 * 10 ** 12
const feeRateUpperBound = bn1e18.mul(new BN(1000)); // 1000 * 10 ** 18
const mpcAddr = process.env.MPCADDR;

// register dest domains
Expand All @@ -68,28 +67,43 @@ async function main() {
await setFeeRate(api, domain.domainID, getNativeAssetId(api), percentageFeeRate, feeRateLowerBound, feeRateUpperBound, true, sudo);
}

// create SygUSD test asset (foreign asset)
// create SygUSD test asset (non-reserved foreign asset)
// SygUSDAssetId: AssetId defined in runtime.rs
const sygUSDAssetID = 2000;
const sygUSDAdmin = sudo.address;
const sygUSDMinBalance = 100;
const sygUSDMinBalance = 1;
const sygUSDName = "sygUSD";
const sygUSDSymbol = "sygUSD";
const sygUSDDecimal = 12;
const sygUSDDecimal = 6;
await createAsset(api, sygUSDAssetID, sygUSDAdmin, sygUSDMinBalance, true, sudo);
await setAssetMetadata(api, sygUSDAssetID, sygUSDName, sygUSDSymbol, sygUSDDecimal, true, sudo);
await mintAsset(api, sygUSDAssetID, sygUSDAdmin, bn1e12.mul(new BN(100)), true, sudo); // mint 100 sygUSD to Alice
await mintAsset(api, sygUSDAssetID, sygUSDAdmin, bn1e6.mul(new BN(100)), true, sudo); // mint 100 sygUSD to Alice

// create PHA test asset (reserved foreign asset)
// PHAAssetId: AssetId defined in runtime.rs
const PHAAssetID = 2001;
const PHAAdmin = sudo.address;
const PHAMinBalance = 1;
const PHAName = "PHA";
const PHASymbol = "PHA";
const PHADecimal = 12;
await createAsset(api, PHAAssetID, PHAAdmin, PHAMinBalance, true, sudo);
await setAssetMetadata(api, PHAAssetID, PHAName, PHASymbol, PHADecimal, true, sudo);
await mintAsset(api, PHAAssetID, PHAAdmin, bn1e12.mul(new BN(100)), true, sudo); // mint 100 PHA to Alice

// set fee for tokens with domains
for (const domain of supportedDestDomains) {
await setFeeHandler(api, domain.domainID, getSygUSDAssetId(api), feeHandlerType.PercentageFeeHandler, true, sudo)
await setFeeRate(api, domain.domainID, getSygUSDAssetId(api), percentageFeeRate, feeRateLowerBound, feeRateUpperBound,true, sudo);

await setFeeHandler(api, domain.domainID, getPHAAssetId(api), feeHandlerType.PercentageFeeHandler, true, sudo)
await setFeeRate(api, domain.domainID, getPHAAssetId(api), percentageFeeRate, feeRateLowerBound, feeRateUpperBound,true, sudo);
}

// transfer some native asset to FeeReserveAccount and TransferReserveAccount as Existential Deposit(aka ED)
await setBalance(api, FeeReserveAccountAddress, bn1e12.mul(new BN(10000)), true, sudo); // set balance to 10000 native asset
await setBalance(api, NativeTokenTransferReserveAccount, bn1e12.mul(new BN(10000)), true, sudo); // set balance to 10000 native asset
await setBalance(api, OtherTokenTransferReserveAccount, bn1e12.mul(new BN(10000)), true, sudo); // set balance to 10000 native asset
await setBalance(api, FeeReserveAccountAddress, bn1e18.mul(new BN(10000)), true, sudo); // set balance to 10000 native asset
await setBalance(api, NativeTokenTransferReserveAccount, bn1e18.mul(new BN(10000)), true, sudo); // set balance to 10000 native asset
await setBalance(api, OtherTokenTransferReserveAccount, bn1e18.mul(new BN(10000)), true, sudo); // set balance to 10000 native asset

// set up MPC address(will also unpause all registered domains)
if (mpcAddr) {
Expand Down
27 changes: 27 additions & 0 deletions scripts/sygma-setup/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,32 @@ async function registerDomain(api, domainID, chainID, finalization, sudo) {
});
}

// see PHALocation definition in runtime.rs
function getPHAAssetId(api) {
return api.createType('StagingXcmV4AssetAssetId', {
parents: 1,
interior: api.createType('StagingXcmV4Junctions', {
X3: [
api.createType('StagingXcmV4Junction', {
Parachain: api.createType('Compact<U32>', 2004)
}),
api.createType('StagingXcmV4Junction', { // sygma
GeneralKey: {
length: 5,
data: '0x7379676d61000000000000000000000000000000000000000000000000000000'
}
}),
api.createType('StagingXcmV4Junction', { // pha
GeneralKey: {
length: 3,
data: '0x7068610000000000000000000000000000000000000000000000000000000000'
}
}),
],
}),
})
}

// 0x7379676d61(sygma) is general key of sygma defined in sygma substrate pallet runtime for testing
// 0x737967757364(sygusd) is general key of sygusd defined in sygma substrate pallet runtime for testing
// see SygUSDLocation definition in runtime.rs
Expand Down Expand Up @@ -419,6 +445,7 @@ async function subEvents (api, eventsList) {
module.exports = {
getNativeAssetId,
getSygUSDAssetId,
getPHAAssetId,
registerDomain,
mintAsset,
setAssetMetadata,
Expand Down

0 comments on commit 76387d3

Please sign in to comment.