bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 20쪽 중 17쪽
Solana = ZebecSolBridgeClient.getProxyUserKey(
withdrawer,
sourceChain,
SOL_ZEBEC_BRIDGE_ADDRESS,
);
const tokenAddrInSolana = "<solana mint key>";
const amount = "1";
const reciept = await ethClient.withdraw(amount, withdrawer, tokenAddrInSolana);
```
Now you can find signed Vaa bytes of your payload sent from wormhole guardian rpc hosts.
```typescript
const sequence = parseSequenceFromLogEth(
receipt,
getBridgeAddressForChain(sourceChain)
);
const zebecEmitterAddress = getEmitterAddressEth(BSC_ZEBEC_BRIDGE_ADDRESS);
const { vaaBytes } = await getSignedVAAWithRetry(
WORMHOLE_RPC_HOSTS,
sourceChain,
zebecEmitterAddress,
sequence
);
```
Now post this signed vaa to wormhole solana core bridge program, parse the payloads from vaa and send the parsed data to solana zebec bridge program. Posting vaa and calling solana bridge (also called as proxy) program. This is optional because it's handled by zebec's specialized relayer.
```typescript
const payerAddress = wallet.publicKey.toString();
const bridgeAddress = getBridgeAddressForChain(targetChain);
const tokenBridgeAddress = getTokenBridgeAddressForChain(targetChain);
const vaaBuf = Buffer.from(vaaBytes);
setDefaultWasm("node"); // use bundler for browser
const { parse_vaa } = await importCoreWasm();
// posting vaa in solana
await postVaaSolanaWithRetry(
connection,
wallet.signTransaction,
bridgeAddress,
payerAddress,
vaaBuf,
MAX_VAA_UPLOAD_RETRIES_SOLANA,
);
const { parse_vaa } = await importCoreWasm();
const parsedVaa = parse_vaa(tes)
const parsedPayload = parseZebecPayload(Buffer.from(parsedVaa.payload));
const result = await solanaClient.withdrawDeposit(withdrawVaa, parsedPayload as TokenWithdrawPayload);
```
# Token Transfer
Transfer assets from proxy account in Solana to EVM.
Token transfer can be done in following way.
```typescript
const amount = "0.1";
const sender = signer.address;
const tokenMint = "AbLwGR8A1wvsiLWrzzA5eYPoQw51NVMcMMTPvAv5LTJ";
const receiver = sender;
const sourceChain = CHAIN_ID_BSC;
const receipt = await ethClient.directTransfer(amount, sender, tokenMint, receiver);
```
You might have noticed that receiver and sender are same here, it's because the transfer will performed from the sender's proxy account in Solana to own account in evm.
Then the vaa can be obtained after this and it must be posted in solana chain.
```typescript
const sequence = parseSequenceFromLogEth(receipt, getBridgeAddressForChain(sourceChain));
const emitterAddress = getEmitterAddressEth(BSC_ZEBEC_BRIDGE_ADDRESS);
const { vaaBytes } = await getSignedVAAWithRetry(
WORMHOLE_RPC_HOSTS,
sourceChain,
emitterAddress,
sequence,
);
```
Now post this signed vaa to wormhole solana core bridge program, parse the payloads from vaa and send the parsed data to solana zebec bridge program.
```typescript
const payerAddress = wallet.publicKey.toString();
const bridgeAddress = getBridgeAddressForChain(targetChain);
const vaaBuf = Buffer.from(vaaBytes);
setDefaultWasm("node"); // use bundler for browser
// posting vaa in solana
await postVaaSolanaWithRetry(
connection,
wallet.signTransaction,
bridgeAddress,
payerAddress,
vaaBuf,
MAX_VAA_UPLOAD_RETRIES_SOLANA,
);
const { parse_vaa } = await importCoreWasm();
const parsedVaa = parse_vaa(vaaBytes);
const parsedPayload = parseZebecPayload(Buffer.from(parsedVaa.payload));
const result = await solanaClient.directTokenTransfer(
vaaBytes,
<DirectTokenTransferPayload>parsedPayload
);
```
Then the zebec bridge program makes cpi call to token bridge program for asset transfer to evm chain. The asset is automatically redeemed by the token bridge relayer. For manual redeeming, you need to get signed vaa by emitted from token bridge and redeem asset in evm using the payload in that vaa.
<pre class="language-typescript"><code class="lang-typescript">if (!result.data) {
throw new Error("data is missing in solana client result.");
}
const executionSignature = result.data.signatures[result.data.signatures.length - 1];
<strong>const transactionResponse = await connection.getTransaction(executionSignature);
</strong>
if (!transactionResponse) {
throw new Error("Could not find Transaction");
}
const anotherSequence = parseSequenceFromLogSolana(transactionResponse);
const tokenBridgeEmitterAddress = await getEmitterAddressSolana(SOL_TOKEN_BRIDGE_ADDRESS);
const { vaaBytes: tokenBridgeVaa } = await getSignedVAAWithRetry(
WORMHOLE_RPC_HOSTS,
"solana",
tokenBridgeEmitterAddress,
anotherSequence,
);
const tokenBridgeAddress = getTokenBridgeAddressForChain(sourceChain);
const bridge = Bridge__factory.connect(tokenBridgeAddress, signer);
const tx = await bridge.completeTransfer(tokenBridgeVaa);
const receipt = await tx.wait();
console.log("receipt", receipt.transactionHash);
</code></pre>
# Init Stream
Asset stream initialization can be performed in following way.
```typescript
const now = nowInSec();
const startTime = now + 180;
/*
* three min added deliberately because it takes times for your payload to hit
* zebec program in solana
**/
const endTime = startTime + 6000; // 100 min
const streamAmount = "1";
const receiver = <evm address>;
const sender = signer.address;
const canCancel = true;
const canUpdate = true;
const tokenMint = "<solana mint address>";
const sourceChain = CHAIN_ID_BSC;
const receipt = await ethClient.initStream(
startTime.toString(),
endTime.toString(),
streamAmount,
receiver,
sender,
canCancel,
canUpdate,
tokenMint,
);
```
Now you can find signed Vaa bytes of your payload sent from wormhole guardian rpc hosts.
```typescript
const sequence = parseSequenceFromLogEth(receipt, getBridgeAddressForChain(sourceChain));
const zebecEmitterAddress = getEmitterAddressEth(BSC_ZEBEC_BRIDGE_ADDRESS);
const { vaaBytes } = await getSignedVAAWithRetry(
WORMHOLE_RPC_HOSTS,
sourceChain,
zebecEmitterAddress,
sequence,
);
```
Now post this signed vaa to wormhole solana core bridge program, parse the payloads from vaa and send the parsed data to solana zebec bridge program. This part is optional because it's handled by zebec's specialized relayer.
```typescript
const payerAddress = wallet.publicKey.toString();
const bridgeAddress = getBridgeAddressForChain(targetChain);
const vaaBuf = Buffer.from(vaaBytes);
setDefaultWasm("node"); // use bundler for browser
// posting vaa in solana
await postVaaSolanaWithRetry(
connection,
wallet.signTransaction,
bridgeAddress,
payerAddress,
vaaBuf,
MAX_VAA_UPLOAD_RETRIES_SOLANA,
);
const parsedVaa = parse_vaa(streamVaa);
const parsedPayload = parseZebecPayload(Buffer.from(parsedVaa.payload));
const result = await solanaClient.initializeStream(streamVaa, <TokenStreamPayload>parsedPayload);
console.log(result);
```
# Pause/Resume Stream
Pause and resume stream both can be performed using same method means same method pause when invoked during ongoing stream and resume when invoked on paused stream can be done in following way.
```typescript
const sourceChain = CHAIN_ID_BSC;
const targetChain = CHAIN_ID_SOLANA;
const receiver = "<evm address>";
const sender = "<evm address>";
const tokenMint = "<solana mint address>";
const dataAccount = "<pubkey of stream data account>";
const receipt = await ethClient.pauseResumeStream(
sender,
receiver,
tokenMint,
dataAccount
);
```
Now you can find signed vaa bytes of your payload sent to wormhole.
```typescript
const sequence = parseSequenceFromLogEth(receipt, getBridgeAddressForChain(sourceChain));
const zebecEmitterAddress = getEmitterAddressEth(BSC_ZEBEC_BRIDGE_ADDRESS);
const { vaaBytes } = await getSignedVAAWithRetry(
WORMHOLE_RPC_HOSTS,
sourceChain,
zebecEmitterAddress,
sequence,
);
```
Now post this signed vaa to wormhole solana core bridge program, parse the payloads from vaa and send the parsed data to solana zebec bridge program. This part is optional because it's handled by zebec's specialized relayer.
```typescript
const payerAddress = wallet.publicKey.toString();
const bridgeAddress = getBridgeAddressForChain(targetChain);
const vaaBuf = Buffer.from(vaaBytes);
setDefaultWasm("node"); // use bundler for browser
// posting vaa in solana
await postVaaSolanaWithRetry(
connection,
wallet.signTransaction,
bridgeAddress,
payerAddress,
vaaBuf,
MAX_VAA_UPLOAD_RETRIES_SOLANA,
);
const parsedVaa = parse_vaa(pauseVaa);
const parsedPayload = parseZebecPayload(Buffer.from(parsedVaa.payload));
const result = await solanaClient.pauseResumeStream(
pauseVaa,
<PauseTokenStreamPayload>parsedPayload
);
```
# Cancel Stream
To cancel the stream, you can follow example below.
```typescript
const sourceChain = CHAIN_ID_BSC;
const targetChain = CHAIN_ID_SOLANA;
const receiver = "<evm address>";
const sender = "<evm address>";
const tokenMint = "<solana mint address>";
const dataAccount = "<pubkey of stream data account>";
const cancelReceipt = await ethClient.cancelStream(sender, receiver, tokenMint, dataAccount);
```
Now you can find signed Vaa bytes of your payload sent to wormhole.
```typescript
const sequence = parseSequenceFromLogEth(cancelReceipt, getBridgeAddressForChain(sourceChain));
const emitterAddress = getEmitterAddressEth(BSC_ZEBEC_BRIDGE_ADDRESS);
const { vaaBytes } = await getSignedVAAWithRetry(
WORMHOLE_RPC_HOSTS,
sourceChain,
emitterAddress,
sequence,
);
```
Now post this signed vaa to wormhole solana core bridge program, parse the payloads from vaa and send the parsed data to solana zebec bridge program. This part is optional because it's handled by zebec's specialized relayer.
```typescript
const payerAddress = wallet.publicKey.toString();
const bridgeAddress = getBridgeAddressForChain(targetChain);
const vaaBuf = Buffer.from(vaaBytes);
setDefaultWasm("node"); // use bundler for browser
// posting vaa in solana
await postVaaSolanaWithRetry(
connection,
wallet.signTransaction,
bridgeAddress,
payerAddress,
vaaBuf,
MAX_VAA_UPLOAD_RETRIES_SOLANA,
);
const parsedVaa = parse_vaa(cancelStreamVaa);
const parsedPayload = parseZebecPayload(Buffer.from(parsedVaa.payload));
const result = await solanaClient.cancelStream(cancelStreamVaa, <CancelTokenStreamPayload>parsedPayload);
```
# Update Stream
For updating stream, you must make sure that your stream has not started. Your can update your stream in following way.
```typescript
const sourceChain = CHAIN_ID_BSC;
const targetChain = CHAIN_ID_SOLANA;
const now = nowInSec();
const startTime = now + 600;
const endTime = startTime + 6000;
const sender = "<evm address>";
const receiver = "<evm address>";
const tokenMint = "<solana mint key>";
const dataAccount = "<solana stream data account key>";
const receipt = await ethClient.updateStream(
startTime.toString(),
endTime.toString(),
amount,
receiver,
sender,
tokenMint,
dataAccount,
);
```
Now you can find signed Vaa bytes of your payload sent to wormhole.
```typescript
const sequence = parseSequenceFromLogEth(receipt, getBridgeAddressForChain(sourceChain));
const emitterAddress = getEmitterAddressEth(BSC_ZEBEC_BRIDGE_ADDRESS);
const { vaaBytes } = await getSignedVAAWithRetry(
WORMHOLE_RPC_HOSTS,
sourceChain,
emitterAddress,
sequence,
);
```
Now post this signed vaa to wormhole solana core bridge program, parse the payloads from vaa and send the parsed data to solana zebec bridge program. This part is optional because it's handled by zebec's specialized relayer.
```typescript
const payerAddress = wallet.publicKey.toString();
const bridgeAddress = getBridgeAddressForChain(targetChain);
const vaaBuf = Buffer.from(vaaBytes);
setDefaultWasm("node"); // use bundler for browser
// posting vaa in solana
await postVaaSolanaWithRetry(
connection,
wallet.signTransaction,
bridgeAddress,
payerAddress,
vaaBuf,
MAX_VAA_UPLOAD_RETRIES_SOLANA,
);
const parsedVaa = parse_vaa(updateStreamVaa);
const parsedPayload = parseZebecPayload(Buffer.from(parsedVaa.payload));
const result = await solanaClient.updateStreamToken(
vaaBytes,
<CancelTokenStreamPayload>parsedPayload
);
```
# Zebec Near Sdk
Software development kit for implementing Zebec protocol in Near chain.
This is a sdk for utilizing the Zebec streaming protocol and multisig streaming protocol in Near blockchain. The sdk features actions factories such as StreamActions, and services class for interacting with our Zebec Protocol smart contracts. It also provides utility class such as `NearAmount`, `FungibleTokenAmout`, etc. and functions.
See the list below for sdk usage examples.
* Normal stream:
* Create Instance
* Initialize stream
* Pause/Resume stream
* Withdraw stream
* Cancel stream
* Update stream
# Normal Stream
To initialize stream, you first create an instance of ZebecStreamService client in the following way:
```typescript
const config = {
networkId: nearConfig.networkId,
nodeUrl: nearConfig.nodeUrl,
contractId: streamContractId,
keyStore, // InMemoryKeystore
};
const actions = {
streamActions: new StreamActions(),
}
const client = new ZebecStreamService(
account.accountId,
config,
actions,
);
```
Note: For Node application, keystore should be passed by creating InMemoryKeyStore object, while for Browser application it can be undefined.
# Initialize Near Stream
Stream can be initialize in the following way:
```typescript
const now = nowInSec();
const streamRate = NearAmount.parse("0.0001").toString();
const startTime = (now + 15).toString();
const endTime = (now + 400).toString(); // 5 min
const receiver = "receiverAccountId.testnet";
const canCancel = true;
const canUpdate = true;
const streamParams: InitStreamParams = {
streamRate,
startTime,
endTime,
receiver,
canCancel,
canUpdate,
};
const streamPayload = await client.initStream(streamParams);
// for node application
const results = await streamPayload.execute(); // throws error if keystore is not provided.
// or
// for browser application
const results = await wallet.signAndSendTransactions(streamPayload);
```
If you require to obtain streamId, then you can get it in the following way.
<pre class="language-typescript"><code class="lang-typescript"><strong> const streamId = JSON.parse(
</strong> Buffer.from((results[results.length - 1].status as any).SuccessValue || "", "base64").toString(),
);
</code></pre>
However, this is not applicable for the case of token stream. See [Next](/zebec-network-archive/zebec-network-archive/zebec-near-sdk/normal-stream/initialize-token-stream) page for code.
# Initialize Token Stream
```typescript
const now = nowInSec();
const streamRate = FungibleTokenAmount.parse("0.0001", 18).toString();
const startTime = (now + 10).toString();
const endTime = (now + 400).toString(); // 5 min
const receiver = "receiverAccountId.testnet";
const canCancel = true;
const canUpdate = true;
const tokenId = "usdc.fakes.testnet";
const streamParams: InitTokenStreamParams = {
streamRate,
startTime,
endTime,
receiver,
canCancel,
canUpdate,
tokenId,
};
const streamPayload = await client.initTokenStream(streamParams);
const results = await wallet.signAndSendTransactions(streamPayload);
```
For obtaining streamId, you can do in the following way.
```typescript
const receipts = result.receipts_outcome;
if (receipts) {
const receipt = receipts.find(
(receipt) => receipt !== undefined && (receipt.outcome as any).executor_id == _contractId,
);
let streamId = "";
if (receipt) {
const evenLog = receipt.outcome.logs.find((log) => log.includes("Token stream created"));
if (evenLog) {
const json = evenLog.substring(11);
streamId = JSON.parse(json).data.stream_id.toString();
}
}
```
# Pause/Resume Stream
Pause and resume stream can be performed in following way.
```typescript
// pause
const pauseParams: PauseStreamParams = {
streamId: "",
};
const pausePayload = await client.pause(pauseParams);
const results = wallet.signAndSendTransactions(pausePayload );
// resume
const resumeParams: ResumeStreamParams = {
streamId: "",
};
const resumePayload = await client.resume(resumeParams);
const results = await wallet.signAndSendTransactions(resumePayload);
```
# Withdraw Stream
```typescript
const withdrawParams: WithdrawStreamParams = {
streamId,
};
const withdrawPayload = await clientReceiver.withdraw(withdrawParams);
const results = await wallet.sendAndSignTransaction();
```
# Update Stream
```typescript
const now = nowInSec();
const streamRate = NearAmount.parse(0.001);
const startTime = (now + 10).toString();
const endTime = (now + 20).toString(); // 5 min
const updateParams: UpdateStreamParams = {
streamId: "<streamId>",
streamRate: streamRate.toString(),
startTime,
endTime,
};
const updatePayload = await client.update(updateParams);
const results = await wallet.signAndSendTransactions(updatePayload);
```
# Cancel Stream
```typescript
const canccelParams: CancelStreamParams = {
streamId: "",
};
const cancelPayload = await client.cancel(cancelParams);
const results = await wallet.signAndSendTransactions(cancelPayload);
```
# Claim Stream
```typescript
const claimParams = {
streamId: ""
}
const claim = await client.claim();
const results = await wallet.signAndSendTransactions(cancelPayload);
```
# View methods
`getStreamId` gets parsed result of stream an object with given id.
```typescript
const result = await streamService.getStreamById({ streamId: "0" });
```
`getIncomingStreamsCount` gets the parsed result of incoming streams of the given userId.
```typescript
const params = {
userId: "",
limit: 10;
fromIndex: "0"
}
const result = await streamService.getIncomingStreamsCount(params);
```
`getOutgoingStreamsCount` gets the parsed result of outgoing streams of the given userId.
```typescript
const params = {
userId: "",
limit: 10;
fromIndex: "0"
}
const result = await streamService.getOutgoingStreamsCount()
```
`getStreamsByUser` gets the parsed result of both incoming and outgoing streams of the given userId.
```typescript
const params = {
userId: "",
limit: 10;
fromIndex: "0"
}
const result = await streamService.getStreamsByUser()
```
`getOutgoingStreamsCount` gets the count of outgoing streams of the given userId.
```typescript
const result = await streamService.getOutgoingStreamsCount({userId: ""});
```
`getIncomingStreamsCount` gets the count of incoming streams of the given userId.
```typescript
const result = await streamService.getIncomingStreamsCount({userId: ""});
```
`getOutgoingOngoingTotalAmount` gets the parsed result of both incoming and outgoing streams of the given userId and tokenId.
```typescript
const params = {
userId: "",
contractId: "",
fromIndex: "100",
limit: 0,
};
const result = await streamService.getStreamsForToken(params )
```
`getStreamsTotalAmount` gets the total amount of balance from outgoing and incoming streams for a given token.
```typescript
const params = {
userId: "",
contractId: "",
fromIndex: "100",
limit: 0,
};
const totalAmount = await streamService.getStreamsTotalAmount();
```
# Multisig Factory
Creates multisig accounts.
Multisig Factory instance can be created in the following way.
```typescript
const signerId = "<>"
const config = {
networkId: "",
nodeUrl: "",
contractId: "",
keyStore: "",
contractId: "multiSigFactoryContractId"
}
const actions = {
multiSigFactoryActions: new MultiSigFactoryActions(),
};
const multisigFactory = new ZebecMultiSigFactoryService(signerId, config, actions);
```
Now to create multisig account, you can follow as below.
# Create Multisig Account
Multisig account can be created in the following way.
```typescript
const name = "<any name>"
const numberOfConfirmations = 2;
const amount = NearAmount.parse(10).toString();
const members: MultiSigMember[] = [
{ type: "Account", accountId: account.accountId },
{ type: "Account", accountId: account1.accountId },
];
const payload = await multisigFactory.createMultiSigAccount({ name, members, numberOfConfirmations, amount });
const results = await wallet.signAndSendTransactions(cancelPayload);
```
# Multisig Stream
For multisig stream service, you can create instanc프로젝트로 돌아가기