Skip to content

Commit

Permalink
sdk/js: Added optional Sui package ID parameters to functions
Browse files Browse the repository at this point in the history
The package IDs can be passed in to avoid fetching them via RPC
  • Loading branch information
kev1n-peters authored and evan-gray committed Jun 22, 2023
1 parent 817f179 commit 55a3ba6
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 31 deletions.
20 changes: 11 additions & 9 deletions sdk/js/src/sui/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ export const publishCoin = async (
coreBridgeStateObjectId: string,
tokenBridgeStateObjectId: string,
decimals: number,
signerAddress: string
signerAddress: string,
coreBridgePackageId?: string,
tokenBridgePackageId?: string
) => {
const coreBridgePackageId = await getPackageId(
provider,
coreBridgeStateObjectId
);
const tokenBridgePackageId = await getPackageId(
provider,
tokenBridgeStateObjectId
);
[coreBridgePackageId, tokenBridgePackageId] = await Promise.all([
coreBridgePackageId
? Promise.resolve(coreBridgePackageId)
: getPackageId(provider, coreBridgeStateObjectId),
tokenBridgePackageId
? Promise.resolve(tokenBridgePackageId)
: getPackageId(provider, tokenBridgeStateObjectId),
]);
const build = await getCoinBuildOutput(
provider,
coreBridgePackageId,
Expand Down
9 changes: 7 additions & 2 deletions sdk/js/src/token_bridge/__tests__/sui-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,9 @@ describe("Sui SDK tests", () => {
suiProvider,
SUI_CORE_BRIDGE_STATE_OBJECT_ID,
SUI_TOKEN_BRIDGE_STATE_OBJECT_ID,
slicedTransferFromEthVAA
slicedTransferFromEthVAA,
suiCoreBridgePackageId,
suiTokenBridgePackageId
);
const suiRedeemTxResult = await executeTransactionBlock(
suiSigner,
Expand Down Expand Up @@ -467,7 +469,10 @@ describe("Sui SDK tests", () => {
suiProvider,
SUI_CORE_BRIDGE_STATE_OBJECT_ID,
SUI_TOKEN_BRIDGE_STATE_OBJECT_ID,
coin8Type
coin8Type,
BigInt(0),
suiCoreBridgePackageId,
suiTokenBridgePackageId
);
result = await executeTransactionBlock(suiSigner, suiAttestTxPayload);
result.effects?.status.status === "failure" &&
Expand Down
14 changes: 10 additions & 4 deletions sdk/js/src/token_bridge/attest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,22 @@ export async function attestFromSui(
coreBridgeStateObjectId: string,
tokenBridgeStateObjectId: string,
coinType: string,
feeAmount: BigInt = BigInt(0)
feeAmount: BigInt = BigInt(0),
coreBridgePackageId?: string,
tokenBridgePackageId?: string
): Promise<TransactionBlock> {
const metadata = await provider.getCoinMetadata({ coinType });
if (metadata === null || metadata.id === null) {
throw new Error(`Coin metadata ID for type ${coinType} not found`);
}

const [coreBridgePackageId, tokenBridgePackageId] = await Promise.all([
getPackageId(provider, coreBridgeStateObjectId),
getPackageId(provider, tokenBridgeStateObjectId),
[coreBridgePackageId, tokenBridgePackageId] = await Promise.all([
coreBridgePackageId
? Promise.resolve(coreBridgePackageId)
: getPackageId(provider, coreBridgeStateObjectId),
tokenBridgePackageId
? Promise.resolve(tokenBridgePackageId)
: getPackageId(provider, tokenBridgeStateObjectId),
]);
const tx = new TransactionBlock();
const [feeCoin] = tx.splitCoins(tx.gas, [tx.pure(feeAmount)]);
Expand Down
14 changes: 10 additions & 4 deletions sdk/js/src/token_bridge/createWrapped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,17 @@ export async function createWrappedOnSui(
signerAddress: string,
coinPackageId: string,
wrappedAssetSetupType: string,
attestVAA: Uint8Array
attestVAA: Uint8Array,
coreBridgePackageId?: string,
tokenBridgePackageId?: string
): Promise<TransactionBlock> {
const [coreBridgePackageId, tokenBridgePackageId] = await Promise.all([
getPackageId(provider, coreBridgeStateObjectId),
getPackageId(provider, tokenBridgeStateObjectId),
[coreBridgePackageId, tokenBridgePackageId] = await Promise.all([
coreBridgePackageId
? Promise.resolve(coreBridgePackageId)
: getPackageId(provider, coreBridgeStateObjectId),
tokenBridgePackageId
? Promise.resolve(tokenBridgePackageId)
: getPackageId(provider, tokenBridgeStateObjectId),
]);

// Get coin metadata
Expand Down
14 changes: 10 additions & 4 deletions sdk/js/src/token_bridge/redeem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,9 @@ export async function redeemOnSui(
provider: JsonRpcProvider,
coreBridgeStateObjectId: string,
tokenBridgeStateObjectId: string,
transferVAA: Uint8Array
transferVAA: Uint8Array,
coreBridgePackageId?: string,
tokenBridgePackageId?: string
): Promise<TransactionBlock> {
const { tokenAddress, tokenChain } = parseTokenTransferVaa(transferVAA);
const coinType = await getTokenCoinType(
Expand All @@ -380,9 +382,13 @@ export async function redeemOnSui(
throw new Error("Unable to fetch token coinType");
}

const [coreBridgePackageId, tokenBridgePackageId] = await Promise.all([
getPackageId(provider, coreBridgeStateObjectId),
getPackageId(provider, tokenBridgeStateObjectId),
[coreBridgePackageId, tokenBridgePackageId] = await Promise.all([
coreBridgePackageId
? Promise.resolve(coreBridgePackageId)
: getPackageId(provider, coreBridgeStateObjectId),
tokenBridgePackageId
? Promise.resolve(tokenBridgePackageId)
: getPackageId(provider, tokenBridgeStateObjectId),
]);
const tx = new TransactionBlock();
const [verifiedVAA] = tx.moveCall({
Expand Down
14 changes: 10 additions & 4 deletions sdk/js/src/token_bridge/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,9 @@ export async function transferFromSui(
recipient: Uint8Array,
feeAmount: bigint = BigInt(0),
relayerFee: bigint = BigInt(0),
payload: Uint8Array | null = null
payload: Uint8Array | null = null,
coreBridgePackageId?: string,
tokenBridgePackageId?: string
) {
if (payload !== null) {
throw new Error("Sui transfer with payload not implemented");
Expand All @@ -948,9 +950,13 @@ export async function transferFromSui(
);
}

const [coreBridgePackageId, tokenBridgePackageId] = await Promise.all([
getPackageId(provider, coreBridgeStateObjectId),
getPackageId(provider, tokenBridgeStateObjectId),
[coreBridgePackageId, tokenBridgePackageId] = await Promise.all([
coreBridgePackageId
? Promise.resolve(coreBridgePackageId)
: getPackageId(provider, coreBridgeStateObjectId),
tokenBridgePackageId
? Promise.resolve(tokenBridgePackageId)
: getPackageId(provider, tokenBridgeStateObjectId),
]);
const tx = new TransactionBlock();
const [transferCoin] = (() => {
Expand Down
14 changes: 10 additions & 4 deletions sdk/js/src/token_bridge/updateWrapped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,17 @@ export async function updateWrappedOnSui(
coreBridgeStateObjectId: string,
tokenBridgeStateObjectId: string,
coinPackageId: string,
attestVAA: Uint8Array
attestVAA: Uint8Array,
coreBridgePackageId?: string,
tokenBridgePackageId?: string
): Promise<TransactionBlock> {
const [coreBridgePackageId, tokenBridgePackageId] = await Promise.all([
getPackageId(provider, coreBridgeStateObjectId),
getPackageId(provider, tokenBridgeStateObjectId),
[coreBridgePackageId, tokenBridgePackageId] = await Promise.all([
coreBridgePackageId
? Promise.resolve(coreBridgePackageId)
: getPackageId(provider, coreBridgeStateObjectId),
tokenBridgePackageId
? Promise.resolve(tokenBridgePackageId)
: getPackageId(provider, tokenBridgeStateObjectId),
]);

// Get coin metadata
Expand Down

0 comments on commit 55a3ba6

Please sign in to comment.