Skip to content

Commit

Permalink
Allow Asset Path to be Omitted (#1860)
Browse files Browse the repository at this point in the history
* update schema for ibc override

* update validation for when path is not used

* fix

* allow for omitting path
  • Loading branch information
JeremyParish69 authored Aug 23, 2024
1 parent b94daf3 commit c5d0473
Show file tree
Hide file tree
Showing 4 changed files with 315 additions and 60 deletions.
149 changes: 119 additions & 30 deletions .github/workflows/utility/generate_assetlist_functions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const traceTypesNeedingProvider = [
"synthetic"
];

let IS_MAINNET;

let assetProperty = new Map();


Expand Down Expand Up @@ -134,23 +136,67 @@ export async function setSourceAsset(asset_data) {

export function getAssetTrace(asset_data) {

const type = (asset_data.source_asset.base_denom.slice(0, 5) === "cw20:") ? "ibc-cw20" : "ibc";
let trace;

//Use IBC Override Data
const ZONE_ASSET_PATH_DEFINED = !!asset_data.zone_asset?.path;
const IBC_OVERRIDE_DATA = !!asset_data.zone_asset?.override_properties?.ibc;
if (IBC_OVERRIDE_DATA) {
if (ZONE_ASSET_PATH_DEFINED) {
if (asset_data.zone_asset?.path !== asset_data.zone_asset?.override_properties?.ibc?.chain?.path) {
console.log("Path doesn't match IBC Override Data.");
console.log(asset_data.zone_asset.path);
console.log(asset_data.zone_asset.override_properties.ibc);
return;
}
}
trace = asset_data.zone_asset?.override_properties?.ibc;
if (!ZONE_ASSET_PATH_DEFINED) {
return trace;
}
}

const segments = asset_data.zone_asset.path.split("/");
if (segments?.length < 3) {
console.log("Invalid path provided.");
console.log(asset_data.zone_asset.path);

//Make sure Path is provided for Mainnet assets
if (!ZONE_ASSET_PATH_DEFINED) {
if (IS_MAINNET === undefined) {
IS_MAINNET = chain_reg.getFileProperty(asset_data.chainName, "chain", "network_type") === "mainnet";
}
if (IS_MAINNET) {
console.log("No path provided.");
console.log(asset_data.zone_asset);
return;
}
}



//--Find IBC Connection--
const channels = chain_reg.getIBCFileProperty(
asset_data.source_asset.chain_name,
asset_data.chainName,
"channels"
);
if (!channels) {
console.log("No IBC channels registered.");
return;
}


//Define the trace skeleton
const type = (asset_data.source_asset.base_denom.slice(0, 5) === "cw20:") ? "ibc-cw20" : "ibc";
let counterparty = {
chain_name: asset_data.source_asset.chain_name,
base_denom: asset_data.source_asset.base_denom
};
let chain = {};
trace = {
type: type,
counterparty: counterparty,
chain: chain
}


let chain = {
channel_id: segments[1]
};

//--Identify chain_1 and chain_2--
let chain_1, chain_2;
Expand All @@ -160,13 +206,51 @@ export function getAssetTrace(asset_data) {
[chain_1, chain_2] = [chain, counterparty];
}

//--Find IBC Connection--
const channels = chain_reg.getIBCFileProperty(
asset_data.source_asset.chain_name,
asset_data.chainName,
"channels"
);

//If no path, then use the first transfer/transfer channel
if (!IS_MAINNET && !ZONE_ASSET_PATH_DEFINED) {
if (type !== "ibc" || asset_data.source_asset.base_denom.startsWith("ibc/")) {
console.log("Path is required for non-standard IBC transfers.");
console.log(asset_data.zone_asset);
return;
}
let standardChannel;
for (let i = 0; i < channels.length; i++) {
console.log(channels[i]);
if (
channels[i].chain_1.port_id === "transfer" &&
channels[i].chain_2.port_id === "transfer"
) {
standardChannel = channels[i];
console.log(standardChannel);
break;
}
}
console.log("Yes");
if (standardChannel) {
chain_1.channel_id = standardChannel.chain_1.channel_id;
chain_2.channel_id = standardChannel.chain_2.channel_id;
chain.path = "transfer/" + chain.channel_id + "/" + counterparty.base_denom;
return trace;
}
return;
}



//Identify chain-side channel-id from path
chain.path = asset_data.zone_asset?.path;
const segments = chain.path.split("/");
if (segments?.length < 3) {
console.log("Invalid path provided.");
console.log(chain.path);
return;
}
chain.channel_id = segments[1];



//Find the matching channel
let foundChannel;
channels.forEach((channel) => {
if (
Expand Down Expand Up @@ -194,23 +278,24 @@ export function getAssetTrace(asset_data) {
});
if (!foundChannel) {
console.log("Channel not registered.");
console.log(asset_data.zone_asset.path);
console.log(chain.path);
console.log(channels);
return;
}

chain.path = asset_data.zone_asset.path;

const trace = {
type: type,
counterparty: counterparty,
chain: chain
}

//Move path to the bottom of trace.chain object
delete chain.path;
chain.path = asset_data.zone_asset?.path;



return trace;

}


export async function setLocalAsset(asset_data) {

if (
Expand All @@ -222,26 +307,30 @@ export async function setLocalAsset(asset_data) {
asset_data.local_asset = asset_data.source_asset;
return;
}
if (!asset_data.zone_asset.path) {
console.log("No path provided.");

let traces = deepCopy(getAssetProperty(asset_data.source_asset, "traces"));
if (!traces || traces?.length <= 0) {
traces = [];
}
const trace = getAssetTrace(asset_data);
traces.push(trace);

if (!trace?.chain?.path) {
console.log("No IBC path.");
console.log(trace);
console.log(asset_data.zone_asset);
return;
}
try {
let ibcHash = await zone.calculateIbcHash(asset_data.zone_asset.path);
let ibcHash = await zone.calculateIbcHash(trace?.chain?.path);
asset_data.local_asset = {
chain_name: asset_data.chainName,
base_denom: ibcHash
}
} catch (error) {
console.error(error);
}
let traces = deepCopy(getAssetProperty(asset_data.source_asset, "traces"));
if (!traces || traces?.length <= 0) {
traces = [];
}
const trace = getAssetTrace(asset_data);
traces.push(trace);

assetProperty.set(createCombinedKey(asset_data.local_asset, "traces"), traces);
}

Expand Down
71 changes: 43 additions & 28 deletions .github/workflows/utility/validate_zone_data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ export function validate_zone_files() {

});

let IS_MAINNET = chain_reg.getFileProperty(chainName, "chain", "network_type") === "mainnet";

zoneAssetsJson.assets.forEach((zoneAsset) => {

let ASSET_EXISTS = false;
Expand All @@ -106,34 +108,7 @@ export function validate_zone_files() {

//see if ibc channel is registered
if (zoneAsset.chain_name != chainName) {
let VALID_PATH = false;
if (!zoneAsset.path) {
throw new Error(`Path missing for ${zoneAsset.base_denom}. Please enter a Path.`);
}

let chain1 = false;
if (chain_reg.getIBCFileProperty(chainName, zoneAsset.chain_name, "chain_1").chain_name == chainName) {
chain1 = true;
}
let ibcChannels = chain_reg.getIBCFileProperty(chainName, zoneAsset.chain_name, "channels");
let thisChannel = "";
let thisPort = "";

VALID_PATH = ibcChannels.some((channel) => {
if (chain1) {
thisChannel = channel.chain_1.channel_id;
thisPort = channel.chain_1.port_id;
} else {
thisChannel = channel.chain_2.channel_id;
thisPort = channel.chain_2.port_id;
}
if (zoneAsset.path.startsWith(thisPort + '/' + thisChannel)) {
return true;
}
});
if (!VALID_PATH) {
throw new Error(`IBC Channel for Path: ${zoneAsset.path} does not exist in the chain registry.`);
}
checkAssetIBCData(zoneAsset, chainName, IS_MAINNET);
}

//see if canonical asset is valid
Expand All @@ -157,6 +132,46 @@ export function validate_zone_files() {

}

function checkAssetIBCData(zoneAsset, chainName, IS_MAINNET) {

if (
(
!IS_MAINNET && !zoneAsset.path
)
||
zoneAsset.override_properties?.ibc
) { return; }

if ( !zoneAsset.path ) {
throw new Error(`Path missing for ${zoneAsset.base_denom}. Please enter a Path.`);
}

let chain1 = false;
if (chain_reg.getIBCFileProperty(chainName, zoneAsset.chain_name, "chain_1").chain_name == chainName) {
chain1 = true;
}
let ibcChannels = chain_reg.getIBCFileProperty(chainName, zoneAsset.chain_name, "channels");
let thisChannel = "";
let thisPort = "";

let VALID_PATH = ibcChannels.some((channel) => {
if (chain1) {
thisChannel = channel.chain_1.channel_id;
thisPort = channel.chain_1.port_id;
} else {
thisChannel = channel.chain_2.channel_id;
thisPort = channel.chain_2.port_id;
}
if (zoneAsset.path.startsWith(thisPort + '/' + thisChannel)) {
return true;
}
});
if (!VALID_PATH) {
throw new Error(`IBC Channel for Path: ${zoneAsset.path} does not exist in the chain registry.`);
}

}

export async function validate_add_asset() {


Expand Down
1 change: 0 additions & 1 deletion osmo-test-5/osmosis.zone_assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@
{
"chain_name": "symphonytestnet",
"base_denom": "note",
"path": "transfer/channel-8743/note",
"osmosis_verified": false
}
]
Expand Down
Loading

0 comments on commit c5d0473

Please sign in to comment.