Skip to content

Commit

Permalink
fix: add deep merge method
Browse files Browse the repository at this point in the history
  • Loading branch information
anondev2323 committed Oct 13, 2023
1 parent e77fdf5 commit df250dc
Show file tree
Hide file tree
Showing 7 changed files with 1,106 additions and 512 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/checkForeignAssetsConfig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Check tokens list

on:
pull_request:
push:
branches:
- development
- staging
- production
- mainnet

jobs:
check-token-list:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20
cache: "npm"
- run: npm ci
- run: npm run build -w sdk
- run: cd core/tokenRegistry && npx ts-node src/scripts/checkForeignAssetsConfig.ts
53 changes: 2 additions & 51 deletions core/tokenRegistry/README.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,3 @@
# Definitions SDK
# Tokens Registry

Replaces these files from old sdk:
* vaa/parser
* mock

## VAA

### Deserialization

```ts
// decode VAA
const vaaBytes = Buffer.from(testCase.vaa, "base64");
// deserializes the base VAA details (emitterChain, emitterAddress, sequence, signatures, payload, hash, etc)
const parsed = deserialize("Uint8Array", new Uint8Array(vaaBytes));
// deserialize the payload, first argument denotes the type of payload
const deserialized = deserializePayload("BAMessage", parsed.payload);
```

#### Available payload types

Token Bridge:
- "AttestMeta"
- "Transfer"
- "TransferWithPayload

Generic Relayer:
- "DeliveryInstruction"
- "RedeliveryInstruction"
- "DeliveryOverride"

Governance
- "CoreBridgeUpgradeContract"
- "CoreBridgeGuardianSetUpgrade"
- "CoreBridgeSetMessageFee"
- "CoreBridgeTransferFees"
- "CoreBridgeRecoverChainId"
- "TokenBridgeRegisterChain"
- "TokenBridgeUpgradeContract"
- "TokenBridgeRecoverChainId"
- "NftBridgeRegisterChain"
- "NftBridgeUpgradeContract"
- "NftBridgeRecoverChainId"
- "RelayerRegisterChain"
- "RelayerUpgradeContract"
- "RelayerUpdateDefaultProvider"

Circle
- "CircleTransferRelay"

BAM
- "BAMessage"
Cache of token details and foreign asset addresses
3 changes: 2 additions & 1 deletion core/tokenRegistry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"rebuild": "npm run clean && npm run build:cjs && npm run build:esm",
"clean": "rm -rf ./dist && rm -f ./*.tsbuildinfo",
"lint": "npm run prettier && eslint --fix",
"prettier": "prettier --write ./src"
"prettier": "prettier --write ./src",
"updateForeignAssets": "npx ts-node ./src/scripts/updateForeignAssetConfig"
},
"dependencies": {
"@wormhole-foundation/connect-sdk": "*",
Expand Down
5 changes: 4 additions & 1 deletion core/tokenRegistry/src/scripts/checkForeignAssetConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ const checkEnvConfig = async (

const [numUpdates, suggestedUpdates] = await getSuggestedUpdates(wh, tokensConfig);
if (numUpdates as number > 0) {
console.log(`${numUpdates} updates available`);
console.log(`
${numUpdates} updates available. To update, run:\n
npm run updateForeignAssets`
);
console.log(JSON.stringify(suggestedUpdates, null, 4));
} else {
console.log('Up to date')
Expand Down
38 changes: 33 additions & 5 deletions core/tokenRegistry/src/scripts/updateForeignAssetConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,38 @@ const TESTNET_TOKENS = JSON.parse(testnetTokens) as TokensConfig;
const mainnetTokens = fs.readFileSync('src/tokens/mainnetTokens.json', 'utf-8');
const MAINNET_TOKENS = JSON.parse(mainnetTokens) as TokensConfig;

/**
* Simple object check.
* @param item
* @returns {boolean}
*/
export function isObject(item: any) {
return (item && typeof item === 'object' && !Array.isArray(item));
}

/**
* Deep merge two objects.
* @param target
* @param ...sources
*/
export function mergeDeep(target: any, ...sources: any) {
if (!sources.length) return target;
const source = sources.shift();

if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
mergeDeep(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}

return mergeDeep(target, ...sources);
}

// warning: be careful optimizing the RPC calls in this script, you may 429 yourself
// slow and steady, or something like that
const checkEnvConfig = async (
Expand All @@ -21,11 +53,7 @@ const checkEnvConfig = async (

const data = await getSuggestedUpdates(wh, tokensConfig);
const suggestedUpdates = data[0] as TokensConfig;
const newConfig = {
...tokensConfig,
...suggestedUpdates,
}
console.log('CONFIG\n', JSON.stringify(newConfig, null, 4));
const newConfig = mergeDeep(tokensConfig, suggestedUpdates);
const filePath = env === 'Mainnet' ? 'src/tokens/mainnetTokens.json' : 'src/tokens/testnetTokens.json';
fs.writeFileSync(filePath, JSON.stringify(newConfig, null, 2));
}
Expand Down
Loading

0 comments on commit df250dc

Please sign in to comment.