Skip to content

Commit

Permalink
fix: vector serialization and tx chain commit
Browse files Browse the repository at this point in the history
  • Loading branch information
R0bi7 committed Nov 22, 2024
1 parent c06bd76 commit aacd8bc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
],
"type": "module",
"dependencies": {
"@mysten/bcs": "^1.1.0",
"@mysten/sui": "^1.14.1",
"@mysten/wallet-standard": "^0.13.9",
"@uniswap/permit2-sdk": "^1.3.0",
Expand Down
44 changes: 31 additions & 13 deletions src/services/SuiIntentService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { ChainConfig, CreateIntentOrderPayload, Result, SuiChainConfig } from "../types.js"
import { Transaction, type TransactionResult } from "@mysten/sui/transactions"
import { signAndExecuteTransaction } from "@mysten/wallet-standard"
import { signAndExecuteTransaction, signTransaction } from "@mysten/wallet-standard"
import { SuiProvider, SwapOrder } from "../entities/index.js"
import { stringToBytes } from "viem"
import { bcs } from "@mysten/bcs"

export class SuiIntentService {
private constructor() {}
Expand All @@ -21,6 +22,8 @@ export class SuiIntentService {
toChainConfig: ChainConfig,
provider: SuiProvider,
): Promise<Result<string>> {
console.log("[SuiIntentService.createIntentOrder] payload", payload)

try {
const intent = new SwapOrder(
0n,
Expand All @@ -33,19 +36,19 @@ export class SuiIntentService {
payload.amount,
payload.toToken,
payload.toAmount,
stringToBytes(
JSON.stringify({
quote_uuid: payload.quote_uuid,
}),
),
stringToBytes(JSON.stringify({ quote_uuid: payload.quote_uuid })),
)

const isNative = payload.token.toLowerCase() == fromChainConfig.nativeToken.toLowerCase()

const tx = new Transaction()
let coin = isNative

console.log("[SuiIntentService.createIntentOrder] isNative", isNative)

let coin: any = isNative
? await SuiIntentService.getNativeCoin(tx, intent)
: await SuiIntentService.getCoin(tx, intent.token, intent.amount.valueOf(), provider.account.address, provider)
console.log("[SuiIntentService.createIntentOrder] coin", coin)

tx.moveCall({
target: `${fromChainConfig.packageId}::main::swap`,
Expand All @@ -56,13 +59,16 @@ export class SuiIntentService {
tx.pure.string(intent.toToken),
tx.pure.string(intent.destinationAddress),
tx.pure.u128(intent.toAmount.valueOf()),
tx.pure.vector("vector<u8>", [].slice.call(intent.data)),
tx.pure(bcs.vector(bcs.u8()).serialize(intent.data)),
],
typeArguments: [intent.token],
})

console.log("[SuiIntentService.createIntentOrder] tx after moveCall", tx)

const signerAccount = provider.account
const chain = signerAccount.chains[0]
console.log("[SuiIntentService.signerAccount] tx after moveCall", tx)

if (!chain) {
return {
Expand All @@ -71,12 +77,24 @@ export class SuiIntentService {
}
}

const result = await signAndExecuteTransaction(provider.wallet, {
console.log("[SuiIntentService.signerAccount] chain", chain)

const { bytes, signature } = await signTransaction(provider.wallet, {
transaction: tx,
account: provider.account,
chain: provider.account.chains[0]!,
chain: chain,
})

const result = await provider.client.executeTransactionBlock({
transactionBlock: bytes,
signature,
options: {
showRawEffects: true,
},
})

console.log("[SuiIntentService.signerAccount] signAndExecuteTransaction result", result)

return {
ok: true,
value: result.digest,
Expand Down Expand Up @@ -140,10 +158,10 @@ export class SuiIntentService {
tx: Transaction,
intent: SwapOrder,
): Promise<{ $kind: "NestedResult"; NestedResult: [number, number] }> {
const [coin] = tx.splitCoins(tx.gas, [intent.amount.valueOf()])
const coin = tx.splitCoins(tx.gas, [intent.amount.valueOf()])[0]

if (!coin) {
throw new Error("[SuiIntentService.getNativeCoin] coin undefined")
if (coin == undefined) {
return Promise.reject(Error("[SuiIntentService.getNativeCoin] coin undefined"))
}

return coin
Expand Down

0 comments on commit aacd8bc

Please sign in to comment.