Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: vector serialization and tx chain commit #24

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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