Skip to content

Commit

Permalink
fix: Sui deposit (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev authored Feb 24, 2025
1 parent a0cef9c commit a79c336
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 66 deletions.
27 changes: 16 additions & 11 deletions packages/localnet/src/suiDeposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,32 @@ import { zetachainDeposit } from "./zetachainDeposit";
import { zetachainSwapToCoverGas } from "./zetachainSwapToCoverGas";

export const suiDeposit = async ({
event,
client,
deployer,
foreignCoins,
fungibleModuleSigner,
gatewayObjectId,
keypair,
moduleId,
protocolContracts,
provider,
args,
withdrawCapObjectId,
}: any) => {
const asset = ethers.ZeroAddress;
const chainID = NetworkID.Sui;
try {
log(chainID, `Gateway deposit event, ${JSON.stringify(args.event)}`);
log(chainID, `Gateway deposit event, ${JSON.stringify(event)}`);
await zetachainDeposit({
args: [null, args.receiver, args.amount, asset],
args: [null, event.receiver, event.amount, asset],
chainID,
foreignCoins,
fungibleModuleSigner,
protocolContracts,
});
} catch (e) {
const { revertGasFee } = await zetachainSwapToCoverGas({
amount: args.amount,
amount: event.amount,
asset,
chainID,
deployer,
Expand All @@ -37,16 +42,16 @@ export const suiDeposit = async ({
protocolContracts,
provider,
});
const revertAmount = BigInt(args.amount) - revertGasFee;
const revertAmount = BigInt(event.amount) - revertGasFee;
if (revertAmount > 0) {
await suiWithdraw({
amount: revertAmount,
client: args.client,
gatewayObjectId: args.gatewayObjectId,
keypair: args.keypair,
moduleId: args.moduleId,
sender: args.sender,
withdrawCapObjectId: args.withdrawCapObjectId,
client: client,
gatewayObjectId: gatewayObjectId,
keypair: keypair,
moduleId: moduleId,
sender: event.sender,
withdrawCapObjectId: withdrawCapObjectId,
});
} else {
console.error(
Expand Down
29 changes: 17 additions & 12 deletions packages/localnet/src/suiDepositAndCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,28 @@ import { zetachainDepositAndCall } from "./zetachainDepositAndCall";
import { zetachainSwapToCoverGas } from "./zetachainSwapToCoverGas";

export const suiDepositAndCall = async ({
event,
client,
deployer,
foreignCoins,
fungibleModuleSigner,
gatewayObjectId,
keypair,
moduleId,
protocolContracts,
provider,
args,
withdrawCapObjectId,
}: any) => {
const asset = ethers.ZeroAddress;
const chainID = NetworkID.Sui;
try {
log(
NetworkID.Sui,
`Gateway deposit and call event, ${JSON.stringify(args.event)}`
`Gateway deposit and call event, ${JSON.stringify(event)}`
);
const message = ethers.hexlify(new Uint8Array(args.payload));
const message = ethers.hexlify(new Uint8Array(event.payload));
await zetachainDepositAndCall({
args: [args.sender, args.receiver, args.amount, asset, message],
args: [event.sender, event.receiver, event.amount, asset, message],
chainID,
foreignCoins,
fungibleModuleSigner,
Expand All @@ -32,7 +37,7 @@ export const suiDepositAndCall = async ({
});
} catch (e) {
const { revertGasFee } = await zetachainSwapToCoverGas({
amount: args.amount,
amount: event.amount,
asset,
chainID,
deployer,
Expand All @@ -42,16 +47,16 @@ export const suiDepositAndCall = async ({
protocolContracts,
provider,
});
const revertAmount = BigInt(args.amount) - revertGasFee;
const revertAmount = BigInt(event.amount) - revertGasFee;
if (revertAmount > 0) {
await suiWithdraw({
amount: revertAmount,
client: args.client,
gatewayObjectId: args.gatewayObjectId,
keypair: args.keypair,
moduleId: args.moduleId,
sender: args.sender,
withdrawCapObjectId: args.withdrawCapObjectId,
client: client,
gatewayObjectId: gatewayObjectId,
keypair: keypair,
moduleId: moduleId,
sender: event.sender,
withdrawCapObjectId: withdrawCapObjectId,
});
} else {
console.error(
Expand Down
64 changes: 23 additions & 41 deletions packages/localnet/src/suiSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,12 @@ export const suiSetup = async ({
deployer,
foreignCoins,
fungibleModuleSigner,
gatewayObjectId,
keypair,
moduleId,
protocolContracts,
provider,
withdrawCapObjectId,
});

return {
Expand Down Expand Up @@ -202,55 +205,34 @@ const waitForConfirmation = async (
throw new Error(`Timeout waiting for confirmation: ${digest}`);
};

const pollEvents = async ({
client,
moduleId,
deployer,
foreignCoins,
fungibleModuleSigner,
protocolContracts,
provider,
}: any) => {
const pollEvents = async (context: any) => {
let currentCursor: EventId | null | undefined = null;
const POLLING_INTERVAL_MS = 3000;
const DEPOSIT_EVENT_TYPE = `${moduleId}::gateway::DepositEvent`;
const DEPOSIT_AND_CALL_EVENT_TYPE = `${moduleId}::gateway::DepositAndCallEvent`;
const DEPOSIT_EVENT = `${context.moduleId}::gateway::DepositEvent`;
const DEPOSIT_AND_CALL_EVENT = `${context.moduleId}::gateway::DepositAndCallEvent`;

while (true) {
try {
const { data, hasNextPage, nextCursor }: any = await client.queryEvents({
cursor: currentCursor || null,
limit: 50,
order: "ascending",
query: {
MoveEventModule: {
module: "gateway",
package: moduleId,
const { data, hasNextPage, nextCursor }: any =
await context.client.queryEvents({
cursor: currentCursor || null,
limit: 50,
order: "ascending",
query: {
MoveEventModule: {
module: "gateway",
package: context.moduleId,
},
},
},
});
});

if (data.length > 0) {
for (const event of data) {
const { amount, receiver, sender, payload } = event.parsedJson as any;
if (event.type === DEPOSIT_EVENT_TYPE) {
suiDeposit({
args: { amount, payload, receiver, sender },
deployer,
foreignCoins,
fungibleModuleSigner,
protocolContracts,
provider,
});
} else if (event.type === DEPOSIT_AND_CALL_EVENT_TYPE) {
suiDepositAndCall({
args: { amount, payload, receiver, sender },
deployer,
foreignCoins,
fungibleModuleSigner,
protocolContracts,
provider,
});
for (const eventData of data) {
const event = eventData.parsedJson;
if (eventData.type === DEPOSIT_EVENT) {
suiDeposit({ event, ...context });
} else if (eventData.type === DEPOSIT_AND_CALL_EVENT) {
suiDepositAndCall({ event, ...context });
}
}

Expand Down
2 changes: 0 additions & 2 deletions packages/localnet/src/zetachainWithdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ export const zetachainWithdraw = async ({
const receiverAddress = ethers.toUtf8String(receiver);
await solanaWithdraw({
amount: amount,
// FIX THIS
decimals: 9,

mint: asset,
recipient: receiverAddress,
});
Expand Down
9 changes: 9 additions & 0 deletions packages/tasks/src/suiDepositAndCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ const suiDepositAndCall = async (args: any) => {
signer: keypair,
transaction: tx,
});

const event = result.events?.find((evt) =>
evt.type.includes("gateway::DepositAndCallEvent")
);
if (event) {
console.log("Event:", event.parsedJson);
} else {
console.log("No Deposit Event found.");
}
};

export const suiDepositAndCallTask = task(
Expand Down

0 comments on commit a79c336

Please sign in to comment.