Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feature/rewards-clean…
Browse files Browse the repository at this point in the history
…up-legacy-code
  • Loading branch information
AntonAndell committed Jun 13, 2024
2 parents 3b6d9df + a17e7fa commit de69bba
Show file tree
Hide file tree
Showing 85 changed files with 3,498 additions and 458 deletions.
4 changes: 0 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,3 @@
path = gochain-local
url = [email protected]:nightowl121/gochain-local.git
branch = master
[submodule "xcall-lib"]
path = xcall-lib
url = [email protected]:AntonAndell/XCall-lib.git
branch = master
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Map;

import static network.balanced.score.lib.utils.Check.*;
import static network.balanced.score.lib.utils.Math.pow;

public class AssetManagerImpl implements AssetManager {

Expand All @@ -42,9 +43,9 @@ public class AssetManagerImpl implements AssetManager {
public static final String ASSETS = "assets";
public static final String NATIVE_ASSET_ADDRESS = "native_asset_address";
public static final String NATIVE_ASSET_ADDRESSES = "native_asset_addresses";
public static final String DATA_MIGRATED = "data_migrated";
public static final String ASSET_DEPOSIT_CHAIN_LIMIT = "asset_deposit_chain_limit";
public static final String ASSET_DEPOSITS = "asset_deposits";
public static final String DECIMAL_TRANSFORMATIONS = "decimal_transformations";

public static String NATIVE_NID;
public static byte[] tokenBytes;
Expand All @@ -58,7 +59,7 @@ public class AssetManagerImpl implements AssetManager {
private final BranchDB<Address, DictDB<String, String>> assetNativeAddresses = Context.newBranchDB(NATIVE_ASSET_ADDRESSES, String.class);
private final DictDB<String, BigInteger> assetChainDepositLimit = Context.newDictDB(ASSET_DEPOSIT_CHAIN_LIMIT, BigInteger.class);
private final DictDB<String, BigInteger> assetDeposits = Context.newDictDB(ASSET_DEPOSITS, BigInteger.class);
private final VarDB<Boolean> dataMigrated = Context.newVarDB(DATA_MIGRATED, Boolean.class);
private final DictDB<String, BigInteger> decimalTransformations = Context.newDictDB(DECIMAL_TRANSFORMATIONS, BigInteger.class);

public AssetManagerImpl(Address _governance, byte[] tokenBytes) {
AssetManagerImpl.tokenBytes = tokenBytes;
Expand All @@ -73,9 +74,6 @@ public AssetManagerImpl(Address _governance, byte[] tokenBytes) {
}

currentVersion.set(Versions.BALANCED_ASSET_MANAGER);
if (dataMigrated.get() == null) {
migrateTokenNativeAddressAndAssetDeposits();
}
}

@External(readonly = true)
Expand Down Expand Up @@ -118,44 +116,52 @@ public void deployAsset(String tokenNetworkAddress, String name, String symbol,
Address token = Context.deploy(tokenBytes, BalancedAddressManager.getGovernance(), name, symbol, decimals);
assets.set(tokenNetworkAddress, token);
assetNativeAddresses.at(token).set(nativeAddress.net(), nativeAddress.account());
if (assetNativeAddress.get(token) == null) {
assetNativeAddress.set(token, tokenNetworkAddress);
}

Address SYSTEM_SCORE_ADDRESS = getSystemScoreAddress();
Context.call(SYSTEM_SCORE_ADDRESS, "setScoreOwner", token, BalancedAddressManager.getGovernance());
}

private void migrateTokenNativeAddressAndAssetDeposits() {
List<String> nativeAddresses = assets.keys();
for (String na : nativeAddresses) {
Address address = assets.get(na);
if (assetNativeAddress.get(address) != null) {
NetworkAddress networkAddress = NetworkAddress.valueOf(na);
assetNativeAddresses.at(address).set(networkAddress.net(), networkAddress.account());
//delete once migrated
//assetNativeAddress.set(address, null);
}

assetDeposits.set(na, getTotalSupply(address));
}

dataMigrated.set(true);
}

@External
public void linkToken(String tokenNetworkAddress, Address token) {
public void linkToken(String tokenNetworkAddress, Address token, @Optional BigInteger decimals) {
onlyGovernance();
BigInteger tokenDecimals = Context.call(BigInteger.class, token, "decimals");
if (decimals != null && !decimals.equals(BigInteger.ZERO) && !decimals.equals(tokenDecimals) ) {
BigInteger diff = decimals.subtract(tokenDecimals);
BigInteger transformation = pow(BigInteger.TEN, diff.abs().intValue()).multiply(BigInteger.valueOf(diff.signum()));
decimalTransformations.set(tokenNetworkAddress, transformation);
}

NetworkAddress networkAddress = NetworkAddress.valueOf(tokenNetworkAddress);
Context.require(spokes.get(networkAddress.net()) != null, "Add the spoke spoke manager first");
Context.require(assets.get(tokenNetworkAddress) == null, "Token is already available");
assets.set(tokenNetworkAddress, token);
assetNativeAddresses.at(token).set(networkAddress.net(), networkAddress.account());
if (assetNativeAddress.get(token) == null) {
assetNativeAddress.set(token, tokenNetworkAddress);
}
}

@External
public void removeToken(Address token, String nid) {
onlyGovernance();
String nativeAddress = assetNativeAddresses.at(token).get(nid);
String networkAddress = new NetworkAddress(nid, nativeAddress).toString();
Context.require(nativeAddress != null, "Token is not available");
assetNativeAddresses.at(token).set(nid, null);
assets.set(new NetworkAddress(nid, nativeAddress).toString(), null);
decimalTransformations.set(networkAddress, null);
assets.set(networkAddress, null);
}

@External
public void overrideChainDeposits(String tokenNetworkAddress, BigInteger addedAmount) {
onlyGovernance();
BigInteger remainingDeposit = getAssetDeposit(tokenNetworkAddress).add(addedAmount);
Context.require(remainingDeposit.signum() >= 0, "Remaining deposit can't be negative");
assetDeposits.set(tokenNetworkAddress, remainingDeposit);
}

@External
Expand Down Expand Up @@ -268,6 +274,7 @@ public void deposit(String from, String tokenAddress, String fromAddress, String
Address assetAddress = assets.get(spokeTokenAddress);
Context.require(assetAddress != null, "Token is not yet deployed");

_amount = translateIncomingDecimals(spokeTokenAddress, _amount);
BigInteger tokenAddressDepositLimit = assetChainDepositLimit.get(spokeTokenAddress);
Context.require(tokenAddressDepositLimit == null || getAssetDeposit(spokeTokenAddress).add(_amount).compareTo(tokenAddressDepositLimit) <= 0, "Max deposit limit exceeded");

Expand Down Expand Up @@ -305,18 +312,42 @@ private void _withdrawTo(Address asset, String from, String to, BigInteger amoun
byte[] msg;
byte[] rollback = AssetManagerMessages.withdrawRollback(tokenAddress.toString(), to, amount);

BigInteger sendAmount = translateOutgoingDecimals(tokenAddress.toString(), amount);
Context.require(sendAmount.compareTo(BigInteger.ZERO) > 0, "Amount needs to be greater than 0 on the destination chain");
if (toNative) {
msg = SpokeAssetManagerMessages.WithdrawNativeTo(tokenAddress.account(), targetAddress.account(), amount);
msg = SpokeAssetManagerMessages.WithdrawNativeTo(tokenAddress.account(), targetAddress.account(), sendAmount);
} else {
msg = SpokeAssetManagerMessages.WithdrawTo(tokenAddress.account(), targetAddress.account(), amount);
msg = SpokeAssetManagerMessages.WithdrawTo(tokenAddress.account(), targetAddress.account(), sendAmount);
}


assetDeposits.set(tokenAddress.toString(), getAssetDeposit(tokenAddress.toString()).subtract(amount));
BigInteger remainingDeposit = getAssetDeposit(tokenAddress.toString()).subtract(amount);
Context.require(remainingDeposit.signum() >= 0, "Remaining deposit can't be negative");
assetDeposits.set(tokenAddress.toString(), remainingDeposit);

XCallUtils.sendCall(fee, spoke, msg, rollback);
}

private BigInteger translateOutgoingDecimals(String token, BigInteger amount) {
return translateDecimals(token, amount, 1);
}

private BigInteger translateIncomingDecimals(String token, BigInteger amount) {
return translateDecimals(token, amount, -1);
}

private BigInteger translateDecimals(String token, BigInteger amount, int sign) {
BigInteger translation = decimalTransformations.get(token);
if (translation == null) {
return amount;
}

if (translation.signum() == sign) {
return amount.multiply(translation).abs();
} else {
return amount.divide(translation).abs();
}
}

private BigInteger getTotalSupply(Address assetAddress) {
return Context.call(BigInteger.class, assetAddress, "totalSupply");
}
Expand Down
Loading

0 comments on commit de69bba

Please sign in to comment.