-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initialize channel connection and connect to ws
- Loading branch information
1 parent
f49ac3e
commit 2255295
Showing
11 changed files
with
341 additions
and
6,027 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<script setup lang="ts"> | ||
import { onMounted, ref } from 'vue'; | ||
import { Channel } from '@aeternity/aepp-sdk'; | ||
import { EncodedData } from '@aeternity/aepp-sdk/es/utils/encoder'; | ||
import { createAccount } from '../sdk/sdk'; | ||
import { ChannelOptions } from '@aeternity/aepp-sdk/es/channel/internal'; | ||
const account = await createAccount(); | ||
const channelConfig = ref<ChannelOptions>(); | ||
const loading = ref(true); | ||
const error = ref<{ message?: string }>({}); | ||
const channelStatus = ref('Initializing'); | ||
function registerEvents(channel: Channel) { | ||
channel.on('statusChanged', (status) => { | ||
channelStatus.value = status; | ||
}); | ||
} | ||
async function getChannelConfig() { | ||
loading.value = true; | ||
return fetch(import.meta.env.VITE_BOT_SERVICE_URL + '/open', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
address: account.selectedAddress, | ||
port: '3001', | ||
host: 'node', | ||
}), | ||
}) | ||
.then((res) => { | ||
// a non-200 response code | ||
if (!res.ok) { | ||
// create error instance with HTTP status text | ||
const error: Error & { json?: Promise<unknown> } = new Error( | ||
res.statusText | ||
); | ||
error.json = res.json(); | ||
throw error; | ||
} | ||
return res.json(); | ||
}) | ||
.then((json) => { | ||
// set the response data | ||
completeChannelConfig(json); | ||
channelConfig.value = json; | ||
}) | ||
.catch((err) => { | ||
error.value = err; | ||
// In case a custom JSON error response was provided | ||
if (err.json) { | ||
return err.json.then((json: any) => { | ||
// set the JSON response message | ||
error.value.message = json.message; | ||
}); | ||
} | ||
}) | ||
.then(() => { | ||
loading.value = false; | ||
}); | ||
} | ||
function completeChannelConfig(channelConf: ChannelOptions) { | ||
Object.assign(channelConf, { | ||
role: 'responder', | ||
sign: (_tag: string, tx: EncodedData<'tx'>) => account.signTransaction(tx), | ||
url: | ||
import.meta.env.VITE_NODE_ENV == 'development' | ||
? 'ws://localhost:3014/channel' | ||
: channelConf.url, | ||
}); | ||
} | ||
onMounted(async () => { | ||
await getChannelConfig(); | ||
if (channelConfig.value == null) return; | ||
const responderCh = await Channel.initialize(channelConfig.value); | ||
registerEvents(responderCh); | ||
}); | ||
</script> | ||
|
||
<template> | ||
<h1>Create channel</h1> | ||
<div>Status: {{ channelStatus }}</div> | ||
<div v-if="!loading && channelConfig"> | ||
<pre>{{ JSON.stringify(channelConfig, null, 2) }}</pre> | ||
</div> | ||
|
||
<p v-if="loading">Still loading..</p> | ||
<p v-if="error"></p> | ||
</template> | ||
|
||
<style scoped></style> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { | ||
AeSdk, | ||
Node, | ||
generateKeyPair, | ||
Channel, | ||
MemoryAccount, | ||
} from '@aeternity/aepp-sdk'; | ||
|
||
export const url = | ||
`${import.meta.env.VITE_NODE_URL}:${import.meta.env.VITE_NODE_PORT}` ?? | ||
'http://localhost:3013'; | ||
export const compilerUrl = | ||
import.meta.env.VITE_COMPILER_URL ?? 'http://localhost:3080'; | ||
export const networkId = import.meta.env.VITE_NETWORK_ID ?? 'ae_devnet'; | ||
export const ignoreVersion = import.meta.env.IGNORE_VERSION === 'true'; | ||
export const ACCOUNT_KEYPAIR = generateKeyPair(); | ||
|
||
export async function createAccount() { | ||
const account = new MemoryAccount({ keypair: ACCOUNT_KEYPAIR }); | ||
const node = new Node(url); | ||
const aeSdk = new AeSdk({ | ||
nodes: [{ name: 'testnet', instance: node }], | ||
}); | ||
await aeSdk.addAccount(account, { select: true }); | ||
return aeSdk; | ||
} | ||
|
||
export async function waitForChannel(channel: Channel): Promise<void> { | ||
return new Promise((resolve) => { | ||
channel.on('statusChanged', (status: string) => { | ||
console.log(status); | ||
if (status === 'open') { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,5 @@ | ||
import { mount } from '@vue/test-utils'; | ||
import { test, expect } from 'vitest'; | ||
import HelloWorld from '../src/components/HelloWorld.vue'; | ||
|
||
test('mount component', async () => { | ||
expect(HelloWorld).toBeTruthy(); | ||
|
||
const wrapper = mount(HelloWorld, { | ||
props: { | ||
msg: 'Hello World!', | ||
}, | ||
}); | ||
|
||
expect(wrapper.text()).toContain('Hello World!'); | ||
expect(wrapper.html()).toMatchSnapshot(); | ||
test('temp test', async () => { | ||
expect(true).toBeTruthy(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,7 @@ export default defineConfig({ | |
}, | ||
port: Number(process.env.PORT) || 8000, | ||
}, | ||
build: { | ||
target: 'es2020', | ||
}, | ||
}); |