This repository has been archived by the owner on Jul 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
export generic function
decodeAssetDataOrThrow
and add ERC20Bridge …
…support
- Loading branch information
Showing
9 changed files
with
111 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { IAssetDataContract } from '@0x/contract-wrappers'; | ||
import { AssetData, AssetProxyId } from '@0x/types'; | ||
import { BigNumber, hexSlice, NULL_ADDRESS } from '@0x/utils'; | ||
|
||
const fakeProvider = { isEIP1193: true } as any; | ||
const assetDataDecoder = new IAssetDataContract(NULL_ADDRESS, fakeProvider); | ||
export function decodeAssetDataOrThrow(assetData: string): AssetData { | ||
const assetProxyId = hexSlice(assetData, 0, 4); // tslint:disable-line:custom-no-magic-numbers | ||
switch (assetProxyId) { | ||
case AssetProxyId.ERC20: { | ||
const tokenAddress = assetDataDecoder.getABIDecodedTransactionData<string>('ERC20Token', assetData); | ||
return { | ||
assetProxyId, | ||
tokenAddress, | ||
}; | ||
} | ||
case AssetProxyId.ERC20Bridge: { | ||
const [tokenAddress, bridgeAddress, bridgeData] = assetDataDecoder.getABIDecodedTransactionData< | ||
[string, string, string] | ||
>('ERC20Bridge', assetData); | ||
return { | ||
assetProxyId, | ||
tokenAddress, | ||
bridgeAddress, | ||
bridgeData, | ||
}; | ||
} | ||
case AssetProxyId.ERC721: { | ||
const [tokenAddress, tokenId] = assetDataDecoder.getABIDecodedTransactionData<[string, BigNumber]>( | ||
'ERC721Token', | ||
assetData, | ||
); | ||
return { | ||
assetProxyId, | ||
tokenAddress, | ||
tokenId, | ||
}; | ||
} | ||
case AssetProxyId.ERC1155: { | ||
const [tokenAddress, tokenIds, tokenValues] = assetDataDecoder.getABIDecodedTransactionData< | ||
[string, BigNumber[], BigNumber[]] | ||
>('ERC1155Assets', assetData); | ||
return { | ||
assetProxyId, | ||
tokenAddress, | ||
tokenIds, | ||
tokenValues, | ||
}; | ||
} | ||
case AssetProxyId.MultiAsset: { | ||
const [amounts, nestedAssetData] = assetDataDecoder.getABIDecodedTransactionData<[BigNumber[], string[]]>( | ||
'MultiAsset', | ||
assetData, | ||
); | ||
return { | ||
assetProxyId, | ||
amounts, | ||
nestedAssetData, | ||
}; | ||
} | ||
case AssetProxyId.StaticCall: | ||
const [callTarget, staticCallData, callResultHash] = assetDataDecoder.getABIDecodedTransactionData< | ||
[string, string, string] | ||
>('StaticCall', assetData); | ||
return { | ||
assetProxyId, | ||
callTarget, | ||
staticCallData, | ||
callResultHash, | ||
}; | ||
default: | ||
throw new Error(`Unhandled asset proxy ID: ${assetProxyId}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 4 additions & 3 deletions
7
contracts/test-utils/src/hex_utils.ts → packages/utils/src/hex_utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters