Skip to content

Commit 9f5e12d

Browse files
committed
feat: pub tssdk
1 parent f9d4d18 commit 9f5e12d

File tree

8 files changed

+379
-51
lines changed

8 files changed

+379
-51
lines changed

.vscode/settings.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
22
"editor.formatOnSave": true,
3-
"editor.defaultFormatter": "esbenp.prettier-vscode"
3+
"editor.defaultFormatter": "esbenp.prettier-vscode",
4+
"editor.codeActionsOnSave": {
5+
"source.organizeImports": "explicit"
6+
},
7+
"vue.codeActions.enabled": true
48
}

apps/web-app/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"preview": "vite preview"
1010
},
1111
"dependencies": {
12+
"@aptos-labs/ts-sdk": "^1.12.2",
13+
"@aptos-labs/wallet-adapter-core": "^3.10.0",
1214
"@boltbunny/ts-sdk": "workspace:^",
1315
"petra-plugin-wallet-adapter": "^0.4.5",
1416
"vue": "^3.4.21"
@@ -17,6 +19,7 @@
1719
"@vitejs/plugin-vue": "^5.0.4",
1820
"typescript": "^5.2.2",
1921
"vite": "^5.2.0",
22+
"vite-plugin-mkcert": "^1.17.5",
2023
"vue-tsc": "^2.0.6"
2124
}
2225
}

apps/web-app/src/App.vue

+78-13
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,56 @@
77
<span class="font-bold">{{ address }}</span>
88
</span>
99

10-
<labe class="input input-bordered w-full flex items-center gap-2">
10+
<label class="input input-bordered w-full flex items-center gap-2">
1111
<span class="w-24 text-gray">Transfer to:</span>
1212
<input v-model="reciept" type="text" placeholder="Type here" class="w-full" />
13-
</labe>
14-
<button class="btn btn-primary" @click="transfer">Transfer 0.01 APT</button>
13+
</label>
14+
15+
<div class="grid grid-cols-2 gap-2">
16+
<button class="btn btn-primary" @click="bbClientTransfer">
17+
Transfer 0.01 APT By boltbunny SDK
18+
</button>
19+
<button class="btn btn-primary" @click="transfer">Transfer 0.01 APT</button>
20+
</div>
1521
</div>
1622
</div>
1723
</template>
1824

1925
<script setup lang="ts">
26+
import {
27+
AccountAddress,
28+
Aptos,
29+
AptosConfig,
30+
Network,
31+
SimpleTransaction,
32+
} from '@aptos-labs/ts-sdk';
33+
import { WalletCore } from '@aptos-labs/wallet-adapter-core';
34+
import { BoltBunnyClient } from '@boltbunny/ts-sdk';
35+
import { PetraWallet } from 'petra-plugin-wallet-adapter';
2036
import { onMounted, ref } from 'vue';
37+
const wallets = [new PetraWallet()];
38+
const walletCore: WalletCore = new WalletCore(wallets);
39+
40+
walletCore.on('connect', () => {
41+
console.log(walletCore.account);
42+
address.value = walletCore.account!.address;
43+
});
2144
2245
const address = ref('');
46+
const bbClient = new BoltBunnyClient({
47+
APISecretKey: 'bb_TESTNET_0_5cGVyv7ZHwXDSnT655ojZXLjJdDfG11B',
48+
network: Network.TESTNET,
49+
});
50+
51+
const AptosClient = new Aptos(
52+
new AptosConfig({
53+
network: Network.TESTNET,
54+
}),
55+
);
2356
2457
const connectWallet = async () => {
25-
const result: any = await window.petra.connect();
26-
address.value = result.address;
27-
window.localStorage.setItem('petra-wallet', 'yes');
58+
await walletCore.connect('Petra');
59+
window.localStorage.setItem('petra-wallet', 'true');
2860
};
2961
3062
const init = async () => {
@@ -33,9 +65,39 @@
3365
}
3466
};
3567
68+
console.log(AccountAddress.from('0x1').toStringLong());
69+
70+
const bbClientTransfer = async () => {
71+
const txn: SimpleTransaction = await AptosClient.transaction.build.simple({
72+
sender: address.value,
73+
data: {
74+
typeArguments: [
75+
'0x0000000000000000000000000000000000000000000000000000000000000001::aptos_coin::AptosCoin',
76+
],
77+
functionArguments: [reciept.value, '101'],
78+
function:
79+
'0x0000000000000000000000000000000000000000000000000000000000000001::coin::transfer',
80+
},
81+
withFeePayer: true,
82+
});
83+
84+
const signature = await walletCore.signTransaction(txn);
85+
86+
try {
87+
const result: any = await bbClient.sendTransaction({
88+
signature: signature.bcsToHex().toString(),
89+
transaction: txn.rawTransaction.bcsToHex().toString(),
90+
});
91+
console.log(result);
92+
alert(`Transaction submitted: ${result.submitTransaction.transactionHash}`);
93+
} catch (e: any) {
94+
alert('Error: ' + e.message);
95+
}
96+
};
97+
3698
const reciept = ref('0xe51c64d1f2595557676053e03ce637f9fe26af706c156cae7fff3ad4893ccc53');
3799
const transfer = async () => {
38-
const networkInfo: any = await window.petra.getNetwork();
100+
const networkInfo: any = walletCore?.network;
39101
console.log(networkInfo);
40102
41103
if (networkInfo.chainId != 2) {
@@ -53,13 +115,16 @@
53115
return;
54116
}
55117
56-
const result: any = await window.petra.signAndSubmitTransaction({
57-
arguments: [reciept.value, '101'],
58-
function: '0x1::coin::transfer',
59-
type: 'entry_function_payload',
60-
type_arguments: ['0x1::aptos_coin::AptosCoin'],
118+
const txn: any = await walletCore.signAndSubmitTransaction({
119+
data: {
120+
functionArguments: [reciept.value, '101'],
121+
function: '0x1::coin::transfer',
122+
typeArguments: ['0x1::aptos_coin::AptosCoin'],
123+
},
124+
});
125+
const result: any = await AptosClient.waitForTransaction({
126+
transactionHash: txn.hash,
61127
});
62-
console.log(result);
63128
alert(`Transaction submitted: ${result.hash}, ${result.vm_status}`);
64129
};
65130

apps/web-app/vite.config.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
import { defineConfig } from 'vite';
21
import vue from '@vitejs/plugin-vue';
2+
import { defineConfig } from 'vite';
3+
import mkcert from 'vite-plugin-mkcert';
34

45
// https://vitejs.dev/config/
56
export default defineConfig({
6-
plugins: [vue()],
7+
plugins: [
8+
vue(),
9+
mkcert({
10+
hosts: ['locahost', '127.0.0.1'],
11+
}),
12+
],
713
});

package.json

-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
"name": "boltbunny",
33
"version": "1.0.0",
44
"description": "",
5-
"main": "index.js",
65
"scripts": {
76
"test": "echo \"Error: no test specified\" && exit 1",
87
"lint": "eslint .",
98
"changeset-version": "pnpm changeset version",
109
"dev:sdk": "pnpm ts-sdk build --watch",
1110
"format": "prettier --write .",
12-
"prepare": "husky install",
1311
"ts-sdk": "pnpm --filter ts-sdk",
1412
"app": "pnpm --filter web-app"
1513
},

packages/ts-sdk/package.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
22
"name": "@boltbunny/ts-sdk",
3-
"private": true,
4-
"version": "0.0.0",
3+
"version": "0.0.2",
54
"type": "module",
65
"license": "Apache-2.0",
76
"author": "",
87
"main": "./dist/ts-sdk.js",
9-
"types": "./dist/main.d.ts",
8+
"types": "./dist/index.d.ts",
109
"scripts": {
1110
"dev": "vite",
1211
"build": "tsc && vite build",
13-
"preview": "vite preview"
12+
"preview": "vite preview",
13+
"pub": "pnpm publish --no-git-checks --access public"
1414
},
1515
"devDependencies": {
1616
"typescript": "^5.4.5",
@@ -29,7 +29,8 @@
2929
},
3030
"homepage": "",
3131
"dependencies": {
32-
"@aptos-labs/ts-sdk": "^1.12.2"
32+
"@aptos-labs/ts-sdk": "^1.12.2",
33+
"graphql-request": "^6.1.0"
3334
},
3435
"peerDependencies": {
3536
"@aptos-labs/ts-sdk": "^1.12.2"

packages/ts-sdk/src/utils/Transaction.ts

+50-11
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,65 @@
11
import { Network } from '@aptos-labs/ts-sdk';
2+
import { GraphQLClient, gql } from 'graphql-request';
23

34
interface BoltBunnyClientConstructorParams {
45
APISecretKey: string;
5-
network: Network;
6+
network: Network.TESTNET | Network.MAINNET;
67
}
78

8-
class BoltBunnyClient {
9+
const GRAPHQL_URL: Record<Network.TESTNET | Network.MAINNET, string> = {
10+
[Network.TESTNET]: 'https://hasura.groupwar.xyz/v1/graphql',
11+
[Network.MAINNET]: 'https://hasura.alcove.pro/v1/graphql',
12+
};
13+
14+
export class BoltBunnyClient {
915
private APISecretKey: string;
16+
private network: Network.TESTNET | Network.MAINNET;
1017

1118
constructor(options: BoltBunnyClientConstructorParams) {
1219
this.APISecretKey = options.APISecretKey;
20+
this.network = options.network;
1321
}
1422

15-
async isActived() {
16-
console.log('Checking if the client is actived with the key');
17-
}
23+
/**
24+
* @param {object} params - Include the signature and the transaction
25+
* @param {string} params.signature - The signature of signed by user
26+
* @param {string} params.transaction - The transaction to be sent
27+
* @returns
28+
*
29+
* @example
30+
* const a = 1;
31+
* const txn: SimpleTransaction = await aptos.transaction.build.simple({
32+
* ...
33+
* });
34+
* const signature = await walletCore.signTransaction(txn);
35+
*
36+
* const result: any = await bbClient.sendTransaction({
37+
* signature: signature.bcsToHex().toString(),
38+
* transaction: txn.rawTransaction.bcsToHex().toString(),
39+
* });
40+
*
41+
*/
42+
async sendTransaction(params: { signature: string; transaction: string }): Promise<any> {
43+
if (!this.APISecretKey) throw new Error('APISecretKey is required');
44+
if (!GRAPHQL_URL[this.network]) throw new Error('Invalid network');
1845

19-
async sendTransaction() {
20-
// key in header
21-
console.log(this.APISecretKey);
22-
console.log('Sending transaction');
46+
const GQL_Client = new GraphQLClient(GRAPHQL_URL[this.network], {
47+
headers: () => ({
48+
'x-alcove-app-secret': this.APISecretKey,
49+
}),
50+
});
51+
52+
return await GQL_Client.request(
53+
gql`
54+
mutation submitTransaction($signature: String!, $transaction: String!) {
55+
submitTransaction(txnParams: { signature: $signature, transaction: $transaction }) {
56+
transactionHash
57+
}
58+
}
59+
`,
60+
{
61+
...params,
62+
},
63+
);
2364
}
2465
}
25-
26-
console.log(BoltBunnyClient);

0 commit comments

Comments
 (0)