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

Adam OSS #1145

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
72 changes: 72 additions & 0 deletions Adam_Oss/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Atomic Transfer

his simply means that transactions that are part of the transfer either all succeed or all fail. Atomic transfers allow complete strangers to trade assets without the need for a trusted intermediary, all while guaranteeing that each party will receive what they agreed to.

On Algorand, atomic transfers are implemented as irreducible batch operations, where a group of transactions are submitted as a unit and all transactions in the batch either pass or fail. This also eliminates the need for more complex solutions like hashed timelock contracts that are implemented on other blockchains. An atomic transfer on Algorand is confirmed in less than 5 seconds, just like any other transaction. Transactions can contain Algos or Algorand Standard Assets and may also be governed by Algorand Smart Contracts.

# Key Benefits of Atomic Transfer

Simple and easy to use

# Js-algorand-sdk

[![Build Status](https://travis-ci.com/algorand/js-algorand-sdk.svg?branch=master)](https://travis-ci.com/algorand/js-algorand-sdk) [![npm version](https://badge.fury.io/js/algosdk.svg)](https://badge.fury.io/js/algosdk)

AlgoSDK is a javascript library for communicating with the Algorand network for modern browsers and node.js.

## STEPS TO RUN

- clone my repository

- "NODE atomicTransfer"

## Quick Start

```javascript
const token = "Your algod API token";
const server = "https://testnet-algorand.api.purestake.io/ps2";
const port = "";
const client = new algosdk.Algod(token, server, port);
(async () => {
console.log(await client.status());
})().catch((e) => {
console.log(e);
});
```

## Youtube Video

[youtube]()

## Functionalities

- [x] User has to add his/her Api key
- [x] Must have Algosigner wallet

```javascript
//...
onst myAlgoWallet = new MyAlgoConnect();
const algoServer = "https://testnet-algorand.api.purestake.io/ps2";
const algoPort = "";
const token = {
"X-API-Key": "", //Your APi key here
};
let algoClient = new algosdk.Algodv2(token, algoServer, algoPort);
// From now, every transaction needs to be sign the SK of the following address
//...
```

## Technologies and Platform used

- Js
- Algorand

## Transaction of dispensed algo in Algoexplorer

## TXID

J4E5DUU7S6S3CJV26ROM6TRFWEWTNXE3K7JPOAMC3CFP2ZMAMX7Q

## Reference

[Reference](https://developer.algorand.org/docs/get-details/accounts/rekey/?from_query=rekeying#create-publication-overlay)
114 changes: 114 additions & 0 deletions Adam_Oss/atomicTransfer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
const algosdk = require("algosdk");
const baseServer = "https://testnet-algorand.api.purestake.io/ps2";
const port = "";
const token = {
"X-API-Key": "", //Your APi key here
};
const algoClient = new algosdk.Algodv2(token, baseServer, port);

const keypress = async () => {
process.stdin.setRawMode(true);
return new Promise((resolve) =>
process.stdin.once("data", () => {
process.stdin.setRawMode(false);
resolve();
})
);
};
const waitForConfirmation = async (algodClient, txId, timeout) => {
if (algodClient == null || txId == null || timeout < 0) {
throw new Error("Bad arguments");
}

const status = await algodClient.status().do();
if (status === undefined) {
throw new Error("Unable to get node status");
}

const startround = status["last-round"] + 1;
let currentround = startround;

while (currentround < startround + timeout) {
const pendingInfo = await algodClient
.pendingTransactionInformation(txId)
.do();
if (pendingInfo !== undefined) {
if (
pendingInfo["confirmed-round"] !== null &&
pendingInfo["confirmed-round"] > 0
) {
//Got the completed Transaction
return pendingInfo;
} else {
if (
pendingInfo["pool-error"] != null &&
pendingInfo["pool-error"].length > 0
) {
// If there was a pool error, then the transaction has been rejected!
throw new Error(
"Transaction " +
txId +
" rejected - pool error: " +
pendingInfo["pool-error"]
);
}
}
}
await algodClient.statusAfterBlock(currentround).do();
currentround++;
}
throw new Error(
"Transaction " + txId + " not confirmed after " + timeout + " rounds!"
);
};
const atomicTf = async () => {
try {
let mrA = algosdk.generateAccount();
let mrB = algosdk.generateAccount();
let mrC = algosdk.generateAccount();
let mnemonicMrA = algosdk.secretKeyToMnemonic(mrA.sk);
let mnemonicMrB = algosdk.secretKeyToMnemonic(mrB.sk);
let mnemonicMrC = algosdk.secretKeyToMnemonic(mrC.sk);
console.log(mrA.addr, mrB.addr, mrC.addr);
console.log(mnemonicMrA, mnemonicMrB, mnemonicMrC);
await keypress();
let params = await algoClient.getTransactionParams().do();
// Transaction A to C
let transaction1 = algosdk.makePaymentTxnWithSuggestedParams(
mrA.addr,
mrB.addr,
100000,
undefined,
undefined,
params
);
// Create transaction B to A
let transaction2 = algosdk.makePaymentTxnWithSuggestedParams(
mrB.addr,
mrA.addr,
100000,
undefined,
undefined,
params
);
// Combine transactions
let txns = [transaction1, transaction2];
// Sign each transaction in the group

let txGroup = algosdk.assignGroupID(txns);
let signedTx1 = transaction1.signTxn(mrA.sk);
let signedTx2 = transaction2.signTxn(mrB.sk);
let signed = [];
signed.push(signedTx1);
signed.push(signedTx2);
let tx = await algoClient.sendRawTransaction(signed).do();
console.log("Transaction : " + tx.txId);

// Wait for transaction to be confirmed
await waitForConfirmation(algoClient, tx.txId, 10);
} catch (error) {
console.log(error);
}
};

atomicTf();
Loading