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

Aptos tssdk #4099

Merged
merged 16 commits into from
Mar 25, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
chore(ts-sdk): added transfer fa-token example
Signed-off-by: Kaan Caglan <[email protected]>
Caglankaan committed Mar 24, 2025
commit 40706fdb34e664eebe08679f076081eb5a8b0944
84 changes: 84 additions & 0 deletions ts-sdk/examples/aptos-write-transfer-fa-token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { Effect } from "effect"
import { AptosPublicClient, createAptosPublicClient } from "../src/aptos/client.ts"
import { queryContract, executeContractWithKey } from "../src/aptos/contract.ts"
import { waitForTransactionReceipt } from "../src/aptos/receipts.ts"
import { Account, Ed25519PrivateKey } from "@aptos-labs/ts-sdk"
import { Aptos, AptosConfig, Network, AptosApiError, MoveVector } from "@aptos-labs/ts-sdk"

// @ts-ignore
BigInt["prototype"].toJSON = function () {
return this.toString()
}

// Replace with your private key
const PRIVATE_KEY =
process.env.PRIVATE_KEY || "0x0000000000000000000000000000000000000000000000000000000000000000"

Effect.runPromiseExit(
Effect.gen(function* () {
// Create account from private key
const privateKey = new Ed25519PrivateKey(PRIVATE_KEY)
const account = Account.fromPrivateKey({ privateKey })

const rpcUrl = "https://aptos.testnet.bardock.movementlabs.xyz/v1"

const config = new AptosConfig({
fullnode: rpcUrl,
network: Network.CUSTOM
})
const publicClient = yield* createAptosPublicClient(config)

const zkgm_address = "0x80a825c8878d4e22f459f76e581cb477d82f0222e136b06f01ad146e2ae9ed84"
const real_token_address = "0x188b41399546602e35658962477fdf72bd52443474a899d9d48636e8bc299c2c"
const token_address = "0x6d756e6f"
const function_name = "ibc_app::predict_wrapped_token"
const typeArguments = []
const destination_channel_id = "2"
const base_token = MoveVector.U8(token_address)
const functionArguments = [0, destination_channel_id, base_token]

const result = yield* queryContract(publicClient, zkgm_address, function_name, typeArguments, functionArguments).pipe(
Effect.provideService(AptosPublicClient, { client: publicClient })
)
yield * Effect.log("Result:", result)

if (result[0] === real_token_address){
yield * Effect.log("Success")
} else {
yield * Effect.logError("Failure")
}

const contract_address = "0x1"
const balance_function_name = "primary_fungible_store::balance"
const balance_typeArguments = ["0x1::fungible_asset::Metadata"]
const balance_functionArguments = [account.accountAddress.toString(), result[0]]

const result_balance = yield* queryContract(publicClient, contract_address, balance_function_name, balance_typeArguments, balance_functionArguments).pipe(
Effect.provideService(AptosPublicClient, { client: publicClient })
)
yield * Effect.log("Result Balance:", result_balance)


const transfer_function_name = "primary_fungible_store::transfer"
const transfer_typeArguments = ["0x1::fungible_asset::Metadata"]
const receiver_address = "0x9ec0ea9b728dd1aa4f0b9b779e7f885099bcea7d28f88f357982d7de746183c9"
const transfer_amount = 1
const transfer_functionArguments = [result[0], receiver_address, transfer_amount]

yield * Effect.log("transfer_functionArguments:", transfer_functionArguments)


const result_execute = yield* executeContractWithKey(publicClient, account, contract_address,
transfer_function_name, transfer_typeArguments, transfer_functionArguments).pipe(
Effect.provideService(AptosPublicClient, { client: publicClient })
)

const txHash = yield* waitForTransactionReceipt(result_execute.hash).pipe(
Effect.provideService(AptosPublicClient, { client: publicClient })
)
yield * Effect.log("transaction receipt:", txHash)



})
).then(exit => console.log(JSON.stringify(exit, null, 2)))
53 changes: 40 additions & 13 deletions ts-sdk/src/aptos/contract.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Effect, Data } from "effect"
import { Aptos, AptosConfig, Network, AptosApiError } from "@aptos-labs/ts-sdk"
import { Aptos, AptosConfig, Network, AptosApiError, type Account as AptosAccount } from "@aptos-labs/ts-sdk"
import { extractErrorDetails } from "../utils/extract-error-details.js"
import type { AptosBrowserWallet } from "./wallet.js"
import { waitForTransactionReceipt } from "./receipts.js"

/**
* Error type for Aptos contract query failures
@@ -41,24 +42,50 @@ export const queryContract = <T = unknown>(
}).pipe(Effect.timeout("10 seconds"), Effect.retry({ times: 5 }))

// TODO: add comments
export const executeContract = (
client: Aptos|AptosBrowserWallet,
export const executeContractWithWallet = (
client: AptosBrowserWallet,
contractAddress: string,
function_name: string, // `ibc_app::predict_wrapped_token` as an example.
typeArguments: Array<TypeArgument>,
functionArguments: Array<EntryFunctionArgumentTypes | SimpleEntryFunctionArgumentTypes>
typeArguments: Array<any>,
functionArguments: Array<any>
) =>
Effect.tryPromise({
try: async () => {
const payload = await client.view({
payload: {
function: `${contractAddress}::${function_name}`,
typeArguments: typeArguments,
functionArguments: functionArguments
}
})
const result = await client.signAndSubmitTransaction({ payload })
const walletPayload = {
function: `${contractAddress}::${function_name}`,
typeArguments: typeArguments,
functionArguments: functionArguments
}
const result = await client.signAndSubmitTransaction({ payload: walletPayload })
return result
},
catch: error => new ExecuteContractError({ cause: extractErrorDetails(error as Error) })
})


export const executeContractWithKey = (
client: Aptos,
signer: AptosAccount,
contractAddress: string,
function_name: string, // `ibc_app::predict_wrapped_token` as an example.
typeArguments: Array<any>,
functionArguments: Array<any>
) =>
Effect.tryPromise({
try: async () => {
const payload = await client.transaction.build.simple({
sender: signer.accountAddress,
data: {
function: `${contractAddress}::${function_name}`,
typeArguments: typeArguments,
functionArguments: functionArguments
}
})

const txn = await client.signAndSubmitTransaction({ signer: signer, transaction: payload})
return txn

},
catch: error => new ExecuteContractError({ cause: extractErrorDetails(error as Error) })
})

7 changes: 5 additions & 2 deletions ts-sdk/src/aptos/receipts.ts
Original file line number Diff line number Diff line change
@@ -21,12 +21,15 @@ export const waitForTransactionReceipt = (hash: Hash) =>
const client = (yield* AptosPublicClient).client

const receipt = yield* Effect.tryPromise({
try: () => client.waitForTransaction({ hash, options: { checkSuccess: false } }),
try: () => client.waitForTransaction({
transactionHash: hash,
options: { checkSuccess: false }
}),
catch: err =>
new WaitForTransactionReceiptError({
cause: extractErrorDetails(err as WaitForTransactionReceiptError)
})
})
}).pipe(Effect.timeout("10 seconds"), Effect.retry({ times: 3 }))

return receipt
})