Sign In

fabric application

Y
yeji Kim

about asset transfer

•
구성 요소
◦
샘플 app
◦
smart contract

샘플 app 준비하기

•
npm install → 종속성 설치, 앱 빌드

1. gateway에 대한 gRPC 연결 설정

2. gateway 연결 생성

•
요구사항
◦
fabric gateway에 대한 gRPC 연결
◦
네트워크와 거래할 때 사용되는 client ID
◦
디지털 서명

3. 호출할 계약에 액세스

•
gateway.getNetwork, network.getContract

4. 샘플 자산으로 원장 채우기

•
submitTranscation은 fabric gateway를 통해 다음을 수행
1.
거래 제안 승인
2.
승인된 거래를 주문 서비스에 제출
3.
트랜젝션이 커밋되고 원장 상태가 업데이트될 때까지 대기
•
샘플 앱에 initLedger 호출. contract.submitTransaction

5. 트랜잭션 함수를 호출하여 자산 읽고 쓰기.

const resultBytes = await contract.evaluateTransaction('GetAllAssets');
const resultJson = utf8Decoder.decode(resultBytes);
const result = JSON.parse(resultJson);
console.log('*** Result:', result);

새 자산 만들기

const assetId = `asset${Date.now()}`;

await contract.submitTransaction(
    'CreateAsset',
    assetId,
    'yellow',
    '5',
    'Tom',
    '1300',
);

자산 업데이트

const commit = await contract.submitAsync('TransferAsset', {
    arguments: [assetId, 'Saptha'],
});
const oldOwner = utf8Decoder.decode(commit.getResult());

console.log(`*** Successfully submitted transaction to transfer ownership from ${oldOwner} to Saptha`);
console.log('*** Waiting for transaction commit');

const status = await commit.getStatus();
if (!status.successful) {
    throw new Error(`Transaction ${status.transactionId} failed to commit with status code ${status.code}`);
}

console.log('*** Transaction committed successfully');

업데이트된 자산 쿼리

const resultBytes = await contract.evaluateTransaction('ReadAsset', assetId);

const resultJson = utf8Decoder.decode(resultBytes);
const result = JSON.parse(resultJson);
console.log('*** Result:', result);

트랜잭션 오류 처리

try {
    await contract.submitTransaction(
        'UpdateAsset',
        'asset70',
        'blue',
        '5',
        'Tomoko',
        '300',
    );
    console.log('******** FAILED to return an error');
} catch (error) {
    console.log('*** Successfully caught the error: \n', error);
}
아무튼-작업일지
Subscribe to '아무튼-작업일지'
Subscribe to my site to be the first to receive notifications and emails about the latest updates, including new posts.
Join Slashpage and subscribe to '아무튼-작업일지'!
Subscribe
👍